KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > gui > tree > JMeterTreeListener


1 // $Header: /home/cvs/jakarta-jmeter/src/core/org/apache/jmeter/gui/tree/JMeterTreeListener.java,v 1.15 2004/02/20 02:20:47 jsalvata Exp $
2
/*
3  * Copyright 2001-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17 */

18
19 package org.apache.jmeter.gui.tree;
20
21 import java.awt.Container JavaDoc;
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.awt.event.ActionListener JavaDoc;
24 import java.awt.event.KeyEvent JavaDoc;
25 import java.awt.event.KeyListener JavaDoc;
26 import java.awt.event.MouseEvent JavaDoc;
27 import java.awt.event.MouseListener JavaDoc;
28 import java.awt.event.MouseMotionListener JavaDoc;
29
30 import javax.swing.JLabel JavaDoc;
31 import javax.swing.JMenuItem JavaDoc;
32 import javax.swing.JPopupMenu JavaDoc;
33 import javax.swing.JTree JavaDoc;
34 import javax.swing.event.TreeSelectionEvent JavaDoc;
35 import javax.swing.event.TreeSelectionListener JavaDoc;
36 import javax.swing.tree.TreeNode JavaDoc;
37 import javax.swing.tree.TreePath JavaDoc;
38
39 import org.apache.jmeter.control.gui.TestPlanGui;
40 import org.apache.jmeter.control.gui.WorkBenchGui;
41 import org.apache.jmeter.gui.GuiPackage;
42 import org.apache.jmeter.gui.MainFrame;
43 import org.apache.jmeter.gui.action.DragNDrop;
44 import org.apache.jmeter.util.JMeterUtils;
45 import org.apache.jorphan.logging.LoggingManager;
46 import org.apache.log.Logger;
47
48 /**
49  * @author Michael Stover
50  * Created March 11, 2001
51  * @version $Revision: 1.15 $ Last updated: $Date: 2004/02/20 02:20:47 $
52  */

