001: // THIS SOFTWARE IS PROVIDED BY SOFTARIS PTY.LTD. AND OTHER METABOSS
002: // CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
003: // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
004: // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTARIS PTY.LTD.
005: // OR OTHER METABOSS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
006: // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
007: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
008: // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
009: // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
010: // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
011: // EVEN IF SOFTARIS PTY.LTD. OR OTHER METABOSS CONTRIBUTORS ARE ADVISED OF THE
012: // POSSIBILITY OF SUCH DAMAGE.
013: //
014: // Copyright 2000-2005 © Softaris Pty.Ltd. All Rights Reserved.
015: package com.metaboss.applications.designstudio.components;
016:
017: import java.awt.BorderLayout;
018: import java.awt.Component;
019: import java.awt.Container;
020: import java.awt.Dimension;
021: import java.awt.Insets;
022: import java.util.ArrayList;
023:
024: import javax.swing.SwingConstants;
025:
026: public class DockLayout extends BorderLayout {
027: private ArrayList north = new ArrayList(1);
028: private ArrayList south = new ArrayList(1);
029: private ArrayList east = new ArrayList(1);
030: private ArrayList west = new ArrayList(1);
031: private Component center = null;
032:
033: public static final int VERTICAL = SwingConstants.VERTICAL;
034: public static final int HORIZONTAL = SwingConstants.HORIZONTAL;
035:
036: public DockLayout() {
037: }
038:
039: public void addLayoutComponent(Component c, Object con) {
040: synchronized (c.getTreeLock()) {
041: if (con != null) {
042: String s = con.toString();
043: if (s.equals(NORTH))
044: north.add(c);
045: else if (s.equals(SOUTH))
046: south.add(c);
047: else if (s.equals(EAST))
048: east.add(c);
049: else if (s.equals(WEST))
050: west.add(c);
051: else if (s.equals(CENTER))
052: center = c;
053: }
054: }
055: }
056:
057: public void removeLayoutComponent(Component c) {
058: north.remove(c);
059: south.remove(c);
060: east.remove(c);
061: west.remove(c);
062: if (c == center) {
063: center = null;
064: }
065: }
066:
067: public void layoutContainer(Container target) {
068: synchronized (target.getTreeLock()) {
069: Insets insets = target.getInsets();
070: int top = insets.top;
071: int bottom = target.getHeight() - insets.bottom;
072: int left = insets.left;
073: int right = target.getWidth() - insets.right;
074: int northHeight = getPreferredDimension(north).height;
075: int southHeight = getPreferredDimension(south).height;
076: int eastWidth = getPreferredDimension(east).width;
077: int westWidth = getPreferredDimension(west).width;
078: placeComponents(north, left, top, right - left,
079: northHeight, HORIZONTAL);
080: top += (northHeight + getVgap());
081: placeComponents(south, left, bottom - southHeight, right
082: - left, southHeight, HORIZONTAL);
083: bottom -= (southHeight + getVgap());
084: placeComponents(east, right - eastWidth, top, eastWidth,
085: bottom - top, VERTICAL);
086: right -= (eastWidth + getHgap());
087: placeComponents(west, left, top, westWidth, bottom - top,
088: VERTICAL);
089: left += (westWidth + getHgap());
090: if (center != null) {
091: center.setBounds(left, top, right - left, bottom - top);
092: }
093: }
094: }
095:
096: // Returns the ideal width for a vertically oriented toolbar
097: // and the ideal height for a horizontally oriented tollbar:
098: private Dimension getPreferredDimension(ArrayList comps) {
099: int w = 0, h = 0;
100: for (int i = 0; i < comps.size(); i++) {
101: Component c = (Component) (comps.get(i));
102: Dimension d = c.getPreferredSize();
103: w = Math.max(w, d.width);
104: h = Math.max(h, d.height);
105: }
106: return new Dimension(w, h);
107: }
108:
109: private void placeComponents(ArrayList comps, int x, int y, int w,
110: int h, int orientation) {
111: int offset = 0;
112: Component c = null;
113: if (orientation == HORIZONTAL) {
114: offset = x;
115: for (int i = 0; i < comps.size(); i++) {
116: c = (Component) (comps.get(i));
117: int cwidth = c.getPreferredSize().width;
118: if (i == comps.size() - 1)
119: cwidth = w - offset;
120: c.setBounds(x + offset, y, cwidth, h);
121: offset += cwidth;
122: }
123: } else {
124: for (int i = 0; i < comps.size(); i++) {
125: c = (Component) (comps.get(i));
126: int cheight = c.getPreferredSize().height;
127: if (i == comps.size() - 1)
128: cheight = h - offset;
129: c.setBounds(x, y + offset, w, cheight);
130: offset += cheight;
131: }
132: }
133: }
134: }
|