%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
ca.spaz.util.ToolBox |
|
|
1 | /* |
|
2 | * Created on 29-May-2005 |
|
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 | 0 | 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 | 30 | String userHome = System.getProperty("user.home"); |
29 | 30 | if (isMacOSX()) { |
30 | 0 | return new File(userHome, "Library/Preferences/"); |
31 | } |
|
32 | 30 | if (isWindows()) { |
33 | 0 | return new File(userHome, "Application Data"); |
34 | } |
|
35 | // default |
|
36 | 30 | return new File(userHome); |
37 | } |
|
38 | ||
39 | public static File getUserAppDirectory(String appname) { |
|
40 | 30 | if (isWindows() || isMacOSX()) { |
41 | 0 | return new File(getUserDirectory(), appname); |
42 | } |
|
43 | 30 | 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 | 60 | String osname = System.getProperty("os.name"); |
52 | 60 | 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 | 60 | String osname = System.getProperty("os.name").toLowerCase(); |
61 | 60 | 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 | 0 | String osname = System.getProperty("os.name"); |
71 | 0 | if (osname.equalsIgnoreCase("Windows 95")) return true; |
72 | 0 | if (osname.equalsIgnoreCase("Windows 98")) return true; |
73 | 0 | if (osname.equalsIgnoreCase("Windows Me")) return true; |
74 | 0 | 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 | 0 | Inflater decompresser = new Inflater(); |
84 | 0 | decompresser.setInput(data); |
85 | 0 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
86 | 0 | byte[] buffer=new byte[1024]; |
87 | 0 | while (!decompresser.finished()) { |
88 | try { |
|
89 | 0 | int cnt = decompresser.inflate(buffer); |
90 | 0 | baos.write(buffer, 0, cnt); |
91 | 0 | } catch (DataFormatException e) { |
92 | 0 | e.printStackTrace(); |
93 | 0 | } |
94 | 0 | } |
95 | 0 | 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 | 0 | DataInputStream din = new DataInputStream( |
107 | new BufferedInputStream(fromURL.openStream())); |
|
108 | 0 | DataOutputStream out = new DataOutputStream( |
109 | new BufferedOutputStream(class="keyword">new FileOutputStream(toFile))); |
|
110 | 0 | int b = din.read(); |
111 | 0 | while (b >= 0) { |
112 | 0 | out.write((byte)b); |
113 | 0 | b = din.read(); |
114 | 0 | } |
115 | 0 | din.close(); |
116 | 0 | out.close(); |
117 | 0 | } |
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 | 0 | if (dir == null) { |
127 | 0 | return false; |
128 | } |
|
129 | // to see if this directory is actually a symbolic link to a directory, |
|
130 | // we want to get its canonical path - that is, we follow the link to |
|
131 | // the file it's actually linked to |
|
132 | File candir; |
|
133 | try { |
|
134 | 0 | candir = dir.getCanonicalFile(); |
135 | 0 | } catch (IOException e) { |
136 | 0 | return false; |
137 | 0 | } |
138 | ||
139 | // a symbolic link has a different canonical path than its actual path, |
|
140 | // unless it's a link to itself |
|
141 | 0 | if (!candir.equals(dir.getAbsoluteFile()) && !isWindows()) { |
142 | // this file is a symbolic link, and there's no reason for us to |
|
143 | // follow it, because then we might be deleting something outside of |
|
144 | // the directory we were told to delete |
|
145 | 0 | return false; |
146 | } |
|
147 | ||
148 | // now we go through all of the files and subdirectories in the |
|
149 | // directory and delete them one by one |
|
150 | 0 | File[] files = candir.listFiles(); |
151 | 0 | if (files != null) { |
152 | 0 | for (int i = 0; i < files.length; i++) { |
153 | 0 | File file = files[i]; |
154 | ||
155 | // in case this directory is actually a symbolic link, or it's |
|
156 | // empty, we want to try to delete the link before we try |
|
157 | // anything |
|
158 | 0 | boolean deleted = file.delete(); |
159 | 0 | if (!deleted) { |
160 | // deleting the file failed, so maybe it's a non-empty |
|
161 | // directory |
|
162 | 0 | if (file.isDirectory()) deleteDir(file); |
163 | ||
164 | // otherwise, there's nothing else we can do |
|
165 | } |
|
166 | } |
|
167 | } |
|
168 | ||
169 | // now that we tried to clear the directory out, we can try to delete it |
|
170 | // again |
|
171 | 0 | 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 | 0 | JDialog dialog = new JDialog(parent); |
185 | 0 | dialog.setTitle(title); |
186 | 0 | dialog.getContentPane().add(content); |
187 | 0 | dialog.pack(); |
188 | 0 | dialog.setModal(true); |
189 | 0 | dialog.setLocationRelativeTo(parent); |
190 | 0 | 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 | 0 | Thread.sleep(tix); |
202 | 0 | } catch (InterruptedException e) { |
203 | 0 | e.printStackTrace(); |
204 | 0 | } |
205 | 0 | } |
206 | ||
207 | /** |
|
208 | * Centers a given frame on the screen. |
|
209 | */ |
|
210 | public static void centerFrame(Window frame) { |
|
211 | 0 | Toolkit defaultToolkit = Toolkit.getDefaultToolkit(); |
212 | 0 | Dimension scrSize = defaultToolkit.getScreenSize(); |
213 | 0 | int width = frame.getWidth(); |
214 | 0 | int height = frame.getHeight(); |
215 | 0 | frame.setLocation(scrSize.width / 2 - width / 2, scrSize.height / 2 - height / 2); |
216 | 0 | } |
217 | ||
218 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |