001: /*
002: * @(#)HeavyweightWrapper.java 10/18/2005
003: *
004: * Copyright 2002 - 2005 JIDE Software Inc. All rights reserved.
005: */
006: package com.jidesoft.swing;
007:
008: import javax.swing.*;
009: import java.awt.*;
010: import java.awt.event.ComponentEvent;
011: import java.awt.event.ComponentListener;
012:
013: /**
014: * HeavyweightWrapper is a special heavyweight Panel that can hold another component.
015: * <p/>
016: * It's package local right now. Whenever it is ready, we will make it public.
017: */
018: public class HeavyweightWrapper extends Panel {
019: private Component _component;
020: private boolean _heavyweight;
021:
022: public HeavyweightWrapper(Component component, boolean heavyweight) {
023: _component = component;
024: ((JComponent) _component).putClientProperty(
025: "HeavyweightWrapper", this );
026: if (_component != null) {
027: _component.addComponentListener(new ComponentListener() {
028: public void componentResized(ComponentEvent e) {
029: // Rectangle rect = e.getComponent().getBounds();
030: // Rectangle parentRect = SwingUtilities.convertRectangle(HeavyweightWrapper.this, rect, HeavyweightWrapper.this.getParent());
031: // System.out.println(rect + " " + parentRect);
032: // HeavyweightWrapper.this.setBounds(parentRect);
033: }
034:
035: public void componentMoved(ComponentEvent e) {
036: }
037:
038: public void componentShown(ComponentEvent e) {
039: setVisible(true);
040: }
041:
042: public void componentHidden(ComponentEvent e) {
043: setVisible(false);
044: }
045: });
046: }
047: setLayout(new BorderLayout());
048: _heavyweight = heavyweight;
049: }
050:
051: public HeavyweightWrapper(Component component) {
052: this (component, false);
053: }
054:
055: public boolean isHeavyweight() {
056: return _heavyweight;
057: }
058:
059: public void setHeavyweight(boolean heavyweight) {
060: _heavyweight = heavyweight;
061: }
062:
063: public void delegateAdd(Container parent, Object constraints) {
064: if (isHeavyweight()) {
065: if (_component.getParent() != this ) {
066: add(_component);
067: // System.out.println("added component");
068: }
069: if (this .getParent() != parent) {
070: parent.add(this , constraints);
071: // System.out.println("added parent");
072: }
073: } else {
074: parent.add(_component, constraints);
075: }
076: }
077:
078: public void delegateRemove(Container parent) {
079: if (isHeavyweight()) {
080: remove(_component);
081: parent.remove(this );
082: // System.out.println("removed");
083: } else {
084: parent.remove(_component);
085: }
086: }
087:
088: public void delegateSetVisible(boolean visible) {
089: if (isHeavyweight()) {
090: this .setVisible(visible);
091: _component.setVisible(visible);
092: } else {
093: _component.setVisible(visible);
094: }
095: }
096:
097: public void delegateSetBounds(Rectangle bounds) {
098: if (isHeavyweight()) {
099: this .setBounds(bounds);
100: _component.setBounds(0, 0, bounds.width, bounds.height);
101: } else {
102: _component.setBounds(bounds);
103: }
104: }
105:
106: public void delegateSetBounds(int x, int y, int width, int height) {
107: if (isHeavyweight()) {
108: this .setBounds(x, y, width, height);
109: _component.setBounds(0, 0, width, height);
110: } else {
111: _component.setBounds(x, y, width, height);
112: }
113: }
114:
115: public void delegateSetLocation(int x, int y) {
116: if (isHeavyweight()) {
117: this .setLocation(x, y);
118: _component.setLocation(0, 0);
119: } else {
120: _component.setLocation(x, y);
121: }
122: }
123:
124: public void delegateSetLocation(Point p) {
125: if (isHeavyweight()) {
126: this .setLocation(p);
127: _component.setLocation(0, 0);
128: } else {
129: _component.setLocation(p);
130: }
131: }
132:
133: public void delegateSetCursor(Cursor cursor) {
134: _component.setCursor(cursor);
135: }
136:
137: public void delegateSetNull() {
138: ((JComponent) _component).putClientProperty(
139: "HeavyweightWrapper", null);
140: _component = null;
141: }
142:
143: public Container delegateGetParent() {
144: if (isHeavyweight()) {
145: return getParent();
146: } else {
147: return _component.getParent();
148: }
149: }
150:
151: public boolean delegateIsVisible() {
152: if (isHeavyweight()) {
153: return isVisible();
154: } else {
155: return _component.isVisible();
156: }
157: }
158:
159: public Rectangle delegateGetBounds() {
160: if (isHeavyweight()) {
161: return getBounds();
162: } else {
163: return _component.getBounds();
164: }
165: }
166:
167: public void delegateRepaint() {
168: if (isHeavyweight()) {
169: repaint();
170: _component.repaint();
171: } else {
172: _component.repaint();
173: }
174: }
175:
176: public Component getComponent() {
177: return _component;
178: }
179:
180: public void setComponent(Component component) {
181: _component = component;
182: }
183: }
|