Mercurial > projects > dwt2
comparison org.eclipse.swt.win32.win32.x86/src/org/eclipse/swt/internal/Compatibility.d @ 0:6dd524f61e62
add dwt win and basic java stuff
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Mon, 02 Mar 2009 14:44:16 +0100 |
parents | |
children | 4c0057e71936 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:6dd524f61e62 |
---|---|
1 /******************************************************************************* | |
2 * Copyright (c) 2000, 2007 IBM Corporation and others. | |
3 * All rights reserved. This program and the accompanying materials | |
4 * are made available under the terms of the Eclipse Public License v1.0 | |
5 * which accompanies this distribution, and is available at | |
6 * http://www.eclipse.org/legal/epl-v10.html | |
7 * | |
8 * Contributors: | |
9 * IBM Corporation - initial API and implementation | |
10 * Port to the D programming language: | |
11 * Frank Benoit <benoit@tionex.de> | |
12 *******************************************************************************/ | |
13 module org.eclipse.swt.internal.Compatibility; | |
14 | |
15 /+ | |
16 import java.io.*; | |
17 import java.io.File; | |
18 import java.text.MessageFormat; | |
19 import java.util.MissingResourceException; | |
20 import java.util.ResourceBundle; | |
21 import java.util.zip.DeflaterOutputStream; | |
22 import java.util.zip.InflaterInputStream; | |
23 +/ | |
24 | |
25 import org.eclipse.swt.SWT; | |
26 public import java.io.FileInputStream; | |
27 public import java.io.FileOutputStream; | |
28 public import java.util.zip.InflaterInputStream; | |
29 | |
30 import Math = tango.math.Math; | |
31 import Unicode = tango.text.Unicode; | |
32 import tango.sys.Process; | |
33 import java.lang.all; | |
34 import java.util.ResourceBundle; | |
35 import tango.io.Path; | |
36 import tango.text.convert.Format; | |
37 | |
38 /** | |
39 * This class is a placeholder for utility methods commonly | |
40 * used on J2SE platforms but not supported on some J2ME | |
41 * profiles. | |
42 * <p> | |
43 * It is part of our effort to provide support for both J2SE | |
44 * and J2ME platforms. | |
45 * </p> | |
46 * <p> | |
47 * IMPORTANT: some of the methods have been modified from their | |
48 * J2SE parents. Refer to the description of each method for | |
49 * specific changes. | |
50 * </p> | |
51 * <ul> | |
52 * <li>Exceptions thrown may differ since J2ME's set of | |
53 * exceptions is a subset of J2SE's one. | |
54 * </li> | |
55 * <li>The range of the mathematic functions is subject to | |
56 * change. | |
57 * </li> | |
58 * </ul> | |
59 */ | |
60 public final class Compatibility { | |
61 | |
62 /** | |
63 * Returns the PI constant as a double. | |
64 */ | |
65 public static const real PI = Math.PI; | |
66 | |
67 static const real toRadians = PI / 180; | |
68 | |
69 /** | |
70 * Answers the length of the side adjacent to the given angle | |
71 * of a right triangle. In other words, it returns the integer | |
72 * conversion of length * cos (angle). | |
73 * <p> | |
74 * IMPORTANT: the j2me version has an additional restriction on | |
75 * the argument. length must be between -32767 and 32767 (inclusive). | |
76 * </p> | |
77 * | |
78 * @param angle the angle in degrees | |
79 * @param length the length of the triangle's hypotenuse | |
80 * @return the integer conversion of length * cos (angle) | |
81 */ | |
82 public static int cos(int angle, int length) { | |
83 return cast(int)(Math.cos(angle * toRadians) * length); | |
84 } | |
85 | |
86 /** | |
87 * Answers the length of the side opposite to the given angle | |
88 * of a right triangle. In other words, it returns the integer | |
89 * conversion of length * sin (angle). | |
90 * <p> | |
91 * IMPORTANT: the j2me version has an additional restriction on | |
92 * the argument. length must be between -32767 and 32767 (inclusive). | |
93 * </p> | |
94 * | |
95 * @param angle the angle in degrees | |
96 * @param length the length of the triangle's hypotenuse | |
97 * @return the integer conversion of length * sin (angle) | |
98 */ | |
99 public static int sin(int angle, int length) { | |
100 return cast(int)(Math.sin(angle * toRadians) * length); | |
101 } | |
102 | |
103 /** | |
104 * Answers the most negative (i.e. closest to negative infinity) | |
105 * integer value which is greater than the number obtained by dividing | |
106 * the first argument p by the second argument q. | |
107 * | |
108 * @param p numerator | |
109 * @param q denominator (must be different from zero) | |
110 * @return the ceiling of the rational number p / q. | |
111 */ | |
112 public static int ceil(int p, int q) { | |
113 return cast(int)Math.ceil(cast(float)p / q); | |
114 } | |
115 | |
116 /** | |
117 * Answers whether the indicated file exists or not. | |
118 * | |
119 * @param parent the file's parent directory | |
120 * @param child the file's name | |
121 * @return true if the file exists | |
122 */ | |
123 public static bool fileExists(String parent, String child) { | |
124 return FS.exists( FS.join(parent, child)); | |
125 } | |
126 | |
127 /** | |
128 * Answers the most positive (i.e. closest to positive infinity) | |
129 * integer value which is less than the number obtained by dividing | |
130 * the first argument p by the second argument q. | |
131 * | |
132 * @param p numerator | |
133 * @param q denominator (must be different from zero) | |
134 * @return the floor of the rational number p / q. | |
135 */ | |
136 public static int floor(int p, int q) { | |
137 return cast(int)Math.floor(cast(double)p / q); | |
138 } | |
139 | |
140 /** | |
141 * Answers the result of rounding to the closest integer the number obtained | |
142 * by dividing the first argument p by the second argument q. | |
143 * <p> | |
144 * IMPORTANT: the j2me version has an additional restriction on | |
145 * the arguments. p must be within the range 0 - 32767 (inclusive). | |
146 * q must be within the range 1 - 32767 (inclusive). | |
147 * </p> | |
148 * | |
149 * @param p numerator | |
150 * @param q denominator (must be different from zero) | |
151 * @return the closest integer to the rational number p / q | |
152 */ | |
153 public static int round(int p, int q) { | |
154 return cast(int)Math.round(cast(float)p / q); | |
155 } | |
156 | |
157 /** | |
158 * Returns 2 raised to the power of the argument. | |
159 * | |
160 * @param n an int value between 0 and 30 (inclusive) | |
161 * @return 2 raised to the power of the argument | |
162 * | |
163 * @exception IllegalArgumentException <ul> | |
164 * <li>ERROR_INVALID_RANGE - if the argument is not between 0 and 30 (inclusive)</li> | |
165 * </ul> | |
166 */ | |
167 public static int pow2(int n) { | |
168 if (n >= 1 && n <= 30) | |
169 return 2 << (n - 1); | |
170 else if (n != 0) { | |
171 SWT.error(SWT.ERROR_INVALID_RANGE); | |
172 } | |
173 return 1; | |
174 } | |
175 | |
176 /** | |
177 * Create an DeflaterOutputStream if such things are supported. | |
178 * | |
179 * @param stream the output stream | |
180 * @return a deflater stream or <code>null</code> | |
181 * @exception IOException | |
182 * | |
183 * @since 3.4 | |
184 */ | |
185 public static OutputStream newDeflaterOutputStream(OutputStream stream) { | |
186 //DWT_TODO | |
187 implMissing(__FILE__,__LINE__); | |
188 return null; | |
189 //return new DeflaterOutputStream(stream); | |
190 } | |
191 | |
192 /** | |
193 * Open a file if such things are supported. | |
194 * | |
195 * @param filename the name of the file to open | |
196 * @return a stream on the file if it could be opened. | |
197 * @exception IOException | |
198 */ | |
199 public static InputStream newFileInputStream(String filename) { | |
200 return new FileInputStream(filename); | |
201 } | |
202 | |
203 /** | |
204 * Open a file if such things are supported. | |
205 * | |
206 * @param filename the name of the file to open | |
207 * @return a stream on the file if it could be opened. | |
208 * @exception IOException | |
209 */ | |
210 public static OutputStream newFileOutputStream(String filename) { | |
211 return new FileOutputStream(filename); | |
212 } | |
213 | |
214 /** | |
215 * Create an InflaterInputStream if such things are supported. | |
216 * | |
217 * @param stream the input stream | |
218 * @return a inflater stream or <code>null</code> | |
219 * @exception IOException | |
220 * | |
221 * @since 3.3 | |
222 */ | |
223 public static InflaterInputStream newInflaterInputStream(InputStream stream) { | |
224 return new InflaterInputStream(stream); | |
225 } | |
226 | |
227 /** | |
228 * Answers whether the character is a letter. | |
229 * | |
230 * @param c the character | |
231 * @return true when the character is a letter | |
232 */ | |
233 public static bool isLetter(dchar c) { | |
234 return Unicode.isLetter(c); | |
235 } | |
236 | |
237 /** | |
238 * Answers whether the character is a letter or a digit. | |
239 * | |
240 * @param c the character | |
241 * @return true when the character is a letter or a digit | |
242 */ | |
243 public static bool isLetterOrDigit(dchar c) { | |
244 return Unicode.isLetterOrDigit(c); | |
245 } | |
246 | |
247 /** | |
248 * Answers whether the character is a Unicode space character. | |
249 * | |
250 * @param c the character | |
251 * @return true when the character is a Unicode space character | |
252 */ | |
253 public static bool isSpaceChar(dchar c) { | |
254 return Unicode.isSpace(c); | |
255 } | |
256 | |
257 /** | |
258 * Answers whether the character is a whitespace character. | |
259 * | |
260 * @param c the character to test | |
261 * @return true if the character is whitespace | |
262 */ | |
263 public static bool isWhitespace(dchar c) { | |
264 return Unicode.isWhitespace(c); | |
265 } | |
266 | |
267 /** | |
268 * Execute a program in a separate platform process if the | |
269 * underlying platform support this. | |
270 * <p> | |
271 * The new process inherits the environment of the caller. | |
272 * </p> | |
273 * | |
274 * @param prog the name of the program to execute | |
275 * | |
276 * @exception ProcessException | |
277 * if the program cannot be executed | |
278 */ | |
279 public static void exec(String prog) { | |
280 auto proc = new Process( prog ); | |
281 proc.execute; | |
282 } | |
283 | |
284 /** | |
285 * Execute progArray[0] in a separate platform process if the | |
286 * underlying platform support this. | |
287 * <p> | |
288 * The new process inherits the environment of the caller. | |
289 * <p> | |
290 * | |
291 * @param progArray array containing the program to execute and its arguments | |
292 * | |
293 * @exception ProcessException | |
294 * if the program cannot be executed | |
295 */ | |
296 public static void exec(String[] progArray) { | |
297 auto proc = new Process( progArray ); | |
298 proc.execute; | |
299 } | |
300 | |
301 const ImportData[] SWTMessagesBundleData = [ | |
302 getImportData!( "swt.internal.SWTMessages.properties" ), | |
303 getImportData!( "swt.internal.SWTMessages_ar.properties" ), | |
304 getImportData!( "swt.internal.SWTMessages_cs.properties" ), | |
305 getImportData!( "swt.internal.SWTMessages_da.properties" ), | |
306 getImportData!( "swt.internal.SWTMessages_de.properties" ), | |
307 getImportData!( "swt.internal.SWTMessages_el.properties" ), | |
308 getImportData!( "swt.internal.SWTMessages_es.properties" ), | |
309 getImportData!( "swt.internal.SWTMessages_fi.properties" ), | |
310 getImportData!( "swt.internal.SWTMessages_fr.properties" ), | |
311 getImportData!( "swt.internal.SWTMessages_hu.properties" ), | |
312 getImportData!( "swt.internal.SWTMessages_it.properties" ), | |
313 getImportData!( "swt.internal.SWTMessages_iw.properties" ), | |
314 getImportData!( "swt.internal.SWTMessages_ja.properties" ), | |
315 getImportData!( "swt.internal.SWTMessages_ko.properties" ), | |
316 getImportData!( "swt.internal.SWTMessages_nl.properties" ), | |
317 getImportData!( "swt.internal.SWTMessages_no.properties" ), | |
318 getImportData!( "swt.internal.SWTMessages_pl.properties" ), | |
319 getImportData!( "swt.internal.SWTMessages_pt_BR.properties" ), | |
320 getImportData!( "swt.internal.SWTMessages_pt.properties" ), | |
321 getImportData!( "swt.internal.SWTMessages_ru.properties" ), | |
322 getImportData!( "swt.internal.SWTMessages_sv.properties" ), | |
323 getImportData!( "swt.internal.SWTMessages_tr.properties" ), | |
324 getImportData!( "swt.internal.SWTMessages_zh_HK.properties" ), | |
325 getImportData!( "swt.internal.SWTMessages_zh.properties" ), | |
326 getImportData!( "swt.internal.SWTMessages_zh_TW.properties" ) | |
327 ]; | |
328 | |
329 private static ResourceBundle msgs = null; | |
330 | |
331 /** | |
332 * Returns the NLS'ed message for the given argument. This is only being | |
333 * called from SWT. | |
334 * | |
335 * @param key the key to look up | |
336 * @return the message for the given key | |
337 * | |
338 * @see SWT#getMessage(String) | |
339 */ | |
340 public static String getMessage(String key) { | |
341 String answer = key; | |
342 | |
343 if (key is null) { | |
344 SWT.error (SWT.ERROR_NULL_ARGUMENT); | |
345 } | |
346 if (msgs is null) { | |
347 try { | |
348 msgs = ResourceBundle.getBundle(SWTMessagesBundleData); //$NON-NLS-1$ | |
349 } catch (MissingResourceException ex) { | |
350 answer = key ~ " (no resource bundle)"; //$NON-NLS-1$ | |
351 } | |
352 } | |
353 if (msgs !is null) { | |
354 try { | |
355 answer = msgs.getString(key); | |
356 } catch (MissingResourceException ex2) {} | |
357 } | |
358 return answer; | |
359 } | |
360 | |
361 public static String getMessage(String key, Object[] args) { | |
362 String answer = key; | |
363 | |
364 if (key is null || args is null) { | |
365 SWT.error (SWT.ERROR_NULL_ARGUMENT); | |
366 } | |
367 if (msgs is null) { | |
368 try { | |
369 msgs = ResourceBundle.getBundle(SWTMessagesBundleData); //$NON-NLS-1$ | |
370 } catch (MissingResourceException ex) { | |
371 answer = key ~ " (no resource bundle)"; //$NON-NLS-1$ | |
372 } | |
373 } | |
374 if (msgs !is null) { | |
375 try { | |
376 char[] frmt = msgs.getString(key); | |
377 switch( args.length ){ | |
378 case 0: answer = Format(frmt); break; | |
379 case 1: answer = Format(frmt, args[0]); break; | |
380 case 2: answer = Format(frmt, args[0], args[1]); break; | |
381 case 3: answer = Format(frmt, args[0], args[1], args[2]); break; | |
382 case 4: answer = Format(frmt, args[0], args[1], args[2], args[3]); break; | |
383 case 5: answer = Format(frmt, args[0], args[1], args[2], args[3], args[4]); break; | |
384 default: | |
385 implMissing(__FILE__, __LINE__ ); | |
386 } | |
387 } catch (MissingResourceException ex2) {} | |
388 } | |
389 return answer; | |
390 } | |
391 | |
392 | |
393 /** | |
394 * Interrupt the current thread. | |
395 * <p> | |
396 * Note that this is not available on CLDC. | |
397 * </p> | |
398 */ | |
399 public static void interrupt() { | |
400 //PORTING_FIXME: how to implement?? | |
401 //Thread.currentThread().interrupt(); | |
402 } | |
403 | |
404 /** | |
405 * Compares two instances of class String ignoring the case of the | |
406 * characters and answers if they are equal. | |
407 * | |
408 * @param s1 string | |
409 * @param s2 string | |
410 * @return true if the two instances of class String are equal | |
411 */ | |
412 public static bool equalsIgnoreCase(String s1, String s2) { | |
413 String s1b = new char[ s1.length ]; | |
414 String s2b = new char[ s1.length ]; | |
415 scope(exit){ | |
416 delete s1b; | |
417 delete s2b; | |
418 } | |
419 String s1c = Unicode.toFold( s1, s1b ); | |
420 String s2c = Unicode.toFold( s2, s2b ); | |
421 return s1c == s2c; | |
422 } | |
423 | |
424 } |