KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > module > integration > JMeterGUISupport


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.apache.jmeter.module.integration;
21
22 import java.awt.Component JavaDoc;
23 import java.awt.Container JavaDoc;
24 import java.awt.Image JavaDoc;
25 import java.awt.event.ActionEvent JavaDoc;
26 import java.awt.event.ActionListener JavaDoc;
27 import java.beans.PropertyChangeListener JavaDoc;
28 import java.beans.PropertyChangeSupport JavaDoc;
29 import java.io.OutputStreamWriter JavaDoc;
30 import java.util.Enumeration JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.LinkedList JavaDoc;
33 import java.util.Properties JavaDoc;
34 import java.util.concurrent.Semaphore JavaDoc;
35
36 import javax.swing.ImageIcon JavaDoc;
37 import javax.swing.JPopupMenu JavaDoc;
38 import javax.swing.JTextPane JavaDoc;
39 import javax.swing.SwingUtilities JavaDoc;
40 import javax.swing.event.TreeModelEvent JavaDoc;
41 import javax.swing.event.TreeModelListener JavaDoc;
42 import javax.swing.event.TreeSelectionEvent JavaDoc;
43 import javax.swing.tree.TreePath JavaDoc;
44
45 import org.apache.jmeter.config.gui.AbstractConfigGui;
46 import org.apache.jmeter.control.gui.AbstractControllerGui;
47 import org.apache.jmeter.control.gui.TestPlanGui;
48 import org.apache.jmeter.control.gui.WorkBenchGui;
49 import org.apache.jmeter.gui.GUIFactory;
50 import org.apache.jmeter.gui.GuiPackage;
51 import org.apache.jmeter.gui.JMeterGUIComponent;
52 import org.apache.jmeter.gui.MainFrame;
53 import org.apache.jmeter.gui.action.ActionNames;
54 import org.apache.jmeter.gui.action.ActionRouter;
55 import org.apache.jmeter.gui.action.Load;
56 import org.apache.jmeter.gui.tree.JMeterTreeListener;
57 import org.apache.jmeter.gui.tree.JMeterTreeModel;
58 import org.apache.jmeter.gui.tree.JMeterTreeNode;
59 import org.apache.jmeter.plugin.JMeterPlugin;
60 import org.apache.jmeter.plugin.PluginManager;
61 import org.apache.jmeter.samplers.gui.AbstractSamplerGui;
62 import org.apache.jmeter.save.SaveService;
63 import org.apache.jmeter.testelement.TestElement;
64 import org.apache.jmeter.threads.gui.ThreadGroupGui;
65 import org.apache.jmeter.timers.gui.AbstractTimerGui;
66 import org.apache.jmeter.util.JMeterUtils;
67 import org.apache.jmeter.visualizers.gui.AbstractVisualizer;
68 import org.apache.jorphan.collections.HashTree;
69 import org.apache.jorphan.util.JOrphanUtils;
70 import org.openide.filesystems.FileLock;
71 import org.openide.filesystems.FileObject;
72 import org.openide.filesystems.FileUtil;
73
74 /**
75  *
76  * @author Jaroslav Bachorik
77  */

