1 11 package org.eclipse.jface.resource; 12 13 import java.util.ArrayList ; 14 import java.util.NoSuchElementException ; 15 import java.util.StringTokenizer ; 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 43 public class StringConverter { 44 45 48 private static final String REGULAR = "regular"; 50 53 private static final String BOLD = "bold"; 55 58 private static final String ITALIC = "italic"; 60 63 private static final String BOLD_ITALIC = "bold italic"; 65 69 private static final char SEPARATOR = '-'; 70 71 75 private static final String FONT_SEPARATOR = ";"; 77 80 private StringConverter() { 81 } 83 84 93 public static String [] asArray(String value) throws DataFormatException { 94 ArrayList list = new ArrayList (); 95 StringTokenizer stok = new StringTokenizer (value); 96 while (stok.hasMoreTokens()) { 97 list.add(stok.nextToken()); 98 } 99 String result[] = new String [list.size()]; 100 list.toArray(result); 101 return result; 102 } 103 104 115 public static String [] asArray(String value, String [] dflt) { 116 try { 117 return asArray(value); 118 } catch (DataFormatException e) { 119 return dflt; 120 } 121 } 122 123 140 public static boolean asBoolean(String value) throws DataFormatException { 141 String v = value.toLowerCase(); 142 if (v.equals("t") || v.equals("true")) { return true; 144 } 145 if (value.equals("f") || v.equals("false")) { return false; 147 } 148 throw new DataFormatException( 149 "Value " + value + "doesn't represent a boolean"); } 151 152 161 public static boolean asBoolean(String value, boolean dflt) { 162 try { 163 return asBoolean(value); 164 } catch (DataFormatException e) { 165 return dflt; 166 } 167 } 168 169 178 public static double asDouble(String value) throws DataFormatException { 179 try { 180 return (Double.valueOf(value)).doubleValue(); 181 } catch (NumberFormatException e) { 182 throw new DataFormatException(e.getMessage()); 183 } 184 } 185 186 195 public static double asDouble(String value, double dflt) { 196 try { 197 return asDouble(value); 198 } catch (DataFormatException e) { 199 return dflt; 200 } 201 } 202 203 212 public static float asFloat(String value) throws DataFormatException { 213 try { 214 return (Float.valueOf(value)).floatValue(); 215 } catch (NumberFormatException e) { 216 throw new DataFormatException(e.getMessage()); 217 } 218 } 219 220 229 public static float asFloat(String value, float dflt) { 230 try { 231 return asFloat(value); 232 } catch (DataFormatException e) { 233 return dflt; 234 } 235 } 236 237 256 public static FontData asFontData(String value) throws DataFormatException { 257 if (value == null) { 258 throw new DataFormatException( 259 "Null doesn't represent a valid font data"); } 261 String 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 + "\""); } 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 + "\""); } 278 String 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 + "\""); } 290 name = value.substring(0, faceIndex); 291 } catch (NoSuchElementException e) { 292 throw new DataFormatException(e.getMessage()); 293 } 294 return new FontData(name, height, style); 295 } 296 297 303 private static String [] getArrayFromList(String prop, String separator) { 304 if (prop == null || prop.trim().equals("")) { return new String [0]; 306 } 307 ArrayList list = new ArrayList (); 308 StringTokenizer tokens = new StringTokenizer (prop, separator); 309 while (tokens.hasMoreTokens()) { 310 String token = tokens.nextToken().trim(); 311 if (!token.equals("")) { list.add(token); 313 } 314 } 315 return list.isEmpty() ? new String [0] : (String []) list.toArray(new String [list.size()]); 316 } 317 318 325 public static FontData[] asFontDataArray(String value) { 326 String [] strings = getArrayFromList(value, FONT_SEPARATOR); 327 ArrayList data = new ArrayList (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 } 334 } 335 return (FontData[]) data.toArray(new FontData[data.size()]); 336 } 337 338 347 public static FontData asFontData(String value, FontData dflt) { 348 try { 349 return asFontData(value); 350 } catch (DataFormatException e) { 351 return dflt; 352 } 353 } 354 355 364 public static int asInt(String value) throws DataFormatException { 365 try { 366 return Integer.parseInt(value); 367 } catch (NumberFormatException e) { 368 throw new DataFormatException(e.getMessage()); 369 } 370 } 371 372 381 public static int asInt(String value, int dflt) { 382 try { 383 return asInt(value); 384 } catch (DataFormatException e) { 385 return dflt; 386 } 387 } 388 389 398 public static long asLong(String value) throws DataFormatException { 399 try { 400 return Long.parseLong(value); 401 } catch (NumberFormatException e) { 402 throw new DataFormatException(e.getMessage()); 403 } 404 } 405 406 415 public static long asLong(String value, long dflt) { 416 try { 417 return asLong(value); 418 } catch (DataFormatException e) { 419 return dflt; 420 } 421 } 422 423 438 public static Point asPoint(String value) throws DataFormatException { 439 if (value == null) { 440 throw new DataFormatException( 441 "Null doesn't represent a valid point"); } 443 StringTokenizer stok = new StringTokenizer (value, ","); String x = stok.nextToken(); 445 String y = stok.nextToken(); 446 int xval = 0, yval = 0; 447 try { 448 xval = Integer.parseInt(x); 449 yval = Integer.parseInt(y); 450 } catch (NumberFormatException e) { 451 throw new DataFormatException(e.getMessage()); 452 } 453 return new Point(xval, yval); 454 } 455 456 465 public static Point asPoint(String value, Point dflt) { 466 try { 467 return asPoint(value); 468 } catch (DataFormatException e) { 469 return dflt; 470 } 471 } 472 473 489 public static Rectangle asRectangle(String value) 490 throws DataFormatException { 491 if (value == null) { 492 throw new DataFormatException( 493 "Null doesn't represent a valid rectangle"); } 495 StringTokenizer stok = new StringTokenizer (value, ","); String x = stok.nextToken(); 497 String y = stok.nextToken(); 498 String width = stok.nextToken(); 499 String 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 e) { 507 throw new DataFormatException(e.getMessage()); 508 } 509 return new Rectangle(xval, yval, wval, hval); 510 } 511 512 521 public static Rectangle asRectangle(String value, Rectangle dflt) { 522 try { 523 return asRectangle(value); 524 } catch (DataFormatException e) { 525 return dflt; 526 } 527 } 528 529 545 public static RGB asRGB(String value) throws DataFormatException { 546 if (value == null) { 547 throw new DataFormatException("Null doesn't represent a valid RGB"); } 549 StringTokenizer stok = new StringTokenizer (value, ","); 551 try { 552 String red = stok.nextToken(); 553 String green = stok.nextToken(); 554 String 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 e) { 561 throw new DataFormatException(e.getMessage()); 562 } 563 return new RGB(rval, gval, bval); 564 } catch (NoSuchElementException e) { 565 throw new DataFormatException(e.getMessage()); 566 } 567 } 568 569 578 public static RGB asRGB(String value, RGB dflt) { 579 try { 580 return asRGB(value); 581 } catch (DataFormatException e) { 582 return dflt; 583 } 584 } 585 586 593 public static String asString(double value) { 594 return String.valueOf(value); 595 } 596 597 604 public static String asString(float value) { 605 return String.valueOf(value); 606 } 607 608 615 public static String asString(int value) { 616 return String.valueOf(value); 617 } 618 619 626 public static String asString(long value) { 627 return String.valueOf(value); 628 } 629 630 637 public static String asString(Boolean value) { 638 Assert.isNotNull(value); 639 return String.valueOf(value.booleanValue()); 640 } 641 642 649 public static String asString(Double value) { 650 Assert.isNotNull(value); 651 return String.valueOf(value.doubleValue()); 652 } 653 654 661 public static String asString(Float value) { 662 Assert.isNotNull(value); 663 return String.valueOf(value.floatValue()); 664 } 665 666 673 public static String asString(Integer value) { 674 Assert.isNotNull(value); 675 return String.valueOf(value.intValue()); 676 } 677 678 685 public static String asString(Long value) { 686 Assert.isNotNull(value); 687 return String.valueOf(value.longValue()); 688 } 689 690 698 public static String asString(FontData[] value) { 699 StringBuffer buffer = new StringBuffer (); 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 715 public static String asString(FontData value) { 716 Assert.isNotNull(value); 717 StringBuffer buffer = new StringBuffer (); 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 750 public static String asString(Point value) { 751 Assert.isNotNull(value); 752 StringBuffer buffer = new StringBuffer (); 753 buffer.append(value.x); 754 buffer.append(','); 755 buffer.append(value.y); 756 return buffer.toString(); 757 } 758 759 772 public static String asString(Rectangle value) { 773 Assert.isNotNull(value); 774 StringBuffer buffer = new StringBuffer (); 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 797 public static String asString(RGB value) { 798 Assert.isNotNull(value); 799 StringBuffer buffer = new StringBuffer (); 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 815 public static String asString(boolean value) { 816 return String.valueOf(value); 817 } 818 819 829 public static String removeWhiteSpaces(String s) { 830 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 result = new StringBuffer (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 863 public static String asDisplayableString(FontData value) { 864 Assert.isNotNull(value); 865 StringBuffer buffer = new StringBuffer (); 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")); } else if (bold) { 874 buffer.append(JFaceResources.getString("BoldFont")); } else if (italic) { 876 buffer.append(JFaceResources.getString("ItalicFont")); } else { 878 buffer.append(JFaceResources.getString("RegularFont")); } 880 buffer.append(SEPARATOR); 881 buffer.append(value.getHeight()); 882 return buffer.toString(); 883 884 } 885 } 886 | Popular Tags |