1 19 20 package com.sshtools.ui.swing; 21 22 import java.awt.Component ; 23 import java.awt.Dimension ; 24 import java.awt.Font ; 25 import java.awt.GraphicsConfiguration ; 26 import java.awt.GraphicsDevice ; 27 import java.awt.GridBagConstraints ; 28 import java.awt.GridBagLayout ; 29 import java.awt.Image ; 30 import java.awt.Rectangle ; 31 import java.awt.Toolkit ; 32 import java.awt.event.KeyEvent ; 33 import java.awt.image.BufferedImage ; 34 import java.awt.image.ImageObserver ; 35 import java.io.PrintWriter ; 36 import java.io.StringWriter ; 37 import java.util.StringTokenizer ; 38 39 import javax.swing.Icon ; 40 import javax.swing.ImageIcon ; 41 import javax.swing.JComboBox ; 42 import javax.swing.JComponent ; 43 import javax.swing.JList ; 44 import javax.swing.JOptionPane ; 45 import javax.swing.KeyStroke ; 46 import javax.swing.SwingConstants ; 47 import javax.swing.UIManager ; 48 import javax.swing.text.SimpleAttributeSet ; 49 import javax.swing.text.StyleConstants ; 50 51 56 public class UIUtil implements SwingConstants { 57 58 64 public static void selectStringInList(String string, JList list) { 65 for (int i = 0; i < list.getModel().getSize(); i++) { 66 if (String.valueOf(list.getModel().getElementAt(i)).equals(string)) { 67 list.setSelectedIndex(i); 68 return; 69 } 70 } 71 } 72 73 79 public static void selectStringInList(String string, JComboBox list) { 80 for (int i = 0; i < list.getModel().getSize(); i++) { 81 if (String.valueOf(list.getModel().getElementAt(i)).equals(string)) { 82 list.setSelectedIndex(i); 83 return; 84 } 85 } 86 } 87 88 99 public static Rectangle stringToRectangle(String string, Rectangle defaultValue) { 100 if (string == null) { 101 return defaultValue; 102 } 103 StringTokenizer t = new StringTokenizer (string, ","); try { 105 return new Rectangle (Integer.parseInt(t.nextToken()), Integer.parseInt(t.nextToken()), Integer.parseInt(t.nextToken()), 106 Integer.parseInt(t.nextToken())); 107 } catch (Exception e) { 108 return defaultValue; 109 } 110 } 111 112 120 public static String rectangleToString(Rectangle rectangle) { 121 StringBuffer buf = new StringBuffer (String.valueOf(rectangle.x)); 122 buf.append(','); 123 buf.append(String.valueOf(rectangle.y)); 124 buf.append(','); 125 buf.append(String.valueOf(rectangle.width)); 126 buf.append(','); 127 buf.append(String.valueOf(rectangle.height)); 128 return buf.toString(); 129 } 130 131 138 public static Integer parseMnemonicString(String string) { 139 try { 140 return new Integer (string); 141 } catch (Throwable t) { 142 return new Integer (-1); 143 } 144 } 145 146 153 public static KeyStroke parseAcceleratorString(String string) { 154 if (string == null || string.equals("")) { return null; 156 } 157 StringTokenizer t = new StringTokenizer (string, "+"); int mod = 0; 159 int key = -1; 160 while (t.hasMoreTokens()) { 161 String x = t.nextToken(); 162 if (x.equalsIgnoreCase("ctrl")) { mod += KeyEvent.CTRL_MASK; 164 } else if (x.equalsIgnoreCase("shift")) { mod += KeyEvent.SHIFT_MASK; 166 } else if (x.equalsIgnoreCase("alt")) { mod += KeyEvent.ALT_MASK; 168 } else { 169 try { 170 java.lang.reflect.Field f = KeyEvent .class.getField(x); 171 key = f.getInt(null); 172 } catch (Throwable ex) { 173 ex.printStackTrace(); 174 } 175 } 176 } 177 if (key != -1) { 178 KeyStroke ks = KeyStroke.getKeyStroke(key, mod); 179 return ks; 180 } 181 return null; 182 183 } 184 185 197 public static void jGridBagAdd(JComponent parent, Component componentToAdd, GridBagConstraints constraints, int pos) { 198 if (!(parent.getLayout() instanceof GridBagLayout )) { 199 throw new IllegalArgumentException (Messages.getString("UIUtil.parentMustHaveAGridBagLayout")); } 201 202 GridBagLayout layout = (GridBagLayout ) parent.getLayout(); 204 205 constraints.gridwidth = pos; 207 layout.setConstraints(componentToAdd, constraints); 208 parent.add(componentToAdd); 209 } 210 211 218 public static void positionComponent(int p, Component c) { 219 220 positionComponent(p, c, c); 221 222 } 223 224 public static void positionComponent(int p, Component c, Component o) { 225 Rectangle d = null; 226 230 try { 231 232 236 237 GraphicsConfiguration config = o.getGraphicsConfiguration(); 239 GraphicsDevice dev = config.getDevice(); 240 d = config.getBounds(); 241 242 } catch (Throwable t) { 244 } 245 positionComponent(p, c, d); 246 247 } 248 249 public static void positionComponent(int p, Component c, Rectangle d) { 250 if (d == null) { 251 Dimension s = Toolkit.getDefaultToolkit().getScreenSize(); 252 d = new Rectangle (0, 0, s != null ? s.width : 800, s != null ? s.height : 600); 253 } 254 255 switch (p) { 256 case NORTH_WEST: 257 c.setLocation(d.x, d.y); 258 break; 259 case NORTH: 260 c.setLocation(d.x + (d.width - c.getSize().width) / 2, d.y); 261 break; 262 case NORTH_EAST: 263 c.setLocation(d.x + (d.width - c.getSize().width), d.y); 264 break; 265 case WEST: 266 c.setLocation(d.x, d.y + (d.height - c.getSize().height) / 2); 267 break; 268 case SOUTH_WEST: 269 c.setLocation(d.x, d.y + (d.height - c.getSize().height)); 270 break; 271 case EAST: 272 c.setLocation(d.x + d.width - c.getSize().width, d.y + (d.height - c.getSize().height) / 2); 273 break; 274 case SOUTH_EAST: 275 c.setLocation(d.x + (d.width - c.getSize().width), d.y + (d.height - c.getSize().height) - 30); 276 break; 277 case CENTER: 278 c.setLocation(d.x + (d.width - c.getSize().width) / 2, d.y + (d.height - c.getSize().height) / 2); 279 break; 280 } 281 } 282 289 public static void showErrorMessage(Component parent, String title, Throwable exception) { 290 showErrorMessage(parent, null, title, exception); 291 } 292 293 301 public static void showErrorMessage(Component parent, String mesg, String title, Throwable exception) { 302 boolean details = false; 303 while (true) { 304 String [] opts = new String [] { 305 details ? "Hide Details" : "Details", "Ok" 306 }; 307 StringBuffer buf = new StringBuffer (); 308 if (mesg != null) { 309 buf.append(mesg); 310 } 311 appendException(exception, 0, buf, details); 312 MultilineLabel message = new MultilineLabel(buf.toString()); 313 int opt = JOptionPane.showOptionDialog(parent, message, title, JOptionPane.OK_CANCEL_OPTION, JOptionPane.ERROR_MESSAGE, 314 null, opts, opts[1]); 315 if (opt == 0) { 316 details = !details; 317 } else { 318 break; 319 } 320 } 321 } 322 323 private static void appendException(Throwable exception, int level, StringBuffer buf, boolean details) { 324 try { 325 if (((exception != null) && (exception.getMessage() != null)) && (exception.getMessage().length() > 0)) { 326 if (details && (level > 0)) { 327 buf.append("\n \nCaused by ...\n"); 328 } 329 buf.append(exception.getMessage()); 330 } 331 if (details) { 332 if (exception != null) { 333 if ((exception.getMessage() != null) && (exception.getMessage().length() == 0)) { 334 buf.append("\n \nCaused by ..."); 335 } else { 336 buf.append("\n \n"); 337 } 338 } 339 StringWriter sw = new StringWriter (); 340 if (exception != null) { 341 exception.printStackTrace(new PrintWriter (sw)); 342 } 343 buf.append(sw.toString()); 344 } 345 try { 346 java.lang.reflect.Method method = exception.getClass().getMethod("getCause", new Class [] {}); 347 Throwable cause = (Throwable ) method.invoke(exception, (Object [])null); 348 if (cause != null) { 349 appendException(cause, level + 1, buf, details); 350 } 351 } catch (Exception e) { 352 } 353 } catch (Throwable ex) { 354 } 355 } 356 357 public static Image scaleWidth(int width, Image image, ImageObserver observer) { 358 if(image == null) { 359 return null; 360 } 361 double scale = (double)width / (double)image.getWidth(observer); 362 return image.getScaledInstance(width, (int)((double)image.getHeight(observer) * scale), Image.SCALE_SMOOTH); 363 } 364 365 public static Image scaleHeight(int height, Image image, ImageObserver observer) { 366 if(image == null) { 367 return null; 368 } 369 double scale = (double)height / (double)image.getHeight(observer); 370 return image.getScaledInstance((int)((double)image.getWidth(observer) * scale), height, Image.SCALE_SMOOTH); 371 } 372 373 public static SimpleAttributeSet getDefaultAttributeSet() { 374 SimpleAttributeSet attrSet = new SimpleAttributeSet (); 375 Font f = UIManager.getFont("Label.font"); 377 StyleConstants.setFontFamily(attrSet, f.getFamily()); 378 StyleConstants.setFontSize(attrSet, f.getSize()); 379 StyleConstants.setBold(attrSet, false); 380 StyleConstants.setItalic(attrSet, ( f.getStyle() & Font.ITALIC ) != 0); 381 return attrSet; 382 } 383 384 public static Image getImage(ImageIcon imageIcon) { 385 return imageIcon != null ? imageIcon.getImage() : null; 386 } 387 388 public static Image getImage(Icon icon) { 389 BufferedImage bim = new BufferedImage (icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB); 390 icon.paintIcon(null, bim.getGraphics(), 0, 0); 391 return bim; 392 } 393 394 public static Image getFrameImage(Icon icon) { 395 if(icon == null) { 396 return null; 397 } 398 BufferedImage bim = new BufferedImage (icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB); 399 icon.paintIcon(null, bim.getGraphics(), 0, 0); 400 return bim; 401 } 402 } 403 | Popular Tags |