1
2
3
4
5
6
7 package com.vikash.firsttool.Diagram;
8
9 import com.vikash.firsttool.ProfileImpl.*;
10 import com.vikash.firsttool.UI.*;
11
12 import java.awt.*;
13 import java.awt.Font;
14 import java.awt.Component;
15 import java.awt.event.*;
16 import java.beans.*;
17 import java.awt.geom.*;
18 import java.awt.geom.Rectangle2D;
19 import java.awt.geom.Point2D;
20 import java.util.Hashtable;
21 import java.util.Map;
22 import java.net.URL;
23
24
25 import javax.swing.*;
26 import javax.swing.tree.*;
27 import javax.swing.border.LineBorder;
28
29
30
31
32
33 import org.jgraph.JGraph;
34 import org.jgraph.graph.GraphModel;
35 import org.jgraph.graph.DefaultGraphCell;
36 import org.jgraph.graph.DefaultPort;
37 import org.jgraph.graph.DefaultEdge;
38 import org.jgraph.graph.DefaultGraphModel;
39 import org.jgraph.graph.GraphLayoutCache;
40 import org.jgraph.graph.DefaultCellViewFactory;
41 import org.jgraph.graph.VertexView;
42 import org.jgraph.graph.PortView;
43 import org.jgraph.graph.Edge;
44 import org.jgraph.graph.Port;
45 import org.jgraph.graph.BasicMarqueeHandler;
46 import org.jgraph.graph.GraphConstants;
47 import org.jgraph.graph.GraphUndoManager;
48 import org.jgraph.event.GraphSelectionListener;
49 import org.jgraph.event.GraphLayoutCacheListener;
50 import org.jgraph.event.GraphLayoutCacheEvent;
51 import org.jgraph.event.GraphModelEvent;
52 import org.jgraph.event.GraphModelListener;
53 import org.jgraph.event.GraphSelectionEvent;
54 import org.jgraph.graph.CellView;
55 /***
56 *
57 * @author studajb
58 */
59 public class EditorPanel extends JPanel implements
60 GraphSelectionListener,GraphModelListener, GraphLayoutCacheListener {
61
62 public ToolGraph graph;
63 public ToolModel model;
64 public GraphUndoManager Toolundomanager;
65 public String buttonselected;
66 final ToolBar bar;
67 public String icontype;
68 JToolBar iconbar;
69 String filename;
70 ToolInternalFrame internalframe;
71 MainFrame frame;
72 DiagramElementTreePane tree;
73 DefaultGraphCell cell;
74 boolean graphmodified=false;
75 public EditorPanel(MainFrame frame,final ToolGraph graph){
76
77 this.frame=frame;
78 this.bar=frame.getToolBar();
79 this.graph=graph;
80 this.tree=frame.getDiagramElementTree();
81 setLayout(new BorderLayout());
82
83
84
85
86 graph.setMarqueeHandler(new ToolMarqueeHandler(this));
87
88
89 Toolundomanager = new ToolUndo(this);
90
91 graph.getModel().addUndoableEditListener(Toolundomanager);
92
93
94 graph.getSelectionModel().addGraphSelectionListener(this);
95 graph.getModel().addGraphModelListener(this);
96 graph.getGraphLayoutCache().addGraphLayoutCacheListener(this);
97
98
99 graph.addMouseListener(new MouseAdapter() {
100
101 public void mouseClicked(MouseEvent e) {
102 int x=e.getX(); int y=e.getY();
103 Object cell=graph.getFirstCellForLocation(x, y);
104
105
106 if(e.getClickCount()>=2)
107 buttonselected="null";
108 if(cell==null) {
109 if(icontype=="vertex")
110 graph.insertVertex(new Point2D.Double(x,y),buttonselected);
111 }
112
113
114 }
115
116
117 });
118
119
120
121 graph.addKeyListener(new KeyAdapter() {
122 public void keyReleased(KeyEvent e){
123
124 }
125 public void keyTyped(KeyEvent e){
126
127 }
128 public void keyPressed(KeyEvent e){
129 if(e.getKeyCode()==KeyEvent.VK_DELETE)
130 if (!graph.isSelectionEmpty()) {
131 bar.getdeleteaction().actionPerformed(null);
132 }
133 }
134 });
135
136
137 JScrollPane scrollpane=new JScrollPane(graph);
138 add(scrollpane);
139
140
141
142 }
143
144 public ToolGraph getgraph(){
145 return graph;
146 }
147 public void IconSelected(String selectedicon){
148 this.icontype=selectedicon;
149 }
150
151
152
153
154 public void updateundoredobuttons() {
155
156
157
158 bar.getUndoAction().setEnabled(Toolundomanager.canUndo(graph.getGraphLayoutCache()));
159 bar.getRedoAction().setEnabled(Toolundomanager.canRedo(graph.getGraphLayoutCache()));
160 }
161
162 public void valueChanged(GraphSelectionEvent event) {
163
164 boolean enabled=!graph.isSelectionEmpty();
165
166 bar.getcopyaction().setEnabled(enabled);
167 bar.getcutaction().setEnabled(enabled);
168
169
170 cell=(DefaultGraphCell)event.getCell();
171
172 if(cell!=null)
173 ((DiagramElementTreeModel)tree.getModel()).showTreeElement(cell);
174 }
175 public ToolGraph getGraph(){
176 return graph;
177 }
178 public ToolModel getModel() {
179 return model;
180 }
181
182 public void setFileName(String file) {
183 filename=file;
184
185 internalframe.setFrameTitle(filename);
186
187 }
188 public String getFileName() {
189 return filename;
190 }
191 public void setInternalFrame(ToolInternalFrame frame) {
192 internalframe=frame;
193 }
194 public void setgraphmodified(boolean graphmodified){
195 this.graphmodified=graphmodified;
196 }
197 public boolean getgraphmodified(){
198 return graphmodified;
199 }
200
201 public void graphChanged(org.jgraph.event.GraphModelEvent e) {
202
203 Object[] cells=e.getChange().getChanged();
204 if(cells.length==0)return;
205 TreeNode node=((DiagramElementTreeModel)tree.getModel()).findTreeElementOf((DefaultGraphCell)cells[cells.length-1]);
206 ((DiagramElementTreeModel)tree.getModel()).nodeChanged(node);
207 setgraphmodified(true);
208 updateStatusBar("");
209 }
210
211 public void graphLayoutCacheChanged(org.jgraph.event.GraphLayoutCacheEvent graphLayoutCacheEvent) {
212 this.graphmodified=true;
213 updateStatusBar("");
214 }
215 public ToolUndo getUndoManager(){
216 return (ToolUndo)Toolundomanager;
217 }
218 public void updateStatusBar(String text){
219 frame.getStatusBar().setMessage(text);
220 }
221 }