KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sshtools > ui > GlobalUIUtil


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20
21 package com.sshtools.ui;
22
23 import java.awt.Color JavaDoc;
24 import java.awt.Font JavaDoc;
25 import java.util.StringTokenizer JavaDoc;
26
27 /**
28  * General UI utitlies that may be used for both Swing and AWT.
29  */

30 public class GlobalUIUtil {
31
32   
33   /**
34    * Create a new Font given an existing font and a different font name.
35    * The base fonts size and style will be used. This
36    * is provided because 1.1 doesnt have a Font.deriveFont() method.
37    *
38    * @param font font to base new font on
39    * @param name new font name
40    * @return derived font
41    */

42   public static Font JavaDoc deriveFont(Font JavaDoc font, String JavaDoc name) {
43     return new Font JavaDoc(name, font.getStyle(), font.getSize());
44   }
45   
46   /**
47    * Create a new Font given an existing font and a different font style.
48    * The base fonts name and size will be used. This
49    * is provided because 1.1 doesnt have a Font.deriveFont() method.
50    *
51    * @param font font to base new font on
52    * @param style new font style
53    * @return derived font
54    */

55   public static Font JavaDoc deriveFont(Font JavaDoc font, int style) {
56     return new Font JavaDoc(font.getName(), style, font.getSize());
57   }
58   
59   /**
60    * Create a new Font given an existing font and a different font size.
61    * The base fonts name and style will be used. This
62    * is provided because 1.1 doesnt have a Font.deriveFont() method.
63    *
64    * @param font font to base new font on
65    * @param size new font size
66    * @return derived font
67    */

68   public static Font JavaDoc deriveFont(Font JavaDoc font, float size) {
69     return new Font JavaDoc(font.getName(), font.getStyle(), (int)size);
70   }
71   /**
72    *
73    *
74    * @param number
75    * @param defaultValue
76    *
77    * @return
78    */

79   public static int stringToInt(String JavaDoc number, int defaultValue) {
80     try {
81       return (number == null) ? defaultValue : Integer.parseInt(number);
82     }
83     catch (NumberFormatException JavaDoc nfe) {
84       return defaultValue;
85     }
86   }
87
88   /**
89    *
90    *
91    * @param color
92    *
93    * @return
94    */

95   public static String JavaDoc colorToString(Color JavaDoc color) {
96     StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
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   /**
106    *
107    *
108    * @param font
109    *
110    * @return
111    */

112   public static String JavaDoc fontToString(Font JavaDoc font) {
113     StringBuffer JavaDoc b = new StringBuffer JavaDoc(font.getName());
114     b.append(","); //$NON-NLS-1$
115
b.append(font.getStyle());
116     b.append(","); //$NON-NLS-1$
117
b.append(font.getSize());
118
119     return b.toString();
120   }
121
122   /**
123    *
124    *
125    * @param fontString
126    *
127    * @return
128    */

129   public static Font JavaDoc stringToFont(String JavaDoc fontString) {
130     StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(fontString, ","); //$NON-NLS-1$
131

132     try {
133       return new Font JavaDoc(st.nextToken(), Integer.parseInt(st.nextToken()),
134                       Integer.parseInt(st.nextToken()));
135     }
136     catch (Exception JavaDoc e) {
137       return null;
138     }
139   }
140
141   /**
142    *
143    *
144    * @param s
145    *
146    * @return
147    *
148    * @throws IllegalArgumentException
149    */

150   public static Color JavaDoc stringToColor(String JavaDoc s) {
151     try {
152       return new Color JavaDoc(Integer.decode("0x" + s.substring(1, 3)).intValue(), //$NON-NLS-1$
153
Integer.decode("0x" + s.substring(3, 5)).intValue(), //$NON-NLS-1$
154
Integer.decode("0x" + s.substring(5, 7)).intValue()); //$NON-NLS-1$
155
}
156     catch (Exception JavaDoc e) {
157       throw new IllegalArgumentException JavaDoc(
158           Messages.getString("GlobalUIUtil.badColorStringFormat")); //$NON-NLS-1$
159
}
160   }
161
162   /**
163    *
164    *
165    * @param s
166    *
167    * @return
168    *
169    * @throws IllegalArgumentException
170    */

171   public static Color JavaDoc stringToColor(String JavaDoc s, Color JavaDoc defaultColor) {
172       try {
173           return new Color JavaDoc(Integer.decode("0x" + s.substring(1, 3)).intValue(), //$NON-NLS-1$
174
Integer.decode("0x" + s.substring(3, 5)).intValue(), //$NON-NLS-1$
175
Integer.decode("0x" + s.substring(5, 7)).intValue()); //$NON-NLS-1$
176
}
177       catch (Throwable JavaDoc t) {
178           return defaultColor;
179       }
180   }
181
182   /**
183    *
184    *
185    * @param number
186    * @param size
187    *
188    * @return
189    *
190    * @throws IllegalArgumentException
191    */

192   public static String JavaDoc numberToPaddedHexString(int number, int size) {
193     String JavaDoc s = Integer.toHexString(number);
194
195     if (s.length() > size) {
196       throw new IllegalArgumentException JavaDoc(
197           Messages.getString("GlobalUIUtil.numberTooBigForPaddedHexString")); //$NON-NLS-1$
198
}
199
200     StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
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