1
2
3
4
5
6
7 package com.vikash.firsttool.UI;
8
9 import com.vikash.firsttool.Diagram.*;
10 import java.awt.*;
11 import java.awt.event.*;
12 import java.awt.Component;
13 import java.awt.geom.*;
14 import java.net.URL;
15
16 import javax.swing.*;
17 import javax.swing.tree.*;
18
19 import org.jgraph.graph.*;
20 /***
21 *
22 * @author studajb
23 */
24 public class ToolMarqueeHandler extends BasicMarqueeHandler {
25 protected Point2D start, current;
26 protected PortView port,firstport;
27 ToolGraph graph;
28 EditorPanel panel;
29
30 public ToolMarqueeHandler(EditorPanel panel){
31 super();
32
33 graph=panel.getgraph();
34 this.panel=panel;
35 }
36
37 public boolean isForceMarqueeEvent(MouseEvent e) {
38
39 if(SwingUtilities.isLeftMouseButton(e)&&graph.getFirstCellForLocation(e.getX(),e.getY())==null)
40 return true;
41
42 if(SwingUtilities.isRightMouseButton(e))
43 return true;
44
45 port = getSourcePortAt(e.getPoint());
46
47 return super.isForceMarqueeEvent(e) || panel.icontype !="Select" ;
48 }
49
50 public void mousePressed(final MouseEvent e) {
51 if(SwingUtilities.isRightMouseButton(e)){
52 Object cell=graph.getFirstCellForLocation(e.getX(),e.getY());
53 JPopupMenu pop=createPopupMenu(e.getPoint(),cell);
54 pop.show(graph,e.getX(),e.getY());
55 }
56 else if (port != null ) {
57 start = graph.toScreen(port.getLocation(null));
58 firstport = port;
59 }
60 else
61 super.mousePressed(e);
62 }
63 public void mouseMoved(MouseEvent e) {
64
65
66 if (e != null && getSourcePortAt(e.getPoint()) != null ) {
67 if(panel.icontype=="dependency"||panel.icontype=="association"||panel.icontype=="uniassociation" ) {
68 Toolkit tk=Toolkit.getDefaultToolkit();
69 URL arrow=getClass().getClassLoader().getResource("com/vikash/firsttool/resources/Arrow.gif");
70 Image img=tk.getImage(arrow);
71 Cursor connectcursor=tk.createCustomCursor(img,new Point(10,10),"connect");
72 graph.setCursor(connectcursor);
73 e.consume();
74 }
75 } else{
76 super.mouseMoved(e);
77 }
78 }
79 public void mouseDragged(MouseEvent e) {
80 if (start != null ) {
81 Graphics g = graph.getGraphics();
82 PortView newPort = getTargetPortAt(e.getPoint());
83 if (newPort == null || newPort != port) {
84 paintConnector(Color.black, graph.getBackground(), g);
85 port = newPort;
86 if (port != null)
87 current = graph.toScreen(port.getLocation(null));
88 else
89 current = graph.snap(e.getPoint());
90 paintConnector(graph.getBackground(), Color.black, g);
91 }
92
93 }
94 super.mouseDragged(e);
95 }
96
97 public PortView getSourcePortAt(Point2D point) {
98
99 if(panel.icontype=="Select"){
100 graph.setJumpToDefaultPort(false);
101 }
102 PortView result;
103
104 result = graph.getPortViewAt(point.getX(), point.getY());
105 try {
106 result = graph.getPortViewAt(point.getX(), point.getY());
107 } finally {
108 graph.setJumpToDefaultPort(true);
109 }
110 return result;
111 }
112
113 protected PortView getTargetPortAt(Point2D point) {
114 return graph.getPortViewAt(point.getX(), point.getY());
115
116 }
117
118
119 public void mouseReleased(MouseEvent e) {
120
121 if (e != null && port != null && firstport != null && firstport != port ) {
122 graph.connect((Port) firstport.getCell(), (Port) port.getCell(),panel.icontype,panel.buttonselected);
123 e.consume();
124 graph.repaint();
125
126 }
127 else
128 graph.repaint();
129
130
131 firstport = port = null;
132 start = current = null;
133
134 super.mouseReleased(e);
135 }
136
137
138 protected void paintConnector(Color fg, Color bg, Graphics g) {
139 g.setColor(fg);
140 g.setXORMode(bg);
141 paintPort(graph.getGraphics());
142
143 if (firstport != null && start != null && current != null){
144 if(panel.icontype=="dependency"||panel.icontype=="association"||panel.icontype=="uniassociation" )
145 g.drawLine((int) start.getX(), (int) start.getY(),(int) current.getX(), (int) current.getY());
146 }
147 }
148
149 protected void paintPort(Graphics g) {
150 if (port != null) {
151 boolean o = (GraphConstants.getOffset(port.getAttributes()) != null);
152 Rectangle2D r = (o) ? port.getBounds() : port.getParentView().getBounds();
153 r = graph.toScreen((Rectangle2D) r.clone());
154 r.setFrame(r.getX() - 3, r.getY() - 3, r.getWidth() + 6, r.getHeight() + 6);
155 graph.getUI().paintCell(g, port, r, true);
156 }
157 }
158 public JPopupMenu createPopupMenu(final Point pt,final Object cell){
159 JPopupMenu popup=new JPopupMenu();
160 if(cell!=null){
161
162 popup.add(new AbstractAction("Edit") {
163 public void actionPerformed(ActionEvent e){
164 graph.startEditingAtCell(cell);
165 }
166 });
167 popup.addSeparator();
168
169 popup.add(new AbstractAction("Delete From Diagram") {
170 public void actionPerformed(ActionEvent e){
171 graph.getGraphLayoutCache().setVisible(cell,false);
172 }
173 });
174
175
176 popup.add(new AbstractAction("Delete From Model") {
177 public void actionPerformed(ActionEvent e){
178 Object[] cells=new Object[]{cell};
179 cells = org.jgraph.graph.DefaultGraphModel.getDescendants(graph.getModel(), cells).toArray();
180 Object[] edges = org.jgraph.graph.DefaultGraphModel.getEdges(graph.getModel(),cells).toArray();
181
182 graph.getModel().remove(edges);
183 graph.getModel().remove(cells);
184 }
185 });
186 }
187 return popup;
188 }
189 }