KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > snow > lookandfeel > ThemesManager


1 package snow.lookandfeel;
2
3 import snow.utils.storage.AppProperties;
4 import snow.utils.gui.*;
5 import snow.sortabletable.*;
6 import snow.Language.Language;
7
8 import java.io.*;
9 import javax.swing.*;
10 import javax.swing.table.*;
11 import javax.swing.tree.*;
12 import javax.swing.event.*;
13 import javax.swing.plaf.metal.*;
14 import javax.swing.plaf.*;
15 import java.awt.*;
16 import java.awt.event.*;
17 import java.util.*;
18
19 /** should be called prior to any UI creation
20 */

21 public final class ThemesManager
22 {
23   final private AppProperties props = new AppProperties();
24
25   private CustomOceanTheme[] themes;
26   private JMenu menu;
27
28   private final File storage;
29
30   // if customized, must be set prior to the first getInstance() call !
31
public static String JavaDoc baseDirectory = ".";
32
33   static ThemesManager instance = null;
34
35   public static ThemesManager getInstance()
36   {
37     if(instance==null)
38     {
39      instance = new ThemesManager();
40     }
41
42     return instance;
43   }
44
45   private ThemesManager()
46   {
47     storage = new File(baseDirectory, "ThemesManager.props");
48     props.load_XML( storage );
49     EventQueue.invokeLater(new Runnable JavaDoc() { public void run() {
50        initialize();
51     }});
52   }
53
54   public void saveThemes()
55   {
56     props.save_XML( storage );
57   }
58
59   /** should be called once only. Installs the menu
60   */

61   private void initialize()
62   {
63     themes = new CustomOceanTheme[]
64     {
65        new CustomOceanTheme("Ocean Theme", props, null),
66        new CustomOceanTheme_Snow(props),
67        new CustomOceanTheme_SnowEFCNSmall(props),
68        new CustomOceanTheme_Forest(props)
69     };
70
71     JFrame.setDefaultLookAndFeelDecorated(true);
72     installSelectedTheme(); // prior to anything else
73

74
75     menu = new JMenu("Themes");
76     ButtonGroup bg = new ButtonGroup();
77     String JavaDoc selectedTheme = props.getProperty("SelectedTheme", "Ocean Theme");
78     for(int i=0; i<themes.length; i++)
79     {
80       boolean isSelected = themes[i].getName().equals(selectedTheme);
81       JCheckBoxMenuItem mi = new JCheckBoxMenuItem(themes[i].getName(), isSelected);
82       menu.add(mi);
83       bg.add(mi);
84
85       final int ii = i;
86       mi.addActionListener(new ActionListener()
87       {
88         public void actionPerformed(ActionEvent ae)
89         {
90            props.setProperty("SelectedTheme", themes[ii].getName());
91            props.save_XML( storage );
92
93            installSelectedTheme();
94
95            //MetalLookAndFeel.setCurrentTheme( themes[ii] );
96
}
97       });
98     }
99
100     menu.addSeparator();
101     JMenuItem editItem = new JMenuItem(Language.translate("Edit selected theme"));
102     menu.add(editItem);
103     editItem.addActionListener(new ActionListener()
104     {
105       public void actionPerformed(ActionEvent ae)
106       {
107         CustomOceanTheme ct = getSelectedTheme();
108         if(ct!=null)
109         {
110           new CustomOceanEditor(frame, ct, props);
111         }
112       }
113     });
114
115   } // Constructor
116

117   /** this is the main frame, used for dialogs in the menu
118   */

119   public static JFrame frame;
120
121   /** install or reinstall the selected theme.
122   */

123   public void installSelectedTheme()
124   {
125     String JavaDoc selectedTheme = props.getProperty("SelectedTheme", "Ocean Theme");
126     for(CustomOceanTheme ti : themes)
127     {
128       boolean isSelected = ti.getName().equals(selectedTheme);
129       if(isSelected)
130       {
131          // important: gradients and all stuff is add here
132
ti.addCustomEntriesToTable(UIManager.getDefaults());
133          MetalLookAndFeel.setCurrentTheme( ti );
134          try
135          {
136            UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
137          } catch(Exception JavaDoc e){}
138
139          for(Frame fi: JFrame.getFrames())
140          {
141            if(fi instanceof JFrame)
142            {
143              JFrame ff = (JFrame) fi;
144              if(ff==null) continue;
145
146              SwingUtilities.updateComponentTreeUI(ff);
147              ff.invalidate();
148              ff.repaint();
149
150              SwingUtilities.updateComponentTreeUI(ff.getContentPane());
151              ff.getContentPane().invalidate();
152              ff.getContentPane().repaint();
153            }
154          }
155       }
156     }
157   }
158
159
160   public CustomOceanTheme getSelectedTheme()
161   {
162     String JavaDoc selectedTheme = props.getProperty("SelectedTheme", "Ocean Theme");
163     for(CustomOceanTheme ti : themes)
164     {
165       boolean isSelected = ti.getName().equals(selectedTheme);
166       if(isSelected) return ti;
167     }
168     return null;
169   }
170
171
172   public JMenu getThemesMenu()
173   {
174     return menu;
175   }
176
177   public Color darkerColor(Color c)
178   {
179     return c.darker();
180   }
181
182   public Color brighterColor(Color c)
183   {
184     return c.brighter();
185   }
186
187   private static final Color redBackground = new Color(250,100,100); // red
188
private static final Color greenBackground = new Color(100,250,100); // green
189
private static Color falsePositiveBackground = Color.RED;
190   private static Font smallFont = new Font("Dialog", Font.PLAIN, 9);
191
192
193
194   public Color getGreen()
195   {
196      CustomOceanTheme cot = getSelectedTheme();
197      if(cot==null) return greenBackground;
198      return cot.getGreen();
199   }
200
201   public Color getRed()
202   {
203      CustomOceanTheme cot = getSelectedTheme();
204      if(cot==null) return redBackground;
205      return cot.getRed();
206   }
207
208   public Font getSmallFont()
209   {
210      CustomOceanTheme cot = getSelectedTheme();
211      if(cot==null) return smallFont;
212      return cot.getSubTextFont();
213   }
214
215   /** for convenience
216   */

217   public static int getLabelFontSize()
218   {
219     return UIManager.getFont("Label.font").getSize();
220   }
221
222
223   /** standalone test
224   */

225   public static void main(String JavaDoc[] aaa)
226   {
227      ThemesManager.getInstance();
228      JFrame f =new JFrame("Test Theme");
229      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
230
231
232      f.setJMenuBar(new JMenuBar());
233      f.getJMenuBar().add( ThemesManager.getInstance().getThemesMenu() );
234
235      JDesktopPane desktop = new JDesktopPane();
236      f.setContentPane(desktop);
237
238      JTree tt = new JTree();
239      JInternalFrame ji1 = new JInternalFrame("Tree test", true, true, true, true);
240      ji1.getContentPane().add(new JScrollPane(tt), BorderLayout.CENTER);
241      ji1.setSize(200,200);
242      desktop.add(ji1);
243      ji1.setVisible(true);
244      try{
245        ji1.setSelected(true);
246      } catch(Exception JavaDoc e) {}
247
248      JTable ta = new JTable();
249      ta.setModel(new TestModel());
250      JInternalFrame ji2 = new JInternalFrame("Table test", true, true, true, true);
251      ji2.getContentPane().add(new JScrollPane(ta), BorderLayout.CENTER);
252      ji2.setSize(400,300);
253      ji2.setLocation(220,10);
254      desktop.add(ji2);
255      ji2.setVisible(true);
256      try{
257        ji2.setSelected(true);
258      } catch(Exception JavaDoc e) {}
259
260
261      JInternalFrame ji3 = new JInternalFrame("Some components", true, true, true, true);
262      JPanel cont = new JPanel();
263      GridLayout3 gl = new GridLayout3(2,cont);
264      gl.add(new JLabel("Label"));
265      gl.add(new JContrastLabel("Contrast Label"));
266      gl.add(new JButton("Button"));
267      gl.add(new JButton("Sense button"));
268      gl.add("TextField:");
269      gl.add( new JTextField("Some text", 30));
270
271      gl.add("Checkbox:");
272      gl.add( new JCheckBox("do you ?", true));
273      gl.add("Combobox:");
274      gl.add( new JComboBox(new String JavaDoc[]{"Hello", "This is a test !"}));
275
276      ji3.setContentPane(cont);
277      ji3.setSize(400,250);
278      ji3.setLocation(20,320);
279      desktop.add(ji3);
280      ji3.setVisible(true);
281      try{
282        ji3.setSelected(true);
283      } catch(Exception JavaDoc e) {}
284
285      //f.getContentPane().add(new JScrollPane(tt), BorderLayout.CENTER);
286

287 /* CloseControlPanel ccp = new CloseControlPanel(f, true, true, "close");
288      f.getContentPane().add(ccp, BorderLayout.SOUTH);*/

289
290
291      f.setSize(700, 700);
292      f.setVisible(true);
293   }
294
295
296 } // ThemesManager
Popular Tags