View Javadoc

1   /*
2   * The contents of this file are subject to the BT "ZEUS" Open Source 
3   * Licence (L77741), Version 1.0 (the "Licence"); you may not use this file 
4   * except in compliance with the Licence. You may obtain a copy of the Licence
5   * from $ZEUS_INSTALL/licence.html or alternatively from
6   * http://www.labs.bt.com/projects/agents/zeus/licence.htm
7   * 
8   * Except as stated in Clause 7 of the Licence, software distributed under the
9   * Licence is distributed WITHOUT WARRANTY OF ANY KIND, either express or 
10  * implied. See the Licence for the specific language governing rights and 
11  * limitations under the Licence.
12  * 
13  * The Original Code is within the package zeus.*.
14  * The Initial Developer of the Original Code is British Telecommunications
15  * public limited company, whose registered office is at 81 Newgate Street, 
16  * London, EC1A 7AJ, England. Portions created by British Telecommunications 
17  * public limited company are Copyright 1996-9. All Rights Reserved.
18  * 
19  * THIS NOTICE MUST BE INCLUDED ON ANY COPY OF THIS FILE
20  */
21  
22  
23  
24  package zeus.gui.graph;
25  
26  import java.awt.*;
27  import java.awt.event.*;
28  import javax.swing.*;
29  import javax.swing.event.*;
30  import javax.swing.border.*;
31  import zeus.util.*;
32  
33  public class GraphIcon extends JPanel implements MouseListener,
34                                                   MouseMotionListener {
35  
36    protected static final int LOCATION_CHANGED     = 0;
37    protected static final int DOUBLE_CLICK_ACTION  = 1;
38    protected static final int LEFT_MOUSE_ACTION    = 2;
39    protected static final int RIGHT_MOUSE_PRESSED  = 3;
40    protected static final int RIGHT_MOUSE_DRAGGED  = 4;
41    protected static final int RIGHT_MOUSE_RELEASED = 5;
42    protected static final int MIDDLE_MOUSE_ACTION  = 6;
43  
44    protected static final int thickness = 3;
45  
46    protected Point     anchor;
47    protected Point     graphPoint;
48    protected Graph     graph;
49    protected Rectangle rect = null;
50    protected Cursor    lastCursor;
51    protected Component rendererComp;
52    protected GraphNode node;
53  
54    protected EventListenerList iconListeners = new EventListenerList();
55    protected boolean dragging     = false;
56    protected boolean isSelected   = false;
57    protected boolean isSelectable = true;
58    protected boolean isMoveable   = true;
59  
60  
61  
62    public GraphIcon(GraphNode node, Graph graph,
63                     boolean isSelectable, boolean isMoveable) {
64      this(node,graph);
65      this.isSelectable = isSelectable;
66      this.isMoveable = isMoveable;
67    }
68  
69    public GraphIcon(GraphNode node, Graph graph) {
70      this.node = node;
71      this.graph = graph;
72      this.setBackground(Color.lightGray);
73      this.setLayout(new GridBagLayout());
74      reset();
75      anchor = getLocation();
76      this.addMouseListener(this);
77      this.addMouseMotionListener(this);
78    }
79  
80    public void reset() {
81      removeAll();
82      GridBagLayout gb = (GridBagLayout)this.getLayout();
83      GridBagConstraints gbc = new GridBagConstraints();
84  
85      GraphNodeRenderer renderer = graph.getNodeRenderer();
86      if ( renderer == null )
87         renderer = new DefaultGraphNodeRenderer();
88  
89      rendererComp = renderer.getNodeRendererComponent(graph,node);
90      rendererComp.addMouseListener(this);
91      rendererComp.addMouseMotionListener(this);
92  
93      gbc.anchor = GridBagConstraints.WEST;
94      gbc.fill = GridBagConstraints.NONE;
95      gbc.insets = new Insets(thickness,thickness,thickness,thickness);
96      gb.setConstraints(rendererComp,gbc);
97      this.add(rendererComp);
98      setBorder(new BevelBorder(BevelBorder.RAISED));
99      isSelected = false;
100   }
101 
102   public void addNotify() {
103     super.addNotify();
104     setSize(getPreferredSize());
105     doLayout();
106   }
107 
108   public void setGraphNode(GraphNode node) {
109      this.node = node;
110      reset();
111      setSize(getPreferredSize());
112      doLayout();
113   }
114 
115   public GraphNode getGraphNode() {
116      return node;
117   }
118 
119   public void setGraph(Graph graph) {
120     this.graph = graph;
121   }
122 
123   public void setMoveable(boolean set) {
124     this.isMoveable = set;
125   }
126 
127   public void setSelectable(boolean set) {
128     this.isSelectable = set;
129   }
130 
131   public boolean isSelected() {
132     return isSelected;
133   }
134 
135   public void setSelected(boolean mode) 	{
136     if ( isSelectable )	{
137        isSelected = mode;
138        if ( mode )
139           setBorder(new BevelBorder(BevelBorder.LOWERED));
140        else
141           setBorder(new BevelBorder(BevelBorder.RAISED));
142     }
143   }
144 
145   public void setLocation(int x, int y) {
146     super.setLocation(x,y);
147     if ( isMoveable )
148        fireAction(LOCATION_CHANGED);
149   }
150 
151   public void mouseClicked(MouseEvent evt) {}
152   public void mouseEntered(MouseEvent evt) {}
153   public void mouseExited(MouseEvent evt)  {}
154   public void mouseMoved(MouseEvent evt)   {}
155 
156   public void mousePressed(MouseEvent evt) {
157     graphPoint = evt.getPoint();
158     if ( evt.getClickCount() == 2 ) {
159        if ( SwingUtilities.isLeftMouseButton(evt) )
160           fireAction(DOUBLE_CLICK_ACTION);
161     }
162     else if ( evt.getClickCount() == 1 ){
163        if ( SwingUtilities.isLeftMouseButton(evt) )
164           fireAction(LEFT_MOUSE_ACTION);
165        else if ( SwingUtilities.isRightMouseButton(evt) ){
166 	  fireAction(RIGHT_MOUSE_PRESSED);
167        }
168        else if ( SwingUtilities.isMiddleMouseButton(evt) )
169           fireAction(MIDDLE_MOUSE_ACTION);
170     }
171 
172     if ( isMoveable ) {
173        dragging = false;
174        anchor = evt.getPoint();
175        rect = getBounds();
176        Graphics g = graph.getGraphics();
177        if ( g != null && rect != null ) {
178             g.setXORMode(graph.getBackground());
179             g.drawRect(rect.x,rect.y,rect.width,rect.height);
180        }
181     }
182   }
183 
184   public void mouseReleased(MouseEvent evt) {
185     graphPoint = evt.getPoint();
186     if ( SwingUtilities.isRightMouseButton(evt) ){
187        fireAction(RIGHT_MOUSE_RELEASED);
188     }
189     else {
190       if ( isMoveable ) {
191        int x = evt.getX();
192        int y = evt.getY();
193 
194        int xx = getLocation().x + x-anchor.x;
195        int yy = getLocation().y + y-anchor.y;
196        xx = ( xx > 0 ) ? xx : 0;
197        yy = ( yy > 0 ) ? yy : 0;
198        Graphics g = graph.getGraphics();
199        if ( g != null && rect != null ) {
200 	  g.setXORMode(graph.getBackground());
201 	  g.drawRect(rect.x,rect.y,rect.width,rect.height);
202        }
203        this.setLocation(xx,yy);
204       }
205 
206       if ( isSelectable && !dragging & SwingUtilities.isLeftMouseButton(evt) )
207        setSelected(!isSelected);
208       else if ( isMoveable && dragging ) {
209       // reset cursor -- end of move
210        JFrame f = (JFrame)SwingUtilities.getRoot(this);
211        f.setCursor(lastCursor);
212       }
213     }
214   }
215 
216   public void mouseDragged(MouseEvent evt) {
217     graphPoint = evt.getPoint();
218     if ( SwingUtilities.isRightMouseButton(evt) ){
219        fireAction(RIGHT_MOUSE_DRAGGED);
220     }
221     else{
222       if ( isMoveable ) {
223        if ( !dragging ) {
224           Frame f = (JFrame)SwingUtilities.getRoot(this);
225           lastCursor = f.getCursor();
226           f.setCursor(new Cursor(Cursor.MOVE_CURSOR));
227           dragging = true;
228         }
229         else {
230           int x = evt.getX(); int y = evt.getY();
231           Graphics g = graph.getGraphics();
232           if ( g != null && rect != null ) {
233 	     g.setXORMode(graph.getBackground());
234 	     g.drawRect(rect.x,rect.y,rect.width,rect.height);
235 	     rect.x = getLocation().x + x-anchor.x;
236 	     rect.y = getLocation().y + y-anchor.y;
237 	     g.drawRect(rect.x,rect.y,rect.width,rect.height);
238           }
239         }
240       }
241     }
242   }
243 
244   public void addGraphIconListener(GraphIconListener x) {
245     iconListeners.add(GraphIconListener.class, x);
246   }
247   public void removeGraphIconListener(GraphIconListener x) {
248     iconListeners.remove(GraphIconListener.class, x);
249   }
250   protected void fireAction(int type) {
251     GraphIconEvent evt = new GraphIconEvent(this);
252     evt.setPoint( graphPoint);
253     Object[] listeners = iconListeners.getListenerList();
254     for(int i = listeners.length-2; i >= 0; i -= 2) {
255        if (listeners[i] == GraphIconListener.class) {
256           GraphIconListener cl = (GraphIconListener)listeners[i+1];
257           switch(type) {
258              case LOCATION_CHANGED:
259                   cl.locationChanged(evt);
260                   break;
261              case DOUBLE_CLICK_ACTION:
262                   cl.performLeftMouseDClickAction(evt);
263                   break;
264              case LEFT_MOUSE_ACTION:
265                   cl.performLeftMouseAction(evt);
266                   break;
267              case RIGHT_MOUSE_PRESSED:
268                   evt.setRightMousePressed();
269                   cl.performRightMouseAction(evt);
270                   break;
271              case RIGHT_MOUSE_DRAGGED:
272                   evt.setRightMouseDragged();
273 		  cl.performRightMouseAction(evt);
274                   break;
275              case RIGHT_MOUSE_RELEASED:
276                   evt.setRightMouseReleased();
277                   cl.performRightMouseAction(evt);
278                   break;
279              case MIDDLE_MOUSE_ACTION:
280                   cl.performMiddleMouseAction(evt);
281                   break;
282           }
283        }
284     }
285   }
286 
287 }