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; either version 2 of the License, or |
---|
5 | * (at your option) any later version. |
---|
6 | * |
---|
7 | * This program is distributed in the hope that it will be useful, |
---|
8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
10 | * GNU General Public License for more details. |
---|
11 | * |
---|
12 | * You should have received a copy of the GNU General Public License |
---|
13 | * along with this program; if not, write to the Free Software |
---|
14 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
15 | */ |
---|
16 | |
---|
17 | /* |
---|
18 | * LookAndFeel.java |
---|
19 | * Copyright (C) 2005 University of Waikato, Hamilton, New Zealand |
---|
20 | * |
---|
21 | */ |
---|
22 | |
---|
23 | |
---|
24 | package weka.gui; |
---|
25 | |
---|
26 | import weka.core.Utils; |
---|
27 | |
---|
28 | import java.util.Properties; |
---|
29 | import javax.swing.JOptionPane; |
---|
30 | import javax.swing.UIManager; |
---|
31 | import javax.swing.UIManager.LookAndFeelInfo; |
---|
32 | |
---|
33 | /** |
---|
34 | * A little helper class for setting the Look and Feel of the user interface. |
---|
35 | * Was necessary, since Java 1.5 sometimes crashed the WEKA GUI (e.g. under |
---|
36 | * Linux/Gnome). Running this class from the commandline will print all |
---|
37 | * available Look and Feel themes. |
---|
38 | * |
---|
39 | * @author FracPete (fracpete at waikato dot ac dot nz) |
---|
40 | * @version $Revision: 1.3 $ |
---|
41 | */ |
---|
42 | public class LookAndFeel { |
---|
43 | |
---|
44 | /** The name of the properties file */ |
---|
45 | public static String PROPERTY_FILE = "weka/gui/LookAndFeel.props"; |
---|
46 | |
---|
47 | /** Contains the look and feel properties */ |
---|
48 | protected static Properties LOOKANDFEEL_PROPERTIES; |
---|
49 | |
---|
50 | static { |
---|
51 | try { |
---|
52 | LOOKANDFEEL_PROPERTIES = Utils.readProperties(PROPERTY_FILE); |
---|
53 | } |
---|
54 | catch (Exception ex) { |
---|
55 | JOptionPane.showMessageDialog(null, |
---|
56 | "LookAndFeel: Could not read a LookAndFeel configuration file.\n" |
---|
57 | +"An example file is included in the Weka distribution.\n" |
---|
58 | +"This file should be named \"" + PROPERTY_FILE + "\" and\n" |
---|
59 | +"should be placed either in your user home (which is set\n" |
---|
60 | +"to \"" + System.getProperties().getProperty("user.home") + "\")\n" |
---|
61 | +"or the directory that java was started from\n", |
---|
62 | "LookAndFeel", |
---|
63 | JOptionPane.ERROR_MESSAGE); |
---|
64 | } |
---|
65 | } |
---|
66 | |
---|
67 | /** |
---|
68 | * sets the look and feel to the specified class |
---|
69 | * |
---|
70 | * @param classname the look and feel to use |
---|
71 | * @return whether setting was successful |
---|
72 | */ |
---|
73 | public static boolean setLookAndFeel(String classname) { |
---|
74 | boolean result; |
---|
75 | |
---|
76 | try { |
---|
77 | UIManager.setLookAndFeel(classname); |
---|
78 | result = true; |
---|
79 | } |
---|
80 | catch (Exception e) { |
---|
81 | e.printStackTrace(); |
---|
82 | result = false; |
---|
83 | } |
---|
84 | |
---|
85 | return result; |
---|
86 | } |
---|
87 | |
---|
88 | /** |
---|
89 | * sets the look and feel to the one in the props-file or if not set the |
---|
90 | * default one of the system |
---|
91 | * |
---|
92 | * @return whether setting was successful |
---|
93 | */ |
---|
94 | public static boolean setLookAndFeel() { |
---|
95 | String classname; |
---|
96 | |
---|
97 | classname = LOOKANDFEEL_PROPERTIES.getProperty("Theme", ""); |
---|
98 | if (classname.equals("")) { |
---|
99 | // Java 1.5 crashes under Gnome if one sets it to the GTKLookAndFeel |
---|
100 | // theme, hence we don't set any theme by default if we're on a Linux |
---|
101 | // box. |
---|
102 | if (System.getProperty("os.name").equalsIgnoreCase("linux")) { |
---|
103 | return true; |
---|
104 | } |
---|
105 | else { |
---|
106 | classname = getSystemLookAndFeel(); |
---|
107 | } |
---|
108 | } |
---|
109 | |
---|
110 | return setLookAndFeel(classname); |
---|
111 | } |
---|
112 | |
---|
113 | /** |
---|
114 | * returns the system LnF classname |
---|
115 | * |
---|
116 | * @return the name of the System LnF class |
---|
117 | */ |
---|
118 | public static String getSystemLookAndFeel() { |
---|
119 | return UIManager.getSystemLookAndFeelClassName(); |
---|
120 | } |
---|
121 | |
---|
122 | /** |
---|
123 | * returns an array with the classnames of all the installed LnFs |
---|
124 | * |
---|
125 | * @return the installed LnFs |
---|
126 | */ |
---|
127 | public static String[] getInstalledLookAndFeels() { |
---|
128 | String[] result; |
---|
129 | LookAndFeelInfo[] laf; |
---|
130 | int i; |
---|
131 | |
---|
132 | laf = UIManager.getInstalledLookAndFeels(); |
---|
133 | result = new String[laf.length]; |
---|
134 | for (i = 0; i < laf.length; i++) |
---|
135 | result[i] = laf[i].getClassName(); |
---|
136 | |
---|
137 | return result; |
---|
138 | } |
---|
139 | |
---|
140 | /** |
---|
141 | * prints all the available LnFs to stdout |
---|
142 | * |
---|
143 | * @param args the commandline options |
---|
144 | */ |
---|
145 | public static void main(String[] args) { |
---|
146 | String[] list; |
---|
147 | int i; |
---|
148 | |
---|
149 | System.out.println("\nInstalled Look and Feel themes:"); |
---|
150 | list = getInstalledLookAndFeels(); |
---|
151 | for (i = 0; i < list.length; i++) |
---|
152 | System.out.println((i+1) + ". " + list[i]); |
---|
153 | |
---|
154 | System.out.println("\nNote: a theme can be set in '" + PROPERTY_FILE + "'."); |
---|
155 | } |
---|
156 | } |
---|