Source code: cor/gui/JspmTreePanel.java
1 /*-----------------------------------------------------------------------------------------------------*/
2 /* */
3 /* Copyright (C) */
4 /* */
5 /* This program is free software; you can redistribute it and/or modify it under the terms of the GNU */
6 /* General Public License as published by the Free Software Foundation; either version 2 of the */
7 /* License, or (at your option) any later version. */
8 /* */
9 /* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; */
10 /* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR */
11 /* PURPOSE. See the GNU General Public License for more details. */
12 /* */
13 /* You should have received a copy of the GNU General Public License along with this program; if */
14 /* not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA */
15 /* 02111-1307 USA */
16 /* */
17 /*-----------------------------------------------------------------------------------------------------*/
18 /* */
19 /* $Author: strand01 $ $Revision: 1.3 $ $Date: 2001/12/20 14:23:02 $ */
20 /* */
21 /*-----------------------------------------------------------------------------------------------------*/
22
23 package cor.gui;
24
25 // Java classes
26 import java.awt.*;
27 import java.awt.event.*;
28 import java.util.*;
29
30 // Swing classes
31 import javax.swing.*;
32 import javax.swing.tree.*;
33
34 // JSPM classes
35 import com.jdk.*;
36 import cor.gui.prf.*;
37
38 /**
39 * Defines the tree panel.
40 *
41 * The tree panel consists of two JComboBoxes on top of the panel and the actual tree panel. The
42 * two combo boxes contain the component selection and the host selection.
43 *
44 * @author Steve Randall (strand012001@yahoo.com)
45 * @version 0.0.8
46 * @date 31/10/2001
47 */
48 public class JspmTreePanel extends JPanel implements MouseListener
49 {
50 /**
51 * Initialization flag
52 */
53 private boolean initialized = false;
54
55 /**
56 * Current component
57 */
58 private String component = null;
59
60 /**
61 * Host combo box
62 */
63 private static JComboBox hostCombo = null;
64
65 /**
66 * Component selection combo.
67 */
68 private static JComboBox selectionCombo = null;
69
70 /**
71 * The actual tree panel.
72 */
73 private JScrollPane treePanel = null;
74
75 /**
76 * The combo panel which contains the selection combo boxes.
77 */
78 private JPanel comboPanel = null;
79
80 /**
81 * The tree structure.
82 */
83 private JTree tree = null;
84
85 /**
86 * JSPM log writer.
87 */
88 private JspmLogWriter logWriter = null;
89
90 /**
91 * JSPM CORe connection.
92 */
93 private JspmCore jspmCore = null;
94
95 /**
96 * Constructor.
97 *
98 * @param listener (ActionListener) the listener who will get the tree selection events.
99 * @param jC (JspmCore) JSPM core connection.
100 * @param size (Dimension) dimension of the panel.
101 * @param comp (String) current component selection.
102 * @param lw (JspmLogWriter) JSPM log writer.
103 */
104 public JspmTreePanel( ActionListener listener, JspmCore jC, Dimension size, String comp, JspmLogWriter lw )
105 {
106 logWriter = lw;
107 jspmCore = jC;
108 component = comp;
109
110 setLayout( new BorderLayout() );
111 setFont( JspmConstants.DEFAULT_FONT );
112
113 treePanel = new JScrollPane();
114 treePanel.setPreferredSize( size );
115
116 comboPanel = new JPanel();
117 comboPanel.setLayout( new BorderLayout() );
118
119 hostCombo = new JComboBox();
120 hostCombo.addActionListener( listener );
121 hostCombo.setEditable( true );
122 hostCombo.setFont( JspmConstants.DEFAULT_FONT );
123 comboPanel.add( hostCombo, BorderLayout.NORTH );
124
125 selectionCombo = new JComboBox();
126 selectionCombo.addActionListener( listener );
127 selectionCombo.setEditable( false );
128 selectionCombo.setFont( JspmConstants.DEFAULT_FONT );
129 comboPanel.add( selectionCombo, BorderLayout.SOUTH );
130
131 add( comboPanel, BorderLayout.NORTH );
132 add( treePanel, BorderLayout.CENTER );
133
134 initialized = true;
135 }
136
137 /**
138 * Adds the tree to the tree panel
139 *
140 * @param t (JTree) the tree control to add.
141 */
142 public void addTree( JTree t )
143 {
144 tree = t;
145 tree.addMouseListener( this );
146
147 // Repaint the tree
148 treePanel.setViewportView( tree );
149 treePanel.validate();
150
151 logWriter.log( 4, "JspmTreePanel", "removeTree", "Tree added" );
152 }
153
154 /**
155 * Removes the tree from the panel
156 */
157 public void removeTree()
158 {
159 treePanel.remove( tree );
160 treePanel.validate();
161 logWriter.log( 4, "JspmTreePanel", "removeTree", "Tree removed" );
162 }
163
164 /**
165 * Adds a selection item to the selection combo box
166 *
167 * @param selection (String) selection item.
168 */
169 public void addSelection(String selection) { selectionCombo.addItem(selection); }
170
171 /**
172 * Sets the selected item
173 *
174 * @param selection (String) selection item.
175 */
176 public void setSelectedItem( String selection ) { selectionCombo.setSelectedItem(selection); }
177
178 /**
179 * Adds a host to the host combo
180 *
181 * @param host (String) host name.
182 */
183 public void addHost(String host) { hostCombo.addItem(host); }
184
185 /**
186 * Returns the selection combo
187 *
188 * @return selectionCombo (JComboBox) selection combo box.
189 */
190 public static JComboBox getSelectionCombo() { return selectionCombo; }
191
192 /**
193 * Return the host combo.
194 *
195 * @return hostCombo (JComboBox) host combo box.
196 */
197 public static JComboBox getHostCombo() { return hostCombo; }
198
199 /**
200 * The host selection changed
201 *
202 * @param core (JspmCore) JSPM CORe connection.
203 */
204 public void hostChanged( JspmCore core )
205 {
206 if( component.equals( "prf" ) )
207 ((JspmPerfTree)tree).hostChanged( core );
208 }
209
210 /**
211 * Mouse event callback.
212 *
213 * We only care about the right mouse click. In case we have one, we open the
214 * JspmPerfTreeNodeMenu.
215 *
216 * @param event (MouseEvent) the mouse event.
217 */
218 public void mouseClicked(MouseEvent e)
219 {
220 if( e.getClickCount() == 1 && e.getModifiers() == 4 ) {
221
222 TreePath selPath = tree.getSelectionPath();
223 mySingleClick(selPath, e.getX(), e.getY());
224
225 }
226 }
227
228 /**
229 * Mouse event callback.
230 *
231 * @param event (MouseEvent) the mouse event.
232 */
233 public void mousePressed(MouseEvent e) {}
234
235 /**
236 * Mouse event callback.
237 *
238 * @param event (MouseEvent) the mouse event.
239 */
240 public void mouseReleased(MouseEvent e) {}
241
242 /**
243 * Mouse event callback.
244 *
245 * @param event (MouseEvent) the mouse event.
246 */
247 public void mouseEntered(MouseEvent e) {}
248
249 /**
250 * Mouse event callback.
251 *
252 * @param event (MouseEvent) the mouse event.
253 */
254 public void mouseExited(MouseEvent e) {}
255
256 /**
257 * Callback for the mouse listener which is acting on the tree structure.
258 *
259 * @param selPath (TreePath) the path to the SLA-object in the tree.
260 * @param x (int) Mouse click x-position.
261 * @param y (int) Mouse click y-position.
262 */
263 public final void mySingleClick(TreePath selPath, int x, int y)
264 {
265 if(selPath == null) return;
266
267 final DefaultMutableTreeNode selNode = (DefaultMutableTreeNode)selPath.getLastPathComponent();
268
269 JspmPerfTreeMenu nodeMenu = new JspmPerfTreeMenu( selNode );
270 nodeMenu.show( this, x, y - (int)treePanel.getViewport().getViewPosition().getY() );
271 }
272
273 /**
274 * Node menu class.
275 *
276 * This class shows the node menu for the tree. Depending on the component different entries
277 * are listed.
278 *
279 * @since 0.0.2
280 * @author Steve Randall (strand012001@yahoo.com)
281 * @version 0.0.2
282 * @date 09/09/2001
283 */
284 public class JspmPerfTreeMenu extends JPopupMenu
285 {
286 /**
287 * The selected node
288 */
289 private final DefaultMutableTreeNode selNode;
290
291 /**
292 * Constructor.
293 *
294 * @param selNode (DefaultMutableTreeNode) selected node in the JSPM perf tree.
295 * @param slaList The current SLA list.
296 */
297 public JspmPerfTreeMenu( DefaultMutableTreeNode selNode )
298 {
299 super();
300
301 this.selNode = selNode;
302
303 if( component.equals( "prf" ) )
304 createPrfNodeMenu();
305 }
306
307 /**
308 * Show the node popup menu.
309 *
310 * @param parent (JspmTreePanel) parent component.
311 * @param x (int) x-coordinate of node menu.
312 * @param y (int) y-coordinate of node menu.
313 */
314 public void show( JspmTreePanel parent, int x, int y )
315 {
316 super.show( parent, x, y );
317 }
318
319 /**
320 * Create the performance management tree menu.
321 */
322 public void createPrfNodeMenu()
323 {
324 JMenuItem menuItem = null;
325
326 menuItem = new JMenuItem( "Start instance" );
327 menuItem.setMnemonic('S');
328 menuItem.addActionListener(new ActionListener() {
329 public void actionPerformed(ActionEvent e) {
330 SetThreshold();
331 }
332 });
333 add(menuItem);
334
335 menuItem = new JMenuItem( "Stop instance" );
336 menuItem.setMnemonic('o');
337 menuItem.addActionListener(new ActionListener() {
338 public void actionPerformed(ActionEvent e) {
339 SetThreshold();
340 }
341 });
342 add(menuItem);
343
344 menuItem = new JMenuItem( "Restart instance" );
345 menuItem.setMnemonic('R');
346 menuItem.addActionListener(new ActionListener() {
347 public void actionPerformed(ActionEvent e) {
348 SetThreshold();
349 }
350 });
351 add(menuItem);
352
353 addSeparator();
354
355 menuItem = new JMenuItem( "Set Thresholds" );
356 menuItem.setMnemonic('S');
357 menuItem.addActionListener(new ActionListener() {
358 public void actionPerformed(ActionEvent e) {
359 SetThreshold();
360 }
361 });
362 add(menuItem);
363 addSeparator();
364
365 menuItem = new JMenuItem( "Delete Sponsor" );
366 menuItem.setMnemonic('D');
367 menuItem.addActionListener(new ActionListener() {
368 public void actionPerformed(ActionEvent e) {
369 deleteSponsor();
370 }
371 });
372 add(menuItem);
373
374 }
375
376 /**
377 * Deletes a sponsor sub tree rooted as the selected node.
378 */
379 private void deleteSponsor()
380 {
381 // Get the DB connection
382 JspmCoreDb jspmCoreDb = jspmCore.getDbConnection();
383
384 // Go recursive through the tree rooted at selNode and delete the nodes
385 for (Enumeration e = selNode.breadthFirstEnumeration(); e.hasMoreElements();) {
386
387 DefaultMutableTreeNode next = ( DefaultMutableTreeNode )e.nextElement();
388
389 if( next.isLeaf() ) {
390
391 Object[] path = next.getPath();
392 if( path.length > 1 ) {
393
394 String host = path[1].toString();
395 String resource = path[2].toString();
396 String subResource = path[3].toString();
397 String instance = path[4].toString();
398
399 jspmCoreDb.deleteSponsor( host, resource, subResource, instance );
400 }
401 }
402 }
403 }
404
405 private void SetThreshold()
406 {
407 // Get the DB connection
408 JspmCoreDb jspmCoreDb = jspmCore.getDbConnection();
409
410 // Go recursive through the tree rooted at selNode and delete the nodes
411 for (Enumeration e = selNode.breadthFirstEnumeration(); e.hasMoreElements();) {
412
413 DefaultMutableTreeNode next = ( DefaultMutableTreeNode )e.nextElement();
414 if( next.isLeaf() ) {
415
416 Object[] path = next.getPath();
417 if( path.length > 1 ) {
418
419 String host = path[1].toString();
420 String resource = path[2].toString();
421 String subResource = path[3].toString();
422 String instance = path[4].toString();
423
424 JspmPerfThresholdDialog dialog = new JspmPerfThresholdDialog( jspmCoreDb, host, resource,
425 subResource, instance, logWriter );
426 }
427 }
428 }
429 }
430 }
431 }
432
433