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