/Users/lyon/j4p/src/classUtils/delegate/DelegateGui.java

1    package classUtils.delegate; 
2     
3    import gui.ClosableJFrame; 
4    import gui.run.RunButton; 
5    import gui.run.RunCheckBox; 
6     
7    import javax.swing.JFrame; 
8    import javax.swing.JLabel; 
9    import javax.swing.JList; 
10   import javax.swing.JOptionPane; 
11   import javax.swing.JPanel; 
12   import javax.swing.JScrollPane; 
13   import java.awt.BorderLayout; 
14   import java.awt.Component; 
15   import java.awt.Container; 
16   import java.awt.Dimension; 
17   import java.awt.FlowLayout; 
18   import java.awt.GridLayout; 
19   import java.awt.Insets; 
20   import java.awt.Point; 
21   import java.util.Vector; 
22    
23   /** 
24    * DelegateGui class To replace inheritance with delegation: I dentify the 
25    * super class. Make an instance of the super class in the child. Using 
26    * semiautomatic static proxy delegation create a proxy for the primordial 
27    * super-class. Remove the extends clause. Replace all references to the 
28    * parent with references to the proxy. 
29    */ 
30   public class DelegateGui { 
31       private JFrame jf = new ClosableJFrame("Disambiguate"); 
32       private JList duplicateJList = new JList(); 
33       private JList doJList = new JList(); 
34       private JScrollPane duplicateScrollPane = new JScrollPane(); 
35       private JScrollPane selectedMethodsScrollPane = new JScrollPane(); 
36       private JLabel selectedMethodLabel = new JLabel("Selected Method"); 
37       private JLabel dupLabel = new JLabel("Duplicate Methods"); 
38       private JPanel listPanel = getScrollPanels(); 
39       private JPanel controlPanel = getControlPanel(); 
40       private boolean isTopologicallySorted = false; 
41       private DelegateSynthesizer ds = null; 
42    
43       public DelegateGui() { 
44       } 
45    
46       public void setVisible(boolean v) { 
47           jf.setVisible(v); 
48       } 
49    
50       public void setInstanceList(Vector v) { 
51           DelegateSynthesizer ds = getDelegateSynthesizer(); 
52           for (int i = 0; i < v.size(); i++) 
53               ds.add(v.elementAt(i)); 
54           ds.process(); 
55           duplicateJList.setListData(ds.getDuplicateMethodVector()); 
56           dupLabel.setText("Duplicate Methods: " 
57                   + 
58                   ds.getDuplicateMethodVector() 
59                   .size()); 
60           selectedMethodLabel.setText("selected Method: " + 
61                   ds.getCompositeMethodVector() 
62                   .size()); 
63       } 
64    
65       public void init() { 
66           buildScrollPane(); 
67           duplicateJList.setVisible(true); 
68           initDoScrollPane(); 
69           doJList.setVisible(true); 
70           initListPanel(); 
71           addPanelsSetLayout(); 
72           jf.setSize(650, 250); 
73       } 
74    
75       private void addPanelsSetLayout() { 
76           Container c = jf.getContentPane(); 
77           c.setLayout(new BorderLayout()); 
78           c.add(getCounterPanel(), "North"); 
79           c.add(listPanel, "Center"); 
80           c.add(controlPanel, "South"); 
81       } 
82    
83       private JPanel getScrollPanels() { 
84           JPanel jp = new JPanel(); 
85           jp.setLayout(new GridLayout(1, 0)); 
86           jp.add(duplicateScrollPane); 
87           jp.add(selectedMethodsScrollPane); 
88           return jp; 
89       } 
90    
91       private JPanel getCounterPanel() { 
92           JPanel jp = new JPanel(); 
93           jp.add(dupLabel); 
94           jp.add(selectedMethodLabel); 
95           jp.setLayout(new GridLayout(1, 0)); 
96           return jp; 
97       } 
98    
99       private JPanel getControlPanel() { 
100          JPanel jp = new JPanel(); 
101          jp.setLayout(new FlowLayout()); 
102          jp.add(new RunButton("generate rmi code") { 
103              public void run() { 
104                  generateRmiCode(); 
105              } 
106          }); 
107          jp.add(new RunButton("process") { 
108              public void run() { 
109                  process(); 
110              } 
111          }); 
112          jp.add(new RunButton("add") { 
113              public void run() { 
114                  doAdd(); 
115              } 
116          }); 
117          jp.add(new RunButton("Delete") { 
118              public void run() { 
119                  doDelete(); 
120              } 
121          }); 
122          jp.add(new RunCheckBox("topological sorting") { 
123              public void run() { 
124                  isTopologicallySorted = isSelected(); 
125              } 
126          }); 
127          jp.add(new RunButton("exit") { 
128              public void run() { 
129                  exit(); 
130              } 
131          }); 
132          return jp; 
133      } 
134   
135      private void initListPanel() { 
136          //getListPanel().setVisible(true); 
137          listPanel.setLayout(new GridLayout(1, 2)); 
138      } 
139   
140      private void initDoScrollPane() { 
141          selectedMethodsScrollPane.setSize(new Dimension(300, 190)); 
142          selectedMethodsScrollPane.setVisible(true); 
143          selectedMethodsScrollPane.setLocation(new Point(370, 30)); 
144          selectedMethodsScrollPane.getViewport().add(doJList); 
145      } 
146   
147      private void buildScrollPane() { 
148          duplicateScrollPane.setSize(new Dimension(300, 190)); 
149          duplicateScrollPane.setLocation(new Point(10, 30)); 
150          duplicateScrollPane.getViewport().add(duplicateJList); 
151      } 
152   
153      private boolean isShowing = false; 
154   
155      public void addNotify() { 
156          jf.addNotify(); 
157          if (isShowing) return; 
158   
159          // move components to account for insets 
160          Insets insets = jf.getInsets(); 
161          Component[] components = jf.getComponents(); 
162          for (int i = 0; 
163               i < components.length; 
164               i++) { 
165              Point location = components[i].getLocation(); 
166              location.move(location.x, 
167                      location.y + 
168                      insets.top); 
169              components[i].setLocation(location); 
170          } 
171          isShowing = true; 
172      } 
173   
174      void exit() { 
175          jf.setVisible(false); 
176          jf.dispose(); 
177          System.exit(0); 
178      } 
179   
180      public void doListValueChanged(javax.swing.event.ListSelectionEvent e) { 
181      } 
182   
183      public DelegateSynthesizer getDelegateSynthesizer() { 
184          if (ds == null) 
185              ds = new DelegateSynthesizer(); 
186          return ds; 
187      } 
188   
189      private void doAdd() { 
190          Object o = 
191                  duplicateJList.getSelectedValue(); 
192          int i = duplicateJList.getSelectedIndex(); 
193          if (getDelegateSynthesizer() 
194                  .getDuplicateMethodVector() 
195                  .size() == 
196                  0 || 
197                  i == -1) { 
198              JOptionPane.showMessageDialog(null, 
199                      "Please select a method" 
200                      + 
201                      " from Duplicate Methods window", 
202                      "STOP", 
203                      JOptionPane.ERROR_MESSAGE); 
204              return; 
205          } 
206          getDelegateSynthesizer() 
207                  .removeDupMethod(o); 
208          // add to do list 
209          getDelegateSynthesizer().addDoMethod(o); 
210          //re-display the duplicate list 
211          duplicateJList.setListData(getDelegateSynthesizer() 
212                  .getDuplicateMethodVector()); 
213          //refresh the do list 
214          doJList.setListData(getDelegateSynthesizer() 
215                  .getCompositeMethodVector()); 
216          dupLabel.setText("Duplicate Methods: " + 
217                  getDelegateSynthesizer() 
218                  .getDuplicateMethodVector() 
219                  .size()); 
220          selectedMethodLabel.setText("selected Method: " + 
221                  getDelegateSynthesizer() 
222                  .getCompositeMethodVector() 
223                  .size()); 
224      } 
225   
226      private void doDelete() { 
227          Object o = doJList.getSelectedValue(); 
228          int i = doJList.getSelectedIndex(); 
229          DelegateSynthesizer delegateSynthesizer = getDelegateSynthesizer(); 
230          Vector doMethodList = delegateSynthesizer 
231                  .getCompositeMethodVector(); 
232          if (doMethodList 
233                  .size() == 
234                  0 || 
235                  i == -1) { 
236              JOptionPane.showMessageDialog(null, 
237                      "Please select a method from Chosed Methods Window", 
238                      "STOP", 
239                      JOptionPane.ERROR_MESSAGE); 
240              return; 
241          } 
242          //delete from the do list 
243          delegateSynthesizer.removeDoMethod(o); 
244          // add to dup list 
245          delegateSynthesizer.addDupMethod(o); 
246          //re-display the duplicate list 
247          Vector dupMethodList = delegateSynthesizer.getDuplicateMethodVector(); 
248          duplicateJList.setListData(dupMethodList); 
249          //refresh the do list 
250          doJList.setListData(doMethodList); 
251          dupLabel.setText("Duplicate Methods: " + 
252                  dupMethodList.size()); 
253          selectedMethodLabel.setText("selected Method: " + 
254                  doMethodList.size()); 
255      } 
256        private void generateRmiCode() { 
257          DelegateSynthesizer ds = getDelegateSynthesizer(); 
258          ds.setToplogicalSorting(isTopologicallySorted); 
259          System.out.println(ds); 
260      } 
261      private void process() { 
262          DelegateSynthesizer ds = getDelegateSynthesizer(); 
263          ds.setToplogicalSorting(isTopologicallySorted); 
264          ds.createFile(); 
265      } 
266   
267      public void setTopologicallySorted(boolean isTopologicallySorted) { 
268          this.isTopologicallySorted = isTopologicallySorted; 
269      } 
270  }