1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sf.magicproject.ui;
20
21 import java.awt.Dimension;
22 import java.awt.Frame;
23 import java.awt.Toolkit;
24
25 import javax.swing.JDialog;
26 import javax.swing.JFrame;
27 import javax.swing.JOptionPane;
28 import javax.swing.JRadioButtonMenuItem;
29 import javax.swing.SwingUtilities;
30 import javax.swing.UIManager;
31 import javax.swing.UnsupportedLookAndFeelException;
32 import javax.swing.plaf.metal.MetalLookAndFeel;
33 import javax.swing.plaf.metal.MetalTheme;
34
35 import net.sf.magicproject.stack.StackManager;
36 import net.sf.magicproject.tools.Configuration;
37 import net.sf.magicproject.tools.Log;
38 import net.sf.magicproject.tools.MToolKit;
39 import net.sf.magicproject.ui.i18n.LanguageManager;
40
41 import com.l2fprod.gui.plaf.skin.Skin;
42 import com.l2fprod.gui.plaf.skin.SkinLookAndFeel;
43
44 /***
45 * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
46 * @since 0.90
47 */
48 public final class SkinLF {
49
50 /***
51 * Prevent this class to be instanciated.
52 */
53 private SkinLF() {
54
55 }
56
57 /***
58 * Uninstall the given SkinLF
59 *
60 * @param lookAndFeelName
61 * the look And Feel Name
62 * @throws Exception
63 */
64 public static void uninstallSkinLF(String lookAndFeelName) throws Exception {
65 System.out.println("Uninstalling SkinLF : " + lookAndFeelName);
66 if (lookAndFeelName.endsWith(".xml")) {
67 SkinLookAndFeel.loadThemePackDefinition(
68 MToolKit.getFile(lookAndFeelName).toURI().toURL()).unload();
69 } else if (lookAndFeelName.startsWith("class:")) {
70 ((Skin) Class.forName(lookAndFeelName.substring(7)).newInstance())
71 .unload();
72
73
74 } else if (lookAndFeelName.startsWith("zip:")) {
75 SkinLookAndFeel.loadThemePack(lookAndFeelName.substring("zip:".length()))
76 .unload();
77 }
78 }
79
80 /***
81 * Install the given SkinLF
82 *
83 * @param lookAndFeelName
84 * the look And Feel Name
85 * @throws Exception
86 */
87 public static void installSkinLF(String lookAndFeelName) throws Exception {
88 System.out.print("Installing SkinLF : " + lookAndFeelName + " ...");
89 if (lookAndFeelName.endsWith(".xml")) {
90 SkinLookAndFeel.setSkin(SkinLookAndFeel.loadThemePackDefinition(MToolKit
91 .getFile(lookAndFeelName).toURI().toURL()));
92 UIManager.setLookAndFeel("com.l2fprod.gui.plaf.skin.SkinLookAndFeel");
93 } else if (lookAndFeelName.startsWith("class:")) {
94 SkinLookAndFeel.setSkin((Skin) Class
95 .forName(lookAndFeelName.substring(7)).newInstance());
96 UIManager.setLookAndFeel("com.l2fprod.gui.plaf.skin.SkinLookAndFeel");
97 } else if (lookAndFeelName.startsWith("theme:")) {
98 MetalTheme theme = (MetalTheme) Class.forName(
99 lookAndFeelName.substring(7)).newInstance();
100 MetalLookAndFeel metal = new MetalLookAndFeel();
101 MetalLookAndFeel.setCurrentTheme(theme);
102 UIManager.setLookAndFeel(metal);
103 } else if (lookAndFeelName.startsWith("zip:")) {
104 SkinLookAndFeel.setSkin(SkinLookAndFeel.loadThemePack(lookAndFeelName
105 .substring("zip:".length())));
106 UIManager.setLookAndFeel("com.l2fprod.gui.plaf.skin.SkinLookAndFeel");
107 }
108 MagicUIComponents.frameDecorated = true;
109 JFrame.setDefaultLookAndFeelDecorated(true);
110 JDialog.setDefaultLookAndFeelDecorated(true);
111 System.out.println(" --> ok");
112 }
113
114 /***
115 * Return true if the given skin is a SkinLF one
116 *
117 * @param lookAndFeelName
118 * the Look&Feel
119 * @return true if the given skin is a SkinLF one
120 */
121 public static boolean isSkinLF(String lookAndFeelName) {
122 return lookAndFeelName.endsWith(".xml")
123 || lookAndFeelName.startsWith("class:")
124 || lookAndFeelName.startsWith("zip:")
125 || lookAndFeelName.startsWith("theme:");
126 }
127
128 /***
129 * Install the given L&F
130 *
131 * @param lookAndFeelName
132 * the look & feel to install.
133 * @param source
134 * the component requesting this install.
135 */
136 public static void installLookAndFeel(String lookAndFeelName, Object source) {
137
138 if (MagicUIComponents.lookAndFeelName == lookAndFeelName) {
139 return;
140 }
141 MagicUIComponents.magicForm.setVisible(false);
142 try {
143 if (isSkinLF(MagicUIComponents.lookAndFeelName)) {
144 uninstallSkinLF(MagicUIComponents.lookAndFeelName);
145 }
146 MagicUIComponents.lookAndFeelName = lookAndFeelName;
147 if (isSkinLF(lookAndFeelName)) {
148 installSkinLF(lookAndFeelName);
149 } else {
150 UIManager.setLookAndFeel(lookAndFeelName);
151 }
152 SwingUtilities.updateComponentTreeUI(MagicUIComponents.magicForm);
153 if (MToolKit.fileChooser != null) {
154 SwingUtilities.updateComponentTreeUI(MToolKit.fileChooser);
155 }
156 MagicUIComponents.magicForm.pack();
157 MagicUIComponents.magicForm.setExtendedState(Frame.MAXIMIZED_BOTH);
158 } catch (UnsupportedLookAndFeelException exc) {
159 JRadioButtonMenuItem button = (JRadioButtonMenuItem) source;
160 button.setEnabled(false);
161 button.setSelected(false);
162 for (int i = 0; i < MagicUIComponents.magicForm.lookAndFeels.length; i++) {
163 if (MagicUIComponents.magicForm.lookAndFeels[i].isEnabled()) {
164 MagicUIComponents.magicForm.lookAndFeels[i].setSelected(true);
165 }
166 }
167 } catch (Exception exc) {
168 Log.error(exc);
169 }
170 MagicUIComponents.magicForm.setVisible(true);
171 SwingUtilities.invokeLater(REFRESH_RUNNER);
172
173 if (UIManager.getLookAndFeel().getSupportsWindowDecorations() != MagicUIComponents.frameDecorated) {
174
175 JOptionPane.showMessageDialog(MagicUIComponents.magicForm,
176 LanguageManager.getString("lfrestart"), LanguageManager
177 .getString("restart"), JOptionPane.INFORMATION_MESSAGE);
178 }
179 }
180
181 /***
182 * This class refresh the split bar
183 */
184 public static final Runnable REFRESH_RUNNER = new Runnable() {
185 public void run() {
186 Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
187 StackManager.PLAYERS[0].handSplitter
188 .setDividerLocation((int) ((dim.width - 200) * 0.75));
189 if (Configuration.getBoolean("reverseSide", false)) {
190 StackManager.PLAYERS[1].handSplitter
191 .setDividerLocation((int) ((dim.width - 200) * 0.25));
192 } else {
193 StackManager.PLAYERS[1].handSplitter
194 .setDividerLocation((int) ((dim.width - 200) * 0.75));
195 }
196 if (MagicUIComponents.splash != null) {
197 MagicUIComponents.splash.close();
198 MagicUIComponents.splash = null;
199 }
200 MagicUIComponents.magicForm.setVisible(true);
201 }
202 };
203
204 }