KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > plaf > CompierePLAF


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.plaf;
15
16 import java.awt.Color JavaDoc;
17 import java.awt.Dimension JavaDoc;
18 import java.awt.Font JavaDoc;
19 import java.awt.Insets JavaDoc;
20 import java.awt.Toolkit JavaDoc;
21 import java.awt.Window JavaDoc;
22 import java.awt.event.WindowEvent JavaDoc;
23 import java.lang.reflect.Method JavaDoc;
24 import java.lang.reflect.Modifier JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Arrays JavaDoc;
27 import java.util.ResourceBundle JavaDoc;
28
29 import javax.swing.ImageIcon JavaDoc;
30 import javax.swing.JFrame JavaDoc;
31 import javax.swing.JOptionPane JavaDoc;
32 import javax.swing.LookAndFeel JavaDoc;
33 import javax.swing.SwingUtilities JavaDoc;
34 import javax.swing.UIManager JavaDoc;
35 import javax.swing.plaf.metal.MetalLookAndFeel JavaDoc;
36 import javax.swing.plaf.metal.MetalTheme JavaDoc;
37
38 import org.compiere.swing.CButton;
39 import org.compiere.util.Ini;
40 import org.compiere.util.ValueNamePair;
41
42 /**
43  * Variable Pluggable Look And Feel.
44  * Provides an easy access to the required currently active PLAF information
45  *
46  * @author Jorg Janke
47  * @version $Id: CompierePLAF.java,v 1.32 2003/09/27 11:08:53 jjanke Exp $
48  */

