6161import javax .swing .ImageIcon ;
6262import javax .swing .JButton ;
6363import javax .swing .JCheckBox ;
64+ import javax .swing .JDialog ;
6465import javax .swing .JLabel ;
6566import javax .swing .JList ;
6667import javax .swing .JPanel ;
@@ -211,6 +212,46 @@ public <PT extends SciJavaPlugin> void sort(final List<PT> instances,
211212 Collections .sort (instances , comparator );
212213 }
213214
215+ // -- Internal methods --
216+
217+ /** Called on the EDT when the search panel wants to appear. */
218+ protected void showPanel (final Container panel ) {
219+ assertDispatchThread ();
220+
221+ // create a dedicated "Quick Search" dialog
222+ final Window w = window ();
223+ final JDialog dialog = new JDialog (w , "Quick Search" );
224+ dialog .setContentPane (panel );
225+ dialog .pack ();
226+
227+ // position below the parent window
228+ final int x = w .getLocation ().x ;
229+ final int y = w .getLocation ().y + w .getHeight () + 1 ;
230+ dialog .setLocation (x , y );
231+ threadService .queue (() -> {
232+ dialog .setVisible (true );
233+ try { Thread .sleep (100 ); }
234+ catch (InterruptedException exc ) {}
235+ grabFocus ();
236+ requestFocus ();
237+ });
238+ }
239+
240+ /** Called on the EDT when the search panel wants to disappear. */
241+ protected void hidePanel (final Container panel ) {
242+ assertDispatchThread ();
243+
244+ // assume panel is inside its dedicated "Quick Search" dialog
245+ final Window w = SwingUtilities .getWindowAncestor (panel );
246+ if (w != null ) w .dispose ();
247+ }
248+
249+ /** Called on the EDT when the search text field wants to lose the focus. */
250+ protected void loseFocus () {
251+ assertDispatchThread ();
252+ window ().requestFocusInWindow ();
253+ }
254+
214255 // -- Helper methods --
215256
216257 /** Defensive programming check to avoid bugs. */
@@ -232,23 +273,8 @@ private void search() {
232273 // NB: Defer creating a new search dialog until something is typed.
233274 return ;
234275 }
235-
236276 searchPanel = new SwingSearchPanel (); // Spawns the SearchOperation!
237- getParent ().add (searchPanel , "south,height 300!" , getParent ()
238- .getComponentCount () - 1 );
239- getParent ().doLayout ();
240- getParent ().revalidate ();
241- window ().pack ();
242- getParent ().repaint ();
243- threadService .queue (() -> {
244- searchPanel .setVisible (true );
245- try {
246- Thread .sleep (100 );
247- }
248- catch (final InterruptedException exc ) {}
249- grabFocus ();
250- requestFocus ();
251- });
277+ showPanel (searchPanel );
252278 }
253279 searchPanel .search (getText ());
254280 }
@@ -270,25 +296,13 @@ private void reset() {
270296 getDocument ().addDocumentListener (documentListener );
271297 }
272298 else {
273- threadService .queue (() -> {
274- getParent ().remove (searchPanel );
275- getParent ().revalidate ();
276- getParent ().repaint ();
277- final Window w = window ();
278- w .revalidate ();
279- w .pack ();
280- w .repaint ();
281- searchPanel = null ;
282- });
299+ hidePanel (searchPanel );
300+ searchPanel = null ;
283301 setText ("" );
284302 requestFocusInWindow ();
285303 }
286304 }
287305
288- private void loseFocus () {
289- SwingUtilities .getWindowAncestor (this ).requestFocusInWindow ();
290- }
291-
292306 // -- Helper classes --
293307
294308 private class SwingSearchPanel extends JPanel {
0 commit comments