1 24 25 package org.objectweb.clif.console.lib.gui; 26 27 import org.objectweb.clif.supervisor.api.BladeState; 28 import org.objectweb.clif.console.lib.ClifDeployDefinition; 29 import java.awt.BorderLayout ; 30 import java.awt.event.ActionListener ; 31 import java.awt.event.ActionEvent ; 32 import java.util.Map ; 33 import java.util.HashMap ; 34 import java.util.Iterator ; 35 import java.util.List ; 36 import java.util.Arrays ; 37 import java.math.BigInteger ; 38 import javax.swing.JPanel ; 39 import javax.swing.JScrollPane ; 40 import javax.swing.JTable ; 41 import javax.swing.JButton ; 42 import javax.swing.DefaultCellEditor ; 43 import javax.swing.JComboBox ; 44 import javax.swing.JFrame ; 45 import javax.swing.event.ListSelectionListener ; 46 import javax.swing.event.ListSelectionEvent ; 47 import javax.swing.table.DefaultTableModel ; 48 import javax.swing.table.TableColumn ; 49 50 51 56 public class GuiPanelBladeState 57 extends JPanel 58 implements ActionListener , ListSelectionListener 59 { 60 static protected final String ADD_CMD = "add"; 61 static protected final String REMOVE_CMD = "remove"; 62 static protected final String CLEAR_CMD = "clear"; 63 static protected final int ID_COL = 0; 64 static protected final int SERVER_COL = 1; 65 static protected final int ROLE_COL = 2; 66 static protected final int CLASS_COL = 3; 67 static protected final int ARGUMENT_COL = 4; 68 static protected final int COMMENT_COL = 5; 69 static protected final int STATE_COL = 6; 70 static protected final String [] cname = { 71 "Blade id", 72 "Server", 73 "Role", 74 "Blade class", 75 "Blade argument", 76 "Comment", 77 "State" }; 78 79 80 protected BigInteger nextBladeId = BigInteger.ZERO; 81 protected JFrame frame; 82 protected InjectorStateTableModel tModel = new InjectorStateTableModel(cname); 83 protected JTable table = new JTable (tModel); 84 protected List servers = Arrays.asList(new String [0]); 85 protected JButton addBtn = new JButton ("Add a blade"); 86 protected JButton removeBtn = new JButton ("Remove a blade"); 87 protected JButton clearBtn = new JButton ("Remove all blades"); 88 protected JPanel buttonPnl = new JPanel (); 89 private boolean editable = true; 90 91 92 95 public GuiPanelBladeState(JFrame frame) 96 { 97 this.frame = frame; 98 setLayout(new BorderLayout ()); 99 JScrollPane scrollPane = new JScrollPane (table); 100 table.getSelectionModel().addListSelectionListener(this); 101 102 TableColumn serverCol = table.getColumnModel().getColumn(ROLE_COL); 103 serverCol.setCellEditor( 104 new DefaultCellEditor ( 105 new JComboBox (new String [] { "injector", "probe" } ))); 106 107 addBtn.setActionCommand(ADD_CMD); 108 addBtn.addActionListener(this); 109 buttonPnl.add(addBtn); 110 111 removeBtn.setActionCommand(REMOVE_CMD); 112 removeBtn.setEnabled(false); 113 removeBtn.addActionListener(this); 114 buttonPnl.add(removeBtn); 115 116 clearBtn.setActionCommand(CLEAR_CMD); 117 clearBtn.addActionListener(this); 118 buttonPnl.add(clearBtn); 119 120 add(BorderLayout.NORTH, buttonPnl); 121 add(BorderLayout.CENTER, scrollPane); 122 setAvailableServers(new String [0]); 123 } 124 125 126 public void setAvailableServers(String [] servers) 127 { 128 this.servers = Arrays.asList(servers); 129 if (table.isEditing()) 130 { 131 table.getCellEditor(table.getEditingRow(), table.getEditingColumn()).stopCellEditing(); 132 } 133 for (int i = 0 ; i < tModel.getRowCount() ; ++i) 134 { 135 if (! this.servers.contains(tModel.getValueAt(i, SERVER_COL))) 136 { 137 tModel.setValueAt("", i, SERVER_COL); 138 } 139 } 140 TableColumn serverCol = table.getColumnModel().getColumn(SERVER_COL); 141 serverCol.setCellEditor(new DefaultCellEditor (new JComboBox (servers))); 142 } 143 144 145 150 private void addBlade(String id, ClifDeployDefinition def) 151 { 152 insertBlade(id, def, -1); 153 } 154 155 156 163 protected void insertBlade(String id, ClifDeployDefinition def, int row) 164 { 165 Object [] values = new Object [GuiPanelBladeState.cname.length]; 166 if (tModel.getBladeRow(id) == -1) 167 { 168 values[ID_COL] = id; 169 } 170 else 171 { 172 while (tModel.getBladeRow(nextBladeId.toString()) != -1) 173 { 174 nextBladeId = nextBladeId.add(BigInteger.ONE); 175 } 176 values[ID_COL] = nextBladeId.toString(); 177 nextBladeId = nextBladeId.add(BigInteger.ONE); 178 } 179 values[STATE_COL] = BladeState.UNDEPLOYED; 180 values[SERVER_COL] = ""; 181 if (def != null) 182 { 183 if (def.isProbe()) 184 { 185 String probeName = (String )def.getContext().get("insert"); 186 probeName = probeName.substring(0, probeName.lastIndexOf('.')); 187 values[CLASS_COL] = probeName.substring(1 + probeName.lastIndexOf('.')); 188 values[ROLE_COL] = "probe"; 189 } 190 else 191 { 192 values[CLASS_COL] = def.getContext().get("insert"); 193 values[ROLE_COL] = "injector"; 194 } 195 values[ARGUMENT_COL] = (def.getArgument() == null ? "" : def.getArgument()); 196 values[COMMENT_COL] = (def.getComment() == null ? "" : def.getComment()); 197 if (servers.contains(def.getServerName())) 198 { 199 values[SERVER_COL] = def.getServerName(); 200 } 201 } 202 else 203 { 204 values[CLASS_COL] = ""; 205 values[ARGUMENT_COL] = ""; 206 values[COMMENT_COL] = ""; 207 values[ROLE_COL] = ""; 208 } 209 if (row < 0) 210 { 211 tModel.addRow(values); 212 } 213 else 214 { 215 tModel.insertRow(row, values); 216 } 217 } 218 219 220 public void setBladeState(String bladeId, BladeState state) 221 { 222 tModel.setBladeValue(bladeId, state, STATE_COL); 223 } 224 225 226 public void setTestPlan(Map testPlan) 227 { 228 clear(); 229 Iterator iter = testPlan.entrySet().iterator(); 230 while (iter.hasNext()) 231 { 232 Map.Entry entry = (Map.Entry )iter.next(); 233 addBlade((String )entry.getKey(), (ClifDeployDefinition)entry.getValue()); 234 } 235 } 236 237 238 public Map getTestPlan() 239 { 240 Map testPlan = new HashMap (); 241 for (int i = 0 ; i < tModel.getRowCount() && testPlan != null ; ++i) 242 { 243 if (((String )tModel.getValueAt(i, SERVER_COL)).trim().equals("")) 244 { 245 testPlan = null; 246 table.setEditingRow(i); 247 table.editCellAt(i, SERVER_COL); 248 } 249 else if (((String )tModel.getValueAt(i, CLASS_COL)).trim().equals("")) 250 { 251 testPlan = null; 252 table.setEditingRow(i); 253 table.editCellAt(i, CLASS_COL); 254 } 255 else if (((String )tModel.getValueAt(i, ID_COL)).trim().equals("")) 256 { 257 testPlan = null; 258 table.setEditingRow(i); 259 table.editCellAt(i, ID_COL); 260 } 261 else 262 { 263 Map context = new HashMap (); 264 boolean isProbe = tModel.getValueAt(i, ROLE_COL).equals("probe"); 265 if (isProbe) 266 { 267 context.put( 268 "insert", 269 "org.objectweb.clif.probe." 270 + tModel.getValueAt(i, CLASS_COL) 271 + ".Insert"); 272 context.put( 273 "datacollector", 274 "org.objectweb.clif.probe." 275 + tModel.getValueAt(i, CLASS_COL) 276 + ".DataCollector"); 277 } 278 else 279 { 280 context.put( 281 "insert", 282 tModel.getValueAt(i, CLASS_COL)); 283 context.put( 284 "datacollector", 285 "org.objectweb.clif.datacollector.lib.InjectorDataCollector"); 286 } 287 testPlan.put( 288 tModel.getValueAt(i, ID_COL), 289 new ClifDeployDefinition( 290 (String )tModel.getValueAt(i, SERVER_COL), 291 "org.objectweb.clif.server.lib.Blade", 292 context, 293 (String )tModel.getValueAt(i, ARGUMENT_COL), 294 (String )tModel.getValueAt(i, COMMENT_COL), 295 isProbe)); 296 } 297 } 298 return testPlan == null || testPlan.size() == 0 ? null : testPlan; 299 } 300 301 302 public boolean isDeployable() 303 { 304 int n = tModel.getRowCount(); 305 boolean answer = n != 0; 306 while (answer && n-- > 0) 307 { 308 if (((String )tModel.getValueAt(n, SERVER_COL)).trim().equals("") 309 || ((String )tModel.getValueAt(n, CLASS_COL)).trim().equals("") 310 || ((String )tModel.getValueAt(n, ID_COL)).trim().equals("")) 311 { 312 answer = false; 313 } 314 } 315 return answer; 316 } 317 318 319 public boolean isEmpty() 320 { 321 return tModel.getRowCount() == 0; 322 } 323 324 325 328 protected void clear() 329 { 330 if (table.isEditing()) 331 { 332 table.getCellEditor(table.getEditingRow(), table.getEditingColumn()).stopCellEditing(); 333 } 334 table.clearSelection(); 335 table.removeAll(); 336 int n = tModel.getRowCount(); 337 while (n-- > 0) 338 { 339 tModel.removeRow(n); 340 } 341 } 342 343 344 347 public void setEditable(boolean enabled) 348 { 349 editable = enabled; 350 addBtn.setEnabled(enabled); 351 removeBtn.setEnabled(enabled && table.getSelectedRow() != -1 ); 352 clearBtn.setEnabled(enabled); 353 tModel.setEditable(enabled); 354 } 355 356 357 361 362 public void actionPerformed(ActionEvent evt) 363 { 364 if (evt.getActionCommand().equals(ADD_CMD)) 365 { 366 insertBlade(nextBladeId.toString(), null, table.getSelectedRow()); 367 nextBladeId = nextBladeId.add(BigInteger.ONE); 368 } 369 else if (evt.getActionCommand().equals(REMOVE_CMD)) 370 { 371 tModel.removeRow(table.getSelectedRow()); 372 } 373 else if (evt.getActionCommand().equals(CLEAR_CMD)) 374 { 375 if (new GuiClearConfirm(frame).ask()) 376 { 377 clear(); 378 } 379 } 380 } 381 382 383 387 388 public synchronized void valueChanged(ListSelectionEvent evt) 389 { 390 if (table.getSelectedRow() == -1) 391 { 392 removeBtn.setEnabled(false); 393 } 394 else 395 { 396 removeBtn.setEnabled(editable); 397 } 398 } 399 } 400 401 402 406 class InjectorStateTableModel extends DefaultTableModel 407 { 408 boolean editable = true; 409 410 411 public InjectorStateTableModel(String [] cname) 412 { 413 super(cname, 0); 414 } 415 416 417 public void setBladeValue(String bladeId, Object value, int col) 418 { 419 setValueAt(value, getBladeRow(bladeId), col); 420 } 421 422 423 int getBladeRow(String bladeId) 424 { 425 for (int i = 0 ; i < getRowCount() ; ++i) 426 { 427 if(getValueAt(i, GuiPanelBladeState.ID_COL).equals(bladeId)) 428 { 429 return i; 430 } 431 } 432 return -1; 433 } 434 435 436 public void setEditable(boolean editable) 437 { 438 this.editable = editable; 439 } 440 441 442 446 public boolean isCellEditable(int rowIndex, int columnIndex) 447 { 448 if (columnIndex == GuiPanelBladeState.STATE_COL) 449 { 450 return false; 451 } 452 else 453 { 454 return editable; 455 } 456 } 457 } | Popular Tags |