View Javadoc

1   /*
2    *   Magic-Project is a turn based strategy simulator
3    *   Copyright (C) 2003-2007 Fabrice Daugan
4    *
5    *   This program is free software; you can redistribute it and/or modify it 
6    * under the terms of the GNU General Public License as published by the Free 
7    * Software Foundation; either version 2 of the License, or (at your option) any
8    * later version.
9    *
10   *   This program is distributed in the hope that it will be useful, but WITHOUT 
11   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12   * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
13   * details.
14   *
15   *   You should have received a copy of the GNU General Public License along  
16   * with this program; if not, write to the Free Software Foundation, Inc., 
17   * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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  		// nothing to do
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  			// } else if (lookAndFeelName.startsWith("theme:")) {
73  			// TODO Implement "theme" L&F
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 		// apply the new UI
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 			// Make sure we have nice window decorations.
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 }