53 public class JMeterTreeListener
54     implements TreeSelectionListener JavaDoc, MouseListener JavaDoc, KeyListener JavaDoc,
55                MouseMotionListener JavaDoc
56 {
57     transient private static Logger log = LoggingManager.getLoggerForClass();
58     //Container endWindow;
59
//JPopupMenu pop;
60
private TreePath JavaDoc currentPath;
61     private ActionListener JavaDoc actionHandler;
62
63     private JMeterTreeModel model;
64     private JTree JavaDoc tree;
65     private boolean dragging = false;
66     private JMeterTreeNode[] draggedNodes;
67     private JLabel JavaDoc dragIcon = new JLabel JavaDoc(JMeterUtils.getImage("leafnode.gif"));
68
69     /**
70      * Constructor for the JMeterTreeListener object.
71      */

72     public JMeterTreeListener(JMeterTreeModel model)
73     {
74         this.model = model;
75         dragIcon.validate();
76         dragIcon.setVisible(true);
77     }
78
79     public JMeterTreeListener()
80     {
81         dragIcon.validate();
82         dragIcon.setVisible(true);
83     }
84
85     public void setModel(JMeterTreeModel m)
86     {
87         model = m;
88     }
89
90     /**
91      * Sets the ActionHandler attribute of the JMeterTreeListener object.
92      *
93      * @param ah the new ActionHandler value
94      */

95     public void setActionHandler(ActionListener JavaDoc ah)
96     {
97         actionHandler = ah;
98     }
99
100     /**
101      * Sets the JTree attribute of the JMeterTreeListener object.
102      *
103      * @param tree the new JTree value
104      */

105     public void setJTree(JTree JavaDoc tree)
106     {
107         this.tree = tree;
108     }
109
110     /**
111      * Sets the EndWindow attribute of the JMeterTreeListener object.
112      *
113      * @param window the new EndWindow value
114      */

115     public void setEndWindow(Container JavaDoc window)
116     {
117         //endWindow = window;
118
}
119
120     /**
121      * Gets the JTree attribute of the JMeterTreeListener object.
122      *
123      * @return tree the current JTree value.
124      */

125     public JTree JavaDoc getJTree()
126     {
127         return tree;
128     }
129
130     /**
131      * Gets the CurrentNode attribute of the JMeterTreeListener object.
132      *
133      * @return the CurrentNode value
134      */

135     public JMeterTreeNode getCurrentNode()
136     {
137         if (currentPath != null)
138         {
139             if (currentPath.getLastPathComponent() != null)
140             {
141                 return (JMeterTreeNode) currentPath.getLastPathComponent();
142             }
143             else
144             {
145                 return (JMeterTreeNode) currentPath
146                     .getParentPath()
147                     .getLastPathComponent();
148             }
149         }
150         else
151         {
152             return (JMeterTreeNode) model.getRoot();
153         }
154     }
155
156     public JMeterTreeNode[] getSelectedNodes()
157     {
158         TreePath JavaDoc[] paths = tree.getSelectionPaths();
159         if (paths == null)
160         {
161             return new JMeterTreeNode[] { getCurrentNode()};
162         }
163         JMeterTreeNode[] nodes = new JMeterTreeNode[paths.length];
164         for (int i = 0; i < paths.length; i++)
165         {
166             nodes[i] = (JMeterTreeNode) paths[i].getLastPathComponent();
167         }
168
169         return nodes;
170     }
171
172     public TreePath JavaDoc removedSelectedNode()
173     {
174         currentPath = currentPath.getParentPath();
175         return currentPath;
176     }
177
178     public void valueChanged(TreeSelectionEvent JavaDoc e)
179     {
180         currentPath = e.getNewLeadSelectionPath();
181         actionHandler.actionPerformed(new ActionEvent JavaDoc(this, 3333, "edit"));
182     }
183
184     public void mouseClicked(MouseEvent JavaDoc ev)
185     {
186     }
187
188     public void mouseReleased(MouseEvent JavaDoc e)
189     {
190         if (dragging && isValidDragAction(draggedNodes, getCurrentNode()))
191         {
192             dragging = false;
193             JPopupMenu JavaDoc dragNdrop = new JPopupMenu JavaDoc();
194             JMenuItem JavaDoc item =
195                 new JMenuItem JavaDoc(JMeterUtils.getResString("Insert Before"));
196             item.addActionListener(actionHandler);
197             item.setActionCommand(DragNDrop.INSERT_BEFORE);
198             dragNdrop.add(item);
199             item = new JMenuItem JavaDoc(JMeterUtils.getResString("Insert After"));
200             item.addActionListener(actionHandler);
201             item.setActionCommand(DragNDrop.INSERT_AFTER);
202             dragNdrop.add(item);
203             item = new JMenuItem JavaDoc(JMeterUtils.getResString("Add as Child"));
204             item.addActionListener(actionHandler);
205             item.setActionCommand(DragNDrop.ADD);
206             dragNdrop.add(item);
207             dragNdrop.addSeparator();
208             item = new JMenuItem JavaDoc(JMeterUtils.getResString("Cancel"));
209             dragNdrop.add(item);
210             displayPopUp(e, dragNdrop);
211         }
212         else
213         {
214             GuiPackage.getInstance().getMainFrame().repaint();
215         }
216         dragging = false;
217     }
218
219     public JMeterTreeNode[] getDraggedNodes()
220     {
221         return draggedNodes;
222     }
223
224     /**
225      * Tests if the node is being dragged into one of it's own sub-nodes, or
226      * into itself.
227      */

228     private boolean isValidDragAction(
229         JMeterTreeNode[] source,
230         JMeterTreeNode dest)
231     {
232         boolean isValid = true;
233         TreeNode JavaDoc[] path = dest.getPath();
234         for (int i = 0; i < path.length; i++)
235         {
236             if (contains(source,path[i]))
237             {
238                 isValid = false;
239             }
240         }
241         return isValid;
242     }
243
244     public void mouseEntered(MouseEvent JavaDoc e)
245     {
246     }
247
248     private void changeSelectionIfDragging(MouseEvent JavaDoc e)
249     {
250         if (dragging)
251         {
252             GuiPackage.getInstance().getMainFrame().drawDraggedComponent(
253                 dragIcon,
254                 e.getX(),
255                 e.getY());
256             if (tree.getPathForLocation(e.getX(), e.getY()) != null)
257             {
258                 currentPath = tree.getPathForLocation(e.getX(), e.getY());
259                 if (!contains(draggedNodes,getCurrentNode()))
260                 {
261                     tree.setSelectionPath(currentPath);
262                 }
263             }
264         }
265     }
266     
267     private boolean contains(Object JavaDoc[] container,Object JavaDoc item)
268     {
269         for (int i = 0; i < container.length; i++)
270         {
271             if(container[i] == item)
272             {
273                 return true;
274             }
275         }
276         return false;
277     }
278
279     public void mousePressed(MouseEvent JavaDoc e)
280     {
281         // Get the Main Frame.
282
MainFrame mainFrame = GuiPackage.getInstance().getMainFrame();
283
284         // Close any Main Menu that is open
285
mainFrame.closeMenu();
286         int selRow = tree.getRowForLocation(e.getX(), e.getY());
287         if (tree.getPathForLocation(e.getX(), e.getY()) != null)
288         {
289             currentPath = tree.getPathForLocation(e.getX(), e.getY());
290         }
291         if (selRow != -1)
292         {
293             //updateMainMenu(((JMeterGUIComponent)
294
// getCurrentNode().getUserObject()).createPopupMenu());
295
if (isRightClick(e))
296             {
297                 if (tree.getSelectionCount() < 2)
298                 {
299                     tree.setSelectionPath(currentPath);
300                 }
301                 if (getCurrentNode() instanceof JMeterTreeNode)
302                 {
303                     log.debug("About to display pop-up");
304                     displayPopUp(e);
305                 }
306             }
307         }
308     }
309
310     public void mouseDragged(MouseEvent JavaDoc e)
311     {
312         if (!dragging)
313         {
314             dragging = true;
315             draggedNodes = getSelectedNodes();
316             if (draggedNodes[0].getUserObject() instanceof TestPlanGui
317                 || draggedNodes[0].getUserObject() instanceof WorkBenchGui)
318             {
319                 dragging = false;
320             }
321
322         }
323         changeSelectionIfDragging(e);
324     }
325
326     public void mouseMoved(MouseEvent JavaDoc e)
327     {
328     }
329
330     public void mouseExited(MouseEvent JavaDoc ev)
331     {
332     }
333
334     public void keyPressed(KeyEvent JavaDoc e)
335     {
336     }
337
338     public void keyReleased(KeyEvent JavaDoc e)
339     {
340     }
341
342     public void keyTyped(KeyEvent JavaDoc e)
343     {
344     }
345
346     private boolean isRightClick(MouseEvent JavaDoc e)
347     {
348         return (MouseEvent.BUTTON2_MASK & e.getModifiers()) > 0
349             || (MouseEvent.BUTTON3_MASK == e.getModifiers());
350     }
351
352 /* NOTUSED
353     private void updateMainMenu(JPopupMenu menu)
354     {
355         try
356         {
357             MainFrame mainFrame = GuiPackage.getInstance().getMainFrame();
358             mainFrame.setEditMenu(menu);
359         }
360         catch (NullPointerException e)
361         {
362             log.error("Null pointer: JMeterTreeListener.updateMenuItem()", e);
363             log.error("", e);
364         }
365     }
366 */

367
368     private void displayPopUp(MouseEvent JavaDoc e)
369     {
370         JPopupMenu JavaDoc pop = getCurrentNode().createPopupMenu();
371         GuiPackage.getInstance().displayPopUp(e, pop);
372     }
373
374     private void displayPopUp(MouseEvent JavaDoc e, JPopupMenu JavaDoc popup)
375     {
376         log.warn("Shouldn't be here");
377         if (popup != null)
378         {
379             popup.pack();
380             popup.show(tree, e.getX(), e.getY());
381             popup.setVisible(true);
382             popup.requestFocus();
383         }
384     }
385 }
386
Popular Tags