View Javadoc

1   /*
2    * Class.java
3    *
4    * Created on 12. mai 2005, 20:00
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          // a reference to tool's main toolbar to update undo,redo,copy,cut buttons.
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          // add custom marqueehandler to graph
86          graph.setMarqueeHandler(new ToolMarqueeHandler(this));
87          
88          // create a custom undomanager and add to the model
89          Toolundomanager = new ToolUndo(this);
90          
91          graph.getModel().addUndoableEditListener(Toolundomanager);
92          
93          //add selection listener to update toolbar
94          graph.getSelectionModel().addGraphSelectionListener(this);
95          graph.getModel().addGraphModelListener(this);
96          graph.getGraphLayoutCache().addGraphLayoutCacheListener(this);
97          
98          //add mouse listener to graph to insert cells on mouse click
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                 // Stop inserting cells in graph after double click
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         // add keylistener to graph to delete cells on pressing DELETE key.
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     // Updates Undo/Redo actions in main toolbar and menubar State based on Undo Manager
154     public void updateundoredobuttons() {
155         // set the undo/redo buttons if the operation can be successful for the given view
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         //updates the copy and paste actions in both main toolbar and menu bar
166         bar.getcopyaction().setEnabled(enabled);
167         bar.getcutaction().setEnabled(enabled);
168         
169         // when a cell is selected in a diagram point to the same cell in diagram element tree
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         //change the respective tree element when a cell is edited in a diagram
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 }