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: WindowDragger.java,v 1.30 2007/01/28 21:25:10 jesper Exp $
023: package net.infonode.docking;
024:
025: import net.infonode.docking.drag.DockingWindowDragger;
026: import net.infonode.docking.internalutil.DropAction;
027:
028: import javax.swing.*;
029: import java.awt.*;
030: import java.awt.dnd.DragSource;
031: import java.awt.event.MouseEvent;
032:
033: /**
034: * @author $Author: jesper $
035: * @version $Revision: 1.30 $
036: */
037: class WindowDragger implements DockingWindowDragger {
038: private DockingWindow dragWindow;
039: private DropAction dropAction;
040: private RootWindow rootWindow;
041:
042: WindowDragger(DockingWindow dragWindow) {
043: this (dragWindow, dragWindow.getRootWindow());
044: }
045:
046: WindowDragger(DockingWindow dragWindow, RootWindow rootWindow) {
047: this .dragWindow = dragWindow;
048: this .rootWindow = rootWindow;
049:
050: rootWindow.internalStartDrag(dragWindow);
051: }
052:
053: public DockingWindow getDragWindow() {
054: return dragWindow;
055: }
056:
057: public RootWindow getDropTarget() {
058: return rootWindow;
059: }
060:
061: void undoDrag(DropAction newAction) {
062: if (dropAction != null) {
063: dropAction.clear(dragWindow, newAction);
064: dropAction = null;
065: }
066: }
067:
068: private void stopDrag() {
069: rootWindow.stopDrag();
070: }
071:
072: public void abortDrag() {
073: stopDrag();
074: undoDrag(null);
075: }
076:
077: public void dropWindow(MouseEvent mouseEvent) {
078: stopDrag();
079:
080: if (dropAction == null)
081: dropAction = dragWindow.getDefaultDropAction();
082:
083: if (dragWindow != null && dropAction != null) {
084: dropAction.execute(dragWindow, mouseEvent);
085:
086: Component c = dragWindow.getTopLevelAncestor();
087: if (c != null && c instanceof Window)
088: ((Window) c).toFront();
089:
090: FocusManager.focusWindow(dragWindow);
091: }
092: }
093:
094: public void dragWindow(MouseEvent mouseEvent) {
095: JRootPane root = rootWindow.getCurrentDragRootPane();
096: Point point = SwingUtilities.convertPoint(
097: (Component) mouseEvent.getSource(), mouseEvent
098: .getPoint(), root);
099:
100: if (root != rootWindow.getRootPane() && !root.contains(point)) {
101: rootWindow.setCurrentDragRootPane(null);
102: root = rootWindow.getCurrentDragRootPane();
103: point = SwingUtilities.convertPoint((Component) mouseEvent
104: .getSource(), mouseEvent.getPoint(), root);
105: }
106:
107: DockingWindow dropWindow = getDeepestWindowAt(root, point.x,
108: point.y);
109:
110: while (dropWindow != null
111: && dropWindow.getWindowParent() != null
112: && !(dropWindow/*.getWindowParent()*/instanceof FloatingWindow)) {
113: Point p2 = SwingUtilities.convertPoint(root, point,
114: dropWindow.getWindowParent());
115:
116: if (!dropWindow.getWindowParent().contains(p2))
117: break;
118:
119: dropWindow = dropWindow.getWindowParent();
120: }
121:
122: DropAction da = dropWindow != null ? dropWindow.acceptDrop(
123: SwingUtilities.convertPoint(root, point, dropWindow),
124: dragWindow) : null;
125:
126: undoDrag(da);
127:
128: Cursor cursor = DragSource.DefaultMoveDrop;
129: if (da == null)
130: cursor = DragSource.DefaultMoveNoDrop;
131:
132: if (dropWindow == null
133: && dragWindow.getWindowProperties()
134: .getUndockOnDropEnabled())
135: cursor = DragSource.DefaultMoveDrop;
136:
137: rootWindow.setDragCursor(cursor);
138: rootWindow.setDragText(da == null || da.showTitle() ? point
139: : null, dragWindow.getTitle());
140: dropAction = da;
141:
142: if (dropAction == null)
143: rootWindow.setDragRectangle(null);
144: }
145:
146: private DockingWindow getDeepestWindowAt(Component component,
147: int x, int y) {
148: if (component == null || !component.isVisible()
149: || !component.contains(x, y))
150: return null;
151:
152: if (component instanceof Container) {
153: Component[] components = ((Container) component)
154: .getComponents();
155:
156: for (int i = 0; i < components.length; i++) {
157: DockingWindow w = getDeepestWindowAt(components[i], x
158: - components[i].getX(), y
159: - components[i].getY());
160:
161: if (w != null)
162: return w;
163: }
164: }
165:
166: if (component instanceof DockingWindow) {
167: DockingWindow w = (DockingWindow) component;
168: return w.getRootWindow() == rootWindow ? w : null;
169: } else
170: return null;
171: }
172: }
|