001: ////////////////////////////////////////////////////////////////////////////////
002: // DockLayout.java
003: // Layout Manager to control positions of docked toolbars.
004: //
005: //
006: //
007:
008: package ti.swing;
009:
010: import java.awt.*;
011: import java.awt.event.*;
012: import java.util.*;
013: import javax.swing.*;
014: import javax.swing.border.*;
015: import javax.swing.event.*;
016: import java.beans.PropertyChangeEvent;
017: import java.beans.PropertyChangeListener;
018:
019: public class DockLayout implements LayoutManager {
020: private boolean dragOutEnabled = true;
021:
022: private DockBoundary oNorth = null;
023: private DockBoundary oSouth = null;
024: private DockBoundary oEast = null;
025: private DockBoundary oWest = null;
026: private Component oCenter = null;
027:
028: public static final int VERTICAL = SwingConstants.VERTICAL;
029: public static final int HORIZONTAL = SwingConstants.HORIZONTAL;
030:
031: public static final String NORTH = BorderLayout.NORTH;
032: public static final String SOUTH = BorderLayout.SOUTH;
033: public static final String EAST = BorderLayout.EAST;
034: public static final String WEST = BorderLayout.WEST;
035: public static final String CENTER = BorderLayout.CENTER;
036:
037: private Container oTargetContainer = null;
038:
039: private int oVerticalSpacing = 0;
040: private int oHorizontalSpacing = 0;
041:
042: private DraggingWindow oDraggingWindow = null;
043: private static final String DRAG_HANDLER_KEY = "DockLayout.dragHandler";
044:
045: public DockLayout(Container target, boolean dragOutEnabled) {
046: oTargetContainer = target;
047: this .dragOutEnabled = dragOutEnabled;
048:
049: oNorth = new DockBoundary(SwingConstants.NORTH, 2);
050: oSouth = new DockBoundary(SwingConstants.SOUTH, 2);
051: oEast = new DockBoundary(SwingConstants.EAST, 2);
052: oWest = new DockBoundary(SwingConstants.WEST, 2);
053: }
054:
055: public void setHorizontalSpacing(int spacing) {
056: oHorizontalSpacing = spacing;
057: }
058:
059: public void setVerticalSpacing(int spacing) {
060: oVerticalSpacing = spacing;
061: }
062:
063: public int getHorizontalSpacing() {
064: return oHorizontalSpacing;
065: }
066:
067: public int getVerticalSpacing() {
068: return oVerticalSpacing;
069: }
070:
071: public void addLayoutComponent(String constraints,
072: Component component) {
073: addLayoutComponent(component, constraints);
074: }
075:
076: public void addLayoutComponent(Component component,
077: Object constraints) {
078: synchronized (component.getTreeLock()) {
079: if (component instanceof JToolBar) {
080: JToolBar toolbar = (JToolBar) component;
081:
082: ToolBarHandler handler = (ToolBarHandler) toolbar
083: .getClientProperty(DRAG_HANDLER_KEY);
084: if (handler == null) {
085: handler = new ToolBarHandler(toolbar);
086: toolbar
087: .putClientProperty(DRAG_HANDLER_KEY,
088: handler);
089: }
090:
091: int dockIndex = handler.getDockIndex();
092:
093: if (constraints != null) {
094: String s = constraints.toString();
095: if (s.equals(NORTH))
096: oNorth.addToolBar(toolbar, dockIndex);
097: else if (s.equals(SOUTH))
098: oSouth.addToolBar(toolbar, dockIndex);
099: else if (s.equals(EAST))
100: oEast.addToolBar(toolbar, dockIndex);
101: else if (s.equals(WEST))
102: oWest.addToolBar(toolbar, dockIndex);
103: }
104:
105: else
106: oNorth.addToolBar(toolbar);
107: }
108:
109: else
110: oCenter = component;
111: }
112:
113: }
114:
115: public void removeLayoutComponent(Component component) {
116: oNorth.removeComponent(component);
117: oSouth.removeComponent(component);
118: oEast.removeComponent(component);
119: oWest.removeComponent(component);
120: if (component == oCenter)
121: oCenter = null;
122: }
123:
124: public void layoutContainer(Container target) {
125: synchronized (target.getTreeLock()) {
126: int width = target.getWidth();
127: int height = target.getHeight();
128:
129: Insets insets = target.getInsets();
130: int top = insets.top;
131: int bottom = height - insets.bottom;
132: int left = insets.left;
133: int right = width - insets.right;
134:
135: oNorth.setPosition(left, top, width);
136: oSouth.setPosition(left, bottom, width);
137:
138: int northHeight = oNorth.getDepth();
139: int southHeight = oSouth.getDepth();
140: if (northHeight > 0)
141: northHeight += oVerticalSpacing;
142: if (southHeight > 0)
143: southHeight += oVerticalSpacing;
144: height = (bottom - top) - northHeight - southHeight;
145: top += northHeight;
146:
147: oWest.setPosition(left, top, height);
148: oEast.setPosition(right, top, height);
149:
150: int eastWidth = oEast.getDepth();
151: int westWidth = oWest.getDepth();
152: if (eastWidth > 0)
153: eastWidth += oHorizontalSpacing;
154: if (westWidth > 0)
155: westWidth += oHorizontalSpacing;
156: width = (right - left) - eastWidth - westWidth;
157: left += westWidth;
158:
159: if (oCenter != null) {
160: oCenter.setBounds(left, top, width, height);
161: }
162: }
163: }
164:
165: public Dimension preferredLayoutSize(Container target) {
166: layoutContainer(target);
167:
168: int width = 0;
169: int height = 0;
170:
171: int northHeight = oNorth.getDepth();
172: int southHeight = oSouth.getDepth();
173: if (northHeight > 0)
174: northHeight += oVerticalSpacing;
175: if (southHeight > 0)
176: southHeight += oVerticalSpacing;
177:
178: height += northHeight;
179: height += southHeight;
180:
181: int eastWidth = oEast.getDepth();
182: int westWidth = oWest.getDepth();
183: if (eastWidth > 0)
184: eastWidth += oHorizontalSpacing;
185: if (westWidth > 0)
186: westWidth += oHorizontalSpacing;
187:
188: width += eastWidth;
189: width += westWidth;
190:
191: if (oCenter != null) {
192: width += oCenter.getWidth();
193: height += oCenter.getHeight();
194: }
195:
196: return new Dimension(width, height);
197: // return new Dimension(0, 0);
198: }
199:
200: public Dimension minimumLayoutSize(Container target) {
201: return preferredLayoutSize(target);
202: }
203:
204: private class ToolBarHandler extends WindowAdapter implements
205: MouseInputListener, PropertyChangeListener {
206: private int nDockIndex = Integer.MAX_VALUE;
207: private int nDockEdge = SwingConstants.NORTH;
208: private JToolBar nToolBar = null;
209: private JDialog nFloatFrame = null;
210:
211: public ToolBarHandler(JToolBar toolbar) {
212: nToolBar = toolbar;
213:
214: MouseListener[] ml = nToolBar.getMouseListeners();
215: for (int i = 0; i < ml.length; i++) {
216: nToolBar.removeMouseListener(ml[i]);
217: }
218:
219: MouseMotionListener[] mml = nToolBar
220: .getMouseMotionListeners();
221: for (int i = 0; i < mml.length; i++) {
222: nToolBar.removeMouseMotionListener(mml[i]);
223: }
224:
225: if (dragOutEnabled) {
226: nToolBar.addMouseListener(this );
227: nToolBar.addMouseMotionListener(this );
228: nToolBar.addPropertyChangeListener("UI", this );
229:
230: Window w = SwingUtilities
231: .getWindowAncestor(oTargetContainer);
232: Frame fr = null;
233: if (w instanceof Frame)
234: fr = (Frame) w;
235: nFloatFrame = new JDialog(fr);
236: nFloatFrame
237: .setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
238: nFloatFrame.addWindowListener(this );
239: nFloatFrame.getContentPane().setLayout(
240: new BorderLayout());
241: nFloatFrame.setTitle(nToolBar.getName());
242: nFloatFrame.setResizable(false);
243: }
244: }
245:
246: public int getDockIndex() {
247: return nDockIndex;
248: }
249:
250: public void mouseClicked(MouseEvent me) {
251: }
252:
253: public void mouseEntered(MouseEvent me) {
254: }
255:
256: public void mouseExited(MouseEvent me) {
257: }
258:
259: public void mouseMoved(MouseEvent me) {
260: }
261:
262: public void mousePressed(MouseEvent me) {
263: }
264:
265: public void mouseDragged(MouseEvent me) {
266: if (oDraggingWindow == null) {
267: Window ancestorWindow = SwingUtilities
268: .getWindowAncestor(nToolBar);
269: oDraggingWindow = new DraggingWindow(ancestorWindow);
270: }
271:
272: Point p = me.getPoint();
273: p = SwingUtilities.convertPoint(nToolBar, p,
274: oTargetContainer);
275:
276: int orient = HORIZONTAL;
277: boolean dockable = false;
278:
279: if (oNorth.isDockablePoint(p)) {
280: dockable = true;
281: }
282:
283: else if (oSouth.isDockablePoint(p)) {
284: dockable = true;
285: }
286:
287: else if (oEast.isDockablePoint(p)) {
288: dockable = true;
289: orient = VERTICAL;
290: }
291:
292: else if (oWest.isDockablePoint(p)) {
293: dockable = true;
294: orient = VERTICAL;
295: }
296:
297: SwingUtilities.convertPointToScreen(p, oTargetContainer);
298: oDraggingWindow
299: .presentWindow(p, nToolBar, orient, dockable);
300: }
301:
302: public void mouseReleased(MouseEvent me) {
303: if (oDraggingWindow == null)
304: return;
305:
306: oDraggingWindow.hideWindow();
307: oDraggingWindow = null;
308:
309: Point p = me.getPoint();
310: Window w = SwingUtilities.getWindowAncestor(nToolBar);
311: p = SwingUtilities.convertPoint(nToolBar, p,
312: oTargetContainer);
313:
314: if (oNorth.isDockablePoint(p)) {
315: nDockIndex = oNorth.getDockIndex(p);
316: nDockEdge = SwingConstants.NORTH;
317: }
318:
319: else if (oSouth.isDockablePoint(p)) {
320: nDockIndex = oSouth.getDockIndex(p);
321: nDockEdge = SwingConstants.SOUTH;
322: }
323:
324: else if (oEast.isDockablePoint(p)) {
325: nDockIndex = oEast.getDockIndex(p);
326: nDockEdge = SwingConstants.EAST;
327: }
328:
329: else if (oWest.isDockablePoint(p)) {
330: nDockIndex = oWest.getDockIndex(p);
331: nDockEdge = SwingConstants.WEST;
332: }
333:
334: oTargetContainer.remove(nToolBar);
335:
336: if (oNorth.isDockablePoint(p)) {
337: oTargetContainer.add(nToolBar, NORTH);
338: }
339:
340: else if (oSouth.isDockablePoint(p)) {
341: oTargetContainer.add(nToolBar, SOUTH);
342: }
343:
344: else if (oEast.isDockablePoint(p)) {
345: oTargetContainer.add(nToolBar, EAST);
346: }
347:
348: else if (oWest.isDockablePoint(p)) {
349: oTargetContainer.add(nToolBar, WEST);
350: }
351:
352: else if (dragOutEnabled) {
353: SwingUtilities
354: .convertPointToScreen(p, oTargetContainer);
355: nToolBar.setOrientation(HORIZONTAL);
356: nFloatFrame.getContentPane().add(nToolBar,
357: BorderLayout.CENTER);
358: nFloatFrame.pack();
359:
360: p.x -= nFloatFrame.getWidth() / 2;
361: p.y -= nFloatFrame.getHeight() / 2;
362:
363: nFloatFrame.setLocation(p);
364: nFloatFrame.setVisible(true);
365: }
366:
367: //oTargetContainer.validate();
368: oTargetContainer.invalidate();
369: oTargetContainer.repaint();
370: }
371:
372: public void windowClosing(WindowEvent we) {
373: nFloatFrame.setVisible(false);
374: nFloatFrame.getContentPane().remove(nToolBar);
375:
376: String edge = NORTH;
377: if (nDockEdge == SwingConstants.SOUTH)
378: edge = SOUTH;
379: else if (nDockEdge == SwingConstants.EAST)
380: edge = EAST;
381: else if (nDockEdge == SwingConstants.WEST)
382: edge = WEST;
383:
384: oTargetContainer.add(nToolBar, edge);
385: //oTargetContainer.validate();
386: oTargetContainer.invalidate();
387: oTargetContainer.repaint();
388: }
389:
390: public void propertyChange(PropertyChangeEvent pce) {
391: MouseListener[] ml = nToolBar.getMouseListeners();
392: for (int i = 0; i < ml.length; i++) {
393: nToolBar.removeMouseListener(ml[i]);
394: }
395:
396: MouseMotionListener[] mml = nToolBar
397: .getMouseMotionListeners();
398: for (int i = 0; i < mml.length; i++) {
399: nToolBar.removeMouseMotionListener(mml[i]);
400: }
401:
402: if (dragOutEnabled) {
403: nToolBar.addMouseListener(this );
404: nToolBar.addMouseMotionListener(this );
405: }
406: }
407:
408: }
409:
410: private class DraggingWindow extends JWindow {
411: private Border floatBorder = null;
412: private Border dockBorder = null;
413: private Color floatColor = null;
414: private Color dockColor = null;
415: private JPanel bgPanel = null;
416:
417: public DraggingWindow(Window ancestor) {
418: super (ancestor);
419:
420: Color floatColor = UIManager
421: .getColor("ToolBar.floatingForeground");
422: Color dockColor = UIManager
423: .getColor("ToolBar.dockingForeground");
424: if (floatColor == null)
425: floatColor = Color.darkGray;
426: if (dockColor == null)
427: dockColor = Color.yellow;
428:
429: floatBorder = BorderFactory.createLineBorder(floatColor, 3);
430: dockBorder = BorderFactory.createLineBorder(dockColor, 3);
431:
432: bgPanel = new JPanel();
433: bgPanel.setOpaque(true);
434: floatColor = UIManager
435: .getColor("ToolBar.floatingBackground");
436: dockColor = UIManager.getColor("ToolBar.dockingBackground");
437: if (floatColor == null)
438: floatColor = bgPanel.getBackground();
439: if (dockColor == null)
440: dockColor = bgPanel.getBackground();
441:
442: getContentPane().setLayout(new BorderLayout());
443: getContentPane().add(bgPanel, BorderLayout.CENTER);
444: }
445:
446: public void presentWindow(Point screenPoint, JToolBar toolbar,
447: int orientation, boolean docking) {
448: int tbOrientation = toolbar.getOrientation();
449: if (orientation == tbOrientation) {
450: Dimension d = toolbar.getPreferredSize();
451: setSize(d.width, d.height);
452: }
453:
454: else
455: setSize(toolbar.getHeight(), toolbar.getWidth());
456:
457: bgPanel.setBorder(docking ? dockBorder : floatBorder);
458: bgPanel.setBackground(docking ? dockColor : floatColor);
459:
460: setLocation(screenPoint.x - getWidth() / 2, screenPoint.y
461: - getHeight() / 2);
462:
463: validate();
464: if (!isVisible())
465: setVisible(true);
466: }
467:
468: public void hideWindow() {
469: setVisible(false);
470: }
471:
472: }
473:
474: }
475:
476: /*
477: * Local Variables:
478: * tab-width: 2
479: * indent-tabs-mode: nil
480: * mode: java
481: * c-indentation-style: java
482: * c-basic-offset: 2
483: * eval: (c-set-offset 'substatement-open '0)
484: * eval: (c-set-offset 'case-label '+)
485: * eval: (c-set-offset 'inclass '+)
486: * eval: (c-set-offset 'inline-open '0)
487: * End:
488: */
|