49 public final class CompierePLAF
50 {
51     /** Version tag */
52     public static final String JavaDoc VERSION = "R1.2.0";
53     /** Key of Client Property to paint in CompiereColor */
54     public static final String JavaDoc BACKGROUND = "CompiereBackground";
55     /** Key of Client Property for Rectangle Items - if exists, the standard background is used */
56     public static final String JavaDoc BACKGROUND_FILL = "CompiereBackgroundFill";
57     /** Key of Client Property for CPanel */
58     public static final String JavaDoc TABLEVEL = "CompiereTabLevel";
59
60     /****** Background *******************************************************/
61
62     /**
63      * Return Normal field background color "text".
64      * Windows = white
65      * @return Color
66      */

67     public static Color JavaDoc getFieldBackground_Normal()
68     {
69         // window => white
70
return ColorBlind.getDichromatColor(UIManager.getColor("text"));
71     } // getFieldBackground_Normal
72

73     /**
74      * Return Error field background (CompiereTheme)
75      * @return Color
76      */

77     public static Color JavaDoc getFieldBackground_Error()
78     {
79         return ColorBlind.getDichromatColor(CompiereTheme.error);
80     } // getFieldBackground_Error
81

82     /**
83      * Return Mandatory field background color (CompiereTheme)
84      * @return Color
85      */

86     public static Color JavaDoc getFieldBackground_Mandatory()
87     {
88         return ColorBlind.getDichromatColor(CompiereTheme.mandatory);
89     } // getFieldBackground_Mandatory
90

91     /**
92      * Return Inactive field background color (CompiereTheme)
93      * @return Color
94      */

95     public static Color JavaDoc getFieldBackground_Inactive()
96     {
97         return ColorBlind.getDichromatColor(CompiereTheme.inactive);
98     } // getFieldBackground_Inactive
99

100     /**
101      * Return form background color "control".
102      * Windows = lightGray
103      * @return Color
104      */

105     public static Color JavaDoc getFormBackground()
106     {
107         return ColorBlind.getDichromatColor(UIManager.getColor("control"));
108     } // getFormBackground
109

110     /**
111      * Info Background Color "info"
112      * Windows = info (light yellow)
113      * @return Color
114      */

115     public static Color JavaDoc getInfoBackground()
116     {
117         return ColorBlind.getDichromatColor(CompiereTheme.info);
118     } // getInfoBackground
119

120
121     /****** Text *************************************************************/
122
123     /**
124      * Normal field text foreground color "textText"
125      * Windows = black
126      * @return Color
127      */

128     public static Color JavaDoc getTextColor_Normal()
129     {
130         return ColorBlind.getDichromatColor(UIManager.getColor("textText"));
131     } // getText_Normal
132

133     /**
134      * OK Text Foreground Color (Theme)
135      * @return Color
136      */

137     public static Color JavaDoc getTextColor_OK()
138     {
139         return ColorBlind.getDichromatColor(CompiereTheme.txt_ok);
140     } // getText_OK
141

142     /**
143      * Issue Text Foreground Color (Theme)
144      * @return Color
145      */

146     public static Color JavaDoc getTextColor_Issue()
147     {
148         return ColorBlind.getDichromatColor(CompiereTheme.txt_error);
149     } // getText_Issue
150

151     /**
152      * Label Text foreground Color "controlText"
153      * Windows = black
154      * @return Color
155      */

156     public static Color JavaDoc getTextColor_Label()
157     {
158         return ColorBlind.getDichromatColor(UIManager.getColor("controlText"));
159     } // getTextColor_Label
160

161
162     public static Color JavaDoc getPrimary1()
163     {
164         return ColorBlind.getDichromatColor(CompiereTheme.primary1);
165     }
166     public static Color JavaDoc getPrimary2()
167     {
168         return ColorBlind.getDichromatColor(CompiereTheme.primary2);
169     }
170     public static Color JavaDoc getPrimary3()
171     {
172         return ColorBlind.getDichromatColor(CompiereTheme.primary3);
173     }
174     public static Color JavaDoc getSecondary1()
175     {
176         return ColorBlind.getDichromatColor(CompiereTheme.secondary1);
177     }
178     public static Color JavaDoc getSecondary2()
179     {
180         return ColorBlind.getDichromatColor(CompiereTheme.secondary2);
181     }
182     public static Color JavaDoc getSecondary3()
183     {
184         return ColorBlind.getDichromatColor(CompiereTheme.secondary3);
185     }
186
187
188     /****** Fonts ************************************************************/
189
190     /**
191      * Get Header Font (window/label font)
192      * @return font
193      */

194     public static Font JavaDoc getFont_Header()
195     {
196         return CompiereTheme.windowFont;
197     // return UIManager.getFont("Label.font");
198
} // getFont_Header
199

200     /**
201      * Get Field Font
202      * @return font
203      */

204     public static Font JavaDoc getFont_Field()
205     {
206         return CompiereTheme.userFont;
207     // return UIManager.getFont("TextField.font");
208
} // getFont_Field
209

210     /**
211      * Get Label Font
212      * @return font
213      */

214     public static Font JavaDoc getFont_Label()
215     {
216         return CompiereTheme.controlFont;
217     // return UIManager.getFont("Label.font");
218
} // setFont_Label
219

220     /**
221      * Get Small (report) Font
222      * @return font
223      */

224     public static Font JavaDoc getFont_Small()
225     {
226         return CompiereTheme.smallFont;
227     } // setFont_Small
228

229     /****** Available L&F ****************************************************/
230
231     /** Default PLAF Name */
232     public static final String JavaDoc DEFAULT_PLAF = CompiereLookAndFeel.NAME;
233     /** Default Theme Name */
234     public static final String JavaDoc DEFAULT_THEME = "";
235
236     /** Availablle Looks */
237     private static ValueNamePair[] s_looks = null;
238     /** Default PLAF */
239     private static ValueNamePair s_defaultPLAF = null;
240     /** Availablle Themes */
241     private static ValueNamePair[] s_themes = null;
242
243     private static ValueNamePair s_vp_compiereTheme = null;
244     private static ValueNamePair s_vp_metalTheme = null;
245     private static ValueNamePair s_vp_kunststoffTheme = null;
246
247     /** PLAF class list
248         com.sun.java.swing.plaf.windows.WindowsLookAndFeel
249         com.sun.java.swing.plaf.motif.MotifLookAndFeel
250         javax.swing.plaf.metal.MetalLookAndFeel
251
252     /**
253      * Static Initializer.
254      * - Fill available PLAFs and Themes
255      */

256     static
257     {
258         ArrayList JavaDoc plafList = new ArrayList JavaDoc();
259         ValueNamePair vp = new ValueNamePair("org.compiere.plaf.CompiereLookAndFeel", CompiereLookAndFeel.NAME);
260         plafList.add (vp);
261         // Themes
262
ArrayList JavaDoc themeList = new ArrayList JavaDoc();
263         vp = new ValueNamePair("org.compiere.plaf.CompiereTheme", CompiereTheme.NAME);
264         themeList.add (vp);
265         s_vp_compiereTheme = vp;
266         vp = new ValueNamePair("javax.swing.plaf.metal.DefaultMetalTheme", "Steel");
267         s_vp_metalTheme = vp;
268         themeList.add (vp);
269
270         // Discover and Install - Kuststoff
271
try
272         {
273             Class JavaDoc c = Class.forName("com.incors.plaf.kunststoff.KunststoffLookAndFeel");
274             vp = new ValueNamePair("com.incors.plaf.kunststoff.KunststoffLookAndFeel", "Kunststoff");
275             plafList.add(vp);
276             vp = new ValueNamePair("com.incors.plaf.kunststoff.KunststoffTheme", "Kuststoff");
277             themeList.add(vp);
278             s_vp_kunststoffTheme = vp;
279         }
280         catch (Exception JavaDoc e)
281         {
282         // System.err.println("CompierePLAF - Kuststoff not found");
283
}
284
285 /** // Discover and Install - jGoodies
286         try
287         {
288             Class c = Class.forName("com.jgoodies.plaf.metal.ExtMetalLookAndFeel");
289             com.jgoodies.plaf.metal.ExtMetalLookAndFeel jGoodies = (com.jgoodies.plaf.metal.ExtMetalLookAndFeel)c.newInstance();
290
291             vp = new ValueNamePair("com.jgoodies.plaf.metal.ExtMetalLookAndFeel", "Extended Metal");
292         // System.out.println("Added PLAF: " + vp.toStringX());
293             plafList.add(vp);
294             vp = new ValueNamePair("com.jgoodies.plaf.metal.dimension3.ExtMetalLookAndFeel3D", "Extended Metal 3D");
295         // System.out.println("Added PLAF: " + vp.toStringX());
296             plafList.add(vp);
297             vp = new ValueNamePair("com.jgoodies.plaf.windows.ExtWindowsLookAndFeel", "Extended Windows");
298         // System.out.println("Added PLAF: " + vp.toStringX());
299             plafList.add(vp);
300             // jGoodies Looks
301             java.util.List thList = jGoodies.getInstalledThemes();
302             Iterator it = thList.iterator();
303             while (it.hasNext())
304             {
305                 MetalTheme theme = (MetalTheme)it.next();
306                 vp = new ValueNamePair(theme.getClass().getName(), theme.getName());
307             // System.out.println("Added jGoodies Theme: " + vp.toStringX());
308                 themeList.add(vp);
309             }
310         }
311         catch (Exception e)
312         {
313         // System.err.println("CompierePLAF - jGoodies not found");
314         }
315 **/

316
317         // Install discovered PLAFs
318
for (int i = 0; i < plafList.size(); i++)
319         {
320             vp = (ValueNamePair)plafList.get(i);
321             UIManager.installLookAndFeel(vp.getName(), vp.getValue());
322         }
323
324         // Fill Available PLAFs
325
plafList = new ArrayList JavaDoc();
326         UIManager.LookAndFeelInfo JavaDoc[] lfInfo = UIManager.getInstalledLookAndFeels();
327         for (int i = 0; i < lfInfo.length; i++)
328         {
329             vp = new ValueNamePair (lfInfo[i].getClassName(), lfInfo[i].getName());
330             plafList.add(vp);
331             if (lfInfo[i].getName().equals(DEFAULT_PLAF))
332                 s_defaultPLAF = vp;
333         }
334         s_looks = new ValueNamePair[plafList.size()];
335         plafList.toArray(s_looks);
336
337         // Fill Available Themes
338
s_themes = new ValueNamePair[themeList.size()];
339         themeList.toArray(s_themes);
340         //
341
// printPLAFDefaults();
342
} // static Initializer
343

344
345     /**
346      * Get available Look And Feels
347      * @return Array of ValueNamePair with name and class of Look and Feel
348      */

349     public static ValueNamePair[] getPLAFs()
350     {
351         return s_looks;
352     } // getPLAFs
353

354     /**
355      * Get the list of available Metal Themes if the current L&F is a Metal L&F
356      * @return Array of Strings with Names of Metal Themes
357      */

358     public static ValueNamePair[] getThemes ()
359     {
360         if (UIManager.getLookAndFeel() instanceof MetalLookAndFeel JavaDoc)
361             return s_themes;
362         return new ValueNamePair[0];
363     } // getThemes
364

365     /*************************************************************************/
366
367     /**
368      * Set PLAF based on Ini Properties
369      * @param win Optional Window
370      */

371     public static void setPLAF (Window JavaDoc win)
372     {
373         String JavaDoc look = Ini.getProperty(Ini.P_UI_LOOK);
374         String JavaDoc lookTheme = Ini.getProperty(Ini.P_UI_THEME);
375         // Search for PLAF
376
ValueNamePair plaf = null;
377         for (int i = 0; i < s_looks.length; i++)
378         {
379             if (s_looks[i].getName().equals(look))
380             {
381                 plaf = s_looks[i];
382                 break;
383             }
384         }
385         // Search for Theme
386
ValueNamePair theme = null;
387         for (int i = 0; i < s_themes.length; i++)
388         {
389             if (s_themes[i].getName().equals(lookTheme))
390             {
391                 theme = s_themes[i];
392                 break;
393             }
394         }
395         // Set PLAF
396
setPLAF (plaf == null ? s_defaultPLAF : plaf, theme, win);
397     } // setPLAF
398

399     /**
400      * Set PLAF and update Ini
401      *
402      * @param plaf ValueNamePair of the PLAF to be set
403      * @param theme Optional Theme name
404      * @param win Optional Window
405      */

406     public static void setPLAF (ValueNamePair plaf, ValueNamePair theme, Window JavaDoc win)
407     {
408         if (plaf == null)
409             return;
410         System.out.println("PLAF = " + plaf + (theme == null ? "" : (" - " + theme)));
411
412         // Look & Feel
413
try
414         {
415             UIManager.setLookAndFeel(plaf.getValue());
416         }
417         catch (Exception JavaDoc e)
418         {
419             System.err.println("CompierePLAF.setPLAF - " + e.getMessage());
420         }
421         LookAndFeel JavaDoc laf = UIManager.getLookAndFeel();
422         Ini.setProperty(Ini.P_UI_LOOK, plaf.getName());
423
424         // Optional Theme
425
Ini.setProperty(Ini.P_UI_THEME, "");
426         // Default Theme
427
if (theme == null && laf instanceof MetalLookAndFeel JavaDoc)
428         {
429             String JavaDoc className = laf.getClass().getName();
430             if (className.equals("javax.swing.plaf.metal.MetalLookAndFeel"))
431                 theme = s_vp_metalTheme;
432             else if (className.equals("org.compiere.plaf.CompiereLookAndFeel"))
433                 theme = s_vp_compiereTheme;
434             else if (className.equals("com.incors.plaf.kunststoff.KunststoffLookAndFeel"))
435                 theme = s_vp_kunststoffTheme;
436         }
437         if (theme != null && laf instanceof MetalLookAndFeel JavaDoc && theme.getValue().length() > 0)
438         {
439             try
440             {
441                 Class JavaDoc c = Class.forName(theme.getValue());
442                 MetalTheme JavaDoc t = (MetalTheme JavaDoc)c.newInstance();
443                 if (laf instanceof CompiereLookAndFeel)
444                     CompiereLookAndFeel.setCurrentTheme(t);
445                 else
446                     MetalLookAndFeel.setCurrentTheme(t);
447                 //
448
CompiereTheme.setTheme(t); // copies it if not CompiereTheme
449
Ini.setProperty(Ini.P_UI_THEME, theme.getName());
450             }
451             catch (Exception JavaDoc e)
452             {
453                 System.err.println("CompierePLAF.setPLAF Theme - " + e.getMessage());
454             }
455         }
456         updateUI (win);
457     // printPLAFDefaults();
458
} // setPLAF
459

460     /**
461      * Update UI of this and parent Windows
462      * @param win window
463      */

464     public static void updateUI (Window JavaDoc win)
465     {
466         if (win == null)
467             return;
468         Window JavaDoc c = win;
469         do
470         {
471             SwingUtilities.updateComponentTreeUI(c);
472             c.invalidate();
473             c.pack();
474             c.validate();
475             c.repaint();
476             c = c.getOwner();
477         }
478         while (c != null);
479     } // updateUI
480

481     /**
482      * Reset PLAF Settings
483      * @param win Window to be reset
484      */

485     public static void reset (Window JavaDoc win)
486     {
487         // Clean Theme Properties
488
CompiereTheme.reset (Ini.getProperties()); // sets properties
489
CompierePLAF.setPLAF (win);
490     } // reset
491

492     /**
493      * Print current UIDefaults
494      */

495     public static void printPLAFDefaults ()
496     {
497         System.out.println(UIManager.getLookAndFeel());
498         Object JavaDoc[] keys = UIManager.getLookAndFeelDefaults().keySet().toArray();
499         Arrays.sort(keys);
500         char lastStart = ' ';
501         for (int i = 0; i < keys.length; i++)
502         {
503             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
504             sb.append(keys[i]).append(" = ").append(UIManager.get(keys[i]));
505             if (keys[i].toString().charAt(0) != lastStart)
506             {
507                 System.out.println();
508                 lastStart = keys[i].toString().charAt(0);
509             }
510             System.out.println(sb);
511         }
512     } // printPLAFDefaults
513

514     /**
515      * Is CompiereL&F the active L&F
516      * @return true if L&F is Compiere
517      */

518     public static boolean isActive()
519     {
520         return UIManager.getLookAndFeel() instanceof CompiereLookAndFeel;
521     } // isActive
522

523     /*************************************************************************/
524
525     static ResourceBundle JavaDoc s_res = ResourceBundle.getBundle("org.compiere.plaf.PlafRes");
526
527     /**
528      * Create OK Button
529      * @return OK button
530      */

531     public static CButton getOKButton()
532     {
533         CButton b = new CButton();
534         b.setIcon(new ImageIcon JavaDoc(CompierePLAF.class.getResource("icons/Ok24.gif")));
535         b.setMargin(new Insets JavaDoc(0,10,0,10));
536         b.setToolTipText (s_res.getString("OK"));
537         return b;
538     } // getOKButton
539

540     /**
541      * Create Cancel Button
542      * @return Cancel button
543      */

544     public static CButton getCancelButton()
545     {
546         CButton b = new CButton();
547         b.setIcon(new ImageIcon JavaDoc(CompierePLAF.class.getResource("icons/Cancel24.gif")));
548         b.setMargin(new Insets JavaDoc(0,10,0,10));
549         b.setToolTipText (s_res.getString("Cancel"));
550         return b;
551     } // getCancelButton
552

553     /**
554      * Center Window on Screen and show it
555      * @param window window
556      */

557     public static void showCenterScreen (Window JavaDoc window)
558     {
559         window.pack();
560         Dimension JavaDoc sSize = Toolkit.getDefaultToolkit().getScreenSize();
561         Dimension JavaDoc wSize = window.getSize();
562         window.setLocation(((sSize.width-wSize.width)/2), ((sSize.height-wSize.height)/2));
563         window.toFront();
564         window.setVisible(true);
565     } // showCenterScreen
566

567     /*************************************************************************/
568
569     /**
570      * Start Class With Compiere Look or Compiere PLAF Editor
571      * @param args first parameter is class to start, if none start PLAF Editor
572      */

573     public static void main (String JavaDoc[] args)
574     {
575         String JavaDoc jVersion = System.getProperty("java.version");
576         if (!(jVersion.startsWith("1.4")))
577         {
578             JOptionPane.showMessageDialog (null,
579                 "Require Java Version 1.4 or up - Not " + jVersion,
580                 "CompierePLAF - Version Conflict",
581                 JOptionPane.ERROR_MESSAGE);
582             System.exit(1);
583         }
584
585         // set the defined PLAF
586
Ini.loadProperties (true);
587         CompiereTheme.load ();
588         setPLAF (null);
589         //
590
if (args.length == 0)
591         {
592             CompierePLAFFrame frame = new CompierePLAFFrame();
593             return;
594         }
595
596         String JavaDoc className = args[0];
597         // find class
598
Class JavaDoc startClass = null;
599         try
600         {
601             startClass = Class.forName(className);
602         }
603         catch (Exception JavaDoc e)
604         {
605             System.err.println("Did not find: " + className);
606             e.printStackTrace();
607             System.exit(1);
608         }
609
610         // try static main method
611
try
612         {
613             Method JavaDoc[] methods = startClass.getMethods();
614             for (int i = 0; i < methods.length; i++)
615             {
616                 if (Modifier.isStatic(methods[i].getModifiers()) && methods[i].getName().equals("main"))
617                 {
618                     String JavaDoc[] startArgs = new String JavaDoc[args.length-1];
619                     for (int ii = 1; ii < args.length; ii++)
620                         startArgs[ii-i] = args[ii];
621                     methods[i].invoke(null, new Object JavaDoc[] {startArgs});
622                 }
623                 return;
624             }
625         }
626         catch (Exception JavaDoc ee)
627         {
628             System.err.println("Problems invoking main");
629             ee.printStackTrace();
630         }
631
632         // start the class
633
try
634         {
635             startClass.newInstance();
636         }
637         catch (Exception JavaDoc e)
638         {
639             System.err.println("Cannot start: " + className);
640             e.printStackTrace();
641             System.exit(1);
642         }
643     } // main
644

645 } // CompierePLAF
646

647 /**
648  * Frame to display Editor
649  */

650 class CompierePLAFFrame extends JFrame JavaDoc
651 {
652     /**
653      * Frame to display Editor
654      */

655     public CompierePLAFFrame()
656     {
657         super("CompierePLAF");
658         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
659         setIconImage(Toolkit.getDefaultToolkit().getImage(CompierePLAF.class.getResource("icons/CL16.gif")));
660         CompierePLAF.showCenterScreen(this);
661     } // CompierePLAFFrame
662

663     /**
664      * Show Editor
665      * @param e event
666      */

667     protected void processWindowEvent (WindowEvent JavaDoc e)
668     {
669         super.processWindowEvent(e);
670         if (e.getID() == WindowEvent.WINDOW_OPENED)
671         {
672             CompierePLAFEditor ed = new CompierePLAFEditor(this, true);
673             dispose();
674         }
675     } // processWindowEvents
676
} // CompierePLAFFrame
677
Popular Tags