KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > nbprefuse > AggregateDragControl


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 /*
20  * AggregateDragControl.java
21  *
22  * Created on April 3, 2006, 5:47 PM
23  *
24  * To change this template, choose Tools | Template Manager
25  * and open the template in the editor.
26  */

27
28 package org.netbeans.modules.xml.nbprefuse;
29
30 import java.awt.Cursor JavaDoc;
31 import java.awt.event.MouseEvent JavaDoc;
32 import java.awt.geom.Point2D JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import javax.swing.SwingUtilities JavaDoc;
35 import prefuse.Display;
36 import prefuse.controls.ControlAdapter;
37 import prefuse.visual.AggregateItem;
38 import prefuse.visual.VisualItem;
39
40 /**
41  *
42  * @author Jeri Lockhart
43  */

44 /**
45  * Interactive drag control that is "aggregate-aware"
46  */

47 public class AggregateDragControl extends ControlAdapter {
48
49     private VisualItem activeItem;
50     protected Point2D JavaDoc down = new Point2D.Double JavaDoc();
51     protected Point2D JavaDoc temp = new Point2D.Double JavaDoc();
52     protected boolean dragged;
53     
54     /**
55      * Creates a new drag control that issues repaint requests as an item
56      * is dragged.
57      */

58     public AggregateDragControl() {
59     }
60         
61     /**
62      * @see prefuse.controls.Control#itemEntered(prefuse.visual.VisualItem, java.awt.event.MouseEvent)
63      */

64     public void itemEntered(VisualItem item, MouseEvent JavaDoc e) {
65         Display d = (Display)e.getSource();
66         d.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
67         activeItem = item;
68         if ( !(item instanceof AggregateItem) )
69             setFixed(item, true);
70     }
71     
72     /**
73      * @see prefuse.controls.Control#itemExited(prefuse.visual.VisualItem, java.awt.event.MouseEvent)
74      */

75     public void itemExited(VisualItem item, MouseEvent JavaDoc e) {
76         if ( activeItem == item ) {
77             activeItem = null;
78             setFixed(item, false);
79         }
80         Display d = (Display)e.getSource();
81         d.setCursor(Cursor.getDefaultCursor());
82     }
83     
84     /**
85      * @see prefuse.controls.Control#itemPressed(prefuse.visual.VisualItem, java.awt.event.MouseEvent)
86      */

87     public void itemPressed(VisualItem item, MouseEvent JavaDoc e) {
88         if (!SwingUtilities.isLeftMouseButton(e)) return;
89         dragged = false;
90         Display d = (Display)e.getComponent();
91         d.getAbsoluteCoordinate(e.getPoint(), down);
92         if ( item instanceof AggregateItem )
93             setFixed(item, true);
94     }
95     
96     /**
97      * @see prefuse.controls.Control#itemReleased(prefuse.visual.VisualItem, java.awt.event.MouseEvent)
98      */

99     public void itemReleased(VisualItem item, MouseEvent JavaDoc e) {
100         if (!SwingUtilities.isLeftMouseButton(e)) return;
101         if ( dragged ) {
102             activeItem = null;
103             setFixed(item, false);
104             dragged = false;
105             item.getVisualization().run(AnalysisConstants.ACTION_UPDATE_AGGREGATE_LAYOUT_REPAINT);
106         }
107     }
108     
109     /**
110      * @see prefuse.controls.Control#itemDragged(prefuse.visual.VisualItem, java.awt.event.MouseEvent)
111      */

112     public void itemDragged(VisualItem item, MouseEvent JavaDoc e) {
113         if (!SwingUtilities.isLeftMouseButton(e)) return;
114         dragged = true;
115         Display d = (Display)e.getComponent();
116         d.getAbsoluteCoordinate(e.getPoint(), temp);
117         double dx = temp.getX()-down.getX();
118         double dy = temp.getY()-down.getY();
119         
120         move(item, dx, dy);
121         
122         down.setLocation(temp);
123         item.getVisualization().run(AnalysisConstants.ACTION_UPDATE_AGGREGATE_LAYOUT_REPAINT);
124     }
125
126     protected static void setFixed(VisualItem item, boolean fixed) {
127         if ( item instanceof AggregateItem ) {
128             Iterator JavaDoc items = ((AggregateItem)item).items();
129             while ( items.hasNext() ) {
130                 setFixed((VisualItem)items.next(), fixed);
131             }
132         } else {
133             item.setFixed(fixed);
134         }
135     }
136     
137     protected static void move(VisualItem item, double dx, double dy) {
138         if ( item instanceof AggregateItem ) {
139             Iterator JavaDoc items = ((AggregateItem)item).items();
140             while ( items.hasNext() ) {
141                 move((VisualItem)items.next(), dx, dy);
142             }
143         } else {
144             double x = item.getX();
145             double y = item.getY();
146             item.setStartX(x); item.setStartY(y);
147             item.setX(x+dx); item.setY(y+dy);
148             item.setEndX(x+dx); item.setEndY(y+dy);
149         }
150     }
151     
152 } // end of class AggregateDragControl
153
Popular Tags