001: /************************************************************************
002: Copyright 2004 Emil Kirschner
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: ************************************************************************/package com.thekirschners.uitoys.autoui;
016:
017: import javax.swing.*;
018: import java.awt.*;
019:
020: class SortableUIElement implements Comparable {
021: private enum SeparatorType {
022: NONE, BEFORE, AFTER
023: };
024:
025: public SortableUIElement(int order, int orderBase,
026: JComponent component, GridBagConstraints constraints) {
027: this .order = order;
028: this .orderBase = orderBase;
029: this .component = component;
030: this .componentConstraints = constraints;
031: }
032:
033: public int compareTo(Object o) {
034: SortableUIElement other = (SortableUIElement) o;
035: if (orderBase < other.orderBase)
036: return -1;
037: else if (orderBase > other.orderBase)
038: return 1;
039: else {
040: if (order < other.order)
041: return -1;
042: else if (order == other.order)
043: return 0;
044: else
045: return 1;
046: }
047: }
048:
049: public void setBeforeLabel(JLabel label,
050: GridBagConstraints constraints) {
051: beforeLabel = label;
052: beforeLabelConstraints = constraints;
053: }
054:
055: public void setAfterLabel(JLabel label,
056: GridBagConstraints constraints) {
057: afterLabel = label;
058: afterLabelConstraints = constraints;
059: }
060:
061: public void applyUIElementToContainer(Container container,
062: GridBagLayout layout) {
063: if (separator == SeparatorType.BEFORE) {
064: JSeparator sep = new JSeparator(JSeparator.HORIZONTAL);
065: GridBagConstraints gbc = new GridBagConstraints();
066: gbc.fill = GridBagConstraints.HORIZONTAL;
067: gbc.gridwidth = GridBagConstraints.REMAINDER;
068: gbc.gridheight = 1;
069: layout.setConstraints(sep, gbc);
070: container.add(sep);
071: }
072:
073: if (beforeLabel != null) {
074: layout.setConstraints(beforeLabel, beforeLabelConstraints);
075: container.add(beforeLabel);
076: }
077:
078: layout.setConstraints(component, componentConstraints);
079: container.add(component);
080:
081: if (afterLabel != null) {
082: layout.setConstraints(afterLabel, afterLabelConstraints);
083: container.add(afterLabel);
084: }
085:
086: if (separator == SeparatorType.AFTER) {
087: JSeparator sep = new JSeparator(JSeparator.HORIZONTAL);
088: GridBagConstraints gbc = new GridBagConstraints();
089: gbc.fill = GridBagConstraints.HORIZONTAL;
090: gbc.gridwidth = GridBagConstraints.REMAINDER;
091: gbc.gridheight = 1;
092: layout.setConstraints(sep, gbc);
093: container.add(sep);
094: }
095: }
096:
097: public void separateBefore() {
098: separator = SeparatorType.BEFORE;
099: }
100:
101: public void separateAfter() {
102: separator = SeparatorType.AFTER;
103: }
104:
105: private int order;
106: private int orderBase;
107: private JComponent component;
108: private GridBagConstraints componentConstraints;
109: private JLabel beforeLabel = null;
110: private GridBagConstraints beforeLabelConstraints = null;
111: private JLabel afterLabel = null;
112: private GridBagConstraints afterLabelConstraints = null;
113: private SeparatorType separator = SeparatorType.NONE;
114: }
|