KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > demo > Main


1 /*
2  * $Id: Main.java,v 1.1 2005/02/25 17:58:16 rbair Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  */

7 package org.jdesktop.demo;
8
9 import java.awt.Color JavaDoc;
10 import java.awt.EventQueue JavaDoc;
11 import java.awt.Font JavaDoc;
12 import java.util.HashMap JavaDoc;
13 import java.util.Iterator JavaDoc;
14 import java.util.List JavaDoc;
15 import java.util.Map JavaDoc;
16
17 import javax.swing.JFrame JavaDoc;
18 import javax.swing.UIManager JavaDoc;
19 import javax.swing.plaf.ColorUIResource JavaDoc;
20 import javax.swing.plaf.metal.MetalLookAndFeel JavaDoc;
21 import javax.swing.plaf.metal.MetalTheme JavaDoc;
22
23
24 import com.jgoodies.looks.plastic.PlasticLookAndFeel;
25 import com.jgoodies.looks.plastic.PlasticTheme;
26
27 /**
28  *
29  * @author Richard Bair
30  */

31 public class Main {
32     /**
33      * Mapping of all of the THEMES currently supported. These are PlasticLookAndFeel themes.
34      */

35     private static Map JavaDoc<String JavaDoc,MetalTheme JavaDoc> THEMES = null;
36
37     public static void main(String JavaDoc[] args) {
38         CmdLineParser parser = new CmdLineParser();
39         CmdLineParser.Option wOption = parser.addStringOption('w', "MainWindow");
40         try {
41             parser.parse(args);
42         } catch (CmdLineParser.UnknownOptionException ex) {
43             ex.printStackTrace();
44             printUsage();
45             System.exit(1);
46         } catch (CmdLineParser.IllegalOptionValueException ex) {
47             ex.printStackTrace();
48             printUsage();
49             System.exit(2);
50         }
51         //look for the -w argument
52
final String JavaDoc wValue = (String JavaDoc)parser.getOptionValue(wOption);
53         
54         setAppLookAndFeel();
55         EventQueue.invokeLater(new Runnable JavaDoc() {
56             public void run() {
57                 try {
58                     JFrame JavaDoc mainWindow = (JFrame JavaDoc)Class.forName(wValue).newInstance();
59                     mainWindow.setVisible(true);
60                 } catch (Exception JavaDoc e) {
61                     System.err.println("Failed to find, create, and open the MainWindow. " +
62                             "The MainWindow class was '" + wValue + "'");
63                     e.printStackTrace();
64                     printUsage();
65                     System.exit(3);
66                 }
67             }
68         });
69     }
70     
71     private static void printUsage() {
72         System.out.println("Pass in the name of the JFrame that is to be the main window " +
73                 "for this app, like so:\n" +
74                 "-w org.jdesktop.binding.demo.blar.BlarWindow");
75     }
76
77     /**
78      * Sets the application look and feel. Currently, the look and feel and theme
79      * can be changed on the command line by the command line properties
80      * "org.jdesktop.demo.BindingDemo.LookAndFeel" and "org.jdesktop.demo.BindingDemo.Theme".
81      */

82     private static void setAppLookAndFeel() {
83         //set the look and feel for the app
84
try {
85             //initialize the THEMES map if they have not already been so initialized
86
if (THEMES == null) {
87                     initThemes();
88             }
89             //The defaultClassName and defaultThemeName are the look and feel class name and theme name to use if no system property
90
//has been set.
91
String JavaDoc defaultClassName = "com.jgoodies.looks.plastic.PlasticXPLookAndFeel";
92             String JavaDoc defaultThemeName = "com.jgoodies.looks.plastic.theme.ExperienceBlue";
93             String JavaDoc className = System.getProperty("org.jdesktop.demo.LookAndFeel", defaultClassName);
94             String JavaDoc themeName = System.getProperty("org.jdesktop.demo.Theme", defaultThemeName);
95     
96             //set the current plasic look and feel theme, and the current look and feel
97
Object JavaDoc theme = THEMES.get(themeName);
98             if (theme == null) {
99                     UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
100             } else if (theme instanceof PlasticTheme){
101                     PlasticLookAndFeel.setMyCurrentTheme((PlasticTheme)THEMES.get(themeName));
102                     UIManager.setLookAndFeel(className);
103             } else if (theme instanceof MetalTheme JavaDoc) {
104                     MetalLookAndFeel.setCurrentTheme((MetalTheme JavaDoc)THEMES.get(themeName));
105                     UIManager.setLookAndFeel(className);
106             }
107             
108             //set some colors not included in the plastic look and feel.
109
if (className.equals("com.jgoodies.plaf.windows.ExtWindowsLookAndFeel")) {
110                     UIManager.put("JTitledPanel.title.foreground", new ColorUIResource JavaDoc(Color.WHITE));
111                     UIManager.put("JTitledPanel.title.darkBackground", UIManager.getColor("InternalFrame.activeTitleBackground"));
112                     UIManager.put("JTitledPanel.title.lightBackground", UIManager.getColor("InternalFrame.activeTitleGradient"));
113                     UIManager.put("JScrollUp.background", UIManager.getColor("InternalFrame.activeTitleGradient"));
114                     UIManager.put("JTitledPanel.title.font", UIManager.getFont("Button.font").deriveFont(Font.BOLD));
115             } else if (className.equals("com.sun.java.swing.plaf.windows.WindowsLookAndFeel")) {
116                     UIManager.put("JTitledPanel.title.foreground", new ColorUIResource JavaDoc(Color.WHITE));
117                     UIManager.put("JTitledPanel.title.darkBackground", UIManager.getColor("InternalFrame.activeTitleBackground"));
118                     UIManager.put("JTitledPanel.title.lightBackground", UIManager.getColor("InternalFrame.activeTitleGradient"));
119                     UIManager.put("JScrollUp.background", UIManager.getColor("InternalFrame.activeTitleGradient"));
120                     UIManager.put("JTitledPanel.title.font", UIManager.getFont("Button.font").deriveFont(Font.BOLD));
121                     UIManager.put("JHyperLink.unclickedColor", new ColorUIResource JavaDoc(Color.WHITE));
122                     UIManager.put("JHyperLink.clickedColor", new ColorUIResource JavaDoc(Color.WHITE));
123             } else if (className.equals("com.jgoodies.looks.plastic.PlasticXPLookAndFeel")) {
124                     UIManager.put("JTitledPanel.title.foreground", new ColorUIResource JavaDoc(Color.WHITE));
125                     UIManager.put("JScrollUp.background", UIManager.getColor("InternalFrame.activeTitleGradient"));
126                     UIManager.put("JTitledPanel.title.font", UIManager.getFont("Button.font").deriveFont(Font.BOLD));
127                     UIManager.put("JHyperLink.unclickedColor", new ColorUIResource JavaDoc(Color.WHITE));
128                     UIManager.put("JHyperLink.clickedColor", new ColorUIResource JavaDoc(Color.WHITE));
129             }
130                 
131         } catch (Exception JavaDoc e) {
132                 e.printStackTrace();
133         }
134             
135     }
136     
137     /**
138      * Initializes the THEMES map.
139      */

140     private static void initThemes() {
141             THEMES = new HashMap JavaDoc<String JavaDoc,MetalTheme JavaDoc>();
142             List JavaDoc list = PlasticLookAndFeel.getInstalledThemes();
143             for (Iterator JavaDoc itr = list.iterator(); itr.hasNext();) {
144                     PlasticTheme element = (PlasticTheme) itr.next();
145                     THEMES.put(element.getClass().getName(), element);
146             }
147             //add the normal swing themes
148
THEMES.put("javax.swing.plaf.metal.DefaultMetalTheme", new javax.swing.plaf.metal.DefaultMetalTheme JavaDoc());
149             THEMES.put("javax.swing.plaf.metal.OceanTheme", new javax.swing.plaf.metal.OceanTheme JavaDoc());
150     }
151 }
Popular Tags