View Javadoc

1   /*
2    * Class.java
3    *
4    * Created on 10. mai 2005, 13:50
5    */
6   
7   package com.vikash.firsttool.UI;
8   import java.awt.Color;
9   import java.awt.Dimension;
10  import java.awt.Graphics2D;
11  import java.awt.Rectangle;
12  import java.awt.geom.Rectangle2D;
13  import java.awt.image.BufferedImage;
14  import java.beans.PropertyVetoException;
15  import java.beans.XMLDecoder;
16  import java.beans.XMLEncoder;
17  import java.io.BufferedInputStream;
18  import java.io.BufferedOutputStream;
19  import java.io.File;
20  import java.io.FileInputStream;
21  import java.io.FileOutputStream;
22  import java.io.IOException;
23  import java.util.Enumeration;
24  import java.util.Hashtable;
25  import java.util.Map;
26  
27  import javax.imageio.ImageIO;
28  import javax.swing.JDesktopPane;
29  import javax.swing.JFileChooser;
30  import javax.swing.JOptionPane;
31  import javax.swing.JPanel;
32  import javax.swing.tree.DefaultMutableTreeNode;
33  
34  import org.jgraph.graph.AttributeMap;
35  import org.jgraph.graph.CellView;
36  import org.jgraph.graph.DefaultGraphCell;
37  import org.jgraph.graph.GraphConstants;
38  
39  import com.vikash.firsttool.Diagram.AssetDiagram;
40  import com.vikash.firsttool.Diagram.EditorPanel;
41  import com.vikash.firsttool.Diagram.RiskDiagram;
42  import com.vikash.firsttool.Diagram.SWOTDiagram;
43  import com.vikash.firsttool.Diagram.ToolGraph;
44  import com.vikash.firsttool.Diagram.TreatmentDiagram;
45  import com.vikash.firsttool.Diagram.TreatmentEffectDiagram;
46  import com.vikash.firsttool.Diagram.UnwantedIncidentDiagram;
47  import com.vikash.firsttool.ProfileImpl.ToolEdge;
48  import com.vikash.firsttool.ProfileImpl.ToolModel;
49  
50  
51  /***
52   *
53   * @author  studajb
54   */
55  public class MainEditor extends JPanel{
56      EditorPanel panel;
57      MainFrame frame;
58      JDesktopPane desktoppane;
59      ToolInternalFrame previousframe;
60      JFileChooser filechooser;
61      int nextFrameX,nextFrameY,distance,width,height;
62      
63  //    File file;
64      DiagramTreePane diagramtree;
65      DiagramElementTreePane elementtree;
66      /*** Creates a new instance of Class */
67      public MainEditor(MainFrame frame){
68          this.frame=frame;
69          this.desktoppane=frame.getDesktopPane();
70          this.diagramtree=frame.getDiagramTree();
71          this.elementtree=frame.getDiagramElementTree();
72      }
73      
74      public boolean isModified() {
75      	if (((DiagramElementTreeModel) elementtree.getModel()).isTreeChanged()
76      		|| ((DiagramTreeModel) diagramtree.getModel()).isTreeChanged())
77      		return true;
78      	// TODO check for diagram modifications
79      	return true;
80      }
81      
82      private void makeClean() {
83      	((DiagramElementTreeModel) elementtree.getModel()).setTreeChanged(false);
84      	((DiagramTreeModel) diagramtree.getModel()).setTreeChanged(false);
85      }
86      
87      public void adddiagram(ToolGraph graph,String frametitle,boolean visible){
88          
89          panel=new EditorPanel(frame,graph);
90          final ToolInternalFrame internalframe=new ToolInternalFrame(this,panel);
91          updateFrame(internalframe,frametitle);
92          // update the Diagram tree(lower tree)
93          ((DiagramTreeModel)(diagramtree.getModel())).addElement(internalframe);
94          
95          desktoppane.add(internalframe);
96          distance=internalframe.getHeight()-internalframe.getContentPane().getHeight();
97          previousframe=(ToolInternalFrame)desktoppane.getSelectedFrame();
98          if(previousframe==null) {
99              width=desktoppane.getWidth();
100             height=desktoppane.getHeight()-distance;
101             nextFrameX=0;
102             nextFrameY=0;
103         }
104         
105         else {
106             width=desktoppane.getWidth();
107             height=previousframe.getHeight()-distance;
108             nextFrameX=previousframe.getX()+distance;
109             nextFrameY=previousframe.getY()+distance;
110             if(nextFrameX + width>desktoppane.getWidth())
111                 nextFrameX=0;
112             if(nextFrameY + height>desktoppane.getHeight())
113                 nextFrameY=0;
114         }
115         
116         
117         internalframe.reshape(nextFrameX,nextFrameY,width,height);
118         internalframe.show();
119         
120         try {
121             internalframe.setSelected(true);
122         }catch(PropertyVetoException excp) {
123             JOptionPane.showMessageDialog(desktoppane,excp);
124         }
125         if(!visible){
126             internalframe.setVisible(false);
127             try {
128                 internalframe.setSelected(false);
129             }catch(PropertyVetoException excp) {
130                 JOptionPane.showMessageDialog(desktoppane,excp);
131             }
132             
133             
134         }
135         
136     }
137     
138     
139     public void openfile(){
140     	if(filechooser==null){
141     		filechooser=new JFileChooser();
142     	}
143     	filechooser.setFileFilter(new ToolFileFilter());
144     	filechooser.setCurrentDirectory(new File("c:/"));
145     	
146     	int result=filechooser.showOpenDialog(desktoppane);
147     	
148     	if (result == JFileChooser.CANCEL_OPTION){
149     		
150     		return;
151     	}
152     	openfile(filechooser.getSelectedFile());
153     }
154     
155     public void openfile(File f){
156     	ToolGraph graph;
157         String filename=f.getName();
158         
159         //File file=new File(filename);
160         if(filename==null) return;
161         //check whether the file is already open
162         
163         
164         try{
165             
166             FileInputStream fis=new FileInputStream(f);
167             XMLDecoder dec=new XMLDecoder(new BufferedInputStream(fis));
168             
169             String file=(String)dec.readObject();
170             frame.setFileName(f.getAbsolutePath()); // file);
171             frame.updateTitle();
172             ToolModel model=ToolModel.getInstance(false);
173             
174             Object[] cells = (Object[]) dec.readObject();
175             Map attributes=new Hashtable();
176             
177             for(int i=0;i<cells.length;i++){
178                 
179                 DefaultGraphCell cell=(DefaultGraphCell)cells[i];
180                 attributes.put(cells[i],cell.getAttributes());
181                 
182             }
183             model.insert(cells,attributes,null,null,null);
184             
185             Object[] diagrams = (Object[]) dec.readObject();
186             int diagcount=diagrams.length; // count.intValue();
187             for(int i=0;i<diagcount;i++){
188                 Object[] diagramData = (Object[]) diagrams[i];
189                 String title=(String) diagramData[0];
190                 String diagtype=(String) diagramData[1];
191                 if(diagtype.equals("SWOTDiagram"))
192                     graph=new SWOTDiagram();
193                 else if (diagtype.equals("AssetDiagram"))
194                     graph=new AssetDiagram();
195                 else if(diagtype.equals("UnwantedDiagram"))
196                     graph=new UnwantedIncidentDiagram();
197                 else if(diagtype.equals("RiskDiagram"))
198                     graph=new RiskDiagram();
199                 else if(diagtype.equals("TreatmentDiagram"))
200                     graph=new TreatmentDiagram();
201                 else
202                     graph=new TreatmentEffectDiagram();
203                 graph.getGraphLayoutCache().setShowsExistingConnections(false);
204                 
205                 
206                 Object[][] cellsData = (Object[][]) diagramData[2];
207                 int diagelements=cellsData.length;
208                 for(int j=0;j<diagelements;j++){
209                     Object cell=cellsData[j][0];
210                     AttributeMap map = (AttributeMap) cellsData[j][1];
211                     if(cell instanceof ToolEdge){
212                         
213                         ((ToolGraphLayoutCache)graph.getGraphLayoutCache()).editCell(cell,map);
214                         // do nothing, making the edges source and vertex visible will automatically make the edge visible in diagram,just simply read the map
215                     }
216                     else{
217                         
218                         Rectangle2D bounds=GraphConstants.getBounds(map);
219                         ((ToolGraphLayoutCache)graph.getGraphLayoutCache()).editCell(cell,map);
220                         ((ToolGraphLayoutCache)graph.getGraphLayoutCache()).cellVisible(cell,true,bounds);
221                     }
222                 }
223                 adddiagram(graph,title,false);
224                 
225                 
226             }
227             
228             fis.close();
229             dec.close();
230             
231         }catch(Exception excp) {
232             excp.printStackTrace();
233             JOptionPane.showMessageDialog(desktoppane,excp);
234         }
235         makeClean();
236         
237         
238         
239         
240     }
241     
242     public void savefile(){
243         ToolInternalFrame internalframe;
244         int count;
245         boolean present=false;
246         
247         count=((DefaultMutableTreeNode)(diagramtree.getModel().getRoot())).getChildCount();
248         boolean diagramspresent=false;
249         for(int i=0;i<count;i++){
250             if(!(((DefaultMutableTreeNode)(diagramtree.getModel().getRoot())).getChildAt(i).isLeaf())){
251                 diagramspresent=true;
252             }
253         }
254         
255         count=((DefaultMutableTreeNode)(elementtree.getModel().getRoot())).getChildCount();
256         boolean elementspresent=false;
257         
258         for(int i=0;i<count;i++){
259             if(!(((DefaultMutableTreeNode)(elementtree.getModel().getRoot())).getChildAt(i).isLeaf())){
260                 elementspresent=true;
261             }
262         }
263         
264         // if there are no diagrams and elementts present then do nothing
265         if (!diagramspresent && !elementspresent){
266             JOptionPane.showMessageDialog(frame,"The project is empty");
267             return;
268         }
269         
270         File file;
271         String filename=frame.getFileName();
272         if(filename==null){
273             filechooser=new JFileChooser();
274             
275             
276             filechooser.setCurrentDirectory(new File("c:/"));
277             filechooser.setFileFilter(new ToolFileFilter());
278             int result=filechooser.showSaveDialog(desktoppane);
279             if (result == JFileChooser.CANCEL_OPTION)return;
280             
281             file=filechooser.getSelectedFile();
282             
283             filename=filechooser.getSelectedFile().toString();
284             
285             if(filename != null) {
286                 if(filename.indexOf(".")<0){
287                     file=new File(filename+".dgm");
288                     
289                 }
290                 
291                 // if file already exists..as whether to overwrite the file or not
292                 if(file.exists()) {
293                     int response=JOptionPane.showConfirmDialog(desktoppane, "File already exists. Do you want to overwrite?");
294                     if(response== JOptionPane.NO_OPTION) return;
295                     
296                 }
297             }
298         } else {
299         	file = new File(filename);
300         }
301         
302         try{
303             
304             FileOutputStream output=new FileOutputStream(file);
305             XMLEncoder enc=new XMLEncoder(new BufferedOutputStream(output));
306             
307             //save the filename
308             enc.writeObject(filename);
309             
310             //save the model elements
311             Object[] cells=ToolModel.getInstance(false).getRoots().toArray();
312             enc.writeObject(cells);
313             
314             
315             DefaultMutableTreeNode root,node=null;
316             DiagramElement diagelement;
317             root=(DefaultMutableTreeNode)diagramtree.getModel().getRoot();
318             
319             Enumeration en=root.breadthFirstEnumeration();
320             int diagramscount=0;
321             for(int i=0;i<root.getChildCount();i++){
322                 diagramscount=diagramscount+root.getChildAt(i).getChildCount();
323                 
324             }
325             
326             Object[] diagrams = new Object[diagramscount];
327             int diagCount = 0;
328             while(en.hasMoreElements()){ // take each diagram and store the elements present in it.
329                 node=(DefaultMutableTreeNode)en.nextElement();
330                 if(node instanceof TreeElement){
331                     
332                     ToolGraph graph=((TreeElement)node).getInternalFrame().getGraph();
333                     String title=((TreeElement)node).getInternalFrame().getTitle();
334                     String type=graph.getDiagramType();
335                     
336                     Object[] diagramData = new Object[3];
337                     diagrams[diagCount++] = diagramData;
338                     diagramData[0] = title;
339                     diagramData[1] = type;
340                     
341                     cells=graph.getGraphLayoutCache().getVisibleCells(graph.getRoots());
342                     Object[][] cellsData = new Object[cells.length][2];
343                     diagramData[2] = cellsData;
344                     
345                     for(int i=0;i<cells.length;i++){
346                         DefaultGraphCell temp=(DefaultGraphCell)cells[i];
347                         CellView view=(CellView)graph.getGraphLayoutCache().getMapping(temp,false);
348                         AttributeMap viewmap=view.getAttributes();
349                         cellsData[i][0] = cells[i];
350                         cellsData[i][1] = viewmap;
351                     }
352                     
353                     
354                 }
355             }
356             enc.writeObject(diagrams);
357             enc.close();
358             output.close();
359         }catch(Exception excp) {
360             excp.printStackTrace();
361             JOptionPane.showMessageDialog(desktoppane,excp);
362             
363             
364         }
365         makeClean();
366         frame.setFileName(filename);
367         frame.updateTitle();
368     }
369     public void saveasfile(){
370         frame.setFileName(null);
371         savefile();
372     }
373     
374     
375     public void saveasimage(String type){
376         String imagetype=type;
377         String filename;
378         if(filechooser==null){
379             filechooser=new JFileChooser();
380         }
381         
382         filechooser.setCurrentDirectory(new File("c:/"));
383         filechooser.setFileFilter(new ImageFileFilter(imagetype));
384         int result=filechooser.showSaveDialog(null);
385         if(result== JFileChooser.CANCEL_OPTION) return;
386         
387         File file=filechooser.getSelectedFile();
388         
389         filename=filechooser.getSelectedFile().toString();
390         
391         if(filename != null) {
392             if(filename.indexOf(".")<0){
393                 file=new File(filename+"."+imagetype);
394                 
395             }
396             
397             // if file already exists..as whether to overwrite the file or not
398             if(file.exists()) {
399                 int response=JOptionPane.showConfirmDialog(null, "File already exists. Do you want to overwrite?");
400                 if(response== JOptionPane.NO_OPTION) return;
401                 
402             }
403         }
404         
405         
406         try {
407             ToolInternalFrame frame=(ToolInternalFrame) desktoppane.getSelectedFrame();
408             ToolGraph graph= frame.getGraph();
409             
410             if (graph.getModel().getRootCount() > 0) {
411                 BufferedImage img = toImage(graph);
412                 ImageIO.write(img, imagetype.toLowerCase(), file);
413             };
414         } catch (IOException excp) {
415             JOptionPane.showMessageDialog(null,excp);
416         }
417     }
418     
419     public BufferedImage toImage(ToolGraph graph){
420         Object[] cells=graph.getRoots();
421         if(cells.length>0) {
422             graph.setVisible(true);
423             
424             
425             Rectangle bounds=graph.getBounds();
426             graph.toScreen(bounds);
427             Dimension d=bounds.getSize();
428             BufferedImage img=new BufferedImage(d.width+5,d.height+5,BufferedImage.TYPE_INT_RGB);
429             Graphics2D graphics=(Graphics2D)img.getGraphics();
430             graphics.setColor(Color.WHITE);
431             graphics.fillRect(0,0,img.getWidth(),img.getHeight());
432             
433             Object[] selection=graph.getSelectionCells();
434             boolean gridvisible=graph.isGridVisible();
435             graph.setGridVisible(false);
436             graph.clearSelection();
437             graphics.setColor(Color.BLACK);
438             graphics.setBackground(Color.WHITE);
439             graph.paint(graphics);
440             graph.setSelectionCells(selection);
441             graph.setGridVisible(gridvisible);
442             return img;
443             
444         }
445         return null;
446     }
447     public void updateFrame(ToolInternalFrame internalframe,String titlename) {
448         String title=titlename;
449         if(titlename==null){
450             internalframe.setFrameTitle(internalframe.getGraph().getDiagramType());
451         }
452         else{
453             internalframe.setFrameTitle(/*internalframe.getGraph().getDiagramType()+"--"+*/titlename);
454             internalframe.getPanel().setgraphmodified(false);
455         }
456     }
457     
458     public JDesktopPane getDesktopPane(){
459         return desktoppane;
460     }
461     
462     
463 }
464 
465 
466 class DiagramElement{
467     String type,title;
468     CellElement[] cells;
469     public DiagramElement(String type,String title,CellElement[] cells){
470         this.setDiagramType(type);
471         this.setDiagramTitle(title);
472         this.setDiagramElements(cells);
473     }
474     public void setDiagramType(String type){
475         this.type=type;
476     }
477     public String getDiagramType(){
478         return this.type;
479     }
480     
481     public void setDiagramTitle(String title){
482         this.title=title;
483     }
484     public String getDiagramTitle(){
485         return this.title;
486     }
487     public void setDiagramElements(CellElement[] cells){
488         this.cells=cells;
489     }
490     public CellElement[] getDiagramElements(){
491         return this.cells;
492     }
493     
494 }
495 
496 class CellElement extends Object{
497     AttributeMap map;
498     DefaultGraphCell cell;
499     public CellElement(DefaultGraphCell cell,AttributeMap  map){
500         this.setCell(cell);
501         this.setMap(map);
502         
503     }
504     public void setCell(DefaultGraphCell cell){
505         this.cell=cell;
506     }
507     public DefaultGraphCell getCell(){
508         return this.cell;
509     }
510     public void setMap(AttributeMap  map){
511         this.map=map;
512     }
513     public Map getMap(){
514         return this.map;
515     }
516 }