View Javadoc

1   
2   
3   /*
4    * IconCellView.java
5    *
6    * Created on 28. mai 2005, 21:46
7    */
8   
9   package com.vikash.firsttool.UI;
10  
11  
12  
13  import java.util.*;
14  import java.awt.*;
15  import java.awt.Image;
16  import java.net.*;
17  import java.io.File;
18  import java.awt.event.ActionEvent;
19  import java.awt.event.KeyEvent;
20  import java.awt.geom.Rectangle2D;
21  import java.util.EventObject;
22  
23  import javax.swing.*;
24  import javax.swing.text.BadLocationException;
25  import javax.swing.text.Document;
26  
27  
28  
29  import org.jgraph.JGraph;
30  import org.jgraph.graph.CellView;
31  import org.jgraph.graph.VertexView;
32  import org.jgraph.graph.CellViewRenderer;
33  import org.jgraph.graph.VertexRenderer;
34  import org.jgraph.graph.GraphConstants;
35  import org.jgraph.graph.GraphCellEditor;
36  import org.jgraph.graph.DefaultGraphCellEditor;
37  
38  /***
39   *
40   * @author  studajb
41   */
42  public class IconGraphCellView extends VertexView{
43      
44      IconVertexRenderer renderer = new IconVertexRenderer();
45      ToolVertexEditor editor = new ToolVertexEditor();
46      
47      
48      /*** Creates a new instance of IconCellView */
49      public IconGraphCellView() {
50          super(); 
51      }
52      
53      public IconGraphCellView(Object cell) {
54          super(cell);
55      }
56      
57      public CellViewRenderer getRenderer() {
58          return renderer;
59      }
60      
61      
62      public GraphCellEditor getEditor() {
63          return editor;
64      }
65  }
66  
67  
68  
69  class IconVertexRenderer extends VertexRenderer {
70      public Component getRendererComponent(JGraph graph, CellView view,
71      boolean sel, boolean focus, boolean preview) {
72          graph = graph;
73          isDoubleBuffered = graph.isDoubleBuffered();
74          if (view instanceof VertexView) {
75              this.view = (VertexView) view;
76              setComponentOrientation(graph.getComponentOrientation());
77              if (graph.getEditingCell() != view.getCell()) {
78                  Object label = graph.convertValueToString(view);
79                  if (this.view.isLeaf()|| GraphConstants.isOpaque(view.getAllAttributes())){
80                      
81                      installAttributes(view);
82                  }
83                  
84                  else {
85                      setText(null);
86                      setBorder(null);
87                      setOpaque(false);
88                      setGradientColor(null);
89                      setIcon(null);
90                  }
91                  Object[] labels=GraphConstants.getExtraLabels(view.getAllAttributes());
92                  Object cellname=graph.convertValueToString(labels[0]);
93                  if (label != null)
94                      setText("<html><p align=\"center\">&lt;&lt;"+cellname.toString()+"&gt;&gt;<br>"+label.toString().replaceAll("\n","<br> ")+"</p></html>");
95                  else{
96                      setText("<<"+cellname.toString()+">>");
97                      
98                  }
99              }
100             
101             else
102                 setText(null);
103             
104             graph = graph;
105             this.hasFocus = focus;
106             this.childrenSelected = graph.getSelectionModel()
107             .isChildrenSelected(view.getCell());
108             this.selected = sel;
109             this.preview = preview;
110             
111             return this;
112         }
113         return null;
114     }
115     
116     protected void installAttributes(CellView view) {
117         Map map = view.getAllAttributes();
118         if(GraphConstants.getIcon(map)!=null){
119             String icon=((ImageIcon)GraphConstants.getIcon(map)).toString();
120             
121             URL file;
122             try {
123                 try {
124                     file = new URL(icon);
125                 }
126                 catch ( MalformedURLException ex ) {
127                     file=new File(icon).toURL();
128                 }
129             }
130             catch ( Exception ex ) {
131                 return;
132             }
133             
134             Image img=Toolkit.getDefaultToolkit().getImage(file);
135             MediaTracker tracker=new MediaTracker(this);
136             tracker.addImage(img,0);
137             try{tracker.waitForID(0);}
138             catch(InterruptedException exception) {}
139             
140             setIcon(new ImageIcon(img));
141             
142         }
143         
144         setFont(GraphConstants.getFont(map));
145         setOpaque(GraphConstants.isOpaque(map));
146         setBorder(GraphConstants.getBorder(map));
147         setVerticalAlignment(GraphConstants.getVerticalAlignment(map));
148         setHorizontalAlignment(GraphConstants.getHorizontalAlignment(map));
149         setVerticalTextPosition(GraphConstants.getVerticalTextPosition(map));
150         setHorizontalTextPosition(GraphConstants.getHorizontalTextPosition(map));
151         bordercolor = GraphConstants.getBorderColor(map);
152         borderWidth = Math.max(1, Math.round(GraphConstants.getLineWidth(map)));
153         if (getBorder() == null && bordercolor != null)
154             setBorder(BorderFactory.createLineBorder(bordercolor, borderWidth));
155         Color foreground = GraphConstants.getForeground(map);
156         setForeground((foreground != null) ? foreground : defaultForeground);
157         Color background = GraphConstants.getBackground(map);
158         setBackground((background != null) ? background : defaultBackground);
159         Color gradientColor = GraphConstants.getGradientColor(map);
160         setGradientColor(gradientColor);
161         
162         
163     }
164 }
165 
166