1 56 57 package org.objectstyle.cayenne.modeler; 58 59 import java.io.File ; 60 import java.io.IOException ; 61 import java.util.Iterator ; 62 import java.util.List ; 63 64 import javax.swing.JOptionPane ; 65 import javax.swing.SwingUtilities ; 66 import javax.swing.UIManager ; 67 68 import org.apache.log4j.FileAppender; 69 import org.apache.log4j.Layout; 70 import org.apache.log4j.Logger; 71 import org.apache.log4j.PatternLayout; 72 import org.objectstyle.cayenne.conf.Configuration; 73 import org.objectstyle.cayenne.project.CayenneUserDir; 74 75 import com.jgoodies.looks.plastic.PlasticLookAndFeel; 76 import com.jgoodies.looks.plastic.PlasticTheme; 77 78 84 public class Main { 85 86 private static Logger logObj = Logger.getLogger(Main.class); 87 88 91 public static void main(String [] args) { 92 Main main = new Main(); 93 94 main.configureLogging(); 96 97 if (!main.checkJDKVersion()) { 99 System.exit(1); 100 } 101 102 main.configureMacOSX(); 104 105 File projectFile = projectFileFromArgs(args); 106 main.runModeler(projectFile); 107 } 108 109 protected static File projectFileFromArgs(String [] args) { 110 if (args.length == 1) { 111 File f = new File (args[0]); 112 if (f.isDirectory()) { 113 f = new File (f, Configuration.DEFAULT_DOMAIN_FILE); 114 } 115 116 if (f.isFile() && Configuration.DEFAULT_DOMAIN_FILE.equals(f.getName())) { 117 return f; 118 } 119 } 120 121 return null; 122 } 123 124 protected void runModeler(File projectFile) { 125 logObj.info("Starting CayenneModeler."); 126 127 configureLookAndFeel(); 129 130 Application.instance = new Application(projectFile); 131 132 Runnable runnable = new Runnable () { 134 135 public void run() { 136 Application.instance.startup(); 137 } 138 }; 139 140 SwingUtilities.invokeLater(runnable); 141 } 142 143 protected boolean checkJDKVersion() { 144 try { 145 Class.forName("javax.swing.SpringLayout"); 146 return true; 147 } 148 catch (Exception ex) { 149 logObj.fatal("CayenneModeler requires JDK 1.4."); 150 logObj.fatal("Found : '" 151 + System.getProperty("java.version") 152 + "' at " 153 + System.getProperty("java.home")); 154 155 JOptionPane.showMessageDialog( 156 null, 157 "Unsupported JDK at " 158 + System.getProperty("java.home") 159 + ". Set JAVA_HOME to the JDK1.4 location.", 160 "Unsupported JDK Version", 161 JOptionPane.ERROR_MESSAGE); 162 return false; 163 } 164 } 165 166 protected void configureMacOSX() { 167 if (System.getProperty("os.name").toLowerCase().indexOf("mac") < 0) { 168 return; 169 } 170 171 try { 172 MacOSXSetup.configureMacOSX(); 173 } 174 catch (Exception ex) { 175 } 177 } 178 179 182 protected void configureLogging() { 183 Configuration.configureCommonLogging(); 185 186 ModelerPreferences prefs = ModelerPreferences.getPreferences(); 188 189 boolean logfileEnabled = prefs.getBoolean( 191 ModelerPreferences.EDITOR_LOGFILE_ENABLED, 192 true); 193 prefs.setProperty(ModelerPreferences.EDITOR_LOGFILE_ENABLED, String 194 .valueOf(logfileEnabled)); 195 196 if (logfileEnabled) { 197 String defaultPath = getLogFile().getPath(); 198 String logfilePath = prefs.getString( 199 ModelerPreferences.EDITOR_LOGFILE, 200 defaultPath); 201 try { 202 204 File logfile = new File (logfilePath); 205 206 if (logfile != null) { 207 if (!logfile.exists()) { 208 File parent = logfile.getParentFile(); 210 if (parent != null) { 211 parent.mkdirs(); 212 } 213 214 if (!logfile.createNewFile()) { 215 logObj.warn("Can't create log file, ignoring."); 216 return; 217 } 218 } 219 220 prefs.setProperty(ModelerPreferences.EDITOR_LOGFILE, logfilePath); 222 223 Logger p1 = logObj; 225 Logger p2 = null; 226 while ((p2 = (Logger) p1.getParent()) != null) { 227 p1 = p2; 228 } 229 230 Layout layout = new PatternLayout( 231 "CayenneModeler %-5p [%t %d{MM-dd HH:mm:ss}] %c: %m%n"); 232 p1.removeAllAppenders(); 233 p1.addAppender(new FileAppender( 234 layout, 235 logfile.getCanonicalPath(), 236 true)); 237 } 238 } 239 catch (IOException ioex) { 240 logObj.warn("Error setting logging - " + logfilePath, ioex); 241 } 242 } 243 } 244 245 248 protected void configureLookAndFeel() { 249 ModelerPreferences prefs = ModelerPreferences.getPreferences(); 251 252 String lfName = prefs.getString( 254 ModelerPreferences.EDITOR_LAFNAME, 255 ModelerConstants.DEFAULT_LAF_NAME); 256 String themeName = prefs.getString( 258 ModelerPreferences.EDITOR_THEMENAME, 259 ModelerConstants.DEFAULT_THEME_NAME); 260 261 try { 262 Class lf = Class.forName(lfName); 265 if (PlasticLookAndFeel.class.isAssignableFrom(lf)) { 266 PlasticTheme foundTheme = themeWithName(themeName); 267 if (foundTheme == null) { 268 logObj.warn("Could not set selected theme '" 269 + themeName 270 + "' - using default '" 271 + ModelerConstants.DEFAULT_THEME_NAME 272 + "'."); 273 274 themeName = ModelerConstants.DEFAULT_THEME_NAME; 275 foundTheme = themeWithName(themeName); 276 } 277 278 PlasticLookAndFeel.setMyCurrentTheme(foundTheme); 280 } 281 282 UIManager.setLookAndFeel(lfName); 284 } 285 catch (Exception e) { 286 logObj.warn("Could not set selected LookAndFeel '" 287 + lfName 288 + "' - using default '" 289 + ModelerConstants.DEFAULT_LAF_NAME 290 + "'."); 291 292 lfName = ModelerConstants.DEFAULT_LAF_NAME; 294 themeName = ModelerConstants.DEFAULT_THEME_NAME; 295 PlasticTheme defaultTheme = themeWithName(themeName); 296 PlasticLookAndFeel.setMyCurrentTheme(defaultTheme); 297 298 try { 299 UIManager.setLookAndFeel(lfName); 300 } 301 catch (Exception retry) { 302 } 304 } 305 finally { 306 prefs.setProperty(ModelerPreferences.EDITOR_LAFNAME, UIManager 308 .getLookAndFeel() 309 .getClass() 310 .getName()); 311 312 prefs.setProperty(ModelerPreferences.EDITOR_THEMENAME, themeName); 313 } 314 } 315 316 protected PlasticTheme themeWithName(String themeName) { 317 List availableThemes = PlasticLookAndFeel.getInstalledThemes(); 318 for (Iterator i = availableThemes.iterator(); i.hasNext();) { 319 PlasticTheme aTheme = (PlasticTheme) i.next(); 320 if (themeName.equals(aTheme.getName())) { 321 return aTheme; 322 } 323 } 324 return null; 325 } 326 327 330 protected File getLogFile() { 331 if (!CayenneUserDir.getInstance().canWrite()) { 332 return null; 333 } 334 335 return CayenneUserDir.getInstance().resolveFile("modeler.log"); 336 } 337 } | Popular Tags |