001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.form.fakepeer;
043:
044: import java.awt.*;
045: import java.awt.event.*;
046: import java.awt.image.*;
047:
048: /**
049: *
050: * @author Tran Duc Trung
051: */
052:
053: abstract class FakeComponentPeer {
054: Component _delegate;
055: Component _target;
056:
057: FakeComponentPeer(Component target) {
058: _target = target;
059: _delegate = createDelegate();
060: initDelegate();
061: }
062:
063: void initDelegate() {
064: Rectangle r = _target.getBounds();
065:
066: setBounds(r.x, r.y, r.width, r.height);
067: setVisible(_target.isVisible());
068: setCursor(_target.getCursor());
069: setEnabled(_target.isEnabled());
070:
071: // how to recognize that the color was set to target explicitly?
072: Container parent = _target.getParent();
073: Color color = _target.getBackground();
074: if (color != null
075: && (parent == null || parent.getBackground() != color))
076: _delegate.setBackground(color);
077: else
078: _target.setBackground(_delegate.getBackground());
079:
080: color = _target.getForeground();
081: if (color != null
082: && (parent == null || parent.getForeground() != color))
083: _delegate.setForeground(color);
084: else
085: _target.setForeground(_delegate.getForeground());
086:
087: Font font = _target.getFont();
088: if (font == null
089: || (parent != null && parent.getFont() == font))
090: font = FakePeerSupport.getDefaultAWTFont();
091: _delegate.setFont(font);
092:
093: _delegate.setName(_target.getName());
094: // _delegate.setLocale(_target.getLocale());
095: _delegate.setDropTarget(_target.getDropTarget());
096: _delegate.setComponentOrientation(_target
097: .getComponentOrientation());
098:
099: repaint();
100: }
101:
102: abstract Component createDelegate();
103:
104: // ---------------
105:
106: // JDK 1.4
107: public boolean isObscured() {
108: return false;
109: }
110:
111: // JDK 1.4
112: public boolean canDetermineObscurity() {
113: return false;
114: }
115:
116: public void setVisible(boolean visible) {
117: _delegate.setVisible(visible);
118: }
119:
120: public void setEnabled(boolean enabled) {
121: _delegate.setEnabled(enabled);
122: }
123:
124: public void paint(Graphics g) {
125: Font oldFont = g.getFont();
126: Color oldColor = g.getColor();
127: try {
128: _delegate.paint(g);
129: _target.paint(g);
130: } finally {
131: g.setColor(oldColor);
132: g.setFont(oldFont);
133: }
134: }
135:
136: public void repaint(long tm, int x, int y, int w, int h) {
137: _delegate.repaint(tm, x, y, w, h);
138: }
139:
140: public void print(Graphics g1) {
141: }
142:
143: public void setBounds(int x, int y, int width, int height) {
144: _delegate.setBounds(x, y, width, height);
145: }
146:
147: // JDK 1.5
148: public void setBounds(int x, int y, int width, int height, int op) {
149: _delegate.setBounds(x, y, width, height);
150: }
151:
152: // JDK 1.5
153: public Rectangle getBounds() {
154: return _delegate.getBounds();
155: }
156:
157: public void handleEvent(AWTEvent e) {
158: }
159:
160: // JDK 1.3
161: public void coalescePaintEvent(PaintEvent e) {
162: }
163:
164: public Point getLocationOnScreen() {
165: // this is called from target
166: return null;
167: }
168:
169: public Dimension getPreferredSize() {
170: return getMinimumSize();
171: }
172:
173: public Dimension getMinimumSize() {
174: return _delegate.getMinimumSize();
175: }
176:
177: public ColorModel getColorModel() {
178: return _delegate.getColorModel();
179: }
180:
181: public Toolkit getToolkit() {
182: return _delegate.getToolkit();
183: }
184:
185: public Graphics getGraphics() {
186: Component parent = _target.getParent();
187: if (parent != null) {
188: Graphics g = parent.getGraphics();
189: if (g != null) {
190: Rectangle bounds = _target.getBounds();
191: g.translate(bounds.x, bounds.y);
192: g.setClip(0, 0, bounds.width, bounds.height);
193: }
194: return g;
195: }
196: return null;
197: }
198:
199: public FontMetrics getFontMetrics(Font font) {
200: // this is called from target
201: return null;
202: }
203:
204: public void dispose() {
205: _target = null;
206: _delegate = null;
207: }
208:
209: public void setForeground(Color color) {
210: _delegate.setForeground(color);
211: }
212:
213: public void setBackground(Color color) {
214: _delegate.setBackground(color);
215: }
216:
217: public void setFont(Font font) {
218: _delegate.setFont(font);
219: }
220:
221: // not in JDK 1.4
222: public void setCursor(Cursor cursor) {
223: _delegate.setCursor(cursor);
224: }
225:
226: // JDK 1.4
227: public void updateCursorImmediately() {
228: }
229:
230: // not in JDK 1.4
231: public void requestFocus() {
232: // this is called from target
233: }
234:
235: // JDK 1.4
236: public boolean requestFocus(Component lightweightChild,
237: boolean temporary, boolean focusedWindowChangeAllowed,
238: long time) {
239: return false;
240: }
241:
242: // not in JDK 1.4
243: public boolean isFocusTraversable() {
244: return false;
245: }
246:
247: // JDK 1.4
248: public boolean isFocusable() {
249: return false;
250: }
251:
252: public Image createImage(ImageProducer producer) {
253: return getToolkit().createImage(producer);
254: }
255:
256: public Image createImage(int width, int height) {
257: return new BufferedImage(width, height,
258: BufferedImage.TYPE_INT_RGB);
259: }
260:
261: // JDK 1.4 (VolatileImage not before 1.4)
262: public VolatileImage createVolatileImage(int width, int height) {
263: GraphicsConfiguration gc = getGraphicsConfiguration();
264: return gc != null ? gc.createCompatibleVolatileImage(width,
265: height) : null;
266: }
267:
268: public boolean prepareImage(Image img, int w, int h,
269: ImageObserver imageObserver) {
270: return _delegate.prepareImage(img, w, h, imageObserver);
271: }
272:
273: public int checkImage(Image img, int w, int h,
274: ImageObserver imageObserver) {
275: return _delegate.checkImage(img, w, h, imageObserver);
276: }
277:
278: // JDK 1.3
279: public GraphicsConfiguration getGraphicsConfiguration() {
280: return _target.getGraphicsConfiguration();
281: }
282:
283: // JDK 1.4
284: public boolean handlesWheelScrolling() {
285: return false;
286: }
287:
288: // JDK 1.4 (BufferCapabilities not before 1.4)
289: public void createBuffers(int numBuffers, BufferCapabilities caps)
290: throws AWTException {
291: }
292:
293: // JDK 1.4
294: public Image getBackBuffer() {
295: return null;
296: }
297:
298: // JDK 1.4 (BufferCapabilities not before 1.4)
299: public void flip(BufferCapabilities.FlipContents flipAction) {
300: }
301:
302: // JDK 1.4
303: public void destroyBuffers() {
304: }
305:
306: // JDK 1.5
307: public void reparent(java.awt.peer.ContainerPeer newContainer) {
308: }
309:
310: // JDK 1.5
311: public boolean isReparentSupported() {
312: return false;
313: }
314:
315: // JDK 1.5
316: public void layout() {
317: }
318:
319: // deprecated
320: public Dimension preferredSize() {
321: return getPreferredSize();
322: }
323:
324: // deprecated
325: public Dimension minimumSize() {
326: return getMinimumSize();
327: }
328:
329: // deprecated
330: public void show() {
331: setVisible(true);
332: }
333:
334: // deprecated
335: public void hide() {
336: setVisible(false);
337: }
338:
339: // deprecated
340: public void enable() {
341: setEnabled(true);
342: }
343:
344: // deprecated
345: public void disable() {
346: setEnabled(false);
347: }
348:
349: // deprecated
350: public void reshape(int x, int y, int width, int height) {
351: setBounds(x, y, width, height);
352: }
353:
354: //
355: // helpers
356: //
357:
358: void clearRectBeforePaint(Graphics g, Rectangle r) {
359: g.clearRect(r.x, r.y, r.width, r.height);
360: }
361:
362: void repaint() {
363: Dimension sz = _target.getSize();
364: repaint(0, 0, 0, sz.width, sz.height);
365: }
366:
367: //
368: //
369: //
370:
371: protected class Delegate extends Component {
372: @Override
373: public void paint(Graphics g) {
374: Dimension sz = _target.getSize();
375:
376: Color c = _target.getBackground();
377: if (c == null)
378: c = SystemColor.window;
379: g.setColor(c);
380: FakePeerUtils.drawLoweredBox(g, 0, 0, sz.width, sz.height);
381:
382: // by default display the class name
383: Font origFont = g.getFont();
384: g.setFont(origFont.deriveFont(Font.BOLD,
385: origFont.getSize() + 1));
386:
387: String className = _target.getClass().getName();
388: className = className
389: .substring(className.lastIndexOf('.') + 1);
390:
391: FontMetrics fm = g.getFontMetrics();
392: int w = fm.stringWidth(className);
393: int h = fm.getHeight() - fm.getDescent();
394:
395: int x = (sz.width - w) / 2;
396:
397: g.setColor(SystemColor.text);
398: g.drawString(className, x, (sz.height - h) / 2 + h - 1);
399: }
400:
401: @Override
402: public Dimension getMinimumSize() {
403: String className = _target.getClass().getName();
404: className = className
405: .substring(className.lastIndexOf('.') + 1);
406:
407: FontMetrics fm = this .getFontMetrics(new Font("Dialog",
408: Font.BOLD, 12)); // NOI18N
409: int w = fm.stringWidth(className);
410: int h = fm.getHeight();
411:
412: return new Dimension(w + 10, h + 4);
413: }
414: }
415: }
|