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 Michael Danilov
019: * @version $Revision$
020: */package java.awt;
021:
022: import java.io.Serializable;
023: import java.util.Hashtable;
024:
025: import org.apache.harmony.awt.internal.nls.Messages;
026:
027: public class CardLayout implements LayoutManager2, Serializable {
028: private static final long serialVersionUID = -4328196481005934313L;
029:
030: private static final int DEFAULT_GAP = 0;
031:
032: private int vGap;
033: private int hGap;
034:
035: private Hashtable<String, Component> nameTable; //Name to component
036: private Hashtable<Component, String> compTable; //Component to name
037: private int curComponent;
038:
039: private final Toolkit toolkit = Toolkit.getDefaultToolkit();
040:
041: public CardLayout(int hgap, int vgap) {
042: toolkit.lockAWT();
043: try {
044: vGap = vgap;
045: hGap = hgap;
046:
047: nameTable = new Hashtable<String, Component>();
048: compTable = new Hashtable<Component, String>();
049: curComponent = 0;
050: } finally {
051: toolkit.unlockAWT();
052: }
053: }
054:
055: public CardLayout() {
056: this (DEFAULT_GAP, DEFAULT_GAP);
057: toolkit.lockAWT();
058: try {
059: } finally {
060: toolkit.unlockAWT();
061: }
062: }
063:
064: @Override
065: public String toString() {
066: /* The format is based on 1.5 release behavior
067: * which can be revealed by the following code:
068: * System.out.println(new CardLayout());
069: */
070:
071: toolkit.lockAWT();
072: try {
073: return getClass().getName()
074: + "[hgap=" + hGap + ",vgap=" + vGap + "]"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
075: } finally {
076: toolkit.unlockAWT();
077: }
078: }
079:
080: public int getHgap() {
081: toolkit.lockAWT();
082: try {
083: return hGap;
084: } finally {
085: toolkit.unlockAWT();
086: }
087: }
088:
089: public int getVgap() {
090: toolkit.lockAWT();
091: try {
092: return vGap;
093: } finally {
094: toolkit.unlockAWT();
095: }
096: }
097:
098: public void setHgap(int hgap) {
099: toolkit.lockAWT();
100: try {
101: hGap = hgap;
102: } finally {
103: toolkit.unlockAWT();
104: }
105: }
106:
107: public void setVgap(int vgap) {
108: toolkit.lockAWT();
109: try {
110: vGap = vgap;
111: } finally {
112: toolkit.unlockAWT();
113: }
114: }
115:
116: public float getLayoutAlignmentX(Container parent) {
117: toolkit.lockAWT();
118: try {
119: return Component.CENTER_ALIGNMENT;
120: } finally {
121: toolkit.unlockAWT();
122: }
123: }
124:
125: public float getLayoutAlignmentY(Container parent) {
126: toolkit.lockAWT();
127: try {
128: return Component.CENTER_ALIGNMENT;
129: } finally {
130: toolkit.unlockAWT();
131: }
132: }
133:
134: /**
135: * @deprecated
136: */
137: @Deprecated
138: public void addLayoutComponent(String name, Component comp) {
139: toolkit.lockAWT();
140: try {
141: if (name == null) {
142: if (compTable.get(comp) != null) {
143: return;
144: }
145: name = comp.toString();
146: }
147:
148: if (!nameTable.isEmpty()) {
149: comp.setVisible(false);
150: }
151: nameTable.put(name, comp);
152: compTable.put(comp, name);
153:
154: } finally {
155: toolkit.unlockAWT();
156: }
157: }
158:
159: public void addLayoutComponent(Component comp, Object constraints) {
160: toolkit.lockAWT();
161: try {
162: if (!String.class.isInstance(constraints)) {
163: // awt.131=AddLayoutComponent: constraint object must be String
164: throw new IllegalArgumentException(Messages
165: .getString("awt.131")); //$NON-NLS-1$
166: }
167: addLayoutComponent((String) constraints, comp);
168: } finally {
169: toolkit.unlockAWT();
170: }
171: }
172:
173: public void removeLayoutComponent(Component comp) {
174: toolkit.lockAWT();
175: try {
176: if (!compTable.containsKey(comp)) {
177: return;
178: }
179: Container parent = comp.getParent();
180: if (parent != null) {
181: int idx = parent.getComponentZOrder(comp);
182: if (idx == curComponent) {
183: next(parent);
184: }
185: }
186:
187: String name = compTable.get(comp);
188: if (name != null) {
189: nameTable.remove(name);
190: }
191: compTable.remove(comp);
192: } finally {
193: toolkit.unlockAWT();
194: }
195: }
196:
197: public void invalidateLayout(Container target) {
198: toolkit.lockAWT();
199: try {
200: //Nothing to invalidate
201: } finally {
202: toolkit.unlockAWT();
203: }
204: }
205:
206: public void layoutContainer(Container parent) {
207: toolkit.lockAWT();
208: try {
209: if (parent.getComponentCount() == 0) {
210: return;
211: }
212:
213: showCurrent(parent);
214: } finally {
215: toolkit.unlockAWT();
216: }
217: }
218:
219: private void showCurrent(Container parent) {
220: toolkit.lockAWT();
221: try {
222: if (curComponent >= parent.getComponentCount()) {
223: curComponent = 0;
224: }
225: Rectangle clientRect = parent.getClient();
226: Component comp = parent.getComponent(curComponent);
227: Rectangle bounds = new Rectangle(clientRect.x + hGap,
228: clientRect.y + vGap, clientRect.width - 2 * hGap,
229: clientRect.height - 2 * vGap);
230:
231: comp.setBounds(bounds);
232: comp.setVisible(true);
233:
234: } finally {
235: toolkit.unlockAWT();
236: }
237: }
238:
239: public void first(Container parent) {
240: toolkit.lockAWT();
241: try {
242: check(parent);
243: int size = parent.getComponentCount();
244: if (size == 0) {
245: return;
246: }
247:
248: hideCurrent(parent);
249: curComponent = 0;
250:
251: showCurrent(parent);
252: } finally {
253: toolkit.unlockAWT();
254: }
255: }
256:
257: private void hideCurrent(Container parent) {
258: if ((curComponent >= 0)
259: && (curComponent < parent.getComponentCount())) {
260: parent.getComponent(curComponent).setVisible(false);
261: }
262: }
263:
264: private void check(Container parent) {
265: if (parent.getLayout() != this ) {
266: // awt.132=wrong parent for CardLayout
267: throw new IllegalArgumentException(Messages
268: .getString("awt.132")); //$NON-NLS-1$
269: }
270: }
271:
272: public void last(Container parent) {
273: toolkit.lockAWT();
274: try {
275: check(parent);
276: int size = parent.getComponentCount();
277: if (size == 0) {
278: return;
279: }
280:
281: hideCurrent(parent);
282: curComponent = size - 1;
283:
284: showCurrent(parent);
285: } finally {
286: toolkit.unlockAWT();
287: }
288: }
289:
290: public void next(Container parent) {
291: toolkit.lockAWT();
292: try {
293: check(parent);
294: int size = parent.getComponentCount();
295: if (size == 0) {
296: return;
297: }
298:
299: hideCurrent(parent);
300: curComponent++;
301: if (curComponent >= size) {
302: curComponent = 0;
303: }
304:
305: showCurrent(parent);
306: } finally {
307: toolkit.unlockAWT();
308: }
309: }
310:
311: public void previous(Container parent) {
312: toolkit.lockAWT();
313: try {
314: check(parent);
315: int size = parent.getComponentCount();
316: if (size == 0) {
317: return;
318: }
319:
320: hideCurrent(parent);
321: curComponent--;
322: if (curComponent < 0) {
323: curComponent = size - 1;
324: }
325:
326: showCurrent(parent);
327: } finally {
328: toolkit.unlockAWT();
329: }
330: }
331:
332: public void show(Container parent, String name) {
333: toolkit.lockAWT();
334: try {
335: check(parent);
336: int size = parent.getComponentCount();
337: if (size == 0) {
338: return;
339: }
340:
341: Component comp = nameTable.get(name);
342:
343: if (comp == null) {
344: return;
345: }
346:
347: hideCurrent(parent);
348: curComponent = parent.getComponentZOrder(comp);
349: showCurrent(parent);
350: } finally {
351: toolkit.unlockAWT();
352: }
353: }
354:
355: public Dimension maximumLayoutSize(Container target) {
356: toolkit.lockAWT();
357: try {
358: return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
359: } finally {
360: toolkit.unlockAWT();
361: }
362: }
363:
364: public Dimension minimumLayoutSize(Container parent) {
365: toolkit.lockAWT();
366: try {
367: if (parent.getComponentCount() == 0) {
368: return parent.addInsets(new Dimension(0, 0));
369: }
370:
371: return parent.addInsets(layoutSize(parent, false));
372: } finally {
373: toolkit.unlockAWT();
374: }
375: }
376:
377: public Dimension preferredLayoutSize(Container parent) {
378: toolkit.lockAWT();
379: try {
380: if (parent.getComponentCount() == 0) {
381: return parent.addInsets(new Dimension(0, 0));
382: }
383:
384: return parent.addInsets(layoutSize(parent, true));
385: } finally {
386: toolkit.unlockAWT();
387: }
388: }
389:
390: private Dimension layoutSize(Container parent, boolean preferred) {
391: int maxWidth = 0;
392: int maxHeight = 0;
393:
394: for (int i = 0; i < parent.getComponentCount(); i++) {
395: Component comp = parent.getComponent(i);
396: Dimension compSize = (preferred ? comp.getPreferredSize()
397: : comp.getMinimumSize());
398:
399: maxWidth = Math.max(maxWidth, compSize.width);
400: maxHeight = Math.max(maxHeight, compSize.height);
401: }
402:
403: return new Dimension(maxWidth + 2 * hGap, maxHeight + 2 * vGap);
404: }
405:
406: }
|