View Javadoc

1   /*
2    * DiagramTreePane.java
3    *
4    * Created on 6. juli 2005, 01:08
5    */
6   
7   package com.vikash.firsttool.UI;
8   import com.vikash.firsttool.Diagram.*;
9   import java.awt.Component;
10  import java.awt.Rectangle;
11  import java.awt.event.*;
12  import java.net.URL;
13  
14  import javax.swing.*;
15  import javax.swing.tree.*;
16  /***
17   *
18   * @author  studajb
19   */
20  public class DiagramTreePane extends JTree{
21      DiagramTreeModel diagtreemodel;
22      JDesktopPane desktoppane;
23      JPopupMenu pop=new JPopupMenu();
24      /*** Creates a new instance of DiagramTreePane */
25      public DiagramTreePane(MainFrame frame) {
26          this.desktoppane=frame.getDesktopPane();
27          DefaultMutableTreeNode diagramroot= new  DefaultMutableTreeNode("Diagrams");
28          diagtreemodel=new DiagramTreeModel(diagramroot,this); 
29          this.setModel(diagtreemodel);
30          this.setShowsRootHandles(true); 
31          this.setEditable(false);
32          this.setCellRenderer(new DiagramTreeCellRenderer());
33          this.addMouseListener(new MouseAdapter(){
34              public void mouseReleased(MouseEvent e){
35                  TreePath path=getPathForLocation(e.getX(),e.getY());
36                  setSelectionPath(path);
37                  if(path==null)return;
38                  DefaultMutableTreeNode selectednode=(DefaultMutableTreeNode)path.getLastPathComponent();
39                  if(!(selectednode instanceof TreeElement))return;
40                  if(e.isPopupTrigger()){
41                      
42                      int x=e.getX();
43                      int y=e.getY();
44                      pop.show(e.getComponent(), x, y);
45                  }
46              }
47              public void mousePressed(MouseEvent e) {
48                  if(e.getClickCount()==2){
49                      // show or display the window(internalframe) conatining the cell represented by the selected tree element
50                      TreePath path=getPathForLocation(e.getX(),e.getY());
51                      int selRow=getRowForLocation(e.getX(),e.getY());
52                      setSelectionPath(path);
53                      if(path==null || selRow==-1)return;
54                      DefaultMutableTreeNode selectednode=(DefaultMutableTreeNode)path.getLastPathComponent();
55                      if(!(selectednode instanceof TreeElement))return;
56                      ToolInternalFrame frame=((TreeElement)selectednode).getInternalFrame();
57                      try{
58                          if(!(frame.isVisible())){
59                              frame.show();
60                              
61                          }
62                          Rectangle r = frame.getBounds();
63                          if (r.width <= 0 || r.height <= 0) {
64                          	r = desktoppane.getBounds();
65                          	int margin = Math.min(r.width, r.height) / 10;
66                          	frame.setBounds(margin, margin, r.width - 2 * margin, r.height - 2 * margin);
67                          }
68                          
69                          if(frame.isIcon())
70                              frame.setIcon(false);
71                          
72                          frame.moveToFront();
73                          desktoppane.setSelectedFrame(frame);
74                          frame.setSelected(true);
75                      }catch(Exception excp) {
76                          JOptionPane.showMessageDialog(null,excp);
77                      }
78                  }
79              }
80          });
81          pop.add(new AbstractAction("Edit Diagram Name"){
82              public void actionPerformed(ActionEvent e){
83                  TreeElement treeelement=(TreeElement)getLastSelectedPathComponent();
84                  String edit = JOptionPane.showInputDialog("Type the name of the diagram",treeelement);
85                  if (edit != null && edit.length() > 0){
86                      
87                      ToolInternalFrame frame=treeelement.getInternalFrame();
88                      frame.setTitle(edit);
89                      treeelement.setUserObject(edit);
90                      ((DiagramTreeModel)getModel()).setTreeChanged(true);
91                      try{
92                          if(!(frame.isVisible())){
93                              frame.setVisible(true);
94                              desktoppane.add(frame);
95                          }
96                          
97                          if(frame.isIcon())frame.setIcon(false);
98                          frame.toFront();
99                          desktoppane.setSelectedFrame(frame);
100                     }catch(Exception excp) {
101                         JOptionPane.showMessageDialog(null,excp);
102                     }
103                 }
104                 
105             }
106         });
107         pop.add(new AbstractAction("Remove the Diagram"){
108             public void actionPerformed(ActionEvent e){
109                 
110                 TreeElement treeelement=(TreeElement)getLastSelectedPathComponent();
111                 ToolInternalFrame frame=treeelement.getInternalFrame();
112                 int selection=JOptionPane.showConfirmDialog(desktoppane,"Delete the diagram?","Delete Diagram",JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE);
113                 if(selection==JOptionPane.NO_OPTION) return;
114                 else{
115                     frame.dispose();
116                     frame.setDiagramRemoved(true);
117                     //desktoppane.remove(frame);
118                     ((DefaultTreeModel)getModel()).removeNodeFromParent(treeelement);
119                 }
120                 
121             }
122             
123         });
124     }
125     public TreeModel getModel(){
126         return (TreeModel)diagtreemodel;
127     }
128 }
129 class DiagramTreeCellRenderer extends DefaultTreeCellRenderer{
130     
131     public Component getTreeCellRendererComponent(
132     JTree tree,
133     Object value,
134     boolean sel,
135     boolean expanded,
136     boolean leaf,
137     int row,
138     boolean _hasFocus) {
139         
140         Component r= super.getTreeCellRendererComponent(tree,value,sel,expanded,leaf,row,_hasFocus);
141         JLabel lab;
142         URL icon;
143         if (r instanceof JLabel) {
144             lab = (JLabel) r;
145             if(value!=null){
146                 
147                 if(value.toString()!=null){
148                     ToolGraph graph=null;
149                     if(value instanceof TreeElement){
150                         graph=((TreeElement)value).getInternalFrame().getGraph();
151                     }
152                     if(value.toString().equals("SWOT Diagrams") || (value instanceof TreeElement && graph instanceof SWOTDiagram)){
153                         icon=getClass().getClassLoader().getResource("com/vikash/firsttool/resources/SWOT.gif");
154                         lab.setIcon(new ImageIcon(icon));
155                         return lab;
156                     }
157                     else if(value.toString().equals("Asset Diagrams")||(value instanceof TreeElement && graph instanceof AssetDiagram)){
158                         icon=getClass().getClassLoader().getResource("com/vikash/firsttool/resources/Asset.gif");
159                         lab.setIcon(new ImageIcon(icon));
160                         return lab;
161                     }
162                     
163                     else if(value.toString().equals("UnwantedIncident Diagrams")||(value instanceof TreeElement && graph instanceof UnwantedIncidentDiagram)){
164                         icon=getClass().getClassLoader().getResource("com/vikash/firsttool/resources/UnwantedIncident.gif");
165                         lab.setIcon(new ImageIcon(icon));
166                         return lab;
167                     }
168                     else if(value.toString().equals("Risk Diagrams")||(value instanceof TreeElement && graph instanceof RiskDiagram)){
169                         icon=getClass().getClassLoader().getResource("com/vikash/firsttool/resources/Risk.gif");
170                         lab.setIcon(new ImageIcon(icon));
171                         return lab;
172                     }
173                     else if(value.toString().equals("Treatment Diagrams")||(value instanceof TreeElement && graph instanceof TreatmentDiagram)){
174                         icon=getClass().getClassLoader().getResource("com/vikash/firsttool/resources/Treatment.gif");
175                         lab.setIcon(new ImageIcon(icon));
176                         return lab;
177                     }
178                     else if(value.toString().equals("TreatmentEffect Diagrams")||(value instanceof TreeElement && graph instanceof TreatmentEffectDiagram)){
179                         icon=getClass().getClassLoader().getResource("com/vikash/firsttool/resources/TreatmentEffect.gif");
180                         lab.setIcon(new ImageIcon(icon));
181                         return lab;
182                     }
183                     
184                     
185                     else {
186                         
187                         return super.getTreeCellRendererComponent(tree,value,sel,expanded,leaf,row,_hasFocus);
188                     }
189                     
190                 }
191                 else
192                     return super.getTreeCellRendererComponent(tree,value,sel,expanded,leaf,row,_hasFocus);
193             }
194             
195             else
196                 return super.getTreeCellRendererComponent(tree,value,sel,expanded,leaf,row,_hasFocus);
197         }
198         else
199             return super.getTreeCellRendererComponent(tree,value,sel,expanded,leaf,row,_hasFocus);
200         
201     }
202     
203 }
204 
205 
206