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 * @(#)QueryUtils.java 022 */ 023 024 package edu.upenn.crimson.gui; 025 026 import edu.upenn.crimson.*; 027 import edu.upenn.crimson.io.*; 028 import java.io.File; 029 import java.util.ArrayList; 030 import javax.swing.*; 031 032 /** 033 * Static methods used throughout the GUI for Query objects. 034 * 035 * @author Stephen Fisher 036 * @version $Id: QueryUtils.java,v 1.6 2007/06/14 19:48:42 fisher Exp $ 037 */ 038 039 public class QueryUtils { 040 041 //-------------------------------------------------------------------------- 042 // Miscellaneous Methods 043 044 /** 045 * Present a dialog box for selecting a particular Query, from 046 * the set of queries in memory. 047 */ 048 public static String querySelector() { 049 // use this to get the return value from ObjectSelectorDialog 050 ArrayList out = new ArrayList(); 051 Object[] objects = ObjectHandles.getQueries().toArray(); 052 new ObjectSelectorDialog("Query Selector", objects, out); 053 054 if (out.size() > 0) { 055 return (String) out.get(0); 056 } else { 057 return ""; 058 } 059 } 060 061 /** 062 * Present a dialog box for selecting a particular Query, from the 063 * queries in the QUERIES table. 064 */ 065 public static String querySelectorDB() { 066 // use this to get the return value from ObjectSelectorDialog 067 ArrayList out = new ArrayList(); 068 Object[] objects = Queries.dbList().toArray(); 069 new ObjectSelectorDialog("Query Selector", objects, out); 070 071 if (out.size() > 0) { 072 return (String) out.get(0); 073 } else { 074 return ""; 075 } 076 } 077 078 /** 079 * This will use a FieldEditDialog to ask the user for a query id. 080 */ 081 public static String getQueryID(String id) { 082 // get query id 083 String[] labels = {" Query ID:"}; 084 085 // use this to get the return value from FieldEditDialog 086 ArrayList textFields = new ArrayList(); 087 088 // get the value the user entered 089 JTextField idField = new JTextField(20); 090 idField.setText(id); 091 textFields.add(idField); 092 093 new FieldEditDialog("Query ID", labels, textFields); 094 095 // check "exit code" for FieldEditDialog, if false then exit 096 Boolean exitCode = (Boolean) textFields.get(textFields.size()-1); 097 if (! exitCode.booleanValue()) return null; 098 099 // return ID 100 return idField.getText(); 101 } 102 103 /** 104 * This will create a new query object. The tree selector will be 105 * used to get the treeID. 106 */ 107 public static void newQuery() { 108 String treeID = TreeUtils.treeSelector(); 109 110 // if ID still empty then quit 111 if (CrimsonUtils.isEmpty(treeID)) return; 112 113 Root.runCommand("newQuery(\"" + treeID + "\")"); 114 } 115 116 /** This will load a query object from the databasee. */ 117 public static void loadQuery() { 118 // if no database connection then error 119 if (! Database.isOpen()) { 120 CrimsonUtils.printError("No database open."); 121 return; 122 } 123 124 String id = QueryUtils.querySelectorDB(); 125 if (! CrimsonUtils.isEmpty(id)) Root.runCommand("loadQuery(\"" + id + "\")"); 126 } 127 128 /** 129 * This will import a query from a python file. 130 * @XXX We don't use a file chooser here because we can only load 131 */ 132 public static void importQuery() { 133 // get file containing query 134 File file = GUIUtils.openFileChooser("Import Query", GUIUtils.getPyFilter()); 135 136 // no valid file selected 137 if (file == null) return; 138 139 String msg = "importQuery(\"" + GUIUtils.getFilename(file) + "\")"; 140 Root.runCommand(msg); 141 } 142 143 /** This will export a query as a python file. */ 144 public static void exportQuery(String id) { 145 // if no ID, then use querySelector() to get query 146 if (CrimsonUtils.isEmpty(id)) { 147 id = querySelector(); 148 149 // if ID still empty then quit 150 if (CrimsonUtils.isEmpty(id)) return; 151 } 152 153 // get file to contain query 154 File file = GUIUtils.saveFileChooser("Export Query", id, GUIUtils.getPyFilter()); 155 156 // no valid file selected 157 if (file == null) return; 158 159 String msg = "exportQuery(\"" + id + "\", \"" + GUIUtils.getFilename(file) + "\")"; 160 Root.runCommand(msg); 161 } 162 163 /** This will save the query to the database. */ 164 public static void publishQuery(String id) { 165 // if no database connection then error 166 if (! Database.isOpen()) { 167 CrimsonUtils.printError("Database not open."); 168 return; 169 } 170 171 // if no ID, then use querySelector() to get query 172 if (CrimsonUtils.isEmpty(id)) { 173 id = querySelector(); 174 175 // if ID still empty then quit 176 if (CrimsonUtils.isEmpty(id)) return; 177 } 178 179 Root.runCommand("publishQuery(\"" + id + "\")"); 180 } 181 182 /** This will run the query. */ 183 public static void runQueryDialog(String id) { 184 runQueryDialog(id, "", false, 1); 185 } 186 187 public static void runQueryDialog(String id, String file, boolean incSequence, int repeat) { 188 // if no database connection then error 189 if (! Database.isOpen()) { 190 CrimsonUtils.printError("Database not open."); 191 return; 192 } 193 194 // if no ID, then use querySelector() to get query 195 if (CrimsonUtils.isEmpty(id)) { 196 id = querySelector(); 197 198 // if ID still empty then quit 199 if (CrimsonUtils.isEmpty(id)) return; 200 } 201 202 // QueryDialog() will handle the actual running of the query 203 new QueryDialog(id, file, incSequence, repeat); 204 } 205 206 /** 207 * This will run the query. 208 * @XXX Should run SaveFileChooser() if empty output file. 209 */ 210 public static void runQuery(String id, String file, boolean incSequence, int repeat) { 211 // if no database connection then error 212 if (! Database.isOpen()) { 213 CrimsonUtils.printError("Database not open."); 214 return; 215 } 216 217 // if no output file then error 218 if (CrimsonUtils.isEmpty(file)) { 219 CrimsonUtils.printError("Invalid output file."); 220 return; 221 } 222 223 // if ID empty then quit 224 if (CrimsonUtils.isEmpty(id)) return; 225 226 String cmd = "runQuery(\"" + id + "\", \"" + file + "\", "; 227 cmd += ((incSequence) ? "1, " : "0, ") + Integer.toString(repeat) + ")"; 228 Root.runCommand(cmd); 229 } 230 231 /** This will edit the query. */ 232 public static void editQuery(String id) { 233 // if no database connection then error 234 if (! Database.isOpen()) { 235 CrimsonUtils.printError("Database not open."); 236 return; 237 } 238 239 // if no ID, then use querySelector() to get query 240 if (CrimsonUtils.isEmpty(id)) { 241 id = querySelector(); 242 243 // if ID still empty then quit 244 if (CrimsonUtils.isEmpty(id)) return; 245 } 246 247 Root.runCommand("editQuery(\"" + id + "\")"); 248 } 249 250 /** 251 * This will delete the specified query. If no query is specified, 252 * then the QuerySelector() will be used to choose a query. 253 */ 254 public static void deleteQuery(String id) { 255 // if no database connection then error 256 if (! Database.isOpen()) { 257 CrimsonUtils.printError("Must open a database before deleting a query."); 258 return; 259 } 260 261 if (CrimsonUtils.isEmpty(id)) id = querySelector(); 262 263 // if 'id' still empty then quit 264 if (CrimsonUtils.isEmpty(id)) return; 265 266 String msg = "Are you sure you want to delete this query?\n"; 267 msg += "Query: " + id; 268 Object[] options = {"Delete Query", "Cancel"}; 269 int flag = JOptionPane.showOptionDialog(null, msg, 270 "Delete Confirmation", 271 JOptionPane.YES_NO_OPTION, 272 JOptionPane.QUESTION_MESSAGE, 273 null, 274 options, 275 options[1]); 276 if (flag == JOptionPane.YES_OPTION) { // "Delete" 277 if (Queries.dbContains(id)) { 278 // query is in database so see if user wants to delete the 279 // database copy as well 280 msg = "Do you also want to remove this query from the database?"; 281 Object[] options2 = {"Yes Delete From Database", "No", "Cancel"}; 282 flag = JOptionPane.showOptionDialog(null, msg, 283 "Delete Confirmation", 284 JOptionPane.YES_NO_CANCEL_OPTION, 285 JOptionPane.QUESTION_MESSAGE, 286 null, 287 options2, 288 options2[2]); 289 if (flag == JOptionPane.YES_OPTION) { // delete from db 290 Root.runCommand("deleteQuery(\"" + id + "\", 1)"); 291 } else if (flag == JOptionPane.NO_OPTION) { // don't delete from db 292 Root.runCommand("deleteQuery(\"" + id + "\", 0)"); 293 } 294 } else { 295 // query not in database so just delete from queryPool 296 Root.runCommand("deleteQuery(\"" + id + "\", 0)"); 297 } 298 } 299 } 300 301 /** 302 * This will display the specified query. If no query is 303 * specified, then the QuerySelector() will be used to choose a 304 * query. 305 */ 306 public static void viewQuery(String id) { 307 // if no database connection then error 308 if (! Database.isOpen()) { 309 CrimsonUtils.printError("Must open a database before viewing a query."); 310 return; 311 } 312 313 // if no ID, then use querySelector() to get query 314 if (CrimsonUtils.isEmpty(id)) { 315 id = querySelector(); 316 317 // if ID still empty then quit 318 if (CrimsonUtils.isEmpty(id)) return; 319 } 320 321 Query query = ObjectHandles.getQuery(id); 322 323 // make sure the query exists 324 if (query == null) return; 325 326 JOptionPane.showMessageDialog(null, query.toString(), "Query Viewer", 327 JOptionPane.INFORMATION_MESSAGE); 328 } 329 330 } // QueryUtils.java