View Javadoc

1   /**
2    * This program is free software: you can redistribute it and/or modify
3    * it under the terms of the GNU General Public License as published by
4    * the Free Software Foundation, version 3.
5    *
6    * This program is distributed in the hope that it will be useful,
7    * but WITHOUT ANY WARRANTY; without even the implied warranty of
8    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9    * GNU General Public License for more details.
10   *
11   * You should have received a copy of the GNU General Public License
12   * along with this program. If not, see <http://www.gnu.org/licenses/>.
13   */
14  
15  package net.curre.prefcount.service;
16  
17  import java.awt.*;
18  import java.util.logging.Level;
19  import java.util.logging.Logger;
20  import javax.swing.*;
21  
22  import net.curre.prefcount.gui.theme.skin.*;
23  import net.curre.prefcount.util.Utilities;
24  
25  import org.jvnet.substance.SubstanceLookAndFeel;
26  
27  /**
28   * This is a helper class to assist with
29   * Look and Feel issues.
30   * <p/>
31   * Created date: Jun 11, 2007
32   *
33   * @author Yevgeny Nyden
34   */
35  public class LafThemeService {
36  
37    /** Private class logger. */
38    private static Logger log = Logger.getLogger(LafThemeService.class.toString());
39  
40    /** This holds a reference to the singleton instance. */
41    private static LafThemeService instance;
42  
43    /** Reference to the substance LAF object. */
44    private static SubstanceLookAndFeel substanceLaf;
45  
46    /** Reference to the current skin; */
47    private static PrefSkin currentSkin;
48  
49    /** Array of available themes/skins. */
50    public static final PrefSkin[] AVAILABLE_SKINS = {
51        new DefaultSkin(), new BusinessSkin(), new ModerateSkin(),
52        new NebulaSkin(), new OfficeBlue2007Skin(), new GreenMagicSkin(),
53        new MangoSkin(), new CremeSkin(), new FieldOfWheatSkin(),
54        new FindingNemoSkin()
55    };
56  
57    static {
58      instance = new LafThemeService();
59      substanceLaf = new SubstanceLookAndFeel();
60  //        substanceLaf.initialize();
61      currentSkin = AVAILABLE_SKINS[0];
62    }
63  
64    /** Private constructor. */
65    private LafThemeService() {
66    }
67  
68    /**
69     * Returns an instance of this class to use.
70     *
71     * @return singleton instance of this class to use.
72     */
73    public static LafThemeService getInstance() {
74      return instance;
75    }
76  
77    /**
78     * Returns reference to the current skin.
79     *
80     * @return reference to the current skin.
81     */
82    public PrefSkin getCurrentSkin() {
83      return currentSkin;
84    }
85  
86    /**
87     * Sets the Look and Feel according to the
88     * passed theme/skin name.
89     *
90     * @param skinId     Theme/skin ID (resource key) to set the LaF to.
91     * @param isFirstRun Flag to indicate the first run of this method (when true).
92     */
93    public void setLookAndFeel(final String skinId, boolean isFirstRun) {
94  
95      // fetch the skin object given its ID
96      try {
97        currentSkin = findSkinById(skinId);
98        if (isFirstRun) {
99          UIManager.setLookAndFeel(substanceLaf);
100 
101         // setting background for all frames
102         for (Frame frame : Frame.getFrames()) {
103           frame.setBackground(currentSkin.getMainBackgroundColor());
104         }
105       }
106       if (!isFirstRun && DefaultSkin.NAME_KEY.equals(skinId)) {
107         Utilities.displayMessageInFrame("pref.settings.skin.needsRestart");
108       } else {
109         SubstanceLookAndFeel.setSkin(currentSkin.getSubstanceSkinClassName());
110       }
111 
112     } catch (Exception e) {
113       log.log(Level.WARNING, "Unable to set LAF", e);
114     }
115   }
116 
117   /**
118    * Returns the skin given its ID (resource key).
119    *
120    * @param skinId Skin ID (resource key).
121    * @return The skin with the given ID (resource key).
122    * @throws ServiceException If skin with given ID was not found.
123    */
124   public PrefSkin findSkinById(String skinId) throws ServiceException {
125     for (PrefSkin skin : AVAILABLE_SKINS) {
126       if (skin.getNameResourceKey().equals(skinId)) {
127         return skin;
128       }
129     }
130     throw new ServiceException("Skin with id \"" + skinId + "\" was not found!");
131   }
132 
133 }