/Users/lyon/j4p/src/graphics/graph/GraphManager.java

1    package graphics.graph; 
2     
3    import java.awt.*; 
4     
5    public class GraphManager { 
6        /** 
7         *  This is the class formerly known as GraphPanel. 
8         *  That name is no longer appropriate since it stopped extending Panel. 
9         *  It owns a Panel (ImagePanel to be precise) and its responsibility 
10        *  is to manager the ThreadManager, the MouseEventHandler and other 
11        *  business objects and insure that they all work together to update 
12        *  the view on the ImagePanel. 
13        */ 
14       private ImagePanel ip = ImagePanel.getImagePanel(); 
15       private EdgesManager em = GraphFactory.getEdgesManager(); 
16       private NodeDrawer nd = GraphFactory.getNodeDrawer(); 
17       private EdgesBean eb = EdgesBean.getEdgesBean(); 
18       private ImageUtil imageUtil; 
19       private MouseEventHandler meh; 
20       private RelaxThreadMgr rtm; 
21    
22       private static GraphManager gp = new GraphManager(); 
23    
24       private GraphManager() { 
25       } 
26    
27       public static GraphManager getGraphManager() { 
28           return gp; 
29       } 
30    
31       public RelaxThreadMgr getRelaxThreadMgr() { 
32           return rtm; 
33       } 
34    
35       private ImageUtil getImageUtil() 
36               /** 
37                *  Getter for the ImageUtil attribute 
38                */ { 
39           return imageUtil; 
40       } 
41    
42       public void update(java.awt.Graphics g) 
43               /** 
44                *  The responsibility of this window is to handle the business 
45                *  rules for how the window is supposed to be updated. 
46                *  ImageUtil is used to double buffer the image so it won't flicker. 
47                *  The NodeDrawer nd is used to actually draw where the nodes are 
48                *  supposed to be at that instant. The EdgesBean and the EdgesManager 
49                *  supply the information. 
50                * 
51                *  @param  java.awt.Graphics 
52                */ { 
53           Dimension d = ip.getSize(); 
54           if ((getImageUtil().getOffScreen() == null) || 
55                   (d.width != getImageUtil().getOffScreenSize().width) || 
56                   (d.height != getImageUtil().getOffScreenSize().height)) 
57               getImageUtil().createBuffer(ip.getSize()); 
58    
59           getImageUtil().getOffGraphics().setColor(ip.getBackground()); 
60           getImageUtil().getOffGraphics().fillRect(0, 0, d.width, d.height); 
61           for (int i = 0; i < eb.getNumberOfEdges(); i++) { 
62               em.drawArcAndString(i); 
63           } 
64           nd.drawNodes(getImageUtil()); 
65           g.drawImage(getImageUtil().getOffScreen(), 0, 0, null); 
66       } 
67    
68       public void start() 
69               /** 
70                *  This method sets up all of the attributes that need 
71                *  to interface with the ImagePanel. This includes: 
72                *  <OL> 
73                *  <LI> ImageUtil   on which it sets the panel to ImagePanel </LI> 
74                *  <LI> MouseEventHandler </LI> 
75                *  <LI> adding MouseListener    </LI> 
76                *  <LI> RelaxThreadMgr    </LI> 
77                *  <LI> Finally, this (GraphManager) gets set on the ImagePanel as well. </LI> 
78                *  </OL> 
79                *  ImageUtil 
80                */ { 
81           imageUtil = GraphFactory.getImageUtil(); 
82           imageUtil.setPanel(ip); 
83    
84           ip.setGraphManager(this); 
85           ip.addMouseListener(meh); 
86           meh = new MouseEventHandler(ip); 
87           rtm = new RelaxThreadMgr(ip); 
88    
89           getRelaxThreadMgr().start(); 
90       } 
91   }