/// <summary> /// Opens a dialog from which users can browse for delta plot files. </summary> /// <returns> the path to the file the user chose, or "" if no file was selected. </returns> private string browseForFile() { JGUIUtil.setWaitCursor(__parent, true); string directory = JGUIUtil.getLastFileDialogDirectory(); JFileChooser fc = null; if (!string.ReferenceEquals(directory, null)) { fc = new JFileChooser(directory); } else { fc = new JFileChooser(); } fc.setDialogTitle("Select File"); SimpleFileFilter xre = new SimpleFileFilter("xdd", "ASCII xre"); SimpleFileFilter b44 = new SimpleFileFilter("b44", "Binary b44"); SimpleFileFilter xdd = new SimpleFileFilter("xdd", "ASCII xdd"); SimpleFileFilter b43 = new SimpleFileFilter("b43", "Binary b43"); fc.addChoosableFileFilter(xre); fc.addChoosableFileFilter(b44); fc.addChoosableFileFilter(xdd); fc.addChoosableFileFilter(b43); fc.setAcceptAllFileFilterUsed(true); fc.setFileFilter(xre); fc.setDialogType(JFileChooser.SAVE_DIALOG); JGUIUtil.setWaitCursor(__parent, false); int retVal = fc.showSaveDialog(__parent); if (retVal != JFileChooser.APPROVE_OPTION) { return(""); } string currDir = (fc.getCurrentDirectory()).ToString(); if (!currDir.Equals(directory, StringComparison.OrdinalIgnoreCase)) { JGUIUtil.setLastFileDialogDirectory(currDir); } string filename = fc.getSelectedFile().getName(); return(currDir + File.separator + filename); }
private string browseForFile() { JGUIUtil.setWaitCursor(this, true); string directory = JGUIUtil.getLastFileDialogDirectory(); JFileChooser fc = JFileChooserFactory.createJFileChooser(directory); string compName = __tableModel.getComponentName(__worksheet.getSelectedRow()); string ext = __dataset.getComponentFileExtension(__tableModel.getComponentTypeForRow(__worksheet.getSelectedRow())); fc.setDialogTitle("Select " + compName + " File"); SimpleFileFilter ff = new SimpleFileFilter(ext, compName + " files"); fc.addChoosableFileFilter(ff); fc.setAcceptAllFileFilterUsed(true); fc.setDialogType(JFileChooser.OPEN_DIALOG); fc.setFileFilter(ff); JGUIUtil.setWaitCursor(this, false); int retVal = fc.showOpenDialog(this); if (retVal != JFileChooser.APPROVE_OPTION) { return(""); } string currDir = (fc.getCurrentDirectory()).ToString(); if (!currDir.Equals(directory, StringComparison.OrdinalIgnoreCase)) { JGUIUtil.setLastFileDialogDirectory(currDir); } string filename = fc.getSelectedFile().getName(); return(currDir + File.separator + filename); }
/// <summary> /// Browse for a statemod output file. /// </summary> private string browseForFile() { JGUIUtil.setWaitCursor(__parent, true); string lastDirectorySelected = JGUIUtil.getLastFileDialogDirectory(); JFileChooser fc = JFileChooserFactory.createJFileChooser(lastDirectorySelected); fc.setDialogTitle("Select file"); // SimpleFileFilter ff = new SimpleFileFilter("???", "?Some kind of file?"); // fc.addChoosableFileFilter(ff); // fc.setAcceptAllFileFilterUsed(false); // fc.setFileFilter(ff); fc.setAcceptAllFileFilterUsed(true); fc.setDialogType(JFileChooser.OPEN_DIALOG); JGUIUtil.setWaitCursor(__parent, false); int retVal = fc.showOpenDialog(__parent); if (retVal != JFileChooser.APPROVE_OPTION) { return(null); } string currDir = (fc.getCurrentDirectory()).ToString(); if (!currDir.Equals(lastDirectorySelected, StringComparison.OrdinalIgnoreCase)) { JGUIUtil.setLastFileDialogDirectory(currDir); } string filename = fc.getSelectedFile().getName(); // do some work with the filename, perhaps return(currDir + File.separator + filename); }
/// <summary> /// Write list files for the main station lists. These can then be used with /// list-based commands in StateDMI. /// The user is prompted for a list file name. /// </summary> protected internal virtual void writeListFiles() { string routine = "StateMod_Network_JComponent.writeListFiles"; string lastDirectorySelected = JGUIUtil.getLastFileDialogDirectory(); JFileChooser fc = JFileChooserFactory.createJFileChooser(lastDirectorySelected); fc.setDialogTitle("Select Base Filename for List Files"); SimpleFileFilter tff = new SimpleFileFilter("txt", "Text Files"); fc.addChoosableFileFilter(tff); SimpleFileFilter csv_ff = new SimpleFileFilter("csv", "Comma-separated Values"); fc.addChoosableFileFilter(csv_ff); fc.setFileFilter(csv_ff); fc.setDialogType(JFileChooser.SAVE_DIALOG); int retVal = fc.showSaveDialog(this.__editorJComponent); if (retVal != JFileChooser.APPROVE_OPTION) { return; } string currDir = (fc.getCurrentDirectory()).ToString(); if (!currDir.Equals(lastDirectorySelected, StringComparison.OrdinalIgnoreCase)) { JGUIUtil.setLastFileDialogDirectory(currDir); } string filename = fc.getSelectedFile().getPath(); // Station types... int[] types = new int[] { -1, HydrologyNode.NODE_TYPE_FLOW, HydrologyNode.NODE_TYPE_DIV, HydrologyNode.NODE_TYPE_DIV_AND_WELL, HydrologyNode.NODE_TYPE_PLAN, HydrologyNode.NODE_TYPE_RES, HydrologyNode.NODE_TYPE_ISF, HydrologyNode.NODE_TYPE_WELL, HydrologyNode.NODE_TYPE_OTHER }; /* TODO SAM 2006-01-03 Just use node abbreviations from network * // Suffix for output, to be added to file basename... * * String[] nodetype_string = { * "All", * "StreamGage", * "Diversion", * "DiversionAndWell", * "Plan", * "Reservoir", * "InstreamFlow", * "Well", * // TODO SAM 2006-01-03 Evaluate similar to node type above. * //"StreamEstimate", * "Other" * }; */ // Put the extension on the file (user may or may not have added)... if (fc.getFileFilter() == tff) { filename = IOUtil.enforceFileExtension(filename, "txt"); } else if (fc.getFileFilter() == csv_ff) { filename = IOUtil.enforceFileExtension(filename, "csv"); } // Now get the base name and remaining extension so that the basename can be adjusted below... int lastIndex = filename.LastIndexOf(".", StringComparison.Ordinal); string front = filename.Substring(0, lastIndex); string end = filename.Substring((lastIndex + 1), filename.Length - (lastIndex + 1)); string outputFilename = null; IList <HydrologyNode> v = null; string warning = ""; string[] comments = null; for (int i = 0; i < types.Length; i++) { v = this.__editorJComponent.getNodesForType(types[i]); if (v != null && v.Count > 0) { comments = new string[1]; if (types[i] == -1) { comments[0] = "The following list contains data for all node types."; outputFilename = front + "_All." + end; } else { comments[0] = "The following list contains data for the following node type: " + HydrologyNode.getTypeString(types[i], HydrologyNode.ABBREVIATION) + " (" + HydrologyNode.getTypeString(types[i], HydrologyNode.FULL) + ")"; outputFilename = front + "_" + HydrologyNode.getTypeString(types[i], HydrologyNode.ABBREVIATION) + "." + end; } try { StateMod_NodeNetwork.writeListFile(outputFilename, ",", false, v, comments, false); } catch (Exception e) { Message.printWarning(3, routine, e); warning += "\nUnable to create list file \"" + outputFilename + "\""; } } } // TODO SAM 2006-01-03 Write at level 1 since this is currently triggered from an // interactive action. However, may need to change if executed in batch mode. if (warning.Length > 0) { Message.printWarning(1, routine, warning); } }
/// <summary> /// Returns the filename and format type of a file selected from a file chooser /// in order that the kind of delimiter for the file can be known when the data /// is formatted for output. Currently the only kinds of files that the data /// can be exported to are delimited files. No StateMod files are yet supported.<para> /// Also sets the last selected file dialog directory to whatever directory the /// file is located in, if the file selection was approved (i.e., Cancel was not /// pressed). /// </para> /// </summary> /// <param name="title"> the title of the file chooser. </param> /// <param name="formats"> a Vector of the valid formats for the file chooser. </param> /// <returns> a two-element String array where the first element is the name of the /// file and the second element is the delimiter selected. </returns> protected internal virtual string[] getFilenameAndFormat() { JGUIUtil.setWaitCursor(this, true); string dir = JGUIUtil.getLastFileDialogDirectory(); JFileChooser fc = JFileChooserFactory.createJFileChooser(dir); fc.setDialogTitle("Select Export File"); SimpleFileFilter tabFF = new SimpleFileFilter("txt", "Tab-delimited"); SimpleFileFilter commaFF = new SimpleFileFilter("csv", "Comma-delimited"); SimpleFileFilter semiFF = new SimpleFileFilter("txt", "Semicolon-delimited"); SimpleFileFilter pipeFF = new SimpleFileFilter("txt", "Pipe-delimited"); fc.addChoosableFileFilter(commaFF); fc.addChoosableFileFilter(pipeFF); fc.addChoosableFileFilter(semiFF); fc.addChoosableFileFilter(tabFF); fc.setAcceptAllFileFilterUsed(false); fc.setFileFilter(commaFF); fc.setDialogType(JFileChooser.SAVE_DIALOG); JGUIUtil.setWaitCursor(this, false); int returnVal = fc.showSaveDialog(this); if (returnVal == JFileChooser.APPROVE_OPTION) { string[] ret = new string[2]; string filename = fc.getCurrentDirectory() + File.separator + fc.getSelectedFile().getName(); JGUIUtil.setLastFileDialogDirectory("" + fc.getCurrentDirectory()); SimpleFileFilter sff = (SimpleFileFilter)fc.getFileFilter(); // this will always return a one-element vector IList <string> extensionV = sff.getFilters(); string extension = extensionV[0]; string desc = sff.getShortDescription(); string delimiter = "\t"; if (desc.Equals("Tab-delimited")) { delimiter = "\t"; } else if (desc.Equals("Comma-delimited")) { delimiter = ","; } else if (desc.Equals("Semicolon-delimited")) { delimiter = ";"; } else if (desc.Equals("Pipe-delimited")) { delimiter = "|"; } ret[0] = IOUtil.enforceFileExtension(filename, extension); ret[1] = delimiter; return(ret); } else { return(null); } }
/// <summary> /// Responds to action performed events. </summary> /// <param name="ae"> the ActionEvent that happened. </param> public virtual void actionPerformed(ActionEvent ae) { string routine = "StateMod_OutputControl_JFrame.actionPerformed"; string action = ae.getActionCommand(); if (action.Equals(__BUTTON_ADD_ROW)) { if (!__tableModel.canAddNewRow()) { return; } __dirty = true; int row = __worksheet.getRowCount(); StateMod_GraphNode n = new StateMod_GraphNode(); __worksheet.addRow(n); n.setType(""); n.setID(""); n.setSwitch(-1); __tableModel.setDirty(true); // when a row is added, mark the second and third columns // as uneditable. They will be set to editable as soon as // the user enters a value in the first column __worksheet.setCellEditable(row, 1, false); __worksheet.setCellEditable(row, 2, false); } else if (action.Equals(__BUTTON_CLEAR_LIST)) { __dirty = true; __worksheet.clear(); setMessages("Add station to list.", "Ready"); __tableModel.setDirty(true); } else if (action.Equals(__BUTTON_CLOSE)) { closeWindow(); } else if (action.Equals(__BUTTON_DELETE_ROW)) { int[] rows = __worksheet.getSelectedRows(); int length = rows.Length; __tableModel.setDirty(true); if (length == 0) { return; } for (int i = (length - 1); i >= 0; i--) { __worksheet.deleteRow(rows[i]); } __dirty = true; setMessages("Time series list has changed.", "Ready"); } else if (action.Equals(__BUTTON_HELP)) { // REVISIT HELP (JTS - 2003-07-09) } else if (action.Equals(__BUTTON_SAVE_FILE)) { JGUIUtil.setWaitCursor(this, true); string lastDirectorySelected = JGUIUtil.getLastFileDialogDirectory(); JFileChooser fc = null; if (!string.ReferenceEquals(lastDirectorySelected, null)) { fc = new JFileChooser(lastDirectorySelected); } else { fc = new JFileChooser(); } fc.setDialogTitle("Select Output Control File"); SimpleFileFilter @out = new SimpleFileFilter("out", "StateMod Output Control Files"); fc.addChoosableFileFilter(@out); SimpleFileFilter tpl = new SimpleFileFilter("tpo", "StateMod Output Control Files"); fc.addChoosableFileFilter(tpl); SimpleFileFilter xou = new SimpleFileFilter("xou", "StateMod Output Control Files"); fc.addChoosableFileFilter(xou); fc.setAcceptAllFileFilterUsed(false); fc.setFileFilter(tpl); fc.setDialogType(JFileChooser.SAVE_DIALOG); JGUIUtil.setWaitCursor(this, false); int retVal = fc.showSaveDialog(this); if (retVal != JFileChooser.APPROVE_OPTION) { return; } string currDir = (fc.getCurrentDirectory()).ToString(); if (!currDir.Equals(lastDirectorySelected, StringComparison.OrdinalIgnoreCase)) { JGUIUtil.setLastFileDialogDirectory(currDir); } string filename = fc.getSelectedFile().getName(); FileFilter ff = fc.getFileFilter(); if (ff == @out) { filename = IOUtil.enforceFileExtension(filename, "out"); } else if (ff == tpl) { filename = IOUtil.enforceFileExtension(filename, "tpo"); } else if (ff == xou) { filename = IOUtil.enforceFileExtension(filename, "xou"); } __dirty = false; __tableModel.setDirty(false); System.Collections.IList theGraphNodes = __worksheet.getAllData(); try { StateMod_GraphNode.writeStateModOutputControlFile(null, currDir + File.separator + filename, theGraphNodes, null); } catch (Exception e) { Message.printWarning(1, routine, "Error saving output control file\n" + "\"" + currDir + File.separator + filename + "\"", this); Message.printWarning(2, routine, e); } } else if (action.Equals(__BUTTON_LOAD_FILE)) { JGUIUtil.setWaitCursor(this, true); string lastDirectorySelected = JGUIUtil.getLastFileDialogDirectory(); JFileChooser fc = null; if (!string.ReferenceEquals(lastDirectorySelected, null)) { fc = new JFileChooser(lastDirectorySelected); } else { fc = new JFileChooser(); } fc.setDialogTitle("Select Output Control File"); SimpleFileFilter @out = new SimpleFileFilter("out", "StateMod Output Control Files"); fc.addChoosableFileFilter(@out); SimpleFileFilter tpl = new SimpleFileFilter("tpo", "StateMod Output Control Files"); fc.addChoosableFileFilter(tpl); SimpleFileFilter xou = new SimpleFileFilter("xou", "StateMod Output Control Files"); fc.addChoosableFileFilter(xou); fc.setAcceptAllFileFilterUsed(false); fc.setFileFilter(tpl); fc.setDialogType(JFileChooser.OPEN_DIALOG); JGUIUtil.setWaitCursor(this, false); int retVal = fc.showOpenDialog(this); if (retVal != JFileChooser.APPROVE_OPTION) { return; } string currDir = (fc.getCurrentDirectory()).ToString(); if (!currDir.Equals(lastDirectorySelected, StringComparison.OrdinalIgnoreCase)) { JGUIUtil.setLastFileDialogDirectory(currDir); } string filename = fc.getSelectedFile().getName(); JGUIUtil.setWaitCursor(this, true); __dirty = false; System.Collections.IList theGraphNodes = new List <object>(20, 1); try { __worksheet.clear(); __tableModel.setDirty(false); StateMod_GraphNode.readStateModOutputControlFile(theGraphNodes, currDir + File.separator + filename); int size = theGraphNodes.Count; StateMod_GraphNode g = null; int row = 0; for (int i = 0; i < size; i++) { row = __worksheet.getRowCount(); g = (StateMod_GraphNode)theGraphNodes[i]; if (i == 0) { if (g.getID().Equals("All")) { __useAllJCheckBox.setSelected(true); } else { __useAllJCheckBox.setSelected(false); } } __worksheet.addRow(g); __tableModel.fillIDColumn(row, g.getType()); } __worksheet.setData(theGraphNodes); } catch (Exception e) { Message.printWarning(1, routine, "Error loading output control file\n" + "\"" + currDir + File.separator + filename + "\"", this); Message.printWarning(2, routine, e); } JGUIUtil.setWaitCursor(this, false); } else if (action.Equals(__CHECKBOX_USE_ALL)) { // if the use all checkbox is selected, subtle changes from the // default functionality are made. The buttons to add and // delete rows are disabled, and the ID of the only record // in the worksheet is set to "All". This ID value is used // in the table model to determine when the checkbox is // selected. In addition, the ComboBox functionality of the // first and third data columns is turned off. if (__useAllJCheckBox.isSelected()) { __addRowButton.setEnabled(false); __deleteRowButton.setEnabled(false); __worksheet.setColumnJComboBoxValues(0, null); __worksheet.setColumnJComboBoxValues(2, null); __worksheet.clear(); StateMod_GraphNode g = new StateMod_GraphNode(); g.setID("All"); __worksheet.setCellEditable(0, 0, false); __worksheet.setCellEditable(0, 1, false); __worksheet.setCellEditable(0, 2, false); __worksheet.addRow(g); } else { __addRowButton.setEnabled(true); __deleteRowButton.setEnabled(true); System.Collections.IList v = StateMod_Util.arrayToList(StateMod_GraphNode.node_types); v.Add("Other"); __worksheet.setColumnJComboBoxValues(0, v); __worksheet.clear(); System.Collections.IList offOn = new List <object>(); offOn.Add("Off"); offOn.Add("On"); __worksheet.setCellEditable(0, 0, true); __worksheet.setColumnJComboBoxValues(2, offOn); } } }