palette searchaction - using a combobox instead of a textfield 59/17059/2
Marcos CARDINOT [Tue, 18 Aug 2015 15:50:34 +0000 (12:50 -0300)]
we now use a combobox to show the last queries (popup)

it implements a custom ComboBoxUI to:
- hide the popup when the user is typing
- hide the arrow button (the arrow_down key is intuitive enough)

we also handle the ENTER key to start the search and fix some minor issues with
border and size.

Change-Id: I98330c241c6f21b15edc080134f6f04ec52218fe

scilab/modules/xcos/src/java/org/scilab/modules/xcos/palette/actions/SearchAction.java

index 3d3305e..0ac4de4 100644 (file)
@@ -14,14 +14,20 @@ package org.scilab.modules.xcos.palette.actions;
 
 import java.awt.Dimension;
 import java.awt.FlowLayout;
-import java.awt.Font;
 import java.awt.event.ActionEvent;
+import java.awt.event.KeyAdapter;
+import java.awt.event.KeyEvent;
 import java.lang.reflect.InvocationTargetException;
 
+import javax.swing.ComboBoxEditor;
 import javax.swing.ImageIcon;
 import javax.swing.JButton;
+import javax.swing.JComboBox;
 import javax.swing.JPanel;
 import javax.swing.JTextField;
+import javax.swing.border.EmptyBorder;
+import javax.swing.plaf.basic.BasicComboBoxEditor;
+import javax.swing.plaf.metal.MetalComboBoxUI;
 
 import org.scilab.modules.commons.gui.FindIconHelper;
 import org.scilab.modules.commons.gui.ScilabLAF;
@@ -41,7 +47,7 @@ public class SearchAction extends CommonCallBack {
     private static final String ICON_SEARCH = FindIconHelper.findIcon("system-search");
 
     private static JButton btnSearch;
-    private static JTextField txtSearch;
+    private static JComboBox txtSearch;
     private static JPanel searchBar;
 
     /**
@@ -59,9 +65,8 @@ public class SearchAction extends CommonCallBack {
         searchBar = new JPanel();
         searchBar.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
 
-        txtSearch = new JTextField();
-        txtSearch.setPreferredSize(new Dimension(150, 22));
-        txtSearch.setFont(new Font("SansSerif", Font.PLAIN, 10));
+        txtSearch = new JComboBox();
+        txtSearch.setUI(new SearchUI());
         searchBar.add(txtSearch);
 
         btnSearch = new JButton();
@@ -105,12 +110,90 @@ public class SearchAction extends CommonCallBack {
      * @param e ActionEvent
      */
     public void actionPerformed(ActionEvent e) {
-        if (!txtSearch.getText().isEmpty()) {
-            PaletteManagerView.get().getSearchManager().search(txtSearch.getText());
-        }
+        search();
     }
 
     @Override
     public void callBack() {
     }
+
+    /**
+     * Search
+     */
+    protected static void search() {
+        String query = txtSearch.getSelectedItem().toString();
+        if (query.isEmpty()) {
+            return;
+        }
+
+        PaletteManagerView.get().getSearchManager().search(query);
+        Object lastItem = txtSearch.getItemAt(0);
+        if (lastItem == null || !lastItem.toString().equals(query)) {
+            txtSearch.insertItemAt(query, 0); // insert on top
+        }
+    }
+}
+
+/**
+ * Implements a comboboxui
+ * @author Marcos CARDINOT <mcardinot@gmail.com>
+ */
+class SearchUI extends MetalComboBoxUI {
+
+    /**
+     * Default constructor
+     */
+    public SearchUI() {
+        super();
+    }
+
+    /**
+     * Hides the arrow button
+     * @return JButton
+     */
+    @SuppressWarnings("serial")
+    protected JButton createArrowButton() {
+        JButton btn = new JButton() {
+            @Override
+            public int getWidth() {
+                return 0;
+            }
+        };
+        btn.setVisible(false);
+        return btn;
+    }
+
+    /**
+     * Create the box editor
+     * @return ComboBoxEditor
+     */
+    protected ComboBoxEditor createEditor() {
+        BasicComboBoxEditor editor = (BasicComboBoxEditor) super.createEditor();
+        JTextField txtField = (JTextField) editor.getEditorComponent();
+
+        comboBox.setEditable(true);
+
+        final int height = txtField.getPreferredSize().height - 2;
+        final int width = 150;
+        Dimension dim = new Dimension(width, height);
+        comboBox.setPreferredSize(dim);
+        txtField.setPreferredSize(dim);
+
+        final int tb = 0;
+        final int lr = 5;
+        txtField.setBorder(new EmptyBorder(tb, lr, tb, lr));
+
+        txtField.addKeyListener(new KeyAdapter() {
+            @Override
+            public void keyReleased(KeyEvent e) {
+                if (e.getKeyCode() == KeyEvent.VK_ENTER) {
+                    SearchAction.search();
+                } else if (e.getKeyCode() != KeyEvent.VK_UP
+                        && e.getKeyCode() != KeyEvent.VK_DOWN) {
+                    comboBox.hidePopup();
+                }
+            }
+        });
+        return editor;
+    }
 }