diff --git a/src/core/org/apache/jmeter/gui/action/SearchTreeCommand.java b/src/core/org/apache/jmeter/gui/action/SearchTreeCommand.java index d0003ef..12974a5 100644 --- a/src/core/org/apache/jmeter/gui/action/SearchTreeCommand.java +++ b/src/core/org/apache/jmeter/gui/action/SearchTreeCommand.java @@ -18,10 +18,17 @@ package org.apache.jmeter.gui.action; +import java.awt.Component; +import java.awt.Window; import java.awt.event.ActionEvent; import java.util.HashSet; import java.util.Set; +import javax.swing.JFrame; +import javax.swing.JMenuItem; +import javax.swing.JPopupMenu; +import javax.swing.SwingUtilities; + /** * Search nodes for a text * TODO Enhance search dialog to select kind of nodes .... @@ -34,12 +41,51 @@ commands.add(ActionNames.SEARCH_TREE); } - private SearchTreeDialog dialog = new SearchTreeDialog(); + private SearchTreeDialog dialog; + + /** + *

+ * Create the search dialog from the specified source component.
+ * This method tries to find a JFrame ancestor from the specified source in + * order to be the parent of the search dialog.
+ * With no parent set the search dialog might be hidden by the main JFrame when + * focus is transfered to that JFrame. + *

+ *

+ * If no parent if found, then we give up and build a search dialog with no + * parent. + *

+ * + * @param source The source object that originated the display of the dialog + * @return A freshly created search dialog with the parent frame that could be + * found, or no parent otherwise. + */ + private SearchTreeDialog createSearchDialog(Object source) { + JFrame parent = null; + if (source instanceof JMenuItem) { + JMenuItem item = (JMenuItem) source; + Component comp = item.getParent(); + if (comp instanceof JPopupMenu) { + JPopupMenu popup = (JPopupMenu) comp; + comp = popup.getInvoker(); + Window window = SwingUtilities.windowForComponent((Component) comp); + if (window != null && window instanceof JFrame) { + parent = (JFrame) window; + } + } + } + return new SearchTreeDialog(parent); + } + /** * @see Command#doAction(ActionEvent) */ @Override public void doAction(ActionEvent e) { + // we create the dialog upon first display event only + if (dialog == null) { + dialog = createSearchDialog(e.getSource()); + } dialog.setVisible(true); } diff --git a/src/core/org/apache/jmeter/gui/action/SearchTreeDialog.java b/src/core/org/apache/jmeter/gui/action/SearchTreeDialog.java index 3bda618..fbcf8a3 100644 --- a/src/core/org/apache/jmeter/gui/action/SearchTreeDialog.java +++ b/src/core/org/apache/jmeter/gui/action/SearchTreeDialog.java @@ -111,8 +111,8 @@ private List lastSearchResult = new ArrayList<>(); private int currentSearchIndex; - public SearchTreeDialog() { - super((JFrame) null, JMeterUtils.getResString("search_tree_title"), false); //$NON-NLS-1$ + public SearchTreeDialog(JFrame parent) { + super(parent, JMeterUtils.getResString("search_tree_title"), false); //$NON-NLS-1$ init(); }