예제 #1
0
        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);
            }
        }
예제 #4
0
        /// <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);
            }
        }