KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > calipso > reportgenerator > userinterface > ColorConditionUI


1 package com.calipso.reportgenerator.userinterface;
2
3 import com.calipso.reportgenerator.common.ReportGeneratorConfiguration;
4 import javax.swing.tree.*;
5 import javax.swing.*;
6 import javax.swing.*;
7 import javax.swing.event.TreeSelectionEvent JavaDoc;
8 import java.awt.*;
9 import java.awt.event.ActionListener JavaDoc;
10 import java.awt.event.ActionEvent JavaDoc;
11 import java.util.*;
12
13 public class ColorConditionUI extends JDialog {
14   private DefaultMutableTreeNode root;
15   private DefaultMutableTreeNode current;
16   private DefaultTreeModel treeModel;
17   private ColorConditionManager conditionManager;
18   private Map metricStates;
19   private JTree tree;
20   private JButton addButton;
21   private JButton removeButton;
22   private JButton modifyButton;
23   private JButton closeButton;
24
25   /**
26    * Genera un frame tipo modal , donde se genera un tree con los valores de las metricas
27    *
28    * @param owner
29    * @param modal
30    * @param conditionManager
31    * @param metricStates
32    */

33   public ColorConditionUI(Frame owner, boolean modal, ColorConditionManager conditionManager, Map metricStates, ReportGeneratorConfiguration conf) {
34     super(owner, modal);
35     Image icon = conf.getImage("icon");
36     if(icon != null) {
37       owner.setIconImage(icon);
38     }
39     this.conditionManager = conditionManager;
40     this.metricStates = metricStates;
41     getContentPane().add(crateCenterJScrollPane(), BorderLayout.CENTER);
42     getContentPane().add(createEastPanel(), BorderLayout.EAST);
43     modifyButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
44       public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
45         modifySelection();
46       }
47     });
48
49     addButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
50       public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
51         executeSelection();
52       }
53     });
54     tree.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener JavaDoc() {
55       public void valueChanged(javax.swing.event.TreeSelectionEvent JavaDoc evt) {
56         treeValueChanged(evt);
57       }
58     });
59     removeButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
60       public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
61         removeColorCondition(current);
62
63       }
64     });
65
66     closeButton.addActionListener(new ActionListener JavaDoc() {
67       public void actionPerformed(ActionEvent JavaDoc e) {
68         close();
69       }
70     });
71     pack();
72     java.awt.Dimension JavaDoc screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
73     setSize(new java.awt.Dimension JavaDoc(587, 226));
74     setLocation((screenSize.width - 587) / 2, (screenSize.height - 226) / 2);
75     setResizable(false);
76     checkBtnState();
77   }
78
79   /**
80    * Cierra el Dialog ColorConditionUI
81    */

82   private void close() {
83     this.setVisible(false);
84     this.dispose();
85   }
86
87   /**
88    * Borra un item del tree
89    * @param current
90    */

91   private void removeColorCondition(DefaultMutableTreeNode current) {
92     if (current.getLevel() > 1) {
93       ColorCondition colorCondition = (ColorCondition) current.getUserObject();
94       conditionManager.remove(colorCondition);
95       treeModel.removeNodeFromParent(current);
96       treeModel.reload();
97     }
98   }
99
100   /**
101    * Este evento mantiene actualizado el nodo que se esta seleccionando actualmente
102    * @param evt
103    */

104   private void treeValueChanged(TreeSelectionEvent JavaDoc evt) {
105     DefaultMutableTreeNode node = (DefaultMutableTreeNode) (evt.getPath().getLastPathComponent());
106     current = node;
107     checkBtnState();
108   }
109
110   /**
111    * Setea el estado de los botones para agregar o borrar
112    */

113   private void checkBtnState() {
114     if (current == null || current.getLevel() == 0) {
115       addButton.setEnabled(false);
116       removeButton.setEnabled(false);
117       modifyButton.setEnabled(false);
118     }
119     else {
120       if (current.getLevel() == 1) {
121         addButton.setEnabled(true);
122         removeButton.setEnabled(false);
123         modifyButton.setEnabled(false);
124       }
125       else {
126         addButton.setEnabled(true);
127         modifyButton.setEnabled(true);
128         removeButton.setEnabled(true);
129       }
130     }
131   }
132
133   /**
134    * Retorna el nodo del tree que ha sido seleccionado
135    * @param node
136    * @return
137    */

