001: ////////////////////////////////////////////////////////////////////////////////
002: // DockBoundary.java
003: // A Rectangle that represents the bounds of the docked toolbars
004: // at a given edge.
005: //
006: //
007:
008: package ti.swing;
009:
010: import java.awt.*;
011: import javax.swing.*;
012: import java.util.*;
013:
014: // package access only...
015: class DockBoundary extends Rectangle implements SwingConstants {
016:
017: private ArrayList oToolBars = new ArrayList();
018: private Rectangle oDockableBoundary = null;
019:
020: private int oDockEdge = NORTH;
021: private int oOrientation = HORIZONTAL;
022: private int oSpacing = 0;
023:
024: private boolean oReverseWrap = false;
025:
026: public DockBoundary(int edge) {
027: super (0, 0, 0, 0);
028: oDockEdge = edge;
029: oDockableBoundary = new Rectangle(0, 0, 10, 10);
030:
031: oReverseWrap = (edge == SOUTH || edge == EAST);
032:
033: if (edge == NORTH || edge == SOUTH)
034: oOrientation = HORIZONTAL;
035: else
036: oOrientation = VERTICAL;
037: }
038:
039: public DockBoundary(int edge, int spacing) {
040: this (edge);
041: setSpacing(spacing);
042: }
043:
044: public void setSpacing(int spacing) {
045: if (spacing >= 0)
046: oSpacing = spacing;
047: else
048: oSpacing = 0;
049: }
050:
051: public int getSpacing() {
052: return oSpacing;
053: }
054:
055: public boolean isDockablePoint(Point p) {
056: return oDockableBoundary.contains(p);
057: }
058:
059: public int getDockIndex(Point p) {
060: int index = Integer.MAX_VALUE;
061:
062: for (int i = 0; i < oToolBars.size(); i++) {
063: JToolBar toolbar = (JToolBar) oToolBars.get(i);
064: if (toolbar.getBounds().contains(p)) {
065: return i;
066: }
067: }
068:
069: return index;
070: }
071:
072: public void setPosition(int x, int y, int length) {
073: super .setLocation(x, y);
074: setLength(length);
075: }
076:
077: public void setLocation(int x, int y) {
078: super .setLocation(x, y);
079: validate();
080: }
081:
082: public void setLength(int length) {
083: if (oOrientation == HORIZONTAL)
084: width = length;
085: else
086: height = length;
087: validate();
088: }
089:
090: public void validate() {
091: int length = 0;
092: if (oOrientation == HORIZONTAL)
093: length = width;
094: else
095: length = height;
096:
097: JToolBar[] bars = getToolBars();
098: int barDepth = getPreferredDepth();
099: int barLength = 0;
100: int totalBarDepth = barDepth;
101: int totalBarLength = 0;
102:
103: for (int i = 0; i < bars.length; i++) {
104: JToolBar toolbar = bars[i];
105:
106: barLength = getPreferredToolBarLength(toolbar);
107:
108: if (totalBarLength != 0)
109: totalBarLength += oSpacing;
110:
111: setToolBarBounds(toolbar, totalBarLength, totalBarDepth
112: - barDepth, Math.min(barLength, length), barDepth);
113:
114: totalBarLength += barLength;
115:
116: if (totalBarLength > length && i > 0) {
117: // Need to move the current toolbar to the
118: // next row and start totalling the length anew.
119: totalBarDepth += barDepth + oSpacing;
120: totalBarLength = 0;
121: setToolBarBounds(toolbar, totalBarLength, totalBarDepth
122: - barDepth, Math.min(barLength, length),
123: barDepth);
124: totalBarLength = barLength;
125: }
126: }
127:
128: if (oOrientation == HORIZONTAL)
129: height = totalBarDepth;
130: else
131: width = totalBarDepth;
132:
133: validateDockableBoundary();
134: }
135:
136: public int getDepth() {
137: if (oOrientation == HORIZONTAL)
138: return height;
139: else
140: return width;
141: }
142:
143: public void addToolBar(JToolBar toolbar) {
144: toolbar.setOrientation(oOrientation);
145: oToolBars.add(toolbar);
146: }
147:
148: public void addToolBar(JToolBar toolbar, Point dropPoint) {
149: toolbar.setOrientation(oOrientation);
150: oToolBars.add(toolbar);
151: }
152:
153: public void addToolBar(JToolBar toolbar, int depthIndex,
154: int lengthIndex) {
155: toolbar.setOrientation(oOrientation);
156: oToolBars.add(toolbar);
157: }
158:
159: public void addToolBar(JToolBar toolbar, int index) {
160: toolbar.setOrientation(oOrientation);
161: if (index < 0 || index > oToolBars.size())
162: oToolBars.add(toolbar);
163: else
164: oToolBars.add(index, toolbar);
165: }
166:
167: public void removeToolBar(JToolBar toolbar) {
168: oToolBars.remove(toolbar);
169: }
170:
171: public void removeComponent(Component component) {
172: oToolBars.remove(component);
173: }
174:
175: public JToolBar[] getToolBars() {
176: JToolBar[] bars = new JToolBar[oToolBars.size()];
177: bars = (JToolBar[]) oToolBars.toArray(bars);
178: return bars;
179: }
180:
181: public int getPreferredLength() {
182: int length = 0;
183:
184: for (int i = 0; i < oToolBars.size(); i++) {
185: JToolBar toolbar = (JToolBar) oToolBars.get(i);
186: int l = getPreferredToolBarLength(toolbar);
187: if (length == 0)
188: length = l;
189: else
190: length += oSpacing + l;
191: }
192:
193: return length;
194: }
195:
196: public int getPreferredDepth() {
197: int depth = 0;
198:
199: for (int i = 0; i < oToolBars.size(); i++) {
200: JToolBar toolbar = (JToolBar) oToolBars.get(i);
201: Dimension d = toolbar.getPreferredSize();
202: if (oOrientation == HORIZONTAL)
203: depth = Math.max(depth, d.height);
204: else
205: depth = Math.max(depth, d.width);
206: }
207:
208: return depth;
209: }
210:
211: private int getPreferredToolBarLength(JToolBar toolbar) {
212: Dimension d = toolbar.getPreferredSize();
213: if (oOrientation == HORIZONTAL)
214: return d.width;
215: else
216: return d.height;
217: }
218:
219: private void validateDockableBoundary() {
220: oDockableBoundary.setBounds(this );
221: if (oOrientation == HORIZONTAL)
222: oDockableBoundary.height += 10;
223: else
224: oDockableBoundary.width += 10;
225:
226: if (oDockEdge == SOUTH)
227: oDockableBoundary.y -= (10 + height);
228: else if (oDockEdge == EAST)
229: oDockableBoundary.x -= (10 + width);
230: }
231:
232: private void mirrorBounds(Rectangle r, int mirrorOffset) {
233: // Converts a bounding rectangle from top-down
234: // (or left-to-right) wrapping to bottom-up
235: // (or right-to-left) wrapping effectively
236: // mirroring the location about the line passing
237: // through mirrorOffset.
238:
239: int distanceFromMirror = 0;
240:
241: if (oOrientation == HORIZONTAL) {
242: // mirrorOffset represents the x-axis
243: distanceFromMirror = r.y - mirrorOffset;
244: int newY = r.y - (2 * distanceFromMirror) - r.height;
245: r.y = newY;
246: }
247:
248: else {
249: // mirrorOffset represents the y-axis
250: distanceFromMirror = r.x - mirrorOffset;
251: int newX = r.x - (2 * distanceFromMirror) - r.width;
252: r.x = newX;
253: }
254: }
255:
256: private void setToolBarBounds(JToolBar toolbar, int lengthOffset,
257: int depthOffset, int length, int depth) {
258: if (oOrientation == HORIZONTAL) {
259: toolbar.setBounds(x + lengthOffset, y + depthOffset,
260: length, depth);
261:
262: if (oReverseWrap) {
263: Rectangle r = toolbar.getBounds();
264: mirrorBounds(r, y);
265: toolbar.setBounds(r);
266: }
267: }
268:
269: else {
270: toolbar.setBounds(x + depthOffset, y + lengthOffset, depth,
271: length);
272:
273: if (oReverseWrap) {
274: Rectangle r = toolbar.getBounds();
275: mirrorBounds(r, x);
276: toolbar.setBounds(r);
277: }
278: }
279: }
280: }
|