KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > form > fakepeer > FakeComponentPeer


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

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 /**
29  *
30  * @author Tran Duc Trung
31  */

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         // how to recognize that the color was set to target explicitly?
53
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.setLocale(_target.getLocale());
73
_delegate.setDropTarget(_target.getDropTarget());
74         _delegate.setComponentOrientation(_target.getComponentOrientation());
75
76         repaint();
77     }
78
79     abstract Component createDelegate();
80
81     // ---------------
82

83     // JDK 1.4
84
public boolean isObscured() {
85         return false;
86     }
87
88     // JDK 1.4
89
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     // JDK 1.5
126
public void setBounds(int x, int y, int width, int height, int op) {
127         _delegate.setBounds(x, y, width, height);
128     }
129
130     // JDK 1.5
131
public Rectangle getBounds() {
132         return _delegate.getBounds();
133     }
134
135     public void handleEvent(AWTEvent e) {
136     }
137
138     // JDK 1.3
139
public void coalescePaintEvent(PaintEvent e) {
140     }
141
142     public Point getLocationOnScreen() {
143         // this is called from target
144
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         // this is called from target
179
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     // not in JDK 1.4
200
public void setCursor(Cursor cursor) {
201         _delegate.setCursor(cursor);
202     }
203
204     // JDK 1.4
205
public void updateCursorImmediately() {
206     }
207
208     // not in JDK 1.4
209
public void requestFocus() {
210         // this is called from target
211
}
212
213     // JDK 1.4
214
public boolean requestFocus(Component lightweightChild,
215                                 boolean temporary,
216                                 boolean focusedWindowChangeAllowed,
217                                 long time)
218     {
219         return false;
220     }
221     
222     // not in JDK 1.4
223
public boolean isFocusTraversable() {
224         return false;
225     }
226
227     // JDK 1.4
228
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     // JDK 1.4 (VolatileImage not before 1.4)
241
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     // JDK 1.3
259
public GraphicsConfiguration getGraphicsConfiguration() {
260         return _target.getGraphicsConfiguration();
261     }
262
263     // JDK 1.4
264
public boolean handlesWheelScrolling() {
265         return false;
266     }
267
268     // JDK 1.4 (BufferCapabilities not before 1.4)
269
public void createBuffers(int numBuffers, BufferCapabilities caps)
270         throws AWTException
271     {
272     }
273
274     // JDK 1.4
275
public Image getBackBuffer() {
276         return null;
277     }
278
279     // JDK 1.4 (BufferCapabilities not before 1.4)
280
public void flip(BufferCapabilities.FlipContents flipAction) {
281     }
282
283     // JDK 1.4
284
public void destroyBuffers() {
285     }
286
287     // JDK 1.5
288
public void reparent(java.awt.peer.ContainerPeer newContainer) {
289     }
290
291     // JDK 1.5
292
public boolean isReparentSupported() {
293         return false;
294     }
295
296     // JDK 1.5
297
public void layout() {
298     }
299
300     // deprecated
301
public Dimension preferredSize() {
302         return getPreferredSize();
303     }
304
305     // deprecated
306
public Dimension minimumSize() {
307         return getMinimumSize();
308     }
309
310     // deprecated
311
public void show() {
312         setVisible(true);
313     }
314
315     // deprecated
316
public void hide() {
317         setVisible(false);
318     }
319
320     // deprecated
321
public void enable() {
322         setEnabled(true);
323     }
324
325     // deprecated
326
public void disable() {
327         setEnabled(false);
328     }
329
330     // deprecated
331
public void reshape(int x, int y, int width, int height) {
332         setBounds(x, y, width, height);
333     }
334
335     //
336
// helpers
337
//
338

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     //
349
//
350
//
351

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             // by default display the class name
364
Font origFont = g.getFont();
365             g.setFont(origFont.deriveFont(Font.BOLD, origFont.getSize() + 1));
366
367             String JavaDoc 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 JavaDoc className = _target.getClass().getName();
382             className = className.substring(className.lastIndexOf('.') + 1);
383
384             FontMetrics fm = this.getFontMetrics(
385                 new Font("Dialog", Font.BOLD, 12)); // NOI18N
386
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