001    /*
002     * CRIMSON
003     * Copyright (c) 2006, Stephen Fisher, Susan Davidson, and Junhyong Kim, 
004     * University of Pennsylvania.
005     *
006     * This program is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU General Public License as
008     * published by the Free Software Foundation; either version 2 of the
009     * License, or (at your option) any later version.
010     *
011     * This program is distributed in the hope that it will be useful, but
012     * WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014     * General Public License for more details.
015     *
016     * You should have received a copy of the GNU General Public License
017     * along with this program; if not, write to the Free Software
018     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019     * 02110-1301 USA.
020     *
021     * @(#)ModelUtils.java
022     */
023    
024    package edu.upenn.crimson.gui;
025    
026    import edu.upenn.crimson.*;
027    import edu.upenn.crimson.io.*;
028    import java.util.ArrayList;
029    import javax.swing.*;
030    
031    /**
032     * Static methods used throughout the GUI for Model objects.
033     *
034     * @author  Stephen Fisher
035     * @version $Id: ModelUtils.java,v 1.7 2007/05/16 18:55:58 fisher Exp $
036     */
037    
038    public class ModelUtils {
039    
040        //--------------------------------------------------------------------------
041        // Miscellaneous Methods
042    
043             /** Present a dialog box for selecting a particular Model. */
044             public static String modelSelector() {
045                      // use this to get the return value from ObjectSelectorDialog
046                      ArrayList out = new ArrayList();
047                      //              Object[] objects = ObjectHandles.getModels().toArray();
048                      Object[] objects = ObjectHandles.getModelPool().values().toArray();
049                      new ObjectSelectorDialog("Model Selector", objects, out);
050    
051                      if (out.size() > 0) {
052                                    return ((Model) out.get(0)).getID();
053                      } else {
054                                    return "";
055                      }
056             }
057    
058             /**
059              * This will use a FieldEditDialog to ask the user for a model ID.
060              */
061             public static String getModelID(String id) {
062                      // get model ID
063                      String[] labels = {" Model ID:" };
064    
065                      // use this to get the return value from FieldEditDialog
066                      ArrayList textFields = new ArrayList();
067    
068                      // get the value the user entered
069                      JTextField idField = new JTextField(20);
070                      idField.setText(id);
071                      textFields.add(idField);
072    
073                      new FieldEditDialog("New Model", labels, textFields);
074    
075                      // check "exit code" for FieldEditDialog, if false then exit
076                      Boolean exitCode = (Boolean) textFields.get(textFields.size()-1);
077                      if (! exitCode.booleanValue()) return null;
078    
079                      // return ID
080                      return idField.getText();
081             }
082    
083             /**
084              * This will delete the model specified by ModelSelector().
085              */
086             public static void deleteModel(String id) {
087                      // if no database connection then error
088                      if (! Database.isOpen()) {
089                                    CrimsonUtils.printMsg("Must open a database before deleting a model.", 
090                                                                                             CrimsonUtils.ERROR);
091                                    return;
092                      }
093    
094                      if (CrimsonUtils.isEmpty(id)) {
095                                    id = modelSelector();
096    
097                                    // if ID still empty then quit
098                                    if (CrimsonUtils.isEmpty(id)) return;
099                      }
100    
101                      String msg = "Are you sure you want to remove this model?\n";
102                      msg += "Model: " + id;
103                      Object[] options = {"Delete Model", "Cancel"};
104                      int flag = JOptionPane.showOptionDialog(null, msg,
105                                                                                                                                    "Delete Confirmation",
106                                                                                                                                    JOptionPane.YES_NO_OPTION,
107                                                                                                                                    JOptionPane.QUESTION_MESSAGE,
108                                                                                                                                    null,
109                                                                                                                                    options,
110                                                                                                                                    options[1]);
111                      if (flag == JOptionPane.YES_OPTION) { // "Delete"
112                                    // delete model
113                                    Root.runCommand("deleteModel(\"" + id + "\")");
114                      }
115             }
116    
117             /**
118              * This will display the specified model. If no model is
119              * specified, then the ModelSelector() will be used to choose a
120              * model.
121              */
122             public static void viewModel(String id) {
123                      // if no database connection then error
124                      if (! Database.isOpen()) {
125                                    CrimsonUtils.printMsg("Must open a database before viewing a model.", 
126                                                                                             CrimsonUtils.ERROR);
127                                    return;
128                      }
129    
130                      // if no ID, then use modelSelector() to get model
131                      if (CrimsonUtils.isEmpty(id)) {
132                                    id = modelSelector();
133    
134                                    // if ID still empty then quit
135                                    if (CrimsonUtils.isEmpty(id)) return;
136                      }
137    
138                      Model model = ObjectHandles.getModel(id);
139    
140                      // make sure the model exists
141                      if (model == null) return;
142    
143                      JOptionPane.showMessageDialog(null, model.toStringFull(), "Model Viewer", 
144                                                                                                      JOptionPane.INFORMATION_MESSAGE);
145             }
146    
147             /** This will edit the model. */
148             public static void editModel(String id) {
149                      // if no database connection then error
150                      if (! Database.isOpen()) {
151                                    CrimsonUtils.printMsg("Database not open.", CrimsonUtils.ERROR);
152                                    return;
153                      }
154    
155                      // if no ID, then use modelSelector() to get model
156                      if (CrimsonUtils.isEmpty(id)) {
157                                    id = modelSelector();
158    
159                                    // if ID still empty then quit
160                                    if (CrimsonUtils.isEmpty(id)) return;
161                      }
162    
163                      Root.runCommand("editModel(\"" + id + "\")");
164             }
165    
166    } // ModelUtils.java