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 ; 8 import java.awt.*; 9 import java.awt.event.ActionListener ; 10 import java.awt.event.ActionEvent ; 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 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 () { 44 public void actionPerformed(java.awt.event.ActionEvent evt) { 45 modifySelection(); 46 } 47 }); 48 49 addButton.addActionListener(new java.awt.event.ActionListener () { 50 public void actionPerformed(java.awt.event.ActionEvent evt) { 51 executeSelection(); 52 } 53 }); 54 tree.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener () { 55 public void valueChanged(javax.swing.event.TreeSelectionEvent evt) { 56 treeValueChanged(evt); 57 } 58 }); 59 removeButton.addActionListener(new java.awt.event.ActionListener () { 60 public void actionPerformed(java.awt.event.ActionEvent evt) { 61 removeColorCondition(current); 62 63 } 64 }); 65 66 closeButton.addActionListener(new ActionListener () { 67 public void actionPerformed(ActionEvent e) { 68 close(); 69 } 70 }); 71 pack(); 72 java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize(); 73 setSize(new java.awt.Dimension (587, 226)); 74 setLocation((screenSize.width - 587) / 2, (screenSize.height - 226) / 2); 75 setResizable(false); 76 checkBtnState(); 77 } 78 79 82 private void close() { 83 this.setVisible(false); 84 this.dispose(); 85 } 86 87 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 104 private void treeValueChanged(TreeSelectionEvent evt) { 105 DefaultMutableTreeNode node = (DefaultMutableTreeNode) (evt.getPath().getLastPathComponent()); 106 current = node; 107 checkBtnState(); 108 } 109 110 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 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 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 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 235 private Component crateCenterJScrollPane() { 236 JScrollPane jScrollPane = new JScrollPane(); 237 jScrollPane.getViewport().add(createCenterPanel()); 238 return jScrollPane; 239 } 240 241 245 private Component createCenterPanel() { 246 JPanel center = new JPanel(new BorderLayout()); 247 center.add(createTree(), BorderLayout.CENTER); 248 return center; 249 } 250 251 255 public Map getMetricStates() { 256 return metricStates; 257 } 258 259 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 281 private void loadAllValuesNode(DefaultMutableTreeNode padre) { 282 String 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 303 public ColorConditionManager getConditionManager() { 304 return conditionManager; 305 } 306 } 307 | Popular Tags |