KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectstyle > cayenne > modeler > Main


1 /* ====================================================================
2  *
3  * The ObjectStyle Group Software License, version 1.1
4  * ObjectStyle Group - http://objectstyle.org/
5  *
6  * Copyright (c) 2002-2005, Andrei (Andrus) Adamchik and individual authors
7  * of the software. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if any,
22  * must include the following acknowlegement:
23  * "This product includes software developed by independent contributors
24  * and hosted on ObjectStyle Group web site (http://objectstyle.org/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The names "ObjectStyle Group" and "Cayenne" must not be used to endorse
29  * or promote products derived from this software without prior written
30  * permission. For written permission, email
31  * "andrus at objectstyle dot org".
32  *
33  * 5. Products derived from this software may not be called "ObjectStyle"
34  * or "Cayenne", nor may "ObjectStyle" or "Cayenne" appear in their
35  * names without prior written permission.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE OBJECTSTYLE GROUP OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals and hosted on ObjectStyle Group web site. For more
53  * information on the ObjectStyle Group, please see
54  * <http://objectstyle.org/>.
55  */

56
57 package org.objectstyle.cayenne.modeler;
58
59 import java.io.File JavaDoc;
60 import java.io.IOException JavaDoc;
61 import java.util.Iterator JavaDoc;
62 import java.util.List JavaDoc;
63
64 import javax.swing.JOptionPane JavaDoc;
65 import javax.swing.SwingUtilities JavaDoc;
66 import javax.swing.UIManager JavaDoc;
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 /**
79  * Main class responsible for starting CayenneModeler.
80  *
81  * @author Andrei Adamchik
82  * @since 1.1
83  */

84 public class Main {
85
86     private static Logger logObj = Logger.getLogger(Main.class);
87
88     /**
89      * Main method that starts the CayenneModeler.
90      */

91     public static void main(String JavaDoc[] args) {
92         Main main = new Main();
93
94         // if configured, redirect all logging to the log file
95
main.configureLogging();
96
97         // check jdk version
98
if (!main.checkJDKVersion()) {
99             System.exit(1);
100         }
101
102         // run Mac hooks
103
main.configureMacOSX();
104
105         File JavaDoc projectFile = projectFileFromArgs(args);
106         main.runModeler(projectFile);
107     }
108
109     protected static File JavaDoc projectFileFromArgs(String JavaDoc[] args) {
110         if (args.length == 1) {
111             File JavaDoc f = new File JavaDoc(args[0]);
112             if (f.isDirectory()) {
113                 f = new File JavaDoc(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 JavaDoc projectFile) {
125         logObj.info("Starting CayenneModeler.");
126
127         // set up UI
128
configureLookAndFeel();
129
130         Application.instance = new Application(projectFile);
131
132         // start frame and load project from EventDispatchThread...
133
Runnable JavaDoc runnable = new Runnable JavaDoc() {
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 JavaDoc 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 JavaDoc ex) {
175             // ignore... not a mac
176
}
177     }
178
179     /**
180      * Configures Log4J appenders to perform logging to $HOME/.cayenne/modeler.log.
181      */

182     protected void configureLogging() {
183         // read default Cayenne log configuration
184
Configuration.configureCommonLogging();
185
186         // get preferences
187
ModelerPreferences prefs = ModelerPreferences.getPreferences();
188
189         // check whether to set up logging to a file
190
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 JavaDoc defaultPath = getLogFile().getPath();
198             String JavaDoc logfilePath = prefs.getString(
199                     ModelerPreferences.EDITOR_LOGFILE,
200                     defaultPath);
201             try {
202                 // use logfile from preferences or default
203

204                 File JavaDoc logfile = new File JavaDoc(logfilePath);
205
206                 if (logfile != null) {
207                     if (!logfile.exists()) {
208                         // create dir path first
209
File JavaDoc 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                     // remember working path
221
prefs.setProperty(ModelerPreferences.EDITOR_LOGFILE, logfilePath);
222
223                     // replace appenders to just log to a file.
224
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 JavaDoc ioex) {
240                 logObj.warn("Error setting logging - " + logfilePath, ioex);
241             }
242         }
243     }
244
245     /**
246      * Set up the UI Look & Feel according to $HOME/.cayenne/modeler.preferences
247      */

248     protected void configureLookAndFeel() {
249         // get preferences
250
ModelerPreferences prefs = ModelerPreferences.getPreferences();
251
252         // get L&F name
253
String JavaDoc lfName = prefs.getString(
254                 ModelerPreferences.EDITOR_LAFNAME,
255                 ModelerConstants.DEFAULT_LAF_NAME);
256         // get UI theme name
257
String JavaDoc themeName = prefs.getString(
258                 ModelerPreferences.EDITOR_THEMENAME,
259                 ModelerConstants.DEFAULT_THEME_NAME);
260
261         try {
262             // only install theme if L&F is Plastic;
263
// bomb out if the L&F class cannot be found at all.
264
Class JavaDoc 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                 // try to configure theme
279
PlasticLookAndFeel.setMyCurrentTheme(foundTheme);
280             }
281
282             // try to set set L&F
283
UIManager.setLookAndFeel(lfName);
284         }
285         catch (Exception JavaDoc e) {
286             logObj.warn("Could not set selected LookAndFeel '"
287                     + lfName
288                     + "' - using default '"
289                     + ModelerConstants.DEFAULT_LAF_NAME
290                     + "'.");
291
292             // re-try with defaults
293
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 JavaDoc retry) {
302                 // give up, continue as-is
303
}
304         }
305         finally {
306             // remember L&F settings
307
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 JavaDoc themeName) {
317         List JavaDoc availableThemes = PlasticLookAndFeel.getInstalledThemes();
318         for (Iterator JavaDoc 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     /**
328      * Returns a file correspinding to $HOME/.cayenne/modeler.log
329      */

330     protected File JavaDoc getLogFile() {
331         if (!CayenneUserDir.getInstance().canWrite()) {
332             return null;
333         }
334
335         return CayenneUserDir.getInstance().resolveFile("modeler.log");
336     }
337 }
Popular Tags