001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019: package org.netbeans.modules.soa.mappercore;
020:
021: import java.awt.Point;
022: import java.awt.datatransfer.Transferable;
023: import java.awt.dnd.DragSourceDropEvent;
024: import java.awt.dnd.DropTargetDragEvent;
025: import java.awt.dnd.DropTargetDropEvent;
026: import java.awt.event.ActionEvent;
027: import java.awt.event.ActionListener;
028: import java.awt.event.MouseEvent;
029: import java.awt.event.MouseListener;
030: import java.awt.event.MouseMotionListener;
031: import javax.swing.JComponent;
032: import javax.swing.JTree;
033: import javax.swing.Timer;
034: import javax.swing.tree.TreePath;
035: import org.netbeans.modules.soa.mappercore.dnd.*;
036: import org.netbeans.modules.soa.mappercore.model.GraphItem;
037: import org.netbeans.modules.soa.mappercore.utils.Utils;
038:
039: /**
040: *
041: * @author anjeleevich
042: */
043: public abstract class AbstractMapperEventHandler extends
044: MapperPropertyAccess implements MouseMotionListener,
045: MouseListener, DnDHandler, DnDConstants {
046:
047: private JComponent component;
048: private DnDSupport dndSupport;
049: /** Swing Timer for expand node's parent with delay time. */
050: private Timer timer;
051: private Point oldPoint;
052:
053: public AbstractMapperEventHandler(Mapper mapper,
054: JComponent component) {
055: super (mapper);
056: this .component = component;
057: dndSupport = new DnDSupport(component, this );
058: component.addMouseListener(this );
059: component.addMouseMotionListener(this );
060: }
061:
062: public JComponent getComponent() {
063: return component;
064: }
065:
066: public abstract void mousePressed(MouseEvent e);
067:
068: public abstract void mouseReleased(MouseEvent e);
069:
070: public void mouseClicked(MouseEvent e) {
071: }
072:
073: public void mouseEntered(MouseEvent e) {
074: }
075:
076: public void mouseExited(MouseEvent e) {
077: }
078:
079: public abstract void mouseDragged(MouseEvent e);
080:
081: public void mouseMoved(MouseEvent e) {
082: }
083:
084: public void drag(JComponent component, DropTargetDragEvent dtde) {
085: autoExpand(component, dtde);
086: setSelected(component, dtde);
087: setMoveOnTop(component, dtde);
088: boolean processed = false;
089: if (!processed) {
090: processed = getLinkTool().drag(component, dtde);
091: }
092: if (!processed) {
093: processed = getMoveTool().drag(component, dtde);
094: }
095:
096: if (!processed) {
097: dtde.rejectDrag();
098: }
099: }
100:
101: public void drop(JComponent component, DropTargetDropEvent dtde) {
102: boolean processed = false;
103: if (!processed) {
104: processed = getLinkTool().drop(component, dtde);
105: }
106: if (!processed) {
107: processed = getMoveTool().drop(component, dtde);
108: }
109: getMapper().setSelectedDndPath(null);
110:
111: removeTimer();
112: }
113:
114: public void dragDone(JComponent component, DragSourceDropEvent dsde) {
115: getLinkTool().done();
116: getMoveTool().dragDone();
117: getMapper().setSelectedDndPath(null);
118: removeTimer();
119: }
120:
121: protected void startDrag(MouseEvent event,
122: Transferable transferable, int action) {
123: dndSupport.startDrag(event, transferable, action);
124: }
125:
126: public void dragExit(JComponent component) {
127: removeTimer();
128: Canvas canvas = getCanvas();
129: RightTree rightTree = getRightTree();
130: if (component == canvas || component == rightTree) {
131: canvas.getMapper().setSelectedDndPath(null);
132: canvas.getMapper().repaint();
133: }
134:
135: }
136:
137: private void autoExpand(JComponent component,
138: DropTargetDragEvent dtde) {
139: LeftTree leftTree = getLeftTree();
140: RightTree rightTree = getRightTree();
141: Canvas canvas = getCanvas();
142: Point point = dtde.getLocation();
143:
144: if (component == leftTree) {
145: TreePath treePath = leftTree.getPathForLocation(point.x,
146: point.y);
147: if (!Utils.equal(point, oldPoint)) {
148: removeTimer();
149: }
150: // expand with a delay
151: if (((timer == null) || !timer.isRunning())
152: && (treePath != null) &&
153: // !tree.isLeaf(treePath) &&
154: !leftTree.isExpanded(treePath)) {
155: // node is candidate for expand
156: final TreePath fTreePath = treePath;
157: final JTree fTree = leftTree;
158: // remove old timer
159: removeTimer();
160: // create new timer
161: timer = new Timer(DELAY_TIME_FOR_EXPAND,
162: new ActionListener() {
163:
164: final public void actionPerformed(
165: ActionEvent e) {
166: fTree.expandPath(fTreePath);
167: }
168: });
169: timer.setRepeats(false);
170: timer.start();
171: }
172: oldPoint = point;
173: }
174:
175: if (component == rightTree) {
176: TreePath treePath = rightTree.getTreePath(point.y);
177: MapperNode dropNode = rightTree.getNodeAt(point.y);
178: if (!Utils.equal(point, oldPoint)) {
179: removeTimer();
180: }
181: // expand with a delay
182: if (((timer == null) || !timer.isRunning())
183: && (treePath != null) && !dropNode.isLeaf()
184: && !dropNode.isExpanded()) {
185: // node is candidate for expand
186: final TreePath fTreePath = treePath;
187: final MapperNode node = dropNode;
188: // remove old timer
189: removeTimer();
190: // create new timer
191: timer = new Timer(DELAY_TIME_FOR_EXPAND,
192: new ActionListener() {
193:
194: final public void actionPerformed(
195: ActionEvent e) {
196: getMapper().setExpandedState(fTreePath,
197: true);
198: }
199: });
200: timer.setRepeats(false);
201: timer.start();
202: }
203: oldPoint = point;
204: }
205:
206: if (component == canvas) {
207: TreePath treePath = rightTree.getTreePath(point.y);
208: MapperNode dropNode = rightTree.getNodeAt(point.y);
209: if (!Utils.equal(point, oldPoint)) {
210: removeTimer();
211: }
212: // expand with a delay
213: if (((timer == null) || !timer.isRunning())
214: && (treePath != null)
215: && !dropNode.isGraphExpanded()) {
216: // node is candidate for expand
217: final TreePath fTreePath = treePath;
218: final Mapper maper = rightTree.getMapper();
219: // remove old timer
220: removeTimer();
221: // create new timer
222: timer = new Timer(DELAY_TIME_FOR_EXPAND,
223: new ActionListener() {
224:
225: final public void actionPerformed(
226: ActionEvent e) {
227: maper.setExpandedGraphState(fTreePath,
228: true);
229: }
230: });
231: timer.setRepeats(false);
232: timer.start();
233: }
234: oldPoint = point;
235: }
236: }
237:
238: /** Removes timer and all listeners. */
239: private void removeTimer() {
240: if (timer != null) {
241: ActionListener[] l = timer
242: .getListeners(ActionListener.class);
243: for (int i = 0; i < l.length; i++) {
244: timer.removeActionListener(l[i]);
245: }
246: timer.stop();
247: timer = null;
248: }
249: }
250:
251: private void setMoveOnTop(JComponent component,
252: DropTargetDragEvent dtde) {
253: Canvas canvas = getCanvas();
254:
255: if (component == canvas) {
256: Point p = dtde.getLocation();
257: CanvasSearchResult searchResult = getCanvas()
258: .find(p.x, p.y);
259: if (searchResult == null)
260: return;
261:
262: GraphItem selectedItem = searchResult.getGraphItem();
263: if (selectedItem == null)
264: return;
265:
266: selectedItem.moveOnTop();
267: }
268: }
269:
270: private void setSelected(JComponent component,
271: DropTargetDragEvent dtde) {
272: Canvas canvas = getCanvas();
273: RightTree rightTree = getRightTree();
274:
275: if (component == canvas || component == rightTree) {
276: Point point = dtde.getLocation();
277: TreePath treePath = rightTree.getTreePath(point.y);
278: canvas.getMapper().setSelectedDndPath(treePath);
279: canvas.getMapper().repaint();
280: }
281: }
282:
283: final static private int DELAY_TIME_FOR_EXPAND = 600;
284: }
|