Xcos src: Java cleanup
[scilab.git] / scilab / modules / xcos / src / java / org / scilab / modules / xcos / palette / actions / SearchAction.java
index 0deb3f2..a4f6af5 100644 (file)
@@ -15,16 +15,24 @@ package org.scilab.modules.xcos.palette.actions;
 import java.awt.Dimension;
 import java.awt.FlowLayout;
 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;
 import org.scilab.modules.gui.events.callback.CommonCallBack;
+import org.scilab.modules.xcos.palette.view.PaletteManagerView;
 import org.scilab.modules.xcos.utils.XcosMessages;
 
 /**
@@ -39,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;
 
     /**
@@ -57,8 +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 = new JComboBox();
+        txtSearch.setUI(new SearchUI());
         searchBar.add(txtSearch);
 
         btnSearch = new JButton();
@@ -101,11 +109,94 @@ public class SearchAction extends CommonCallBack {
      * Action
      * @param e ActionEvent
      */
+    @Override
     public void actionPerformed(ActionEvent e) {
-        // TODO
+        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
+     */
+    @Override
+    @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
+     */
+    @Override
+    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;
+    }
 }