78 public class JMeterGUISupport implements JMeterPlugin {
79   public static final String JavaDoc DIRTY = "dirty";
80   
81   private static Semaphore JavaDoc initLock = new Semaphore JavaDoc(1);
82   private static JMeterGUISupport instance = null;
83   private static ThreadLocal JavaDoc currentNodeHolder = new ThreadLocal JavaDoc();
84   
85   private JMeterTreeModel treeModel = new JMeterTreeModel();
86   
87   private static TestElement lastSelectedElementCopy = null, lastSelectedElement = null;
88   
89   private static boolean modified = false;
90   private static boolean editing = false;
91   private static boolean loading = false;
92   
93   private PropertyChangeSupport JavaDoc pcs = new PropertyChangeSupport JavaDoc(this);
94   
95   private final JMeterTreeListener modifiedListener = new JMeterTreeListener() {
96     public void valueChanged(final TreeSelectionEvent JavaDoc e) {
97       super.valueChanged(e);
98       SwingUtilities.invokeLater(new Runnable JavaDoc() {
99         public void run() {
100           TreePath JavaDoc path = e.getPath();
101           JMeterTreeNode node = (JMeterTreeNode)path.getPathComponent(path.getPathCount() - 1);
102           TestElement element = (TestElement)node.getUserObject();
103           
104           if (lastSelectedElement != null) {
105             if (lastSelectedElementCopy != null) {
106               if (!lastSelectedElement.equals(lastSelectedElementCopy)) {
107                 setDirty(true);
108               }
109             } else {
110               setDirty(true);
111             }
112           }
113           
114           lastSelectedElement = element;
115           lastSelectedElementCopy = (TestElement)element.clone();
116         }
117       });
118     }
119     
120   };
121   private final JMeterTreeListener normalListener = new JMeterTreeListener();
122   
123   private GuiPackage jmeterGui = null;
124   private MainFrame frame = null;
125   
126   /** Creates a new instance of JMeterGUISupport */
127   private JMeterGUISupport() {
128     // NB proprietary EditorKit hack
129
final String JavaDoc editorClassBackup = JTextPane.getEditorKitClassNameForContentType("text/html");
130     JTextPane.registerEditorKitForContentType("text/html", "org.apache.jmeter.module.integration.HTMLEditorKit");
131     // ****
132

133     PluginManager.install(this, true);
134     
135     modifiedListener.setActionHandler(ActionRouter.getInstance());
136     normalListener.setActionHandler(ActionRouter.getInstance());
137     
138     jmeterGui = GuiPackage.getInstance(modifiedListener, treeModel);
139     frame = new MainFrame(ActionRouter.getInstance(), treeModel, modifiedListener);
140     jmeterGui.setMainFrame(frame);
141     ActionRouter.getInstance().addPostActionListener(Object JavaDoc.class, new ActionListener JavaDoc() {
142       public void actionPerformed(ActionEvent JavaDoc e) {
143 // System.out.println("Event: " + e.getActionCommand());
144
}
145     });
146     // NB proprietary EditorKit hack
147
JTextPane.registerEditorKitForContentType("text/html", editorClassBackup);
148     // ***
149

150     treeModel.addTreeModelListener(new TreeModelListener JavaDoc() {
151       public void treeNodesChanged(TreeModelEvent JavaDoc e) {
152 // System.out.println("Nodes changed");
153
}
154       public void treeNodesInserted(TreeModelEvent JavaDoc e) {
155         if (!loading) {
156 // System.out.println("Nodes inserted");
157
setDirty(true);
158         }
159       }
160       public void treeNodesRemoved(TreeModelEvent JavaDoc e) {
161         setDirty(true);
162       }
163       public void treeStructureChanged(TreeModelEvent JavaDoc e) {
164 // System.out.println("Tree changed");
165
}
166     });
167   }
168   
169   public final static JMeterGUISupport getDefault() {
170     if (instance == null) {
171       try {
172         initLock.acquire();
173         if (instance == null) {
174           instance = new JMeterGUISupport();
175         }
176         initLock.release();
177       } catch (InterruptedException JavaDoc e) {}
178     }
179     return instance;
180   }
181   
182   // <editor-fold defaultstate="collapsed" desc="JMeterPlugin implementation" >
183
private static final String JavaDoc[][] DEFAULT_ICONS = {
184     { TestPlanGui.class.getName(), "org/apache/jmeter/images/beaker.gif" },//$NON-NLS-1$
185
{ AbstractTimerGui.class.getName(), "org/apache/jmeter/images/timer.gif" },//$NON-NLS-1$
186
{ ThreadGroupGui.class.getName(), "org/apache/jmeter/images/thread.gif" },//$NON-NLS-1$
187
{ AbstractVisualizer.class.getName(), "org/apache/jmeter/images/meter.png" },//$NON-NLS-1$
188
{ AbstractConfigGui.class.getName(), "org/apache/jmeter/images/testtubes.png" },//$NON-NLS-1$
189
// Note: these were the original settings (just moved to a static
190
// array)
191
// Commented out because there is no such file
192
// {
193
// AbstractPreProcessorGui.class.getName(),
194
// "org/apache/jmeter/images/testtubes.gif" },
195
// {
196
// AbstractPostProcessorGui.class.getName(),
197
// "org/apache/jmeter/images/testtubes.gif" },
198
{ AbstractControllerGui.class.getName(), "org/apache/jmeter/images/knob.gif" },//$NON-NLS-1$
199
{ WorkBenchGui.class.getName(), "org/apache/jmeter/images/clipboard.gif" },//$NON-NLS-1$
200
{ AbstractSamplerGui.class.getName(), "org/apache/jmeter/images/pipet.png" }//$NON-NLS-1$
201
// AbstractAssertionGUI not defined
202
};
203   
204   public String JavaDoc[][] getIconMappings() {
205     String JavaDoc iconProp = JMeterUtils.getPropDefault("jmeter.icons",//$NON-NLS-1$
206
"org/apache/jmeter/images/icon.properties");//$NON-NLS-1$
207
Properties JavaDoc p = JMeterUtils.loadProperties(iconProp);
208     if (p == null) {
209       return DEFAULT_ICONS;
210     }
211     String JavaDoc[][] iconlist = new String JavaDoc[p.size()][3];
212     Enumeration JavaDoc pe = p.keys();
213     int i = 0;
214     while (pe.hasMoreElements()) {
215       String JavaDoc key = (String JavaDoc) pe.nextElement();
216       String JavaDoc icons[] = JOrphanUtils.split(p.getProperty(key), " ");//$NON-NLS-1$
217
iconlist[i][0] = key;
218       iconlist[i][1] = icons[0];
219       if (icons.length > 1)
220         iconlist[i][2] = icons[1];
221       i++;
222     }
223     return iconlist;
224   }
225   
226   public String JavaDoc[][] getResourceBundles() {
227     return new String JavaDoc[0][];
228   }
229 // </editor-fold>
230

231   // <editor-fold defaultstate="collapsed" desc="Property change support">
232
public void addPropertyChangeListener(final PropertyChangeListener JavaDoc pcl) {
233     pcs.addPropertyChangeListener(pcl);
234   }
235   
236   public void addPropertyChangeListener(final String JavaDoc propertyName, final PropertyChangeListener JavaDoc pcl) {
237     pcs.addPropertyChangeListener(propertyName, pcl);
238   }
239   
240   public void removePropertyChangeListener(final PropertyChangeListener JavaDoc pcl) {
241     pcs.removePropertyChangeListener(pcl);
242   }
243   
244   public void removePropertyChangeListener(final String JavaDoc propertyName, final PropertyChangeListener JavaDoc pcl) {
245     pcs.removePropertyChangeListener(propertyName, pcl);
246   }
247   // </editor-fold>
248

249   public Component JavaDoc getGui(final TestElement element) {
250     JMeterGUIComponent gui = jmeterGui.getGui(element);
251     gui.configure(element);
252     return (Component JavaDoc)gui;
253   }
254   
255   public JPopupMenu JavaDoc getPopup(final TestElement element) {
256     currentNodeHolder.set(element);
257     return jmeterGui.getGui(element).createPopupMenu();
258   }
259   
260   public Image JavaDoc getIcon(final TestElement element) {
261     Object JavaDoc gui = jmeterGui.getGui(element);
262     ImageIcon JavaDoc icon = GUIFactory.getIcon(gui.getClass(), element.isEnabled());
263     return icon != null ? icon.getImage() : null;
264   }
265   
266   public void loadScript(final FileObject script) {
267     try {
268       loading = true;
269       if (isDirty()) {
270         ActionRouter.getInstance().doActionNow(new ActionEvent JavaDoc(this, 1, ActionNames.CLOSE));
271       }
272       HashTree tree = SaveService.loadTree(script.getInputStream());
273       GuiPackage.getInstance().setTestPlanFile(FileUtil.toFile(script).getAbsolutePath());
274       
275       
276       new Load().insertLoadedTree(1, tree);
277       editing = true;
278     } catch (Exception JavaDoc e) {
279       e.printStackTrace();
280     } finally {
281       loading = false;
282     }
283   }
284   
285   public void unloadScript() {
286     GuiPackage.getInstance().getTreeModel().clearTestPlan();
287     GuiPackage.getInstance().getTreeListener().getJTree().setSelectionRow(1);
288     
289     // Clear the name of the test plan file
290
GuiPackage.getInstance().setTestPlanFile(null);
291     
292     ActionRouter.getInstance().actionPerformed(new ActionEvent JavaDoc(this, 1, ActionNames.ADD_ALL));
293     
294     editing = false;
295     setDirty(false);
296   }
297   
298   public void saveScript(final FileObject script) throws Exception JavaDoc {
299     FileLock aLock = script.lock();
300     try {
301       OutputStreamWriter JavaDoc osw = new OutputStreamWriter JavaDoc(script.getOutputStream(aLock));
302       HashTree plan = treeModel.getTestPlan();
303       convertSubTree(plan);
304       SaveService.saveTree(plan, osw);
305       ActionRouter.getInstance().doActionNow(new ActionEvent JavaDoc(this, 1, ActionNames.SUB_TREE_SAVED));
306     } finally {
307       aLock.releaseLock();
308     }
309   }
310   
311   public Container JavaDoc getEditor() {
312     return frame != null ? frame.getContentPane() : null;
313   }
314   
315   public void setDirty(final boolean value) {
316     if (value != modified) {
317       pcs.firePropertyChange(DIRTY, modified, value);
318     }
319     modified = value;
320   }
321   
322   public boolean isDirty() {
323     return modified;
324   }
325   
326   private void convertSubTree(HashTree tree) {
327     Iterator JavaDoc iter = new LinkedList JavaDoc(tree.list()).iterator();
328     while (iter.hasNext()) {
329       JMeterTreeNode item = (JMeterTreeNode) iter.next();
330       convertSubTree(tree.getTree(item));
331       TestElement testElement = item.getTestElement();
332       tree.replace(item, testElement);
333     }
334   }
335 }
Popular Tags