1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package net.curre.prefcount.util;
16
17 import java.awt.*;
18 import java.util.Calendar;
19 import java.util.ResourceBundle;
20 import java.util.logging.Logger;
21 import javax.swing.*;
22
23 import net.curre.prefcount.App;
24 import net.curre.prefcount.gui.aa.AAJPanel;
25 import net.curre.prefcount.gui.aa.AAJLabel;
26
27 import org.apache.commons.lang.StringUtils;
28 import org.apache.commons.lang.time.FastDateFormat;
29
30
31
32
33
34
35
36
37
38 public class Utilities {
39
40
41 private static Logger log = Logger.getLogger(Utilities.class.toString());
42
43
44 public static enum FieldType {
45
46 UNDEFINED, INTEGER
47 }
48
49
50 public static enum PlatformType {
51
52 MAC_OS, LINUX, WINDOWS, UNKNOWN
53 }
54
55
56 public static Calendar lastTime = Calendar.getInstance();
57
58
59
60
61
62
63
64
65 public static boolean validateTextField(JTextField field, FieldType type) {
66 String str = field.getText().trim();
67 switch (type) {
68 case INTEGER:
69 return StringUtils.isNumeric(str);
70 default:
71 log.severe("ERROR: validateTextField: Unknown type: " + type);
72 }
73 return false;
74 }
75
76
77
78
79
80
81
82
83
84 public static String getFirstLetterFromField(JTextField field) {
85 String str = field.getText();
86 if (StringUtils.isBlank(str)) {
87 return null;
88 }
89 return str.trim().substring(0, 1).toUpperCase();
90 }
91
92
93
94
95
96
97
98
99
100
101 public static int parseIntFromTextField(JTextField field) {
102 if (field != null) {
103 String value = field.getText().trim();
104 if (value.length() != 0) {
105 return Integer.parseInt(value);
106 }
107 }
108 return 0;
109 }
110
111
112
113
114
115
116
117
118 public static Dimension determineSizeOfString(Graphics2D g2, String str) {
119 FontMetrics metrics = g2.getFontMetrics(g2.getFont());
120 int height = metrics.getHeight();
121 int width = metrics.stringWidth(str);
122 return new Dimension(width, height);
123 }
124
125
126
127
128
129
130
131
132
133 public static ImageIcon createImage(String fileName) {
134 return new ImageIcon(App.class.getResource("images/" + fileName + ".gif"));
135 }
136
137
138
139
140
141
142
143
144
145
146 public static String underlineLetter(String str, int ind) {
147 return "<HTML>" + str.substring(0, ind) + "<U>" + str.charAt(ind) +
148 "</U>" + str.substring(ind + 1);
149
150 }
151
152
153
154
155
156
157
158
159
160 public static Color createDarkerColor(Color color, int decrease) {
161 return new Color(color.getRed() - decrease,
162 color.getGreen() - decrease,
163 color.getBlue() - decrease);
164 }
165
166
167
168
169
170
171
172 public static void displayMessageInFrame(String messageKey) {
173 String msg = ResourceBundle.getBundle("default").getString(messageKey);
174
175 }
176
177
178
179
180
181
182
183
184 public static void printTime(String msg) {
185 Calendar currTime = Calendar.getInstance();
186 Calendar diff = Calendar.getInstance();
187 long currMls = currTime.getTimeInMillis();
188 diff.setTimeInMillis(currMls - lastTime.getTimeInMillis());
189 FastDateFormat f = FastDateFormat.getInstance("mm:ss:SSS");
190 System.out.println("TIME:::::: " + f.format(currTime.getTime()) +
191 " (" + f.format(diff) + ") - " + msg);
192 lastTime.setTimeInMillis(currMls);
193 }
194
195
196 public static void printLookAndFeels() {
197 UIManager.LookAndFeelInfo laf[] = UIManager.getInstalledLookAndFeels();
198 for (int i = 0, n = laf.length; i < n; i++) {
199 System.out.print("LAF Name: " + laf[i].getName() + "\t");
200 System.out.println(" LAF Class name: " + laf[i].getClassName());
201 }
202 }
203
204
205 public static void printAvailableFonts() {
206 JFrame f = new JFrame("Testing Fonts");
207 f.setSize(400, 400);
208 JPanel mainPanel = new AAJPanel();
209 f.add(mainPanel);
210 GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
211 String fontNames[] = ge.getAvailableFontFamilyNames();
212 int j = fontNames.length;
213 for (int i = 0; i < j; ++i) {
214 JPanel panel = new JPanel();
215 JLabel label = new AAJLabel(fontNames[i]);
216 label.setFont(new Font(fontNames[i], Font.PLAIN, 16));
217 panel.add(label);
218 mainPanel.add(panel);
219
220 System.out.println(fontNames[i]);
221 }
222 f.pack();
223 f.setVisible(true);
224 }
225
226
227
228
229
230
231 public static PlatformType getPlatformType() {
232 if (System.getProperty("mrj.version") == null) {
233 String osProp = System.getProperty("os.name").toLowerCase();
234 if (osProp.indexOf("windows") != -1) {
235 return PlatformType.WINDOWS;
236 } else if (osProp.indexOf("mac") != -1) {
237 return PlatformType.MAC_OS;
238 } else if (osProp.indexOf("linux") != -1) {
239 return PlatformType.LINUX;
240 } else {
241 return PlatformType.UNKNOWN;
242 }
243 }
244 return PlatformType.MAC_OS;
245 }
246
247
248
249
250
251
252 public static boolean isMacOs() {
253 return getPlatformType() == PlatformType.MAC_OS;
254 }
255
256 }