Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. /
 
 
 
 package org.apache.cayenne.modeler;
 
 import java.io.File;
 import java.util.List;
 
 
 import  org.apache.cayenne.conf.Configuration;
 import  org.apache.cayenne.project.CayenneUserDir;
 
 import  com.jgoodies.looks.plastic.PlasticLookAndFeel;
 import  com.jgoodies.looks.plastic.PlasticTheme;

Main class responsible for starting CayenneModeler.

Since:
1.1
 
 public class Main {
 
     private static Log logObj = LogFactory.getLog(Main.class);

    
Main method that starts the CayenneModeler.
 
     public static void main(String[] args) {
         Main main = new Main();
 
         // if configured, redirect all logging to the log file
         main.configureLogging();
 
         // check jdk version
         if (!main.checkJDKVersion()) {
             System.exit(1);
         }
 
         File projectFile = projectFileFromArgs(args);
         main.runModeler(projectFile);
     }
 
     protected static File projectFileFromArgs(String[] args) {
         if (args.length == 1) {
             File f = new File(args[0]);
             if (f.isDirectory()) {
                 f = new File(f, Configuration.DEFAULT_DOMAIN_FILE);
             }
 
             if (f.isFile() && Configuration.DEFAULT_DOMAIN_FILE.equals(f.getName())) {
                 return f;
             }
         }
 
         return null;
     }
 
     protected static File projectFileFromPrefs() {
         // This must be run after the application has already been bootstrapped.  Otherwise, the returned
         // app instance will be null.
         
         if ((autoLoadPref != null) && (true == autoLoadPref.getBooleanProperty(.))) {
             ModelerPreferences modelerPreferences = ModelerPreferences.getPreferences();
             Vector arr = modelerPreferences.getVector(.);
 
             return new File((Stringarr.get(0));
         }
 
         return null;
     }
    protected void runModeler(final File projectFile) {
        .info("Starting CayenneModeler.");
        // set up UI
        configureLookAndFeel();
        . = new Application(projectFile);
        // start frame and load project from EventDispatchThread...
        Runnable runnable = new Runnable() {
            public void run() {
                ..startup();
                if (null == projectFile) {
                    File projectFileFromPrefs = projectFileFromPrefs();
                    if (null != projectFileFromPrefs) {
                        OpenProjectAction action = new OpenProjectAction(.);
                        action.openProject(projectFileFromPrefs);
                    }
                }
            }
        };
        SwingUtilities.invokeLater(runnable);
    }
    protected boolean checkJDKVersion() {
        try {
            Class.forName("java.lang.StringBuilder");
            return true;
        }
        catch (Exception ex) {
            .fatal("CayenneModeler requires JDK 1.5.");
            .fatal("Found : '"
                    + System.getProperty("java.version")
                    + "' at "
                    + System.getProperty("java.home"));
            JOptionPane.showMessageDialog(
                    null,
                    "Unsupported JDK at "
                            + System.getProperty("java.home")
                            + ". Set JAVA_HOME to the JDK1.5 location.",
                    "Unsupported JDK Version",
                    .);
            return false;
        }
    }

    
Configures Log4J appenders to perform logging to $HOME/.cayenne/modeler.log.
    protected void configureLogging() {
 
        // get preferences
        ModelerPreferences prefs = ModelerPreferences.getPreferences();
        // check whether to set up logging to a file
        boolean logfileEnabled = prefs.getBoolean(
                .,
                true);
                .valueOf(logfileEnabled));
        if (logfileEnabled) {
            String defaultPath = getLogFile().getPath();
            String logfilePath = prefs.getString(
                    .,
                    defaultPath);
            try {
                // use logfile from preferences or default
                File logfile = new File(logfilePath);
                if (logfile != null) {
                    if (!logfile.exists()) {
                        // create dir path first
                        File parent = logfile.getParentFile();
                        if (parent != null) {
                            parent.mkdirs();
                        }
                        if (!logfile.createNewFile()) {
                            return;
                        }
                    }
                    // remember working path
                    prefs.setProperty(.logfilePath);
                    // TODO: andrus, 8/16/2006 - redirect STDOUT and STDERR to file??
                    // TODO: andrus, 8/16/2006 - use Java logging API with comons-logging
                }
            }
            catch (IOException ioex) {
                .warn("Error setting logging - " + logfilePathioex);
            }
        }
    }
    protected String getLookAndFeelName() {
        ModelerPreferences prefs = ModelerPreferences.getPreferences();
        return prefs.getString(
                .,
                .);
    }
    protected String getThemeName() {
        ModelerPreferences prefs = ModelerPreferences.getPreferences();
        return prefs.getString(
                .,
                .);
    }

    
Set up the UI Look & Feel according to $HOME/.cayenne/modeler.preferences
    protected void configureLookAndFeel() {
        // get preferences
        ModelerPreferences prefs = ModelerPreferences.getPreferences();
  
        String lfName = getLookAndFeelName();
        String themeName = getThemeName();
        try {
            // only install theme if L&F is Plastic;
            // bomb out if the L&F class cannot be found at all.
            Class lf = Class.forName(lfName);
            if (PlasticLookAndFeel.class.isAssignableFrom(lf)) {
                PlasticTheme foundTheme = themeWithName(themeName);
                if (foundTheme == null) {
                    .warn("Could not set selected theme '"
                            + themeName
                            + "' - using default '"
                            + .
                            + "'.");
                    themeName = .;
                    foundTheme = themeWithName(themeName);
                }
                // try to configure theme
                PlasticLookAndFeel.setMyCurrentTheme(foundTheme);
            }
            // try to set set L&F
            UIManager.setLookAndFeel(lfName);
        }
        catch (Exception e) {
            .warn("Could not set selected LookAndFeel '"
                    + lfName
                    + "' - using default '"
                    + .
                    + "'.");
            // re-try with defaults
            lfName = .;
            themeName = .;
            PlasticTheme defaultTheme = themeWithName(themeName);
            PlasticLookAndFeel.setMyCurrentTheme(defaultTheme);
            try {
                UIManager.setLookAndFeel(lfName);
            }
            catch (Exception retry) {
                // give up, continue as-is
            }
        }
        finally {
            // remember L&F settings
            prefs.setProperty(., UIManager
                    .getLookAndFeel()
                    .getClass()
                    .getName());
            prefs.setProperty(.themeName);
        }
    }
    protected PlasticTheme themeWithName(String themeName) {
        List availableThemes = PlasticLookAndFeel.getInstalledThemes();
        for (Iterator i = availableThemes.iterator(); i.hasNext();) {
            PlasticTheme aTheme = (PlasticTheme) i.next();
            if (themeName.equals(aTheme.getName())) {
                return aTheme;
            }
        }
        return null;
    }

    
Returns a file corresponding to $HOME/.cayenne/modeler.log
    protected File getLogFile() {
        if (!CayenneUserDir.getInstance().canWrite()) {
            return null;
        }
        return CayenneUserDir.getInstance().resolveFile("modeler.log");
    }
New to GrepCode? Check out our FAQ X