1 19 20 21 package com.sshtools.ui; 22 23 import java.awt.Color ; 24 import java.awt.Font ; 25 import java.util.StringTokenizer ; 26 27 30 public class GlobalUIUtil { 31 32 33 42 public static Font deriveFont(Font font, String name) { 43 return new Font (name, font.getStyle(), font.getSize()); 44 } 45 46 55 public static Font deriveFont(Font font, int style) { 56 return new Font (font.getName(), style, font.getSize()); 57 } 58 59 68 public static Font deriveFont(Font font, float size) { 69 return new Font (font.getName(), font.getStyle(), (int)size); 70 } 71 79 public static int stringToInt(String number, int defaultValue) { 80 try { 81 return (number == null) ? defaultValue : Integer.parseInt(number); 82 } 83 catch (NumberFormatException nfe) { 84 return defaultValue; 85 } 86 } 87 88 95 public static String colorToString(Color color) { 96 StringBuffer buf = new StringBuffer (); 97 buf.append('#'); 98 buf.append(numberToPaddedHexString(color.getRed(), 2)); 99 buf.append(numberToPaddedHexString(color.getGreen(), 2)); 100 buf.append(numberToPaddedHexString(color.getBlue(), 2)); 101 102 return buf.toString(); 103 } 104 105 112 public static String fontToString(Font font) { 113 StringBuffer b = new StringBuffer (font.getName()); 114 b.append(","); b.append(font.getStyle()); 116 b.append(","); b.append(font.getSize()); 118 119 return b.toString(); 120 } 121 122 129 public static Font stringToFont(String fontString) { 130 StringTokenizer st = new StringTokenizer (fontString, ","); 132 try { 133 return new Font (st.nextToken(), Integer.parseInt(st.nextToken()), 134 Integer.parseInt(st.nextToken())); 135 } 136 catch (Exception e) { 137 return null; 138 } 139 } 140 141 150 public static Color stringToColor(String s) { 151 try { 152 return new Color (Integer.decode("0x" + s.substring(1, 3)).intValue(), Integer.decode("0x" + s.substring(3, 5)).intValue(), Integer.decode("0x" + s.substring(5, 7)).intValue()); } 156 catch (Exception e) { 157 throw new IllegalArgumentException ( 158 Messages.getString("GlobalUIUtil.badColorStringFormat")); } 160 } 161 162 171 public static Color stringToColor(String s, Color defaultColor) { 172 try { 173 return new Color (Integer.decode("0x" + s.substring(1, 3)).intValue(), Integer.decode("0x" + s.substring(3, 5)).intValue(), Integer.decode("0x" + s.substring(5, 7)).intValue()); } 177 catch (Throwable t) { 178 return defaultColor; 179 } 180 } 181 182 192 public static String numberToPaddedHexString(int number, int size) { 193 String s = Integer.toHexString(number); 194 195 if (s.length() > size) { 196 throw new IllegalArgumentException ( 197 Messages.getString("GlobalUIUtil.numberTooBigForPaddedHexString")); } 199 200 StringBuffer buf = new StringBuffer (); 201 202 for (int i = 0; i < (size - s.length()); i++) { 203 buf.append('0'); 204 } 205 206 buf.append(s); 207 208 return buf.toString(); 209 } 210 } 211 | Popular Tags |