| 1 19 20 package com.sshtools.ui.awt; 21 22 import java.awt.Component ; 23 import java.awt.Container ; 24 import java.awt.Dimension ; 25 import java.awt.Frame ; 26 import java.awt.GridBagConstraints ; 27 import java.awt.GridBagLayout ; 28 import java.awt.Image ; 29 import java.awt.MediaTracker ; 30 import java.awt.Rectangle ; 31 import java.awt.Toolkit ; 32 import java.awt.Window ; 33 import java.net.MalformedURLException ; 34 import java.net.URL ; 35 import java.text.MessageFormat ; 36 import java.util.Hashtable ; 37 38 43 public class UIUtil { 44 45 private static URL codebase; 46 private static Hashtable imageCache = new Hashtable (); 47 private static Hashtable stockIds = new Hashtable (); 48 private static Frame sharedFrame; 49 50 55 public static final int CENTER = 0; 56 57 60 public static final int NORTH = 1; 61 64 public static final int NORTH_EAST = 2; 65 68 public static final int EAST = 3; 69 72 public static final int SOUTH_EAST = 4; 73 76 public static final int SOUTH = 5; 77 80 public static final int SOUTH_WEST = 6; 81 84 public static final int WEST = 7; 85 88 public static final int NORTH_WEST = 8; 89 90 97 public static Image waitFor(Image image, Component component) { 98 if (image != null) { 99 MediaTracker tracker = new MediaTracker (component); 100 tracker.addImage(image, 0); 101 try { 102 tracker.waitForAll(); 103 return image; 104 } 105 catch (InterruptedException ie) { 106 ie.printStackTrace(); 107 } 108 if (tracker.isErrorAny()) { 109 System.err.println(MessageFormat.format(Messages.getString("UIUtil.imageDidNotLoad"), new Object [] { image.toString() } ) ); } 111 } 112 System.err.println(Messages.getString("UIUtil.noImage")); return null; 114 } 115 116 122 public static void setCodeBase(URL codebase) { 123 UIUtil.codebase = codebase; 124 } 125 126 134 public static Image loadImage(Class clazz, String resource) { 135 String path = resource; 136 137 150 URL url = clazz.getResource(path); 151 Image img = null; 152 if (url != null) { 153 img = (Image )imageCache.get(url.toExternalForm()); 154 if(img == null) { 155 img = Toolkit.getDefaultToolkit().getImage(url); 156 if(img != null) { 157 imageCache.put(url.toExternalForm(), img); 158 } 159 } 160 } 161 else { 162 if(codebase != null) { 163 URL loc; 164 try { 165 loc = new URL (codebase, resource); 166 img = (Image )imageCache.get(loc.toExternalForm()); 167 if(img == null) { 168 img = Toolkit.getDefaultToolkit().getImage(loc); 169 if(img != null) { 170 imageCache.put(loc.toExternalForm(), img); 171 } 172 } 173 } catch (MalformedURLException e) { 174 e.printStackTrace(); 175 img = null; 176 } 177 178 } 179 } 180 if(img == null) { 181 img = (Image )imageCache.get(path); 182 if(img == null) { 183 img = Toolkit.getDefaultToolkit().getImage(path); 184 if(img != null) { 185 imageCache.put(path, img); 186 } 187 } 188 } 189 190 if(img == null) { 191 System.err.println(MessageFormat.format(Messages.getString("UIUtil.couldNotLocateImage"), new Object [] { resource } ) ); } 193 return img; 194 } 195 196 207 public static void gridBagAdd(Container parent, Component componentToAdd, 208 GridBagConstraints constraints, int pos) { 209 if (! (parent.getLayout()instanceof GridBagLayout )) { 210 throw new IllegalArgumentException (Messages.getString("UIUtil.parentMustHaveGridBagLayout")); } 212 213 GridBagLayout layout = (GridBagLayout ) parent.getLayout(); 215 216 constraints.gridwidth = pos; 218 layout.setConstraints(componentToAdd, constraints); 219 parent.add(componentToAdd); 220 } 221 222 228 public static Window getWindowAncestor(Component c) { 229 for (Container p = c.getParent(); p != null; p = p.getParent()) { 230 if (p instanceof Window ) { 231 return (Window ) p; 232 } 233 } 234 return null; 235 } 236 237 243 public static Frame getFrameAncestor(Component c) { 244 if(c == null) { 245 return null; 246 } 247 for (Container p = c.getParent(); p != null; p = p.getParent()) { 248 if (p instanceof Frame ) { 249 return (Frame ) p; 250 } 251 } 252 return null; 253 } 254 255 256 263 public static void positionComponent(int p, Component c) { 264 265 268 269 Rectangle d = null; 270 try { 271 275 d = c.getGraphicsConfiguration().getDevice().getDefaultConfiguration(). 277 getBounds(); 278 } 280 catch (Throwable t) { 281 } 282 if(d == null) { 283 Dimension s = Toolkit.getDefaultToolkit().getScreenSize(); 284 d = new Rectangle (0, 0, s != null ? s.width : 800, s != null ? s.height : 600); 285 if( d.width > ( 2 * d.height ) ) { 287 d.width = d.width / 2; 288 } 289 } 290 291 switch (p) { 292 case NORTH_WEST: 293 c.setLocation(d.x, d.y); 294 break; 295 case NORTH: 296 c.setLocation(d.x + (d.width - c.getSize().width) / 2, d.y); 297 break; 298 case NORTH_EAST: 299 c.setLocation(d.x + (d.width - c.getSize().width), d.y); 300 break; 301 case WEST: 302 c.setLocation(d.x, d.y + (d.height - c.getSize().height) / 2); 303 break; 304 case SOUTH_WEST: 305 c.setLocation(d.x, d.y + (d.height - c.getSize().height)); 306 break; 307 case EAST: 308 c.setLocation(d.x + d.width - c.getSize().width, 309 d.y + (d.height - c.getSize().height) / 2); 310 break; 311 case SOUTH_EAST: 312 c.setLocation(d.x + (d.width - c.getSize().width), 313 d.y + (d.height - c.getSize().height) - 30); 314 break; 315 case CENTER: 316 c.setLocation(d.x + (d.width - c.getSize().width) / 2, 317 d.y + (d.height - c.getSize().height) / 2); 318 break; 319 } 320 } 321 322 325 public static Frame getSharedFrame() { 326 if(sharedFrame == null) { 327 sharedFrame = new Frame (); 328 } 329 return sharedFrame; 330 } 331 332 338 public static Image getStockImage(String id, Class clazz) { 339 String resource = (String )stockIds.get(id); 340 return loadImage(clazz, resource == null ? id : resource); 341 } 342 343 350 public static void setStockImage(String id, String resource) { 351 if(resource == null) { 352 stockIds.remove(id); 353 } 354 else { 355 stockIds.put(id, resource); 356 } 357 } 358 } 359 | Popular Tags |