1
2
3
4 package ca.spaz.util;
5
6 import java.awt.*;
7 import java.io.*;
8 import java.net.URL;
9 import java.util.zip.*;
10
11 import javax.swing.*;
12
13 /***
14 * A class with misc. static utility methods.
15 *
16 * @author Aaron Davidson
17 */
18 public class ToolBox {
19
20 /***
21 * Get the system specific user data directory.
22 * Returns the appropriate location to store application
23 * data for the user, on the current platform.
24 *
25 * @return a File for the base user data directory
26 */
27 public static File getUserDirectory() {
28 String userHome = System.getProperty("user.home");
29 if (isMacOSX()) {
30 return new File(userHome, "Library/Preferences/");
31 }
32 if (isWindows()) {
33 return new File(userHome, "Application Data");
34 }
35
36 return new File(userHome);
37 }
38
39 public static File getUserAppDirectory(String appname) {
40 if (isWindows() || isMacOSX()) {
41 return new File(getUserDirectory(), appname);
42 }
43 return new File(getUserDirectory(), "." + appname );
44 }
45
46 /***
47 * Returns true if we are currently running on Mac OS X
48 * @return true if we are currently running on Mac OS X
49 */
50 public static boolean isMacOSX() {
51 String osname = System.getProperty("os.name");
52 return (osname.equals("Mac OS X"));
53 }
54
55 /***
56 * Returns true if we are currently running on Windows
57 * @return true if we are currently running on Windows
58 */
59 private static boolean isWindows() {
60 String osname = System.getProperty("os.name").toLowerCase();
61 return osname.startsWith("windows");
62 }
63
64 /***
65 * Returns true if we are currently running on an older Windows OS
66 * such as Windows 95, 98, ME
67 * @return true if we are currently running on an older Windows OS
68 */
69 private static boolean isOlderWindows() {
70 String osname = System.getProperty("os.name");
71 if (osname.equalsIgnoreCase("Windows 95")) return true;
72 if (osname.equalsIgnoreCase("Windows 98")) return true;
73 if (osname.equalsIgnoreCase("Windows Me")) return true;
74 return false;
75 }
76
77 /***
78 * Decompress a byte array
79 * @param data the compressed bytes
80 * @return the uncompressed bytes
81 */
82 public static byte[] uncompress(byte[] data) {
83 Inflater decompresser = new Inflater();
84 decompresser.setInput(data);
85 ByteArrayOutputStream baos = new ByteArrayOutputStream();
86 byte[] buffer=new byte[1024];
87 while (!decompresser.finished()) {
88 try {
89 int cnt = decompresser.inflate(buffer);
90 baos.write(buffer, 0, cnt);
91 } catch (DataFormatException e) {
92 e.printStackTrace();
93 }
94 }
95 return baos.toByteArray();
96 }
97
98 /***
99 * Download a URL to a file
100 *
101 * @param fromURL
102 * @param toFile
103 * @throws IOException
104 */
105 public static void downloadBinary(URL fromURL, File toFile) throws IOException {
106 DataInputStream din = new DataInputStream(
107 new BufferedInputStream(fromURL.openStream()));
108 DataOutputStream out = new DataOutputStream(
109 new BufferedOutputStream(new FileOutputStream(toFile)));
110 int b = din.read();
111 while (b >= 0) {
112 out.write((byte)b);
113 b = din.read();
114 }
115 din.close();
116 out.close();
117 }
118
119 /***
120 * Recursively delete a directory
121 *
122 * @param dir The directory to delete.
123 * @return <code>true</code> if the delete succeeded, <code>false</code> if it failed at some point.
124 */
125 public static boolean deleteDir(File dir) {
126 if (dir == null) {
127 return false;
128 }
129
130
131
132 File candir;
133 try {
134 candir = dir.getCanonicalFile();
135 } catch (IOException e) {
136 return false;
137 }
138
139
140
141 if (!candir.equals(dir.getAbsoluteFile()) && !isWindows()) {
142
143
144
145 return false;
146 }
147
148
149
150 File[] files = candir.listFiles();
151 if (files != null) {
152 for (int i = 0; i < files.length; i++) {
153 File file = files[i];
154
155
156
157
158 boolean deleted = file.delete();
159 if (!deleted) {
160
161
162 if (file.isDirectory()) deleteDir(file);
163
164
165 }
166 }
167 }
168
169
170
171 return dir.delete();
172 }
173
174 /***
175 * Handy dandy method to pop up a quick and easy Dialog.
176 *
177 * @param parent the parent frame
178 * @param title the dialog title
179 * @param content the dialog content panel
180 *
181 * @return a dialog ready to display
182 */
183 public static JDialog getDialog(JFrame parent, String title, JComponent content) {
184 JDialog dialog = new JDialog(parent);
185 dialog.setTitle(title);
186 dialog.getContentPane().add(content);
187 dialog.pack();
188 dialog.setModal(true);
189 dialog.setLocationRelativeTo(parent);
190 return dialog;
191 }
192
193 /***
194 * Sleep for some amount of time.
195 * Conveniently wraps sleep in exception.
196 *
197 * @param tix milliseconds to sleep.
198 */
199 public static void sleep(int tix) {
200 try {
201 Thread.sleep(tix);
202 } catch (InterruptedException e) {
203 e.printStackTrace();
204 }
205 }
206
207 /***
208 * Centers a given frame on the screen.
209 */
210 public static void centerFrame(Window frame) {
211 Toolkit defaultToolkit = Toolkit.getDefaultToolkit();
212 Dimension scrSize = defaultToolkit.getScreenSize();
213 int width = frame.getWidth();
214 int height = frame.getHeight();
215 frame.setLocation(scrSize.width / 2 - width / 2, scrSize.height / 2 - height / 2);
216 }
217
218 }