View Javadoc

1   package net.sf.mmapps.applications.developer.modules.appview;
2   
3   import java.awt.*;
4   import java.awt.datatransfer.*;
5   import java.awt.dnd.*;
6   import java.awt.event.*;
7   import java.awt.image.BufferedImage;
8   import java.io.*;
9   import java.util.Iterator;
10  
11  import javax.swing.*;
12  
13  import net.sf.mmapps.applications.developer.Developer;
14  import net.sf.mmapps.applications.developer.dnd.XMLConfigurationTransferable;
15  import javax.imageio.*;
16  import net.sf.mmapps.modules.config.*;
17  
18  import com.touchgraph.graphlayout.*;
19  import com.touchgraph.graphlayout.interaction.*;
20  import org.apache.commons.logging.*;
21  /***
22   * @author Kees Jongenburger
23   * @version $Id: ApplicationViewer.java,v 1.3 2004/07/16 20:55:48 keesj Exp $
24   */
25  public class ApplicationViewer extends GLPanel implements ActionListener, DropTargetListener {
26      private static Log log = LogFactory.getLog(ApplicationViewer.class);
27      JButton saveButton;
28      JRadioButton stopDampButton;
29      JRadioButton drawFieldsButton;
30      //TGMMBasePanel tgMMBasePanel;
31  
32      String appName;
33  
34      public ApplicationViewer(Configuration conf, String appName) {
35  
36          setBackground(javax.swing.plaf.metal.MetalLookAndFeel.getControl());
37          this.appName = appName;
38  
39          tgLensSet = new TGLensSet();
40          tgPanel = new TGMMBasePanel();
41          hvScroll = new HVScroll(tgPanel, tgLensSet);
42          zoomScroll = new ZoomScroll(tgPanel);
43          rotateScroll = new RotateScroll(tgPanel);
44          localityScroll = new LocalityScroll(tgPanel);
45  
46          buildPanel();
47          buildLens();
48          tgPanel.setLensSet(tgLensSet);
49          addUIs();
50  
51          //quick hack
52          JToolBar panel = new JToolBar();
53          panel.add(saveButton = new JButton("save"));
54          saveButton.addActionListener(this);
55  
56          panel.add(stopDampButton = new JRadioButton("damp", true));
57          stopDampButton.addActionListener(this);
58          panel.add(drawFieldsButton = new JRadioButton("fields", false));
59          drawFieldsButton.addActionListener(this);
60  
61          panel.setLayout(new FlowLayout());
62          add(panel, BorderLayout.SOUTH);
63  
64          try {
65              addConfig(conf, appName);
66          } catch (Exception e) {
67              log.warn("failed to add config["+ appName+"]" , e);
68          };
69  
70          //add(tgMMBasePanel, BorderLayout.CENTER);
71          setVisible(true);
72          DropTarget dropTarget = new DropTarget(this, this);
73      }
74  
75      public Dimension getPreferredSize() {
76          return new Dimension(600, 400);
77      }
78  
79      public void addConfig(Configuration conf, String appName) {
80  
81          try {
82              ApplicationConfiguration appConfig = conf.getApplicationConfigurations().getApplicationConfiguration(appName);
83              Strings neededBuilders = appConfig.getRequiredNodeManagers();
84              for (int y = 0; y < neededBuilders.size(); y++) {
85                  String neededBuilder = neededBuilders.getString(y);
86                  Node node = tgPanel.findNode(neededBuilder);
87                  if (node == null) {
88                      NodeManagerConfiguration config = conf.getNodeManagerConfiguration(neededBuilder);
89                      if (config != null) {
90                          node = new MMBaseNode(config);
91                      } else {
92                          node = new Node(neededBuilder, Node.TYPE_ELLIPSE, javax.swing.plaf.metal.MetalLookAndFeel.getControl(), neededBuilder);
93                      }
94                      tgPanel.addNode(node);
95                  }
96              }
97              RelationManagerConfigurations relmans = appConfig.getRelationManagerConfigurations();
98              for (int y = 0; y < relmans.size(); y++) {
99                  RelationManagerConfiguration relman = relmans.getRelationManagerConfiguration(y);
100                 log.info(relman.getSourceNodeManagerName() + " " + relman.getDestinationNodeManagerName() + " " + relman.getRelationTypeName());
101                 Node startNode = tgPanel.findNode(relman.getSourceNodeManagerName());
102                 Node endNode = tgPanel.findNode(relman.getDestinationNodeManagerName());
103                 if (startNode == null) {
104                     startNode = new Node(relman.getSourceNodeManagerName(), relman.getSourceNodeManagerName());
105                     log.warn("start node not defined.. adding one on the fly. this is bad bad bad. make sure all resources are defined in the needebuilderlist");
106                     tgPanel.addNode(startNode);
107                 }
108                 if (endNode == null) {
109                     endNode = new Node(relman.getDestinationNodeManagerName(), relman.getDestinationNodeManagerName());
110                     log.warn("end node not defined.. adding one on the fly. this is bad bad bad. make sure all resources are defined in the needebuilderlist");
111                     tgPanel.addNode(endNode);
112                 }
113                 Edge e = tgPanel.findEdge(startNode, endNode);
114                 //for some reasone findEdge also returns if only from or to is valid
115                 if (e != null && ((e.from == startNode && e.to == endNode) || (e.to == endNode && e.from == startNode))) {
116 
117                     log.debug("found edge");
118                     if (e instanceof MMBaseEdge) {
119                         log.debug("instanceof MMBaseEdge");
120 
121                         MMBaseEdge edge = (MMBaseEdge) e;
122                         edge.addRelationManagerConfiguration(relman);
123                     } else {
124                         MMBaseEdge edge = new MMBaseEdge(relman, startNode, endNode, 1000);
125                         tgPanel.addEdge(edge);
126                     }
127                 } else {
128                     MMBaseEdge edge = new MMBaseEdge(relman, startNode, endNode, 1000);
129                     tgPanel.addEdge(edge);
130                 }
131             }
132 
133             //inheritance code
134             for (int y = 0; y < neededBuilders.size(); y++) {
135                 String neededBuilder = neededBuilders.getString(y);
136                 Node node = tgPanel.findNode(neededBuilder);
137                 NodeManagerConfiguration nm = appConfig.getNodeManagerConfigurations().getNodeManagerConfiguration(neededBuilder);
138                 if (nm != null) {
139                     NodeManagerConfiguration parent = appConfig.getNodeManagerConfigurations().getNodeManagerConfiguration(nm.getExtends());
140                     if (parent != null) {
141                         Node parentNode = tgPanel.findNode(parent.getName());
142                         MMBaseInheritanceEdge edge = new MMBaseInheritanceEdge(node, parentNode, 4000);
143                         tgPanel.addEdge(edge);
144 
145                     }
146                 }
147             }
148 
149         } catch (Exception e) {
150             log.warn(e);
151         };
152 
153     }
154 
155     public void actionPerformed(ActionEvent actionEvent) {
156 
157         if (actionEvent.getSource() == stopDampButton) {
158             if (((JRadioButton) stopDampButton).getModel().isSelected()) {
159                 tgPanel.kstartDamper();
160             } else {
161                 tgPanel.kstopDamper();
162             }
163         }
164 
165         if (actionEvent.getSource() == drawFieldsButton) {
166             boolean show = false;
167             if (((JRadioButton) drawFieldsButton).getModel().isSelected()) {
168                 show = true;
169             }
170             for (Iterator iter = tgPanel.getAllNodes(); iter.hasNext();) {
171                 Node e = (Node) iter.next();
172                 if (e instanceof MMBaseNode) {
173                     ((MMBaseNode) e).showFields(show);
174                 }
175             }
176         }
177         if (actionEvent.getSource() == saveButton) { //HACK HACK HACK
178             try {
179                 Dimension d = tgPanel.getSize();
180                 BufferedImage bufferedImage = new BufferedImage(d.width, d.height,BufferedImage.TYPE_INT_RGB);
181                 //Image img = createImage(d.width, d.height);
182                 Graphics2D g2d = (Graphics2D)bufferedImage.getGraphics();
183                 tgPanel.update(g2d);
184                 File file = new File(System.getProperty("user.home") + "/" + appName + ".png");
185 
186                 ImageIO.write(bufferedImage, "png", file);
187                 Developer.getInstance().getStatusBar().showStatus("image saved to " + file.getCanonicalPath());
188             } catch (Exception e) {
189                 log.warn("error while saving graphics" ,e);
190             };
191         }
192     }
193 
194     public void dragEnter(DropTargetDragEvent dtde) {}
195     public void dragExit(DropTargetEvent dte) {}
196     public void dragOver(DropTargetDragEvent dtde) {}
197     public void dropActionChanged(DropTargetDragEvent dtde) {}
198     public void drop(DropTargetDropEvent dtde) {
199         Transferable tr = dtde.getTransferable();
200         try {
201             if (tr.isDataFlavorSupported(XMLConfigurationTransferable.XML_FLAVOR)) {
202                 XMLConfigurationTransferable confData = (XMLConfigurationTransferable) tr.getTransferData(XMLConfigurationTransferable.XML_FLAVOR);
203                 log.debug(confData.getXMLData());
204 
205                 dtde.dropComplete(true);
206             } else {
207                 dtde.rejectDrop();
208             }
209         } catch (UnsupportedFlavorException e) {
210             dtde.rejectDrop();
211         } catch (IOException ioe) {
212             dtde.rejectDrop();
213         }
214     }
215 
216 }