001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Dmitry A. Durnev
019: * @version $Revision$
020: */package java.awt;
021:
022: import org.apache.harmony.awt.gl.MultiRectArea;
023: import org.apache.harmony.awt.wtk.NativeWindow;
024:
025: /**
026: * A lightweight component specific behaviour.
027: *
028: */
029:
030: class LWBehavior implements ComponentBehavior {
031:
032: /*
033: * only lightweights store their position/size, others get it from native
034: * resources each time
035: */
036:
037: private int x, y, w, h;
038:
039: private final Component component;
040:
041: private boolean visible;
042:
043: private boolean displayable;
044:
045: LWBehavior(Component comp) {
046: component = comp;
047: /* lightweight components are initially visible */
048: visible = component.isVisible();
049: }
050:
051: public void addNotify() {
052: displayable = true;
053: if (visible) {
054: component.repaint();
055: }
056: }
057:
058: public void setVisible(boolean b) {
059: visible = b;
060: if (visible) {
061: component.repaint();
062: } else if (isParentShowing()) {
063: component.parent.repaint(x, y, w, h);
064: }
065: }
066:
067: public Graphics getGraphics(int translationX, int translationY,
068: int width, int height) {
069: Graphics g = null;
070: Container parent = component.getParent();
071:
072: if (parent != null) {
073: g = parent.behaviour.getGraphics(
074: translationX + component.x, translationY
075: + component.y, width, height);
076: }
077:
078: return g;
079: }
080:
081: public int getX() {
082: return x;
083: }
084:
085: public int getY() {
086: return y;
087: }
088:
089: public void setBounds(int x, int y, int width, int height) {
090: Rectangle oldBounds = new Rectangle(this .x, this .y, this .w,
091: this .h);
092: Rectangle newBounds = new Rectangle(x, y, width, height);
093:
094: if (oldBounds.equals(newBounds)) {
095: return;
096: }
097: w = width;
098: h = height;
099: this .x = x;
100: this .y = y;
101:
102: boolean grow = newBounds.contains(oldBounds);
103: boolean shrink = oldBounds.contains(newBounds);
104: if (isParentShowing() && !grow) {
105: Component parent = component.parent;
106: if (!shrink) {
107: parent.repaintRegion = new MultiRectArea(oldBounds);
108: parent.repaintRegion.substract(newBounds);
109: }
110: parent.repaint(oldBounds.x, oldBounds.y, oldBounds.width,
111: oldBounds.height);
112: }
113: if (visible && displayable && !shrink) {
114: component.repaint();
115: }
116: }
117:
118: public boolean isLightweight() {
119: return true;
120: }
121:
122: public boolean isOpaque() {
123: return false;
124: }
125:
126: public void setBounds(int x, int y, int w, int h, int bMask) {
127: setBounds(component.x, component.y, component.w, component.h);
128: onMove(x, y);
129: }
130:
131: public boolean isDisplayable() {
132: return displayable;
133: }
134:
135: public void setEnabled(boolean value) {
136: }
137:
138: public void removeNotify() {
139: displayable = false;
140: if (visible && isParentShowing()) {
141: component.parent.repaint(x, y, w, h);
142: }
143: }
144:
145: public NativeWindow getNativeWindow() {
146: return component.getHWAncestor().getNativeWindow();
147: }
148:
149: public void onMove(int x, int y) {
150: if (component instanceof Container) {
151: Container cont = (Container) component;
152: for (int i = 0; i < cont.getComponentCount(); i++) {
153: cont.getComponent(i).behaviour.onMove(x, y);
154: }
155: }
156:
157: }
158:
159: public void setZOrder(int newIndex, int oldIndex) {
160: // do nothing
161: }
162:
163: public boolean setFocus(boolean focus, Component opposite) {
164:
165: // request [native] focus from the nearest heavyweight ancestor
166: // don't post event, so call only cb
167: Component hw = component.getHWAncestor();
168: if (hw != null) {
169: hw.behaviour.setFocus(focus, opposite);
170: }
171:
172: return true;
173: }
174:
175: private boolean isParentShowing() {
176: return component.parent != null && component.parent.visible
177: && component.parent.behaviour.isDisplayable();
178: }
179: }
|