3232import java .awt .BorderLayout ;
3333import java .awt .Color ;
3434import java .awt .Component ;
35+ import java .awt .Container ;
3536import java .awt .Dimension ;
3637import java .awt .Font ;
3738import java .awt .GridLayout ;
5657import javax .swing .ImageIcon ;
5758import javax .swing .JButton ;
5859import javax .swing .JCheckBox ;
59- import javax .swing .JDialog ;
6060import javax .swing .JLabel ;
6161import javax .swing .JList ;
6262import javax .swing .JPanel ;
6868import javax .swing .ScrollPaneConstants ;
6969import javax .swing .SwingConstants ;
7070import javax .swing .SwingUtilities ;
71+ import javax .swing .border .Border ;
72+ import javax .swing .border .CompoundBorder ;
7173import javax .swing .border .EmptyBorder ;
7274import javax .swing .event .DocumentEvent ;
7375import javax .swing .event .DocumentListener ;
9395 *
9496 * @author Curtis Rueden
9597 */
98+
9699public class SwingSearchBar extends JTextField {
97100
98101 private static final String DEFAULT_MESSAGE = "Click here to search" ;
99102 private static final int MAX_RESULTS = 8 ;
100103
101104 private static final Color SELECTED_COLOR = new Color (70 , 152 , 251 );
102105 private static final Color HEADER_COLOR = new Color (234 , 234 , 234 );
106+ private static final Color PANEL_COLOR = new Color (77 , 77 , 77 );
103107 private static final int ICON_SIZE = 16 ;
104108 private static final int PAD = 5 ;
105109
@@ -112,14 +116,19 @@ public class SwingSearchBar extends JTextField {
112116 @ Parameter
113117 private PluginService pluginService ;
114118
115- private final Window parent ;
116- private JDialog dialog ;
119+ private final Window window ;
120+ private final Container parent ;
117121 private SwingSearchPanel searchPanel ;
118122
119- public SwingSearchBar (final Context context , final Window parent ) {
123+ public SwingSearchBar (final Context context , final Window window , final Container parent ) {
120124 super (DEFAULT_MESSAGE , 12 );
121125 this .parent = parent ;
126+ this .window = window ;
122127 context .inject (this );
128+
129+ setBorder (BorderFactory .createCompoundBorder (
130+ BorderFactory .createLineBorder (new Color (237 ,237 ,237 ), 5 ),
131+ BorderFactory .createEmptyBorder (5 ,5 ,5 ,5 )));
123132
124133 addActionListener (e -> run ());
125134 addKeyListener (new SearchBarKeyAdapter ());
@@ -193,32 +202,28 @@ private void assertDispatchThread() {
193202 /** Called whenever the user types something. */
194203 private void search () {
195204 assertDispatchThread ();
196- if (dialog == null ) {
205+ if (searchPanel == null ) {
197206 if (getText ().equals ("" ) || getText ().equals (DEFAULT_MESSAGE )) {
198207 // NB: Defer creating a new search dialog until something is typed.
199208 return ;
200209 }
201210
202- dialog = new JDialog (parent , "Quick Search" );
203211 searchPanel = new SwingSearchPanel (); // Spawns the SearchOperation!
204- dialog .setContentPane (searchPanel );
205- dialog .pack ();
206-
207- // position below the parent window
208- final int x = parent .getLocation ().x ;
209- final int y = parent .getLocation ().y + parent .getHeight () + 1 ;
210- dialog .setLocation (x , y );
211- }
212- searchPanel .search (getText ());
213- if (!dialog .isVisible ()) {
212+ searchPanel .setBorder (BorderFactory .createEmptyBorder (0 ,5 ,0 ,5 ));
213+ parent .add (searchPanel , "south,height 300!" , getParent ().getComponentCount ()-1 );
214+ parent .doLayout ();
215+ parent .revalidate ();
216+ parent .repaint ();
217+ window .pack ();
214218 threadService .queue (() -> {
215- dialog .setVisible (true );
219+ searchPanel .setVisible (true );
216220 try { Thread .sleep (100 ); }
217221 catch (InterruptedException exc ) {}
218222 grabFocus ();
219223 requestFocus ();
220224 });
221225 }
226+ searchPanel .search (getText ());
222227 }
223228
224229 /** Called when the user hits ENTER. */
@@ -230,11 +235,17 @@ private void run() {
230235
231236 private void reset () {
232237 assertDispatchThread ();
233- if (dialog == null ) loseFocus ();
238+ if (searchPanel == null ) loseFocus ();
234239 else {
235- searchPanel = null ;
236- dialog .dispose ();
237- dialog = null ;
240+ threadService .queue (() -> {
241+ parent .remove (searchPanel );
242+ parent .revalidate ();
243+ parent .repaint (50L );
244+ window .revalidate ();
245+ window .pack ();
246+ window .repaint (50L );
247+ searchPanel = null ;
248+ });
238249 }
239250 setText ("" );
240251 }
@@ -291,17 +302,19 @@ public SwingSearchPanel() {
291302 item .setBackground (isSelected ? SELECTED_COLOR : list .getBackground ());
292303 return item ;
293304 });
294- resultsList .setBorder (new EmptyBorder (0 , 0 , PAD , 0 ));
305+ resultsList .setBorder (new EmptyBorder (0 , 0 , 0 , 0 ));
295306 final JScrollPane resultsPane = new JScrollPane (resultsList );
296307 resultsPane .setHorizontalScrollBarPolicy (
297308 ScrollPaneConstants .HORIZONTAL_SCROLLBAR_NEVER );
309+ resultsPane .setBorder (BorderFactory .createEmptyBorder ());
310+ resultsPane .setBackground (PANEL_COLOR );
298311
299312 final JPanel detailsPane = new JPanel ();
300313 final JLabel detailsTitle = new JLabel ();
301314 detailsTitle .setHorizontalAlignment (SwingConstants .LEFT );
302315 final JPanel detailsProps = new JPanel ();
303316 final JPanel detailsButtons = new JPanel ();
304- detailsButtons .setLayout (new BoxLayout ( detailsButtons , BoxLayout . X_AXIS ));
317+ detailsButtons .setLayout (new MigLayout ( ));
305318
306319 detailsPane .setLayout (new MigLayout ("wrap" ,"[grow]" , "[][grow][]" ));
307320 detailsPane .setBorder (BorderFactory .createEmptyBorder (PAD , PAD , PAD , PAD ));
@@ -368,12 +381,18 @@ public SwingSearchPanel() {
368381 }
369382 });
370383 button .addKeyListener (new SearchBarKeyAdapter ());
371- detailsButtons .add (button );
372384 if (first ){
385+ Border border = new CompoundBorder (new EmptyBorder (5 ,0 ,0 ,0 ), button .getBorder ());
386+ button .setBorder (border );
387+ detailsButtons .add (button , "south" );
373388 JRootPane rootPane = this .getRootPane ();
374- rootPane .setDefaultButton (button );
375- detailsButtons .add (Box .createHorizontalGlue ());
389+ if (rootPane != null ){
390+ rootPane .setDefaultButton (button );
391+ }
392+ // detailsButtons.add(Box.createHorizontalGlue());
376393 first = false ;
394+ }else {
395+ detailsButtons .add (button , "growx" );
377396 }
378397 }
379398 }
@@ -383,12 +402,16 @@ public SwingSearchPanel() {
383402
384403 setLayout (new BorderLayout ());
385404 setPreferredSize (new Dimension (800 , 300 ));
405+ setBorder (BorderFactory .createEmptyBorder ());
386406
407+ // Border border = new CompoundBorder(new LineBorder(PANEL_COLOR, 1), new LineBorder(new Color(33,33,33), 5));
408+ // UIManager.put("SplitPaneDivider.border", border);
387409 final JSplitPane splitPane = new JSplitPane (JSplitPane .HORIZONTAL_SPLIT );
388410 splitPane .setLeftComponent (resultsPane );
389- splitPane .setRightComponent (detailsPane );
411+ splitPane .setRightComponent (detailsPane );
412+ splitPane .setDividerLocation (0.3 );
413+ splitPane .setBorder (BorderFactory .createEmptyBorder ());
390414 add (splitPane , BorderLayout .CENTER );
391-
392415 }
393416
394417 public void search (final String text ) {
@@ -480,8 +503,9 @@ private void rebuild() {
480503 resultsList .setModel (listModel );
481504
482505 // TODO: Improve retainment of previous selection.
483- if (previous == null ) resultsList .setSelectedIndex (firstResultIndex ());
484- else resultsList .setSelectedValue (previous , true );
506+ // TODO check why this crashes with nullpointer
507+ // if (previous == null) resultsList.setSelectedIndex(firstResultIndex());
508+ // else resultsList.setSelectedValue(previous, true);
485509 }
486510
487511 private Component icon (final String iconPath ) {
0 commit comments