KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > resource > StringConverter


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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  *******************************************************************************/

11 package org.eclipse.jface.resource;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.NoSuchElementException JavaDoc;
15 import java.util.StringTokenizer JavaDoc;
16
17 import org.eclipse.core.runtime.Assert;
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.graphics.FontData;
20 import org.eclipse.swt.graphics.Point;
21 import org.eclipse.swt.graphics.RGB;
22 import org.eclipse.swt.graphics.Rectangle;
23
24 /**
25  * Helper class for converting various data types to and from
26  * strings. Supported types include:
27  * <ul>
28  * <li><code>boolean</code></li>
29  * <li><code>int</code></li>
30  * <li><code>long</code></li>
31  * <li><code>float</code></li>
32  * <li><code>double</code></li>
33  * <li><code>org.eclipse.swt.graphics.Point</code></li>
34  * <li><code>org.eclipse.swt.graphics.Rectangle</code></li>
35  * <li><code>org.eclipse.swt.graphics.RGB</code></li>
36  * <li><code>org.eclipse.swt.graphics.FontData</code></li>
37  * </ul>
38  * <p>
39  * All methods declared on this class are static. This
40  * class cannot be instantiated.
41  * </p>
42  */

43 public class StringConverter {
44
45     /**
46      * Internal font style constant for regular fonts.
47      */

48     private static final String JavaDoc REGULAR = "regular"; //$NON-NLS-1$
49

50     /**
51      * Internal font style constant for bold fonts.
52      */

53     private static final String JavaDoc BOLD = "bold"; //$NON-NLS-1$
54

55     /**
56      * Internal font style constant for italic fonts.
57      */

58     private static final String JavaDoc ITALIC = "italic"; //$NON-NLS-1$
59

60     /**
61      * Internal font style constant for bold italic fonts.
62      */

63     private static final String JavaDoc BOLD_ITALIC = "bold italic"; //$NON-NLS-1$
64

65     /**
66      * Internal constant for the separator character used in
67      * font specifications.
68      */

69     private static final char SEPARATOR = '-';
70
71     /**
72      * Internal constant for the seperator character used in font list
73      * specifications.
74      */

75     private static final String JavaDoc FONT_SEPARATOR = ";"; //$NON-NLS-1$
76

77     /* (non-Javadoc)
78      * Declare a private constructor to block instantiation.
79      */

80     private StringConverter() {
81         //no-op
82
}
83
84     /**
85      * Breaks out space-separated words into an array of words.
86      * For example: <code>"no comment"</code> into an array
87      * <code>a[0]="no"</code> and <code>a[1]= "comment"</code>.
88      *
89      * @param value the string to be converted
90      * @return the list of words
91      * @throws DataFormatException thrown if request string could not seperated
92      */

93     public static String JavaDoc[] asArray(String JavaDoc value) throws DataFormatException {
94         ArrayList JavaDoc list = new ArrayList JavaDoc();
95         StringTokenizer JavaDoc stok = new StringTokenizer JavaDoc(value);
96         while (stok.hasMoreTokens()) {
97             list.add(stok.nextToken());
98         }
99         String JavaDoc result[] = new String JavaDoc[list.size()];
100         list.toArray(result);
101         return result;
102     }
103
104     /**
105      /**
106      * Breaks out space-separated words into an array of words.
107      * For example: <code>"no comment"</code> into an array
108      * <code>a[0]="no"</code> and <code>a[1]= "comment"</code>.
109      * Returns the given default value if the value cannot be parsed.
110      *
111      * @param value the string to be converted
112      * @param dflt the default value
113      * @return the list of words, or the default value
114      */

115     public static String JavaDoc[] asArray(String JavaDoc value, String JavaDoc[] dflt) {
116         try {
117             return asArray(value);
118         } catch (DataFormatException e) {
119             return dflt;
120         }
121     }
122
123     /**
124      * Converts the given value into a boolean.
125      * This method fails if the value does not represent a boolean.
126      * <p>
127      * Valid representations of <code>true</code> include the strings
128      * "<code>t</code>", "<code>true</code>", or equivalent in mixed
129      * or upper case.
130      * Similarly, valid representations of <code>false</code> include the strings
131      * "<code>f</code>", "<code>false</code>", or equivalent in mixed
132      * or upper case.
133      * </p>
134      *
135      * @param value the value to be converted
136      * @return the value as a boolean
137      * @exception DataFormatException if the given value does not represent
138      * a boolean
139      */

140     public static boolean asBoolean(String JavaDoc value) throws DataFormatException {
141         String JavaDoc v = value.toLowerCase();
142         if (v.equals("t") || v.equals("true")) { //$NON-NLS-1$ //$NON-NLS-2$
143
return true;
144         }
145         if (value.equals("f") || v.equals("false")) { //$NON-NLS-1$ //$NON-NLS-2$
146
return false;
147         }
148         throw new DataFormatException(
149                 "Value " + value + "doesn't represent a boolean"); //$NON-NLS-2$//$NON-NLS-1$
150
}
151
152     /**
153      * Converts the given value into a boolean.
154      * Returns the given default value if the
155      * value does not represent a boolean.
156      *
157      * @param value the value to be converted
158      * @param dflt the default value
159      * @return the value as a boolean, or the default value
160      */

161     public static boolean asBoolean(String JavaDoc value, boolean dflt) {
162         try {
163             return asBoolean(value);
164         } catch (DataFormatException e) {
165             return dflt;
166         }
167     }
168
169     /**
170      * Converts the given value into a double.
171      * This method fails if the value does not represent a double.
172      *
173      * @param value the value to be converted
174      * @return the value as a double
175      * @exception DataFormatException if the given value does not represent
176      * a double
177      */

178     public static double asDouble(String JavaDoc value) throws DataFormatException {
179         try {
180             return (Double.valueOf(value)).doubleValue();
181         } catch (NumberFormatException JavaDoc e) {
182             throw new DataFormatException(e.getMessage());
183         }
184     }
185
186     /**
187      * Converts the given value into a double.
188      * Returns the given default value if the
189      * value does not represent a double.
190      *
191      * @param value the value to be converted
192      * @param dflt the default value
193      * @return the value as a double, or the default value
194      */

195     public static double asDouble(String JavaDoc value, double dflt) {
196         try {
197             return asDouble(value);
198         } catch (DataFormatException e) {
199             return dflt;
200         }
201     }
202
203     /**
204      * Converts the given value into a float.
205      * This method fails if the value does not represent a float.
206      *
207      * @param value the value to be converted
208      * @return the value as a float
209      * @exception DataFormatException if the given value does not represent
210      * a float
211      */

212     public static float asFloat(String JavaDoc value) throws DataFormatException {
213         try {
214             return (Float.valueOf(value)).floatValue();
215         } catch (NumberFormatException JavaDoc e) {
216             throw new DataFormatException(e.getMessage());
217         }
218     }
219
220     /**
221      * Converts the given value into a float.
222      * Returns the given default value if the
223      * value does not represent a float.
224      *
225      * @param value the value to be converted
226      * @param dflt the default value
227      * @return the value as a float, or the default value
228      */

229     public static float asFloat(String JavaDoc value, float dflt) {
230         try {
231             return asFloat(value);
232         } catch (DataFormatException e) {
233             return dflt;
234         }
235     }
236
237     /**
238      * Converts the given value into an SWT font data object.
239      * This method fails if the value does not represent font data.
240      * <p>
241      * A valid font data representation is a string of the form
242      * <code><it>fontname</it>-<it>style</it>-<it>height</it></code> where
243      * <code><it>fontname</it></code> is the name of a font,
244      * <code><it>style</it></code> is a font style (one of
245      * <code>"regular"</code>, <code>"bold"</code>,
246      * <code>"italic"</code>, or <code>"bold italic"</code>)
247      * and <code><it>height</it></code> is an integer representing the
248      * font height. Example: <code>Times New Roman-bold-36</code>.
249      * </p>
250      *
251      * @param value the value to be converted
252      * @return the value as font data
253      * @exception DataFormatException if the given value does not represent
254      * font data
255      */

256     public static FontData asFontData(String JavaDoc value) throws DataFormatException {
257         if (value == null) {
258             throw new DataFormatException(
259                     "Null doesn't represent a valid font data"); //$NON-NLS-1$
260
}
261         String JavaDoc name = null;
262         int height = 0;
263         int style = 0;
264         try {
265             int length = value.length();
266             int heightIndex = value.lastIndexOf(SEPARATOR);
267             if (heightIndex == -1) {
268                 throw new DataFormatException(
269                         "No correct font data format \"" + value + "\""); //$NON-NLS-2$//$NON-NLS-1$
270
}
271             height = StringConverter.asInt(value.substring(heightIndex + 1,
272                     length));
273             int faceIndex = value.lastIndexOf(SEPARATOR, heightIndex - 1);
274             if (faceIndex == -1) {
275                 throw new DataFormatException(
276                         "No correct font data format \"" + value + "\""); //$NON-NLS-2$//$NON-NLS-1$
277
}
278             String JavaDoc s = value.substring(faceIndex + 1, heightIndex);
279             if (BOLD_ITALIC.equals(s)) {
280                 style = SWT.BOLD | SWT.ITALIC;
281             } else if (BOLD.equals(s)) {
282                 style = SWT.BOLD;
283             } else if (ITALIC.equals(s)) {
284                 style = SWT.ITALIC;
285             } else if (REGULAR.equals(s)) {
286                 style = SWT.NORMAL;
287             } else {
288                 throw new DataFormatException("Unknown face name \"" + s + "\""); //$NON-NLS-2$//$NON-NLS-1$
289
}
290             name = value.substring(0, faceIndex);
291         } catch (NoSuchElementException JavaDoc e) {
292             throw new DataFormatException(e.getMessage());
293         }
294         return new FontData(name, height, style);
295     }
296
297     /**
298      * Returns the result of converting a list of comma-separated tokens into an array
299      *
300      * @return the array of string tokens
301      * @param prop the initial comma-separated string
302      */

303     private static String JavaDoc[] getArrayFromList(String JavaDoc prop, String JavaDoc separator) {
304         if (prop == null || prop.trim().equals("")) { //$NON-NLS-1$
305
return new String JavaDoc[0];
306         }
307         ArrayList JavaDoc list = new ArrayList JavaDoc();
308         StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc(prop, separator);
309         while (tokens.hasMoreTokens()) {
310             String JavaDoc token = tokens.nextToken().trim();
311             if (!token.equals("")) { //$NON-NLS-1$
312
list.add(token);
313             }
314         }
315         return list.isEmpty() ? new String JavaDoc[0] : (String JavaDoc[]) list.toArray(new String JavaDoc[list.size()]);
316     }
317
318     /**
319      * Convert the given value into an array of SWT font data objects.
320      *
321      * @param value the font list string
322      * @return the value as a font list
323      * @since 3.0
324      */

325     public static FontData[] asFontDataArray(String JavaDoc value) {
326         String JavaDoc[] strings = getArrayFromList(value, FONT_SEPARATOR);
327         ArrayList JavaDoc data = new ArrayList JavaDoc(strings.length);
328         for (int i = 0; i < strings.length; i++) {
329             try {
330                 data.add(StringConverter.asFontData(strings[i]));
331             } catch (DataFormatException e) {
332                 //do-nothing
333
}
334         }
335         return (FontData[]) data.toArray(new FontData[data.size()]);
336     }
337
338     /**
339      * Converts the given value into an SWT font data object.
340      * Returns the given default value if the
341      * value does not represent a font data object.
342      *
343      * @param value the value to be converted
344      * @param dflt the default value
345      * @return the value as a font data object, or the default value
346      */

347     public static FontData asFontData(String JavaDoc value, FontData dflt) {
348         try {
349             return asFontData(value);
350         } catch (DataFormatException e) {
351             return dflt;
352         }
353     }
354
355     /**
356      * Converts the given value into an int.
357      * This method fails if the value does not represent an int.
358      *
359      * @param value the value to be converted
360      * @return the value as an int
361      * @exception DataFormatException if the given value does not represent
362      * an int
363      */

364     public static int asInt(String JavaDoc value) throws DataFormatException {
365         try {
366             return Integer.parseInt(value);
367         } catch (NumberFormatException JavaDoc e) {
368             throw new DataFormatException(e.getMessage());
369         }
370     }
371
372     /**
373      * Converts the given value into an int.
374      * Returns the given default value if the
375      * value does not represent an int.
376      *
377      * @param value the value to be converted
378      * @param dflt the default value
379      * @return the value as an int, or the default value
380      */

381     public static int asInt(String JavaDoc value, int dflt) {
382         try {
383             return asInt(value);
384         } catch (DataFormatException e) {
385             return dflt;
386         }
387     }
388
389     /**
390      * Converts the given value into a long.
391      * This method fails if the value does not represent a long.
392      *
393      * @param value the value to be converted
394      * @return the value as a long
395      * @exception DataFormatException if the given value does not represent
396      * a long
397      */

398     public static long asLong(String JavaDoc value) throws DataFormatException {
399         try {
400             return Long.parseLong(value);
401         } catch (NumberFormatException JavaDoc e) {
402             throw new DataFormatException(e.getMessage());
403         }
404     }
405
406     /**
407      * Converts the given value into a long.
408      * Returns the given default value if the
409      * value does not represent a long.
410      *
411      * @param value the value to be converted
412      * @param dflt the default value
413      * @return the value as a long, or the default value
414      */

415     public static long asLong(String JavaDoc value, long dflt) {
416         try {
417             return asLong(value);
418         } catch (DataFormatException e) {
419             return dflt;
420         }
421     }
422
423     /**
424      * Converts the given value into an SWT point.
425      * This method fails if the value does not represent a point.
426      * <p>
427      * A valid point representation is a string of the form
428      * <code><it>x</it>,<it>y</it></code> where
429      * <code><it>x</it></code> and <code><it>y</it></code>
430      * are valid ints.
431      * </p>
432      *
433      * @param value the value to be converted
434      * @return the value as a point
435      * @exception DataFormatException if the given value does not represent
436      * a point
437      */

438     public static Point asPoint(String JavaDoc value) throws DataFormatException {
439         if (value == null) {
440             throw new DataFormatException(
441                     "Null doesn't represent a valid point"); //$NON-NLS-1$
442
}
443         StringTokenizer JavaDoc stok = new StringTokenizer JavaDoc(value, ","); //$NON-NLS-1$
444
String JavaDoc x = stok.nextToken();
445         String JavaDoc y = stok.nextToken();
446         int xval = 0, yval = 0;
447         try {
448             xval = Integer.parseInt(x);
449             yval = Integer.parseInt(y);
450         } catch (NumberFormatException JavaDoc e) {
451             throw new DataFormatException(e.getMessage());
452         }
453         return new Point(xval, yval);
454     }
455
456     /**
457      * Converts the given value into an SWT point.
458      * Returns the given default value if the
459      * value does not represent a point.
460      *
461      * @param value the value to be converted
462      * @param dflt the default value
463      * @return the value as a point, or the default value
464      */

465     public static Point asPoint(String JavaDoc value, Point dflt) {
466         try {
467             return asPoint(value);
468         } catch (DataFormatException e) {
469             return dflt;
470         }
471     }
472
473     /**
474      * Converts the given value into an SWT rectangle.
475      * This method fails if the value does not represent a rectangle.
476      * <p>
477      * A valid rectangle representation is a string of the form
478      * <code><it>x</it>,<it>y</it>,<it>width</it>,<it>height</it></code>
479      * where <code><it>x</it></code>, <code><it>y</it></code>,
480      * <code><it>width</it></code>, and <code><it>height</it></code>
481      * are valid ints.
482      * </p>
483      *
484      * @param value the value to be converted
485      * @return the value as a rectangle
486      * @exception DataFormatException if the given value does not represent
487      * a rectangle
488      */

489     public static Rectangle asRectangle(String JavaDoc value)
490             throws DataFormatException {
491         if (value == null) {
492             throw new DataFormatException(
493                     "Null doesn't represent a valid rectangle"); //$NON-NLS-1$
494
}
495         StringTokenizer JavaDoc stok = new StringTokenizer JavaDoc(value, ","); //$NON-NLS-1$
496
String JavaDoc x = stok.nextToken();
497         String JavaDoc y = stok.nextToken();
498         String JavaDoc width = stok.nextToken();
499         String JavaDoc height = stok.nextToken();
500         int xval = 0, yval = 0, wval = 0, hval = 0;
501         try {
502             xval = Integer.parseInt(x);
503             yval = Integer.parseInt(y);
504             wval = Integer.parseInt(width);
505             hval = Integer.parseInt(height);
506         } catch (NumberFormatException JavaDoc e) {
507             throw new DataFormatException(e.getMessage());
508         }
509         return new Rectangle(xval, yval, wval, hval);
510     }
511
512     /**
513      * Converts the given value into an SWT rectangle.
514      * Returns the given default value if the
515      * value does not represent a rectangle.
516      *
517      * @param value the value to be converted
518      * @param dflt the default value
519      * @return the value as a rectangle, or the default value
520      */

521     public static Rectangle asRectangle(String JavaDoc value, Rectangle dflt) {
522         try {
523             return asRectangle(value);
524         } catch (DataFormatException e) {
525             return dflt;
526         }
527     }
528
529     /**
530      * Converts the given value into an SWT RGB color value.
531      * This method fails if the value does not represent an RGB
532      * color value.
533      * <p>
534      * A valid RGB color value representation is a string of the form
535      * <code><it>red</it>,<it>green</it></code>,<it>blue</it></code> where
536      * <code><it>red</it></code>, <it>green</it></code>, and
537      * <code><it>blue</it></code> are valid ints.
538      * </p>
539      *
540      * @param value the value to be converted
541      * @return the value as an RGB color value
542      * @exception DataFormatException if the given value does not represent
543      * an RGB color value
544      */

545     public static RGB asRGB(String JavaDoc value) throws DataFormatException {
546         if (value == null) {
547             throw new DataFormatException("Null doesn't represent a valid RGB"); //$NON-NLS-1$
548
}
549         StringTokenizer JavaDoc stok = new StringTokenizer JavaDoc(value, ","); //$NON-NLS-1$
550

551         try {
552             String JavaDoc red = stok.nextToken();
553             String JavaDoc green = stok.nextToken();
554             String JavaDoc blue = stok.nextToken();
555             int rval = 0, gval = 0, bval = 0;
556             try {
557                 rval = Integer.parseInt(red);
558                 gval = Integer.parseInt(green);
559                 bval = Integer.parseInt(blue);
560             } catch (NumberFormatException JavaDoc e) {
561                 throw new DataFormatException(e.getMessage());
562             }
563             return new RGB(rval, gval, bval);
564         } catch (NoSuchElementException JavaDoc e) {
565             throw new DataFormatException(e.getMessage());
566         }
567     }
568
569     /**
570      * Converts the given value into an SWT RGB color value.
571      * Returns the given default value if the
572      * value does not represent an RGB color value.
573      *
574      * @param value the value to be converted
575      * @param dflt the default value
576      * @return the value as a RGB color value, or the default value
577      */

578     public static RGB asRGB(String JavaDoc value, RGB dflt) {
579         try {
580             return asRGB(value);
581         } catch (DataFormatException e) {
582             return dflt;
583         }
584     }
585
586     /**
587      * Converts the given double value to a string.
588      * Equivalent to <code>String.valueOf(value)</code>.
589      *
590      * @param value the double value
591      * @return the string representing the given double
592      */

593     public static String JavaDoc asString(double value) {
594         return String.valueOf(value);
595     }
596
597     /**
598      * Converts the given float value to a string.
599      * Equivalent to <code>String.valueOf(value)</code>.
600      *
601      * @param value the float value
602      * @return the string representing the given float
603      */

604     public static String JavaDoc asString(float value) {
605         return String.valueOf(value);
606     }
607
608     /**
609      * Converts the given int value to a string.
610      * Equivalent to <code>String.valueOf(value)</code>.
611      *
612      * @param value the int value
613      * @return the string representing the given int
614      */

615     public static String JavaDoc asString(int value) {
616         return String.valueOf(value);
617     }
618
619     /**
620      * Converts the given long value to a string.
621      * Equivalent to <code>String.valueOf(value)</code>.
622      *
623      * @param value the long value
624      * @return the string representing the given long
625      */

626     public static String JavaDoc asString(long value) {
627         return String.valueOf(value);
628     }
629
630     /**
631      * Converts the given boolean object to a string.
632      * Equivalent to <code>String.valueOf(value.booleanValue())</code>.
633      *
634      * @param value the boolean object
635      * @return the string representing the given boolean value
636      */

637     public static String JavaDoc asString(Boolean JavaDoc value) {
638         Assert.isNotNull(value);
639         return String.valueOf(value.booleanValue());
640     }
641
642     /**
643      * Converts the given double object to a string.
644      * Equivalent to <code>String.valueOf(value.doubleValue())</code>.
645      *
646      * @param value the double object
647      * @return the string representing the given double value
648      */

649     public static String JavaDoc asString(Double JavaDoc value) {
650         Assert.isNotNull(value);
651         return String.valueOf(value.doubleValue());
652     }
653
654     /**
655      * Converts the given float object to a string.
656      * Equivalent to <code>String.valueOf(value.floatValue())</code>.
657      *
658      * @param value the float object
659      * @return the string representing the given float value
660      */

661     public static String JavaDoc asString(Float JavaDoc value) {
662         Assert.isNotNull(value);
663         return String.valueOf(value.floatValue());
664     }
665
666     /**
667      * Converts the given integer object to a string.
668      * Equivalent to <code>String.valueOf(value.intValue())</code>.
669      *
670      * @param value the integer object
671      * @return the string representing the given integer value
672      */

673     public static String JavaDoc asString(Integer JavaDoc value) {
674         Assert.isNotNull(value);
675         return String.valueOf(value.intValue());
676     }
677
678     /**
679      * Converts the given long object to a string.
680      * Equivalent to <code>String.valueOf(value.longValue())</code>.
681      *
682      * @param value the long object
683      * @return the string representing the given long value
684      */

685     public static String JavaDoc asString(Long JavaDoc value) {
686         Assert.isNotNull(value);
687         return String.valueOf(value.longValue());
688     }
689
690     /**
691      * Converts a font data array to a string. The string representation is
692      * that of asString(FontData) seperated by ';'
693      *
694      * @param value The font data.
695      * @return The string representation of the font data arra.
696      * @since 3.0
697      */

698     public static String JavaDoc asString(FontData[] value) {
699         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
700         for (int i = 0; i < value.length; i++) {
701             buffer.append(asString(value[i]));
702             if (i != value.length - 1) {
703                 buffer.append(FONT_SEPARATOR);
704             }
705         }
706         return buffer.toString();
707     }
708
709     /**
710      * Converts a font data object to a string. The string representation is
711      * "font name-style-height" (for example "Times New Roman-bold-36").
712      * @param value The font data.
713      * @return The string representation of the font data object.
714      */

715     public static String JavaDoc asString(FontData value) {
716         Assert.isNotNull(value);
717         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
718         buffer.append(value.getName());
719         buffer.append(SEPARATOR);
720         int style = value.getStyle();
721         boolean bold = (style & SWT.BOLD) == SWT.BOLD;
722         boolean italic = (style & SWT.ITALIC) == SWT.ITALIC;
723         if (bold && italic) {
724             buffer.append(BOLD_ITALIC);
725         } else if (bold) {
726             buffer.append(BOLD);
727         } else if (italic) {
728             buffer.append(ITALIC);
729         } else {
730             buffer.append(REGULAR);
731         }
732
733         buffer.append(SEPARATOR);
734         buffer.append(value.getHeight());
735         return buffer.toString();
736     }
737
738     /**
739      * Converts the given SWT point object to a string.
740      * <p>
741      * The string representation of a point has the form
742      * <code><it>x</it>,<it>y</it></code> where
743      * <code><it>x</it></code> and <code><it>y</it></code>
744      * are string representations of integers.
745      * </p>
746      *
747      * @param value the point object
748      * @return the string representing the given point
749      */

750     public static String JavaDoc asString(Point value) {
751         Assert.isNotNull(value);
752         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
753         buffer.append(value.x);
754         buffer.append(',');
755         buffer.append(value.y);
756         return buffer.toString();
757     }
758
759     /**
760      * Converts the given SWT rectangle object to a string.
761      * <p>
762      * The string representation of a rectangle has the form
763      * <code><it>x</it>,<it>y</it>,<it>width</it>,<it>height</it></code>
764      * where <code><it>x</it></code>, <code><it>y</it></code>,
765      * <code><it>width</it></code>, and <code><it>height</it></code>
766      * are string representations of integers.
767      * </p>
768      *
769      * @param value the rectangle object
770      * @return the string representing the given rectangle
771      */

772     public static String JavaDoc asString(Rectangle value) {
773         Assert.isNotNull(value);
774         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
775         buffer.append(value.x);
776         buffer.append(',');
777         buffer.append(value.y);
778         buffer.append(',');
779         buffer.append(value.width);
780         buffer.append(',');
781         buffer.append(value.height);
782         return buffer.toString();
783     }
784
785     /**
786      * Converts the given SWT RGB color value object to a string.
787      * <p>
788      * The string representation of an RGB color value has the form
789      * <code><it>red</it>,<it>green</it></code>,<it>blue</it></code> where
790      * <code><it>red</it></code>, <it>green</it></code>, and
791      * <code><it>blue</it></code> are string representations of integers.
792      * </p>
793      *
794      * @param value the RGB color value object
795      * @return the string representing the given RGB color value
796      */

797     public static String JavaDoc asString(RGB value) {
798         Assert.isNotNull(value);
799         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
800         buffer.append(value.red);
801         buffer.append(',');
802         buffer.append(value.green);
803         buffer.append(',');
804         buffer.append(value.blue);
805         return buffer.toString();
806     }
807
808     /**
809      * Converts the given boolean value to a string.
810      * Equivalent to <code>String.valueOf(value)</code>.
811      *
812      * @param value the boolean value
813      * @return the string representing the given boolean
814      */

815     public static String JavaDoc asString(boolean value) {
816         return String.valueOf(value);
817     }
818
819     /**
820      * Returns the given string with all whitespace characters removed.
821      * <p>
822      * All characters that have codes less than or equal to <code>'&#92;u0020'</code>
823      * (the space character) are considered to be a white space.
824      * </p>
825      *
826      * @param s the source string
827      * @return the string with all whitespace characters removed
828      */

829     public static String JavaDoc removeWhiteSpaces(String JavaDoc s) {
830         //check for no whitespace (common case)
831
boolean found = false;
832         int wsIndex = -1;
833         int size = s.length();
834         for (int i = 0; i < size; i++) {
835             found = Character.isWhitespace(s.charAt(i));
836             if (found) {
837                 wsIndex = i;
838                 break;
839             }
840         }
841         if (!found) {
842             return s;
843         }
844
845         StringBuffer JavaDoc result = new StringBuffer JavaDoc(s.substring(0, wsIndex));
846         for (int i = wsIndex + 1; i < size; i++) {
847             char ch = s.charAt(i);
848             if (!Character.isWhitespace(ch)) {
849                 result.append(ch);
850             }
851         }
852         return result.toString();
853     }
854
855     /**
856      * Converts a font data object to a string representation for display.
857      * The string representation is
858      * "font name-style-height" (for example "Times New Roman-bold-36").
859      * @param value The font data.
860      * @return The string representation of the font data object.
861      * @deprecated use asString(FontData)
862      */

863     public static String JavaDoc asDisplayableString(FontData value) {
864         Assert.isNotNull(value);
865         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
866         buffer.append(value.getName());
867         buffer.append(SEPARATOR);
868         int style = value.getStyle();
869         boolean bold = (style & SWT.BOLD) == SWT.BOLD;
870         boolean italic = (style & SWT.ITALIC) == SWT.ITALIC;
871         if (bold && italic) {
872             buffer.append(JFaceResources.getString("BoldItalicFont")); //$NON-NLS-1$
873
} else if (bold) {
874             buffer.append(JFaceResources.getString("BoldFont")); //$NON-NLS-1$
875
} else if (italic) {
876             buffer.append(JFaceResources.getString("ItalicFont")); //$NON-NLS-1$
877
} else {
878             buffer.append(JFaceResources.getString("RegularFont")); //$NON-NLS-1$
879
}
880         buffer.append(SEPARATOR);
881         buffer.append(value.getHeight());
882         return buffer.toString();
883
884     }
885 }
886
Popular Tags