KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > clif > console > lib > gui > GuiPanelBladeState


1 /*
2 * CLIF is a Load Injection Framework
3 * Copyright (C) 2003, 2004 France Telecom R&D
4 * Copyright (C) 2003 INRIA
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * CLIF $Name: $
21 *
22 * Contact: clif@objectweb.org
23 */

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 JavaDoc;
30 import java.awt.event.ActionListener JavaDoc;
31 import java.awt.event.ActionEvent JavaDoc;
32 import java.util.Map JavaDoc;
33 import java.util.HashMap JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.util.List JavaDoc;
36 import java.util.Arrays JavaDoc;
37 import java.math.BigInteger JavaDoc;
38 import javax.swing.JPanel JavaDoc;
39 import javax.swing.JScrollPane JavaDoc;
40 import javax.swing.JTable JavaDoc;
41 import javax.swing.JButton JavaDoc;
42 import javax.swing.DefaultCellEditor JavaDoc;
43 import javax.swing.JComboBox JavaDoc;
44 import javax.swing.JFrame JavaDoc;
45 import javax.swing.event.ListSelectionListener JavaDoc;
46 import javax.swing.event.ListSelectionEvent JavaDoc;
47 import javax.swing.table.DefaultTableModel JavaDoc;
48 import javax.swing.table.TableColumn JavaDoc;
49
50
51 /**
52  * @author Julien Buret
53  * @author Nicolas Droze
54  * @author Bruno Dillenseger
55  */

56 public class GuiPanelBladeState
57     extends JPanel JavaDoc
58     implements ActionListener JavaDoc, ListSelectionListener JavaDoc
59 {
60     static protected final String JavaDoc ADD_CMD = "add";
61     static protected final String JavaDoc REMOVE_CMD = "remove";
62     static protected final String JavaDoc 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 JavaDoc[] cname = {
71         "Blade id",
72         "Server",
73         "Role",
74         "Blade class",
75         "Blade argument",
76         "Comment",
77         "State" };
78
79
80     protected BigInteger JavaDoc nextBladeId = BigInteger.ZERO;
81     protected JFrame JavaDoc frame;
82     protected InjectorStateTableModel tModel = new InjectorStateTableModel(cname);
83     protected JTable JavaDoc table = new JTable JavaDoc(tModel);
84     protected List JavaDoc servers = Arrays.asList(new String JavaDoc[0]);
85     protected JButton JavaDoc addBtn = new JButton JavaDoc("Add a blade");
86     protected JButton JavaDoc removeBtn = new JButton JavaDoc("Remove a blade");
87     protected JButton JavaDoc clearBtn = new JButton JavaDoc("Remove all blades");
88     protected JPanel JavaDoc buttonPnl = new JPanel JavaDoc();
89     private boolean editable = true;
90
91
92     /**
93      * The constructor with no initial hosts.
94      */

95     public GuiPanelBladeState(JFrame JavaDoc frame)
96     {
97         this.frame = frame;
98         setLayout(new BorderLayout JavaDoc());
99         JScrollPane JavaDoc scrollPane = new JScrollPane JavaDoc(table);
100         table.getSelectionModel().addListSelectionListener(this);
101
102         TableColumn JavaDoc serverCol = table.getColumnModel().getColumn(ROLE_COL);
103         serverCol.setCellEditor(
104             new DefaultCellEditor JavaDoc(
105                 new JComboBox JavaDoc(new String JavaDoc[] { "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 JavaDoc[0]);
123     }
124
125
126     public void setAvailableServers(String JavaDoc[] 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 JavaDoc serverCol = table.getColumnModel().getColumn(SERVER_COL);
141         serverCol.setCellEditor(new DefaultCellEditor JavaDoc(new JComboBox JavaDoc(servers)));
142     }
143
144
145     /**
146      * Adds a blade at the bottom of the table
147      * @param id The blade identifier
148      * @param def the deployment definition, or null if no deployment is defined
149      */

150     private void addBlade(String JavaDoc id, ClifDeployDefinition def)
151     {
152         insertBlade(id, def, -1);
153     }
154
155
156     /**
157      * Inserts a blade at the specified row index of the table.
158      * @param id the blade identifier
159      * @param def the deployment definition, or null if no deployment is defined
160      * @param row the insertion index in the table for the new row. A negative value
161      * results in adding the new row at the bottom of the table.
162      */

163     protected void insertBlade(String JavaDoc id, ClifDeployDefinition def, int row)
164     {
165         Object JavaDoc[] values = new Object JavaDoc[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 JavaDoc probeName = (String JavaDoc)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 JavaDoc bladeId, BladeState state)
221     {
222         tModel.setBladeValue(bladeId, state, STATE_COL);
223     }
224
225
226     public void setTestPlan(Map JavaDoc testPlan)
227     {
228         clear();
229         Iterator JavaDoc iter = testPlan.entrySet().iterator();
230         while (iter.hasNext())
231         {
232             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)iter.next();
233             addBlade((String JavaDoc)entry.getKey(), (ClifDeployDefinition)entry.getValue());
234         }
235     }
236
237
238     public Map JavaDoc getTestPlan()
239     {
240         Map JavaDoc testPlan = new HashMap JavaDoc();
241         for (int i = 0 ; i < tModel.getRowCount() && testPlan != null ; ++i)
242         {
243             if (((String JavaDoc)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 JavaDoc)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 JavaDoc)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 JavaDoc context = new HashMap JavaDoc();
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 JavaDoc)tModel.getValueAt(i, SERVER_COL),
291                         "org.objectweb.clif.server.lib.Blade",
292                         context,
293                         (String JavaDoc)tModel.getValueAt(i, ARGUMENT_COL),
294                         (String JavaDoc)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 JavaDoc)tModel.getValueAt(n, SERVER_COL)).trim().equals("")
309                 || ((String JavaDoc)tModel.getValueAt(n, CLASS_COL)).trim().equals("")
310                 || ((String JavaDoc)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     /**
326      * Removes all lines from the table
327      */

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     /**
345      * Enables or disable testplan edition
346      */

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     //////////////////////////////
358
// interface ActionListener //
359
//////////////////////////////
360

361
362     public void actionPerformed(ActionEvent JavaDoc 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     /////////////////////////////////////
384
// interface ListSelectionListener //
385
/////////////////////////////////////
386

387
388     public synchronized void valueChanged(ListSelectionEvent JavaDoc 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 /**
403  * This class perform operations upon the injector state panel,
404  * like retrieving a given type of host,...
405  */

406 class InjectorStateTableModel extends DefaultTableModel JavaDoc
407 {
408     boolean editable = true;
409
410
411     public InjectorStateTableModel(String JavaDoc[] cname)
412     {
413         super(cname, 0);
414     }
415
416
417     public void setBladeValue(String JavaDoc bladeId, Object JavaDoc value, int col)
418     {
419         setValueAt(value, getBladeRow(bladeId), col);
420     }
421
422
423     int getBladeRow(String JavaDoc 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     /**
443      * Return true if the specified cell is editable.
444      * @see javax.swing.table.TableModel#isCellEditable(int, int)
445      */

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