001: /**
002: * Copyright 2004 Carlos Silva A.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: *
016: */package com.csa.lgantt.view;
017:
018: import java.awt.*;
019: import java.util.*;
020: import javax.swing.*;
021:
022: public class LabeledPairLayout implements LayoutManager {
023: public static String LABEL = "label";
024: public static String FIELD = "field";
025:
026: Vector labels = new Vector();
027: Vector fields = new Vector();
028:
029: int yGap = 2;
030: int xGap = 2;
031:
032: public void addLayoutComponent(String s, Component c) {
033: if (s.equals(LABEL)) {
034: labels.addElement(c);
035: } else {
036: fields.addElement(c);
037: }
038: }
039:
040: public void layoutContainer(Container c) {
041: Insets insets = c.getInsets();
042:
043: int labelWidth = 0;
044: Enumeration labelIter = labels.elements();
045: while (labelIter.hasMoreElements()) {
046: JComponent comp = (JComponent) labelIter.nextElement();
047: labelWidth = Math.max(labelWidth,
048: comp.getPreferredSize().width);
049: }
050:
051: int yPos = insets.top;
052:
053: Enumeration fieldIter = fields.elements();
054: labelIter = labels.elements();
055: while (labelIter.hasMoreElements()
056: && fieldIter.hasMoreElements()) {
057: JComponent label = (JComponent) labelIter.nextElement();
058: JComponent field = (JComponent) fieldIter.nextElement();
059: int height = Math.max(label.getPreferredSize().height,
060: field.getPreferredSize().height);
061: label.setBounds(insets.left, yPos, labelWidth, height);
062: field.setBounds(insets.left + labelWidth + xGap, yPos, c
063: .getSize().width
064: - (labelWidth + xGap + insets.left + insets.right),
065: height);
066: yPos += (height + yGap);
067: }
068:
069: }
070:
071: public Dimension minimumLayoutSize(Container c) {
072: Insets insets = c.getInsets();
073:
074: int labelWidth = 0;
075: Enumeration labelIter = labels.elements();
076: while (labelIter.hasMoreElements()) {
077: JComponent comp = (JComponent) labelIter.nextElement();
078: labelWidth = Math.max(labelWidth,
079: comp.getPreferredSize().width);
080: }
081:
082: int yPos = insets.top;
083:
084: labelIter = labels.elements();
085: Enumeration fieldIter = fields.elements();
086: while (labelIter.hasMoreElements()
087: && fieldIter.hasMoreElements()) {
088: JComponent label = (JComponent) labelIter.nextElement();
089: JComponent field = (JComponent) fieldIter.nextElement();
090: int height = Math.max(label.getPreferredSize().height,
091: field.getPreferredSize().height);
092: yPos += (height + yGap);
093: }
094: return new Dimension(labelWidth * 3, yPos);
095: }
096:
097: public Dimension preferredLayoutSize(Container c) {
098: Dimension d = minimumLayoutSize(c);
099: //d.width *= 2;
100: return d;
101: }
102:
103: public void removeLayoutComponent(Component c) {
104: }
105:
106: }
|