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: package javax.swing;
019:
020: import java.awt.AWTError;
021: import java.awt.Component;
022: import java.awt.Container;
023: import java.awt.Dimension;
024: import java.awt.LayoutManager2;
025: import java.io.Serializable;
026:
027: import org.apache.harmony.x.swing.internal.nls.Messages;
028:
029: public class BoxLayout implements LayoutManager2, Serializable {
030: private static final long serialVersionUID = -2474455742719112368L;
031:
032: public static final int X_AXIS = 0;
033:
034: public static final int Y_AXIS = 1;
035:
036: public static final int LINE_AXIS = 2;
037:
038: public static final int PAGE_AXIS = 3;
039:
040: private final Container target;
041:
042: private final transient LayoutParameters layoutParams;
043:
044: public BoxLayout(final Container target, final int axis) {
045: super ();
046: int alignment = axisToAlignment(target, axis);
047: this .target = target;
048: layoutParams = new LayoutParameters(target, alignment);
049: }
050:
051: public void addLayoutComponent(final Component component,
052: final Object constraints) {
053: // Specified by LayoutManager2 but is not used
054: }
055:
056: public Dimension preferredLayoutSize(final Container target) {
057: checkTarget(target);
058: layoutParams.calculateLayoutParameters();
059: return layoutParams.preferredSize;
060: }
061:
062: public Dimension minimumLayoutSize(final Container target) {
063: checkTarget(target);
064: layoutParams.calculateLayoutParameters();
065: return layoutParams.minimumSize;
066: }
067:
068: public Dimension maximumLayoutSize(final Container target) {
069: checkTarget(target);
070: layoutParams.calculateLayoutParameters();
071: return layoutParams.maximumSize;
072: }
073:
074: public void addLayoutComponent(final String name,
075: final Component component) {
076: // Specified by LayoutManager but is not used
077: }
078:
079: public synchronized void invalidateLayout(final Container target) {
080: checkTarget(target);
081: layoutParams.invalidate();
082: }
083:
084: public synchronized float getLayoutAlignmentY(final Container target) {
085: checkTarget(target);
086: layoutParams.calculateLayoutParameters();
087: return layoutParams.alignmentY;
088: }
089:
090: public synchronized float getLayoutAlignmentX(final Container target) {
091: checkTarget(target);
092: layoutParams.calculateLayoutParameters();
093: return layoutParams.alignmentX;
094: }
095:
096: public void removeLayoutComponent(final Component component) {
097: // Specified by LayoutManager but is not used
098: }
099:
100: public void layoutContainer(final Container target) {
101: checkTarget(target);
102: layoutParams.layoutTarget();
103: }
104:
105: void setAxis(final int axis) {
106: layoutParams.setAlignment(axisToAlignment(target, axis));
107: }
108:
109: private int axisToAlignment(final Container target, final int axis)
110: throws AWTError {
111: int alignment = LayoutParameters.HORIZONTAL_ALIGNMENT;
112: if (axis == X_AXIS) {
113: alignment = LayoutParameters.HORIZONTAL_ALIGNMENT;
114: } else if (axis == Y_AXIS) {
115: alignment = LayoutParameters.VERTICAL_ALIGNMENT;
116: } else if (axis == LINE_AXIS) {
117: if (target != null) {
118: alignment = target.getComponentOrientation()
119: .isHorizontal() ? LayoutParameters.HORIZONTAL_ALIGNMENT
120: : LayoutParameters.VERTICAL_ALIGNMENT;
121: }
122: } else if (axis == PAGE_AXIS) {
123: if (target != null) {
124: alignment = target.getComponentOrientation()
125: .isHorizontal() ? LayoutParameters.VERTICAL_ALIGNMENT
126: : LayoutParameters.HORIZONTAL_ALIGNMENT;
127: }
128: } else {
129: throw new AWTError(Messages.getString("swing.err.04")); //$NON-NLS-1$
130: }
131: return alignment;
132: }
133:
134: /**
135: * Checks if we want to deal with the same target that was mentioned in constructor
136: *
137: * @param target
138: */
139: private void checkTarget(final Container target) {
140: if (this .target != target) {
141: throw new AWTError(Messages.getString("swing.err.02")); //$NON-NLS-1$
142: }
143: }
144: }
|