138   private DefaultMutableTreeNode getCurrent(DefaultMutableTreeNode node, boolean modify) {
139     int level = node.getLevel();
140     if (level > 0) {
141       if (level == 1) {
142         return node;
143       } else if(level == 2 && modify){
144         return node;
145       } else {
146         return (DefaultMutableTreeNode) node.getParent();
147       }
148     }
149     return null;
150   }
151
152   private void modifySelection() {
153     DefaultMutableTreeNode node = getCurrent(current, true);
154     DefaultMutableTreeNode previous = node;
155     DefaultMutableTreeNode parent = (DefaultMutableTreeNode) node.getParent();
156     Collection collection = conditionManager.getConditionsFor(((MetricState)parent.getUserObject()).getName());
157     Iterator iterator = collection.iterator();
158     ColorCondition currentCondition = null;
159     while(iterator.hasNext()) {
160       currentCondition = (ColorCondition) iterator.next();
161       if(currentCondition.toString().equals(previous.getUserObject().toString())) {
162         break;
163       }
164     }
165
166     if(node != null) {
167       ColorCondition colorCondition = ChoosSemaphoreSelection.newColorCondition(null, (MetricState) parent.getUserObject(), currentCondition);
168       if (colorCondition != null) {
169         conditionManager.put(colorCondition);
170         conditionManager.remove(currentCondition);
171         treeModel.removeNodeFromParent(node);
172         treeModel.nodesWereRemoved(node, null, null);
173         treeModel.insertNodeInto(new DefaultMutableTreeNode(colorCondition), parent, 0);
174         treeModel.nodeStructureChanged(parent);
175         treeModel.reload();
176       }
177     }
178   }
179
180   private ColorCondition getConditionManagerFrom(DefaultMutableTreeNode previous, DefaultMutableTreeNode parent) {
181     ColorCondition returnValue = null;
182     Collection collection = conditionManager.getConditionsFor(parent.getUserObject().toString().toUpperCase());
183     Iterator iterator = collection.iterator();
184     while(iterator.hasNext()) {
185       ColorCondition colorCondition = (ColorCondition) iterator.next();
186       if(colorCondition.toString().equals(previous.getUserObject().toString())) {
187         returnValue = colorCondition;
188         break;
189       }
190     }
191     return returnValue;
192   }
193
194   /**
195    * Mustra un color chooser , para seleccionar el color con que se desea pintar los valores de la tabla
196    */

197   private void executeSelection() {
198     DefaultMutableTreeNode node = getCurrent(current, false);
199     if (node != null) {
200       ColorCondition colorCondition = ChoosSemaphoreSelection.newColorCondition(null, (MetricState) node.getUserObject(), null);
201       if (colorCondition != null) {
202         conditionManager.put(colorCondition);
203         node.add(new DefaultMutableTreeNode(colorCondition));
204         treeModel.reload();
205       }
206     }
207   }
208
209   /**
210    * crea el panel ubicado al este del frame , donde se incorpora al norte del mismo el botonn de agregar
211    * opciones.
212    * @return
213    */

214   private Component createEastPanel() {
215     JPanel eastPanel = new JPanel(new GridLayout(7, 1));
216     addButton = new JButton(com.calipso.reportgenerator.common.LanguageTraslator.traslate("130"));
217     addButton.setMnemonic('a');
218     modifyButton = new JButton(com.calipso.reportgenerator.common.LanguageTraslator.traslate("333"));
219     modifyButton.setMnemonic('m');
220     removeButton = new JButton(com.calipso.reportgenerator.common.LanguageTraslator.traslate("131"));
221     removeButton.setMnemonic('b');
222     closeButton = new JButton(com.calipso.reportgenerator.common.LanguageTraslator.traslate("132"));
223     closeButton.setMnemonic('c');
224     eastPanel.add(addButton);
225     eastPanel.add(modifyButton);
226     eastPanel.add(removeButton);
227     eastPanel.add(closeButton);
228     return eastPanel;
229   }
230
231   /**
232    * Crea un JScrollPane en el centro
233    * @return
234    */

235   private Component crateCenterJScrollPane() {
236     JScrollPane jScrollPane = new JScrollPane();
237     jScrollPane.getViewport().add(createCenterPanel());
238     return jScrollPane;
239   }
240
241   /**
242    * Crea el JTree , adentro del JScrollPane
243    * @return
244    */

245   private Component createCenterPanel() {
246     JPanel center = new JPanel(new BorderLayout());
247     center.add(createTree(), BorderLayout.CENTER);
248     return center;
249   }
250
251   /**
252    * Retorna un map , com los estados de las metricas
253    * @return
254    */

255   public Map getMetricStates() {
256     return metricStates;
257   }
258
259   /**
260    * Crea el JTree y carga sus valores
261    * @return
262    */

263   private Component createTree() {
264     Iterator iterator = getMetricStates().values().iterator();
265     root = new DefaultMutableTreeNode(com.calipso.reportgenerator.common.LanguageTraslator.traslate("170"));
266     while (iterator.hasNext()) {
267       MetricState value = (MetricState) iterator.next();
268       DefaultMutableTreeNode node = new DefaultMutableTreeNode(value);
269       loadAllValuesNode(node);
270       root.add(node);
271     }
272     treeModel = new DefaultTreeModel(root);
273     tree = new JTree(treeModel);
274     return tree;
275   }
276
277   /**
278    * Carga los valores para cada metrica , si es que los hay
279    * @param padre
280    */

281   private void loadAllValuesNode(DefaultMutableTreeNode padre) {
282     String JavaDoc name = ((MetricState) padre.getUserObject()).getName();
283     Collection conditions = conditionManager.getConditionsFor(name);
284     if (padre.isLeaf()) {
285       if (conditions != null) {
286         Iterator iterator = conditions.iterator();
287         while (iterator.hasNext()) {
288           ColorCondition colorCondition = (ColorCondition) iterator.next();
289           if (colorCondition.getExpression() != null) {
290             DefaultMutableTreeNode aux = new DefaultMutableTreeNode(colorCondition);
291             padre.add(aux);
292           }
293         }
294       }
295     }
296   }
297
298
299   /**
300    * Retotrna el ColorConditionManager
301    * @return
302    */

303   public ColorConditionManager getConditionManager() {
304     return conditionManager;
305   }
306 }
307
Popular Tags