/// <summary> /// Sets up the data to be displayed in the table. </summary> /// <param name="data"> a Vector of StateMod_DelayTable objects from which the data to b /// be displayed in the table will be gathered. </param> private void setupData(System.Collections.IList data) { int num = 0; int size = data.Count; StateMod_DelayTable dt = null; string id = null; __data = new System.Collections.IList[__COLUMNS]; for (int i = 0; i < __COLUMNS; i++) { __data[i] = new List <object>(); } __rowMap = new List <object>(); double total = 0; int rowCount = 0; for (int i = 0; i < size; i++) { total = 0; dt = (StateMod_DelayTable)data[i]; id = dt.getID(); num = dt.getNdly(); for (int j = 0; j < num; j++) { __data[COL_ID].Add(id); __data[COL_DATE].Add(new int?(j + 1)); __data[COL_RETURN_AMT].Add(new double?(dt.getRet_val(j))); total += dt.getRet_val(j); __rowMap.Add(new int?(rowCount)); rowCount++; } __data[COL_ID].Add("TOTAL " + id); __data[COL_DATE].Add(new int?(-999)); __data[COL_RETURN_AMT].Add(new double?(total)); rowCount++; } _rows = rowCount; }
/// <summary> /// Updates the right table based on a new selection from the left table, /// or if rows were added or deleted to the right table. </summary> /// <param name="dt"> the StateMod_DelayTable object selected in the left table. </param> private void updateRightTable(StateMod_DelayTable dt) { IList<double?> v = dt.getRet_val(); IList<double?> v2 = new List<double?>(); for (int i = 0; i < v.Count; i++) { v2.Add(new double?(v[i].Value)); } ((StateMod_DelayTable_TableModel)__worksheetR.getModel()).setSubDelays(v2); ((StateMod_DelayTable_TableModel)__worksheetR.getModel()).fireTableDataChanged(); ((StateMod_DelayTable_TableModel)__worksheetR.getModel()).fireTableDataChanged(); __deleteReturn.setEnabled(false); }
/// <summary> /// Writes a list of StateMod_DelayTable objects to a list file. A header is /// printed to the top of the file, containing the commands used to generate the /// file. Any strings in the body of the file that contain the field delimiter will be wrapped in "...". </summary> /// <param name="filename"> the name of the file to which the data will be written. </param> /// <param name="delimiter"> the delimiter to use for separating field values. </param> /// <param name="update"> whether to update an existing file, retaining the current /// header (true) or to create a new file with a new header. </param> /// <param name="data"> the list of objects to write. </param> /// <param name="newComments"> the list of new comments to write to the header, or null if none. </param> /// <param name="comp"> the component type being written (to distinguish between daily and monthly delay tables), /// either StateMod_DataSet.COMP_DELAY_TABLES_DAILY or StateMod_DataSet.COMP_DELAY_TABLES_MONTHLY. </param> /// <exception cref="Exception"> if an error occurs. </exception> //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: public static void writeListFile(String filename, String delimiter, boolean update, java.util.List<StateMod_DelayTable> data, java.util.List<String> newComments, int comp) throws Exception public static void writeListFile(string filename, string delimiter, bool update, IList <StateMod_DelayTable> data, IList <string> newComments, int comp) { int size = 0; if (data != null) { size = data.Count; } IList <string> fields = new List <string>(); fields.Add("DelayTableID"); fields.Add("Date"); fields.Add("ReturnAmount"); int fieldCount = fields.Count; string[] names = new string[fieldCount]; string[] formats = new string[fieldCount]; string s = null; for (int i = 0; i < fieldCount; i++) { s = (string)fields[i]; names[i] = StateMod_Util.lookupPropValue(comp, "FieldName", s); formats[i] = StateMod_Util.lookupPropValue(comp, "Format", s); } string oldFile = null; if (update) { oldFile = IOUtil.getPathUsingWorkingDir(filename); } int j = 0; int k = 0; int num = 0; PrintWriter @out = null; StateMod_DelayTable delay = null; IList <string> commentIndicators = new List <string>(1); commentIndicators.Add("#"); IList <string> ignoredCommentIndicators = new List <string>(1); ignoredCommentIndicators.Add("#>"); string[] line = new string[fieldCount]; string id = null; StringBuilder buffer = new StringBuilder(); try { // Add some basic comments at the top of the file. Do this to a copy of the // incoming comments so that they are not modified in the calling code. IList <string> newComments2 = null; if (newComments == null) { newComments2 = new List <string>(); } else { newComments2 = new List <string>(newComments); } newComments2.Insert(0, ""); if (comp == StateMod_DataSet.COMP_DELAY_TABLES_DAILY) { newComments2.Insert(1, "StateMod delay tables (daily) as a delimited list file."); } else { newComments2.Insert(1, "StateMod delay tables (monthly) as a delimited list file."); } newComments2.Insert(2, ""); @out = IOUtil.processFileHeaders(IOUtil.getPathUsingWorkingDir(oldFile), IOUtil.getPathUsingWorkingDir(filename), newComments2, commentIndicators, ignoredCommentIndicators, 0); for (int i = 0; i < fieldCount; i++) { buffer.Append("\"" + names[i] + "\""); if (i < (fieldCount - 1)) { buffer.Append(delimiter); } } @out.println(buffer.ToString()); for (int i = 0; i < size; i++) { delay = (StateMod_DelayTable)data[i]; id = delay.getID(); num = delay.getNdly(); for (j = 0; j < num; j++) { line[0] = StringUtil.formatString(id, formats[0]).Trim(); line[1] = StringUtil.formatString((j + 1), formats[1]).Trim(); line[2] = StringUtil.formatString(delay.getRet_val(j), formats[2]).Trim(); buffer = new StringBuilder(); for (k = 0; k < fieldCount; k++) { if (k > 0) { buffer.Append(delimiter); } if (line[k].IndexOf(delimiter, StringComparison.Ordinal) > -1) { line[k] = "\"" + line[k] + "\""; } buffer.Append(line[k]); } @out.println(buffer.ToString()); } } @out.flush(); @out.close(); @out = null; } catch (Exception e) { // TODO SAM 2009-01-11 Log it? throw e; } finally { if (@out != null) { @out.flush(); @out.close(); } } }
/// <summary> /// Write the new (updated) delay table file. This routine writes the new delay table file. /// If an original file is specified, then the original header is carried into the new file. /// The writing of data is done by the dumpDelayFile routine which now does not mess with headers. </summary> /// <param name="inputFile"> old file (used as input) </param> /// <param name="outputFile"> new file to create </param> /// <param name="dly"> list of delays </param> /// <param name="newcomments"> new comments to save with the header of the file </param> /// <param name="interv"> interv variable specified in control file (if negative, include the number /// of entries at the start of each record). </param> /// <param name="precision"> number of digits after the decimal to write. If negative, 2 is assumed. </param> /// <exception cref="Exception"> if an error occurs </exception> //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: public static void writeStateModFile(String inputFile, String outputFile, java.util.List<StateMod_DelayTable> dly, java.util.List<String> newcomments, int interv, int precision) throws Exception public static void writeStateModFile(string inputFile, string outputFile, IList <StateMod_DelayTable> dly, IList <string> newcomments, int interv, int precision) { PrintWriter @out = null; IList <string> commentIndicators = new List <string>(1); commentIndicators.Add("#"); IList <string> ignoredCommentIndicators = new List <string>(1); ignoredCommentIndicators.Add("#>"); string routine = "StateMod_DelayTable.writeStateModFile"; if (precision < 0) { precision = 2; } Message.printStatus(2, routine, "Writing delay tables to file \"" + outputFile + "\" using \"" + inputFile + "\" header..."); try { // Process the header from the old file... @out = IOUtil.processFileHeaders(inputFile, outputFile, newcomments, commentIndicators, ignoredCommentIndicators, 0); // Now write the new data... string delayVal = null; string cmnt = "#>"; string idFormat = "%8.8s"; // Right justify because StateMod actually uses integers string delayValueFormat0 = "%8."; // no precision string delayValueFormat = "%8." + precision + "f"; string countFormat = "%4d"; @out.println(cmnt); @out.println(cmnt + " *******************************************************"); @out.println(cmnt + " StateMod Delay (Return flow) Table"); @out.println(cmnt); @out.println(cmnt + " Format (a8, i4, (12f8.2)"); @out.println(cmnt); @out.println(cmnt + " ID idly: Delay table id"); @out.println(cmnt + " Ndly ndly: Number of entries in delay table idly."); @out.println(cmnt + " Include only if \"interv\" in the"); @out.println(cmnt + " control file is equal to -1."); @out.println(cmnt + " interv = -1 = Variable number of entries"); @out.println(cmnt + " as percent (0-100)"); @out.println(cmnt + " interv = -100 = Variable number of entries"); @out.println(cmnt + " as fraction (0-1)"); @out.println(cmnt + " Ret dlyrat(1-n,idl): Return for month n, station idl"); @out.println(cmnt); @out.println(cmnt + " ID Ndly Ret1 Ret2 Ret3 Ret4 Ret5 Ret6 Ret7 Ret8 Ret9 Ret10 Ret11 Ret12"); @out.println(cmnt + "-----eb--eb------eb------eb------eb------eb------eb------eb------eb------eb------eb------eb------eb------e...next line"); @out.println(cmnt + "EndHeader"); @out.println(cmnt); int ndly = 0; if (dly != null) { ndly = dly.Count; } int numWritten, numToWrite; if (Message.isDebugOn) { Message.printDebug(3, routine, "Writing " + ndly + " delay table entries."); } StringBuilder iline = new StringBuilder(); StateMod_DelayTable delay = null; for (int i = 0; i < ndly; i++) { delay = dly[i]; // Clear out the string buffer for the new delay table iline.Length = 0; // Create one delay table entry with ID, Nvals, and 12 return values per line. if (interv < 0) { iline.Append(StringUtil.formatString(delay.getTableID(), idFormat)); iline.Append(StringUtil.formatString(delay.getNdly(), countFormat)); } else { iline.Append(StringUtil.formatString(delay.getTableID(), idFormat)); } numWritten = 0; // Number of values written for this delay table int numDelayVals = delay.getNdly(); while (true) { if (numWritten > 0) { // Clear lines 2+ before adding values iline.Length = 0; // Add spaces as per the record header info if (interv < 0) { iline.Append(" "); } else { iline.Append(" "); } } // Number of values remaining to write numToWrite = numDelayVals - numWritten; if (numToWrite > 12) { // Have more than 12 so only write 12 on this line numToWrite = 12; } for (int j = 0; j < numToWrite; j++) { delayVal = StringUtil.formatString(delay.getRet_val(numWritten + j), delayValueFormat); if (delayVal.IndexOf(' ') < 0) { // There are no spaces - this will be a problem because the file is free format. // Do a little more work here to reduce the precision until there is a leading // space. for (int iprecision = precision - 1; iprecision >= 0; iprecision--) { delayVal = StringUtil.formatString(delay.getRet_val(numWritten + j), delayValueFormat0 + iprecision + ".f"); if (delayVal.IndexOf(' ') >= 0) { // Done break; } } } iline.Append(delayVal); } // Now output the line: @out.println(iline); numWritten += numToWrite; if (numWritten == numDelayVals) { // Done writing so break out break; } } } } catch (Exception e) { if (@out != null) { @out.close(); } Message.printWarning(3, routine, e); throw e; } finally { if (@out != null) { @out.flush(); @out.close(); } } }
/// <summary> /// Responds to action performed events. </summary> /// <param name="e"> the ActionEvent that happened. </param> public virtual void actionPerformed(ActionEvent e) { string routine = "StateMod_DelayTable_JFrame" + ".actionPerformed"; if (Message.isDebugOn) { Message.printDebug(1, routine, "In actionPerformed: " + e.getActionCommand()); } string action = e.getActionCommand(); if (action.Equals(__BUTTON_HELP)) { // REVISIT HELP (JTS - 2003-06-09) } else if (action.Equals(__BUTTON_CLOSE)) { closeWindow(); } else if (action.Equals(__BUTTON_APPLY)) { saveDelayTable(); int size = __delaysVector.Count; StateMod_DelayTable dt = null; bool changed = false; for (int i = 0; i < size; i++) { dt = (StateMod_DelayTable)__delaysVector[i]; if (!changed && dt.changed()) { changed = true; } dt.createBackup(); } if (changed) { __dataset.setDirty(__componentType, true); } } else if (action.Equals(__BUTTON_CANCEL)) { __worksheetR.deselectAll(); int size = __delaysVector.Count; StateMod_DelayTable dt = null; bool changed = false; for (int i = 0; i < size; i++) { dt = (StateMod_DelayTable)__delaysVector[i]; if (!changed && dt.changed()) { changed = true; } dt.restoreOriginal(); } if (__dataset_wm != null) { __dataset_wm.closeWindow(__window_type); } else { JGUIUtil.close(this); } } else if (action.Equals(__BUTTON_ADD_RETURN)) { int row = __worksheetR.getSelectedRow(); int total_num_rows = __worksheetR.getRowCount() - 1; if (row == -1) { row = total_num_rows; } if (row != -1) { if (row == total_num_rows) { int x = new ResponseJDialog(this, "Insert row", "Do you wish to add a new row above " + "the last row?\n" + "uniquetempvar.response(); if (x == ResponseJDialog.CANCEL) { return; } else if (x == ResponseJDialog.NO) { row += 1; } } __worksheetR.insertRowAt(new double?(0), row); __worksheetR.scrollToRow(row); __worksheetR.selectRow(row); } else { __worksheetR.addRow(new double?(0)); __worksheetR.scrollToRow(0); __worksheetR.selectRow(0); } __deleteReturn.setEnabled(true); } else if (action.Equals(__BUTTON_DELETE_RETURN)) { int row = __worksheetR.getSelectedRow(); if (row != -1) { int x = (new ResponseJDialog(this, "Delete Return", "Delete return?", ResponseJDialog.YES | ResponseJDialog.NO)).response(); if (x == ResponseJDialog.NO) { return; } //StateMod_DelayTable dt = (StateMod_DelayTable) //__worksheetL.getRowData( //__worksheetL.getSelectedRow()); __worksheetR.deleteRow(row); __deleteReturn.setEnabled(false); } else { Message.printWarning(1, routine, "Must select desired right to delete."); } } else if (e.getSource() == __findNextDelay) { searchLeftWorksheet(__worksheetL.getSelectedRow() + 1); } else if (e.getSource() == __searchID) { searchLeftWorksheet(); } else { if (__worksheetL.getSelectedRow() == -1) { new ResponseJDialog(this, "You must first select a delay from the list.", ResponseJDialog.OK); return; } else if (e.getSource() == __graphDelayJButton) { try { __worksheetR.deselectAll(); int index = __worksheetL.getSelectedRow(); if (index == -1) { return; } StateMod_DelayTable currentDelay = ((StateMod_DelayTable) __delaysVector[index]); int j; DateTime date; TSIdent tsident = new TSIdent(); tsident.setLocation(currentDelay.getID()); tsident.setSource("StateMod"); if (__monthly_data) { tsident.setInterval("Month"); } else { tsident.setInterval("Day"); } tsident.setType("Delay"); DateTime date1 = null; DateTime date2 = null; int interval_base; if (__monthly_data) { date1 = new DateTime(DateTime.PRECISION_MONTH); date2 = new DateTime(DateTime.PRECISION_MONTH); interval_base = TimeInterval.MONTH; } else { date1 = new DateTime(DateTime.PRECISION_DAY); date2 = new DateTime(DateTime.PRECISION_DAY); interval_base = TimeInterval.DAY; } date1.setMonth(1); date1.setYear(1); date2.setMonth(1); date2.setYear(1); date2.addInterval(interval_base, (currentDelay.getNdly() - 1)); TS ts = null; if (__monthly_data) { ts = new MonthTS(); } else { ts = new DayTS(); } ts.setDate1(date1); ts.setDate2(date2); ts.setIdentifier(tsident); if (__monthly_data) { ts.setDescription(ts.getLocation() + " Monthly Delay Table"); } else { ts.setDescription(ts.getLocation() + " Daily Delay Table"); } ts.setDataType("Delay"); ts.setDataUnits(currentDelay.getUnits()); ts.allocateDataSpace(); double max = 0.0; for (date = new DateTime(date1), j = 0; date.lessThanOrEqualTo(date2); date.addInterval(interval_base, 1), j++) { ts.setDataValue(date, currentDelay.getRet_val(j)); if (currentDelay.getRet_val(j) > max) { max = currentDelay.getRet_val(j); } } IList<TS> tslist = new List<TS>(); tslist.Add(ts); PropList graphProps = new PropList("TSView"); // If dealing with small values, use a high // of precision... if (max < 1.0) { graphProps.set("YAxisPrecision","6"); graphProps.set("OutputPrecision","6"); } else { graphProps.set("YAxisPrecision","3"); graphProps.set("OutputPrecision","3"); } graphProps.set("InitialView", "Graph"); graphProps.set("TotalWidth", "600"); graphProps.set("TotalHeight", "400"); if (__monthly_data) { graphProps.set("Title", ts.getLocation() + " Monthly Delay Table"); } else { graphProps.set("Title", ts.getLocation() + " Daily Delay Table"); } graphProps.set("DisplayFont", "Courier"); graphProps.set("DisplaySize", "11"); graphProps.set("PrintFont", "Courier"); graphProps.set("PrintSize", "7"); graphProps.set("PageLength", "100"); new TSViewJFrame(tslist, graphProps); } catch (Exception) { Message.printWarning(1, routine, "Unable to graph delay. "); } } } }