1 19 20 21 package org.netbeans.modules.form.fakepeer; 22 23 import java.awt.*; 24 import java.awt.event.*; 25 import java.awt.image.*; 26 27 28 32 33 abstract class FakeComponentPeer 34 { 35 Component _delegate; 36 Component _target; 37 38 FakeComponentPeer(Component target) { 39 _target = target; 40 _delegate = createDelegate(); 41 initDelegate(); 42 } 43 44 void initDelegate() { 45 Rectangle r = _target.getBounds(); 46 47 setBounds(r.x, r.y, r.width, r.height); 48 setVisible(_target.isVisible()); 49 setCursor(_target.getCursor()); 50 setEnabled(_target.isEnabled()); 51 52 Container parent = _target.getParent(); 54 Color color = _target.getBackground(); 55 if (color != null && (parent == null || parent.getBackground() != color)) 56 _delegate.setBackground(color); 57 else 58 _target.setBackground(_delegate.getBackground()); 59 60 color = _target.getForeground(); 61 if (color != null && (parent == null || parent.getForeground() != color)) 62 _delegate.setForeground(color); 63 else 64 _target.setForeground(_delegate.getForeground()); 65 66 Font font = _target.getFont(); 67 if (font == null || (parent != null && parent.getFont() == font)) 68 font = FakePeerSupport.getDefaultAWTFont(); 69 _delegate.setFont(font); 70 71 _delegate.setName(_target.getName()); 72 _delegate.setDropTarget(_target.getDropTarget()); 74 _delegate.setComponentOrientation(_target.getComponentOrientation()); 75 76 repaint(); 77 } 78 79 abstract Component createDelegate(); 80 81 83 public boolean isObscured() { 85 return false; 86 } 87 88 public boolean canDetermineObscurity() { 90 return false; 91 } 92 93 public void setVisible(boolean visible) { 94 _delegate.setVisible(visible); 95 } 96 97 public void setEnabled(boolean enabled) { 98 _delegate.setEnabled(enabled); 99 } 100 101 public void paint(Graphics g) { 102 Font oldFont = g.getFont(); 103 Color oldColor = g.getColor(); 104 try { 105 _delegate.paint(g); 106 _target.paint(g); 107 } 108 finally { 109 g.setColor(oldColor); 110 g.setFont(oldFont); 111 } 112 } 113 114 public void repaint(long tm, int x, int y, int w, int h) { 115 _delegate.repaint(tm, x, y, w, h); 116 } 117 118 public void print(Graphics g1) { 119 } 120 121 public void setBounds(int x, int y, int width, int height) { 122 _delegate.setBounds(x, y, width, height); 123 } 124 125 public void setBounds(int x, int y, int width, int height, int op) { 127 _delegate.setBounds(x, y, width, height); 128 } 129 130 public Rectangle getBounds() { 132 return _delegate.getBounds(); 133 } 134 135 public void handleEvent(AWTEvent e) { 136 } 137 138 public void coalescePaintEvent(PaintEvent e) { 140 } 141 142 public Point getLocationOnScreen() { 143 return null; 145 } 146 147 public Dimension getPreferredSize() { 148 return getMinimumSize(); 149 } 150 151 public Dimension getMinimumSize() { 152 return _delegate.getMinimumSize(); 153 } 154 155 public ColorModel getColorModel() { 156 return _delegate.getColorModel(); 157 } 158 159 public Toolkit getToolkit() { 160 return _delegate.getToolkit(); 161 } 162 163 public Graphics getGraphics() { 164 Component parent = _target.getParent(); 165 if (parent != null) { 166 Graphics g = parent.getGraphics(); 167 if (g != null) { 168 Rectangle bounds = _target.getBounds(); 169 g.translate(bounds.x, bounds.y); 170 g.setClip(0, 0, bounds.width, bounds.height); 171 } 172 return g; 173 } 174 return null; 175 } 176 177 public FontMetrics getFontMetrics(Font font) { 178 return null; 180 } 181 182 public void dispose() { 183 _target = null; 184 _delegate = null; 185 } 186 187 public void setForeground(Color color) { 188 _delegate.setForeground(color); 189 } 190 191 public void setBackground(Color color) { 192 _delegate.setBackground(color); 193 } 194 195 public void setFont(Font font) { 196 _delegate.setFont(font); 197 } 198 199 public void setCursor(Cursor cursor) { 201 _delegate.setCursor(cursor); 202 } 203 204 public void updateCursorImmediately() { 206 } 207 208 public void requestFocus() { 210 } 212 213 public boolean requestFocus(Component lightweightChild, 215 boolean temporary, 216 boolean focusedWindowChangeAllowed, 217 long time) 218 { 219 return false; 220 } 221 222 public boolean isFocusTraversable() { 224 return false; 225 } 226 227 public boolean isFocusable() { 229 return false; 230 } 231 232 public Image createImage(ImageProducer producer) { 233 return getToolkit().createImage(producer); 234 } 235 236 public Image createImage(int width, int height) { 237 return new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 238 } 239 240 public VolatileImage createVolatileImage(int width, int height) { 242 GraphicsConfiguration gc = getGraphicsConfiguration(); 243 return gc != null ? gc.createCompatibleVolatileImage(width, height) : null; 244 } 245 246 public boolean prepareImage(Image img, int w, int h, 247 ImageObserver imageObserver) 248 { 249 return _delegate.prepareImage(img, w, h, imageObserver); 250 } 251 252 public int checkImage(Image img, int w, int h, 253 ImageObserver imageObserver) 254 { 255 return _delegate.checkImage(img, w, h, imageObserver); 256 } 257 258 public GraphicsConfiguration getGraphicsConfiguration() { 260 return _target.getGraphicsConfiguration(); 261 } 262 263 public boolean handlesWheelScrolling() { 265 return false; 266 } 267 268 public void createBuffers(int numBuffers, BufferCapabilities caps) 270 throws AWTException 271 { 272 } 273 274 public Image getBackBuffer() { 276 return null; 277 } 278 279 public void flip(BufferCapabilities.FlipContents flipAction) { 281 } 282 283 public void destroyBuffers() { 285 } 286 287 public void reparent(java.awt.peer.ContainerPeer newContainer) { 289 } 290 291 public boolean isReparentSupported() { 293 return false; 294 } 295 296 public void layout() { 298 } 299 300 public Dimension preferredSize() { 302 return getPreferredSize(); 303 } 304 305 public Dimension minimumSize() { 307 return getMinimumSize(); 308 } 309 310 public void show() { 312 setVisible(true); 313 } 314 315 public void hide() { 317 setVisible(false); 318 } 319 320 public void enable() { 322 setEnabled(true); 323 } 324 325 public void disable() { 327 setEnabled(false); 328 } 329 330 public void reshape(int x, int y, int width, int height) { 332 setBounds(x, y, width, height); 333 } 334 335 339 void clearRectBeforePaint(Graphics g, Rectangle r) { 340 g.clearRect(r.x, r.y, r.width, r.height); 341 } 342 343 void repaint() { 344 Dimension sz = _target.getSize(); 345 repaint(0, 0, 0, sz.width, sz.height); 346 } 347 348 352 protected class Delegate extends Component 353 { 354 public void paint(Graphics g) { 355 Dimension sz = _target.getSize(); 356 357 Color c = _target.getBackground(); 358 if (c == null) 359 c = SystemColor.window; 360 g.setColor(c); 361 FakePeerUtils.drawLoweredBox(g,0,0,sz.width,sz.height); 362 363 Font origFont = g.getFont(); 365 g.setFont(origFont.deriveFont(Font.BOLD, origFont.getSize() + 1)); 366 367 String className = _target.getClass().getName(); 368 className = className.substring(className.lastIndexOf('.') + 1); 369 370 FontMetrics fm = g.getFontMetrics(); 371 int w = fm.stringWidth(className); 372 int h = fm.getHeight() - fm.getDescent(); 373 374 int x = (sz.width - w) / 2; 375 376 g.setColor(SystemColor.text); 377 g.drawString(className, x,(sz.height - h) / 2 + h - 1); 378 } 379 380 public Dimension getMinimumSize() { 381 String className = _target.getClass().getName(); 382 className = className.substring(className.lastIndexOf('.') + 1); 383 384 FontMetrics fm = this.getFontMetrics( 385 new Font("Dialog", Font.BOLD, 12)); int w = fm.stringWidth(className); 387 int h = fm.getHeight(); 388 389 return new Dimension(w + 10, h + 4); 390 } 391 } 392 } 393 | Popular Tags |