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 * @(#)CrimsonUtils.java 022 */ 023 024 package edu.upenn.crimson; 025 026 import edu.upenn.crimson.gui.Root; 027 import java.util.Collection; 028 import java.util.HashSet; 029 import java.util.Iterator; 030 import java.io.File; 031 import java.awt.Toolkit; 032 import javax.swing.JTextArea; 033 import java.util.Random; 034 035 /** 036 * CrimsonUtils. Miscellaneous functions. 037 * 038 * @author Stephen Fisher 039 * @version $Id: CrimsonUtils.java,v 1.19 2009/07/25 03:37:09 fisher Exp $ 040 */ 041 042 public class CrimsonUtils { 043 044 public final static int WARNING = 1; 045 public final static int ERROR = 2; 046 public final static int FEEDBACK = 3; 047 048 /** 049 * Amount of detailed feedback: 2 = lots, 1 = no warnings 050 * (default), 0 = no feedback. 051 */ 052 private static int VERBOSE = CrimsonMain.userDefaults.getInt("VERBOSE", 2); 053 054 /** String containing the location of the temporary dataloader files. */ 055 private static String TMP_DIR = CrimsonMain.userDefaults.get("TMP_DIR", ""); 056 057 /** String containing username for database connection. */ 058 private static String USERNAME = CrimsonMain.userDefaults.get("USERNAME", ""); 059 060 /** String containing server connection information. */ 061 private static String SERVER = CrimsonMain.userDefaults.get("SERVER", ""); 062 063 /** String containing port connection information (default = 1521). */ 064 private static int PORT = CrimsonMain.userDefaults.getInt("PORT", 1521); 065 066 /** String containing type of database ("oracle" or "mysql"). */ 067 private static String DB_TYPE = CrimsonMain.userDefaults.get("DB_TYPE", ""); 068 069 /** String containing database name (SID). */ 070 private static String DATABASE = CrimsonMain.userDefaults.get("DATABASE", ""); 071 072 /** Global random object. */ 073 public static Random random = new Random(System.currentTimeMillis()); 074 075 /** 076 * If not null, all error and warning messages will be sent here. 077 * This messages panel can be disabled by setting the value to 078 * null. When disabled, all messages printed will be sent to 079 * stderr, which will effectively display them in the console. If 080 * the GUI menu is not visible then messages will be sent to 081 * stderr in addition to guiMessages (if it exists). 082 */ 083 public static JTextArea guiMessages = null; 084 085 //-------------------------------------------------------------------------- 086 // Getters and Setters 087 088 /** Set the VERBOSE flag. */ 089 public static void setVerbose(int verbose) { 090 VERBOSE = verbose; 091 CrimsonMain.userDefaults.putInt("VERBOSE", verbose); 092 } 093 094 /** Get the VERBOSE flag. */ 095 public static int getVerbose() { return VERBOSE; } 096 097 /** Set the TMP_DIR variable. */ 098 public static void setTmpDir(String tmpDir) { 099 File dir = new File(tmpDir); 100 101 // only change value if the new value is valid 102 if ((! dir.exists()) || dir.isDirectory()) { 103 TMP_DIR = tmpDir.trim(); 104 CrimsonMain.userDefaults.put("TMP_DIR", tmpDir); 105 } else { 106 printError("TMP_DIR must be set to a directory: " + tmpDir); 107 } 108 } 109 110 /** Get the TMP_DIR. */ 111 public static String getTmpDir() { return TMP_DIR; } 112 113 /** Set the USERNAME flag. */ 114 public static void setUsername(String username) { 115 USERNAME = username.trim(); 116 CrimsonMain.userDefaults.put("USERNAME", username); 117 } 118 119 /** Get the USERNAME flag. */ 120 public static String getUsername() { return USERNAME; } 121 122 /** Set the SERVER flag. */ 123 public static void setServer(String server) { 124 SERVER = server.trim(); 125 CrimsonMain.userDefaults.put("SERVER", server); 126 } 127 128 /** Get the SERVER flag. */ 129 public static String getServer() { return SERVER; } 130 131 /** Set the PORT flag. */ 132 public static void setPort(int port) { 133 PORT = port; 134 CrimsonMain.userDefaults.putInt("PORT", port); 135 } 136 137 /** Get the PORT flag. */ 138 public static int getPort() { return PORT; } 139 140 /** Set the DB_TYPE flag. */ 141 public static void setDBType(String dbType) { 142 DB_TYPE = dbType.trim(); 143 CrimsonMain.userDefaults.put("DB_TYPE", dbType); 144 } 145 146 /** Get the DB_TYPE flag. */ 147 public static String getDBType() { return DB_TYPE; } 148 149 /** Set the DATABASE flag. */ 150 public static void setDatabase(String database) { 151 DATABASE = database.trim(); 152 CrimsonMain.userDefaults.put("DATABASE", database); 153 } 154 155 /** Get the DATABASE flag. */ 156 public static String getDatabase() { return DATABASE; } 157 158 /** 159 * The seed is initially set to the system time. This routine can 160 * be used to change the seed. 161 */ 162 public static void setRandomSeed(long seed) { random.setSeed(seed); } 163 164 //-------------------------------------------------------------------------- 165 // Miscellaneous Methods 166 167 /** Returns true if 'str' is empty (ignores spaces) or null. */ 168 public static boolean isEmpty(String str) { 169 if ((str == null) || (str.trim().length() == 0)) return true; 170 else return false; 171 } 172 173 /** Returns true if 'collection' is empty or null. */ 174 public static boolean isEmpty(Collection collection) { 175 if ((collection == null) || (collection.size() == 0)) return true; 176 else return false; 177 } 178 179 /** RE-set Crimson user defaults. */ 180 public static void resetCrimsonDefaults() { 181 setVerbose(1); 182 setTmpDir(""); 183 setUsername(""); 184 setServer(""); 185 setPort(1521); 186 setDBType(""); 187 setDatabase(""); 188 } 189 190 /** 191 * This will create the temporary directory, if necessary, 192 * returning 'true' if successful. Any ancestorial directories 193 * that don't already exist will also be created. We don't 194 * provide a method to remove the temporary directory. It is 195 * advised that the user perform this function manually through 196 * the operating system, in order to make sure critical files are 197 * not accidentally removed. 198 */ 199 public static boolean createTmpDir() { 200 File dir = new File(TMP_DIR); 201 if (dir.exists()) return true; 202 203 if (dir.mkdirs()) { 204 return true; 205 } else { 206 CrimsonUtils.printWarning("Unable to create temporary directory: " + TMP_DIR); 207 return false; 208 } 209 } 210 211 /** 212 * This will delete the specified file, if it exists in TMP_DIR. 213 * If the file doesn't exist, then this won't do anything. 214 */ 215 public static boolean deleteTmpFile(String filename) { 216 File file = new File(TMP_DIR, filename); 217 218 // if files doesn't exist then just return 219 if (! file.exists()) return true; 220 221 if (file.delete()) { 222 printMsg("Deleted temporary file: " + file.getName()); 223 return true; 224 } else { 225 CrimsonUtils.printWarning("Unable to delete temporary file: " + TMP_DIR + "\\" + filename); 226 return false; 227 } 228 } 229 230 /** 231 * This will return the elements of a HashSet as a ':' delimited 232 * string. 233 */ 234 public static String setToList(HashSet set) { 235 StringBuffer buffer = new StringBuffer(""); 236 if ((set != null) && (set.size() > 0)) { 237 // write items separated by ':' 238 Iterator i = set.iterator(); 239 buffer.append(i.next()); 240 while (i.hasNext()) buffer.append(":" + i.next()); 241 } 242 return buffer.toString(); 243 } 244 245 /** Get the class name without any package info. */ 246 public static String getClassName(Object o) { 247 String classString = o.getClass().getName(); 248 int dotIndex = classString.lastIndexOf("."); 249 return classString.substring(dotIndex+1); 250 } 251 252 /** Display the message to either the gui or stderr. */ 253 public static void printWarning(String msg) { 254 printMsg(msg, WARNING, true); 255 } 256 257 /** Display the message to either the gui or stderr. */ 258 public static void printError(String msg) { 259 printMsg(msg, ERROR, true); 260 } 261 262 /** Display the message to either the gui or stderr. */ 263 public static void printMsg(String msg) { 264 printMsg(msg, 0, true); 265 } 266 267 /** Display the message to either the gui or stderr. */ 268 public static void printMsg(String msg, int type) { 269 printMsg(msg, type, true); 270 } 271 272 /** Display the message to either the gui or stderr. */ 273 public static void printMsg(String msg, boolean newline) { 274 printMsg(msg, 0, newline); 275 } 276 277 /** 278 * Display the message to either the gui or stderr. The message 279 * type can be set which will add the appropriate label to the 280 * message (1 = warning, 2 = error). 281 */ 282 public static void printMsg(String msg, int type, boolean newline) { 283 // if VERBOSE = 0, then don't print any messages 284 if (VERBOSE == 0) return; 285 286 // add appropriate message label 287 switch (type) { 288 case FEEDBACK: 289 if (VERBOSE < 2) return; 290 break; 291 case WARNING: 292 if (VERBOSE < 2) return; 293 msg = " * WARNING: " + msg; 294 break; 295 case ERROR: 296 if (VERBOSE < 1) return; 297 msg = " ** ERROR: " + msg; 298 Toolkit.getDefaultToolkit().beep(); 299 break; 300 // default: msg = " " + msg; 301 } 302 303 if (newline) msg += "\n"; 304 305 if (guiMessages == null) { 306 System.err.print(msg); 307 } else { 308 guiMessages.append(msg); 309 // if menu() not visible, then also send messages to the 310 // command line 311 if (! Root.isVisible()) System.err.print(msg); 312 } 313 } 314 315 } // CrimsonUtils.java