001: /*
002: * Copyright (C) 2004 NNL Technology AB
003: * Visit www.infonode.net for information about InfoNode(R)
004: * products and how to contact NNL Technology AB.
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
019: * MA 02111-1307, USA.
020: */
021:
022: // $Id: StackableLayout.java,v 1.23 2005/12/04 13:46:03 jesper Exp $
023: package net.infonode.gui.layout;
024:
025: import net.infonode.gui.ComponentUtil;
026:
027: import java.awt.*;
028:
029: public class StackableLayout implements LayoutManager2 {
030: private Container container;
031: private Component component;
032: private boolean autoShowFirstComponent = true;
033: private boolean useSelectedComponentSize;
034:
035: public StackableLayout(Container container) {
036: this .container = container;
037: }
038:
039: public boolean usesSelectedComponentSize() {
040: return useSelectedComponentSize;
041: }
042:
043: public boolean isAutoShowFirstComponent() {
044: return autoShowFirstComponent;
045: }
046:
047: public void setAutoShowFirstComponent(boolean autoShowFirstComponent) {
048: this .autoShowFirstComponent = autoShowFirstComponent;
049: }
050:
051: public void setUseSelectedComponentSize(
052: boolean useSelectedComponentSize) {
053: if (this .useSelectedComponentSize != useSelectedComponentSize) {
054: this .useSelectedComponentSize = useSelectedComponentSize;
055: ComponentUtil.validate(container);
056: /*if (container instanceof JComponent)
057: ((JComponent)container).revalidate();
058: else
059: container.validate();*/
060: }
061: }
062:
063: public Dimension maximumLayoutSize(Container target) {
064: return LayoutUtil.add(LayoutUtil.getMinMaximumSize(target
065: .getComponents()), target.getInsets());
066: }
067:
068: public void invalidateLayout(Container target) {
069: }
070:
071: public float getLayoutAlignmentY(Container target) {
072: return 0;
073: }
074:
075: public float getLayoutAlignmentX(Container target) {
076: return 0;
077: }
078:
079: public void addLayoutComponent(Component comp, Object constraints) {
080: comp.setVisible(autoShowFirstComponent
081: && comp.getParent().getComponentCount() == 1);
082:
083: if (comp.isVisible()) {
084: component = comp;
085: }
086: }
087:
088: public void addLayoutComponent(String name, Component comp) {
089: addLayoutComponent(comp, null);
090: }
091:
092: public void removeLayoutComponent(Component comp) {
093: if (comp == component)
094: component = null;
095:
096: comp.setVisible(true);
097: }
098:
099: public Dimension preferredLayoutSize(Container parent) {
100: return LayoutUtil
101: .add(
102: useSelectedComponentSize ? component == null ? new Dimension(
103: 0, 0)
104: : component.getPreferredSize()
105: : LayoutUtil.getMaxPreferredSize(parent
106: .getComponents()), parent
107: .getInsets());
108: }
109:
110: public Dimension minimumLayoutSize(Container parent) {
111: return LayoutUtil.add(LayoutUtil.getMaxMinimumSize(parent
112: .getComponents()), parent.getInsets());
113: }
114:
115: public void layoutContainer(Container parent) {
116: Component[] c = parent.getComponents();
117: Insets parentInsets = parent.getInsets();
118: Dimension size = LayoutUtil.getInteriorSize(parent);
119:
120: for (int i = 0; i < c.length; i++) {
121: c[i].setBounds(parentInsets.left, parentInsets.top,
122: size.width, size.height);
123: }
124: }
125:
126: public Component getVisibleComponent() {
127: return component;
128: }
129:
130: public void showComponent(Component c) {
131: final Component oldComponent = component;
132:
133: if (oldComponent == c)
134: return;
135:
136: component = c;
137:
138: boolean hasFocus = oldComponent != null
139: && LayoutUtil.isDescendingFrom(KeyboardFocusManager
140: .getCurrentKeyboardFocusManager()
141: .getFocusOwner(), oldComponent);
142:
143: if (oldComponent != null)
144: oldComponent.setVisible(false);
145:
146: if (component != null) {
147: component.setVisible(true);
148:
149: if (hasFocus)
150: //component.requestFocusInWindow();
151: ComponentUtil.smartRequestFocus(component);
152: }
153:
154: /* if (oldComponent != null) {
155: // oldComponent.setVisible(false);
156: SwingUtilities.invokeLater(new Runnable() {
157: public void run() {
158: if (oldComponent.getParent() == container && oldComponent != component) {
159: oldComponent.setVisible(false);
160: }
161:
162: // FocusUtil.unblockFocusChanges();
163: }
164: });
165: }*/
166: }
167:
168: }
|