001: package snow.lookandfeel;
002:
003: import snow.utils.storage.AppProperties;
004: import snow.utils.gui.*;
005: import snow.sortabletable.*;
006: import snow.Language.Language;
007: import java.io.*;
008: import javax.swing.*;
009: import javax.swing.table.*;
010: import javax.swing.tree.*;
011: import javax.swing.event.*;
012: import javax.swing.plaf.metal.*;
013: import javax.swing.plaf.*;
014: import java.awt.*;
015: import java.awt.event.*;
016: import java.util.*;
017:
018: /** Should be called prior to any UI creation.
019: */
020: public class ThemesManager {
021:
022: /* TODO: also allow choosing, if present among com.jgoodies.looks feels...
023: try
024: {
025: UIManager.setLookAndFeel("com.jgoodies.looks.windows.WindowsLookAndFeel");
026: //UIManager.setLookAndFeel("com.jgoodies.looks.plastic.Plastic3DLookAndFeel");
027: //UIManager.setLookAndFeel("com.jgoodies.looks.plastic.PlasticLookAndFeel");
028: //UIManager.setLookAndFeel("com.jgoodies.looks.plastic.PlasticXPLookAndFeel");
029: } catch (Exception e) { e.printStackTrace(); }
030:
031: */
032: final private AppProperties props = new AppProperties();
033:
034: CustomOceanTheme[] themes;
035: JMenu menu;
036: File storage = new File("ThemesManager.props");
037:
038: static ThemesManager instance = null;
039:
040: public static ThemesManager getInstance() {
041: if (instance == null) {
042: instance = new ThemesManager();
043: }
044:
045: return instance;
046: }
047:
048: private ThemesManager() {
049: props.load_XML(storage);
050: initialize();
051: }
052:
053: public void saveThemes() {
054: props.save_XML(storage);
055: }
056:
057: /** should be called once only. Install the menu
058: */
059: private void initialize() {
060: themes = new CustomOceanTheme[] {
061: new CustomOceanTheme("Ocean Theme", props, null),
062: new CustomOceanTheme_Snow(props)
063: //new CustomOceanTheme_SnowEFCNSmall(props),
064: //new CustomOceanTheme_Forest(props)
065: };
066:
067: JFrame.setDefaultLookAndFeelDecorated(true);
068: installSelectedTheme(); // prior to anything else
069:
070: menu = new JMenu("Themes");
071: ButtonGroup bg = new ButtonGroup();
072: String selectedTheme = props.getProperty("SelectedTheme",
073: "Ocean Theme");
074: for (int i = 0; i < themes.length; i++) {
075: boolean isSelected = themes[i].getName().equals(
076: selectedTheme);
077: JCheckBoxMenuItem mi = new JCheckBoxMenuItem(themes[i]
078: .getName(), isSelected);
079: menu.add(mi);
080: bg.add(mi);
081:
082: final int ii = i;
083: mi.addActionListener(new ActionListener() {
084: public void actionPerformed(ActionEvent ae) {
085: props.setProperty("SelectedTheme", themes[ii]
086: .getName());
087: props.save_XML(storage);
088:
089: installSelectedTheme();
090:
091: JOptionPane.showMessageDialog(null,
092: "Please restart", "Theme changed",
093: JOptionPane.INFORMATION_MESSAGE);
094:
095: //MetalLookAndFeel.setCurrentTheme( themes[ii] );
096: }
097: });
098: }
099:
100: menu.addSeparator();
101: JMenuItem editItem = new JMenuItem(Language
102: .translate("Edit selected theme"));
103: menu.add(editItem);
104: editItem.addActionListener(new ActionListener() {
105: public void actionPerformed(ActionEvent ae) {
106: CustomOceanTheme ct = getSelectedTheme();
107: if (ct != null) {
108: new CustomOceanEditor(frame, ct, props);
109: }
110: }
111: });
112:
113: } // Constructor
114:
115: /** this is the main frame, used for dialogs in the menu
116: */
117: public static JFrame frame;
118:
119: /** install or reinstall the selected theme.
120: */
121: public void installSelectedTheme() {
122: String selectedTheme = props.getProperty("SelectedTheme",
123: "Ocean Theme");
124: //System.out.println("Selected theme: "+selectedTheme);
125:
126: for (int i = 0; i < themes.length; i++) {
127: boolean isSelected = themes[i].getName().equals(
128: selectedTheme);
129: if (isSelected) {
130: // important: gradients and all stuff is added here
131: themes[i].addCustomEntriesToTable(UIManager
132: .getDefaults());
133: MetalLookAndFeel.setCurrentTheme(themes[i]);
134: try {
135: UIManager
136: .setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
137:
138: //com.sun.java.swing.plaf.windows.WindowsLookAndFeel
139: } catch (Exception ignored) {
140: ignored.printStackTrace();
141: }
142:
143: Frame[] frames = JFrame.getFrames();
144: for (int f = 0; f < frames.length; f++) {
145: if (frames[f] instanceof JFrame) {
146: JFrame ff = (JFrame) frames[f];
147: SwingUtilities.updateComponentTreeUI(ff);
148: ff.invalidate();
149: ff.repaint();
150:
151: SwingUtilities.updateComponentTreeUI(ff
152: .getContentPane());
153: ff.getContentPane().invalidate();
154: ff.getContentPane().repaint();
155: }
156:
157: }
158: }
159: }
160:
161: }
162:
163: /** @return null if none of CustomOceanTheme is selected.
164: */
165: public CustomOceanTheme getSelectedTheme() {
166: String selectedTheme = props.getProperty("SelectedTheme",
167: "Ocean Theme");
168: for (CustomOceanTheme theme : themes) {
169: if (theme.getName().equals(selectedTheme))
170: return theme;
171: }
172: return null;
173: }
174:
175: public JMenu getThemesMenu() {
176: return menu;
177: }
178:
179: public Color darkerColor(Color c) {
180: return c.darker();
181: }
182:
183: public Color brighterColor(Color c) {
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,
189: 100); // green
190: //private static Color falsePositiveBackground = Color.RED;
191: private static Font smallFont = new Font("Dialog", Font.PLAIN, 9);
192:
193: public Color getGreen() {
194: CustomOceanTheme cot = getSelectedTheme();
195: if (cot == null)
196: return greenBackground;
197: return cot.getGreen();
198: }
199:
200: public Color getRed() {
201: CustomOceanTheme cot = getSelectedTheme();
202: if (cot == null)
203: return redBackground;
204: return cot.getRed();
205: }
206:
207: public Color getBlack() {
208: CustomOceanTheme cot = getSelectedTheme();
209: if (cot == null)
210: return Color.black;
211: return cot.getBlack();
212: }
213:
214: public Color getWhite() {
215: CustomOceanTheme cot = getSelectedTheme();
216: if (cot == null)
217: return Color.white;
218: return cot.getWhite();
219: }
220:
221: public ColorUIResource get_tideEditor_commentColor() {
222: CustomOceanTheme cot = getSelectedTheme();
223: if (cot == null)
224: return new ColorUIResource(Color.blue);
225: return cot.get_tideEditor_commentColor();
226: }
227:
228: public ColorUIResource get_tideEditor_litteralColor() {
229: CustomOceanTheme cot = getSelectedTheme();
230: if (cot == null)
231: return new ColorUIResource(Color.green);
232: return cot.get_tideEditor_litteralsColor();
233: }
234:
235: public ColorUIResource get_tideEditor_keywordColor() {
236: CustomOceanTheme cot = getSelectedTheme();
237: if (cot == null)
238: return new ColorUIResource(Color.green);
239: return cot.get_tideEditor_keywordColor();
240: }
241:
242: public ColorUIResource get_tideEditor_textColor() {
243: CustomOceanTheme cot = getSelectedTheme();
244: if (cot == null)
245: return new ColorUIResource(Color.green);
246: return cot.get_tideEditor_textColor();
247: }
248:
249: public Font getSmallFont() {
250: CustomOceanTheme cot = getSelectedTheme();
251: if (cot == null)
252: return smallFont;
253: return cot.getSubTextFont();
254: }
255:
256: /** For convenience.
257: */
258: public static int getLabelFontSize() {
259: return UIManager.getFont("Label.font").getSize();
260: }
261:
262: /** Standalone test app.
263: */
264: public static void main(String[] aaa) {
265: EventQueue.invokeLater(new Runnable() {
266: public void run() {
267:
268: ThemesManager.getInstance();
269: JFrame f = new JFrame("Test Theme");
270: f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
271:
272: f.setJMenuBar(new JMenuBar());
273: f.getJMenuBar().add(
274: ThemesManager.getInstance().getThemesMenu());
275:
276: JMenu menu2 = new JMenu("Utils");
277: f.getJMenuBar().add(menu2);
278: JMenuItem uikeys = new JMenuItem(
279: "Swing UI Keys explorer");
280: menu2.add(uikeys);
281: uikeys.addActionListener(new ActionListener() {
282: public void actionPerformed(ActionEvent ae) {
283: new snow.sortabletable.UIKeysViewer(false);
284: }
285: });
286:
287: JDesktopPane desktop = new JDesktopPane();
288: f.setContentPane(desktop);
289:
290: JTree tt = new JTree();
291: JInternalFrame ji1 = new JInternalFrame("Tree test",
292: true, true, true, true);
293: ji1.add(new JScrollPane(tt), BorderLayout.CENTER);
294: ji1.setSize(200, 200);
295: desktop.add(ji1);
296: ji1.setVisible(true);
297: try {
298: ji1.setSelected(true);
299: } catch (Exception e) {
300: }
301:
302: JTable ta = new JTable();
303: ta.setModel(new TestModel());
304: JInternalFrame ji2 = new JInternalFrame("Table test",
305: true, true, true, true);
306: ji2.add(new JScrollPane(ta), BorderLayout.CENTER);
307: ji2.setSize(400, 300);
308: ji2.setLocation(220, 10);
309: desktop.add(ji2);
310: ji2.setVisible(true);
311: try {
312: ji2.setSelected(true);
313: } catch (Exception ignored) {
314: } // NOPMD
315:
316: JInternalFrame ji3 = new JInternalFrame(
317: "Some components", true, true, true, true);
318: JTabbedPane tp = new JTabbedPane();
319: JPanel cont = new JPanel();
320: tp.addTab("Hello", cont);
321: tp.addTab("A simple Label", new JLabel("Label"));
322:
323: GridLayout3 gl = new GridLayout3(2, cont);
324: gl.add(new JLabel("Label"));
325: gl.add(new JButton("Button"));
326: gl.add("TextField:");
327: gl.add(new JTextField("Some text", 30), true);
328:
329: gl.add("Checkbox:");
330: gl.add(new JCheckBox("do you ?", true));
331: gl.add("Combobox:");
332: gl.add(new JComboBox(new String[] { "Hello",
333: "This is a test !" }));
334:
335: ji3.setContentPane(tp);
336: ji3.setSize(450, 250);
337: ji3.setLocation(20, 320);
338: desktop.add(ji3);
339: ji3.setVisible(true);
340: try {
341: ji3.setSelected(true);
342: } catch (Exception e) {
343: } // NOPMD
344:
345: //f.add(new JScrollPane(tt), BorderLayout.CENTER);
346:
347: // CloseControlPanel ccp = new CloseControlPanel(f, true, true, "close");
348: // f.add(ccp, BorderLayout.SOUTH);
349:
350: f.setSize(700, 700);
351: f.setVisible(true);
352:
353: }
354: });
355: }
356:
357: }
|