0ac4de47237189e6e8c2ea5c8375022850260d73
[scilab.git] / scilab / modules / xcos / src / java / org / scilab / modules / xcos / palette / actions / SearchAction.java
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2015 - Marcos CARDINOT
4  *
5  * This file must be used under the terms of the CeCILL.
6  * This source file is licensed as described in the file COPYING, which
7  * you should have received as part of this distribution.  The terms
8  * are also available at
9  * http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
10  *
11  */
12
13 package org.scilab.modules.xcos.palette.actions;
14
15 import java.awt.Dimension;
16 import java.awt.FlowLayout;
17 import java.awt.event.ActionEvent;
18 import java.awt.event.KeyAdapter;
19 import java.awt.event.KeyEvent;
20 import java.lang.reflect.InvocationTargetException;
21
22 import javax.swing.ComboBoxEditor;
23 import javax.swing.ImageIcon;
24 import javax.swing.JButton;
25 import javax.swing.JComboBox;
26 import javax.swing.JPanel;
27 import javax.swing.JTextField;
28 import javax.swing.border.EmptyBorder;
29 import javax.swing.plaf.basic.BasicComboBoxEditor;
30 import javax.swing.plaf.metal.MetalComboBoxUI;
31
32 import org.scilab.modules.commons.gui.FindIconHelper;
33 import org.scilab.modules.commons.gui.ScilabLAF;
34 import org.scilab.modules.gui.events.callback.CommonCallBack;
35 import org.scilab.modules.xcos.palette.view.PaletteManagerView;
36 import org.scilab.modules.xcos.utils.XcosMessages;
37
38 /**
39  * Search Palettes Action
40  * @author Marcos CARDINOT <mcardinot@gmail.com>
41  */
42 public class SearchAction extends CommonCallBack {
43
44     private static final long serialVersionUID = 1L;
45
46     private static final String LABEL_SEARCH = XcosMessages.SEARCH;
47     private static final String ICON_SEARCH = FindIconHelper.findIcon("system-search");
48
49     private static JButton btnSearch;
50     private static JComboBox txtSearch;
51     private static JPanel searchBar;
52
53     /**
54      * Constructor
55      */
56     public SearchAction() {
57         super("");
58     }
59
60     /**
61      * Create the search bar
62      * @return JPanel
63      */
64     public static JPanel createSearchBar() {
65         searchBar = new JPanel();
66         searchBar.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
67
68         txtSearch = new JComboBox();
69         txtSearch.setUI(new SearchUI());
70         searchBar.add(txtSearch);
71
72         btnSearch = new JButton();
73         ScilabLAF.setDefaultProperties(btnSearch);
74         btnSearch.setIcon(new ImageIcon(ICON_SEARCH));
75         btnSearch.setToolTipText(LABEL_SEARCH);
76         btnSearch.addActionListener(getCallBack());
77         btnSearch.setFocusable(true);
78         btnSearch.setBorderPainted(false);
79         searchBar.add(btnSearch);
80
81         return searchBar;
82     }
83
84     /**
85      * Create a new class instance
86      * @return the instance
87      */
88     private static CommonCallBack getCallBack() {
89         CommonCallBack callback = null;
90         try {
91             callback = SearchAction.class.getConstructor().newInstance();
92         } catch (IllegalArgumentException e) {
93             e.printStackTrace();
94         } catch (SecurityException e) {
95             e.printStackTrace();
96         } catch (InstantiationException e) {
97             e.printStackTrace();
98         } catch (IllegalAccessException e) {
99             e.printStackTrace();
100         } catch (InvocationTargetException e) {
101             e.printStackTrace();
102         } catch (NoSuchMethodException e) {
103             e.printStackTrace();
104         }
105         return callback;
106     }
107
108     /**
109      * Action
110      * @param e ActionEvent
111      */
112     public void actionPerformed(ActionEvent e) {
113         search();
114     }
115
116     @Override
117     public void callBack() {
118     }
119
120     /**
121      * Search
122      */
123     protected static void search() {
124         String query = txtSearch.getSelectedItem().toString();
125         if (query.isEmpty()) {
126             return;
127         }
128
129         PaletteManagerView.get().getSearchManager().search(query);
130         Object lastItem = txtSearch.getItemAt(0);
131         if (lastItem == null || !lastItem.toString().equals(query)) {
132             txtSearch.insertItemAt(query, 0); // insert on top
133         }
134     }
135 }
136
137 /**
138  * Implements a comboboxui
139  * @author Marcos CARDINOT <mcardinot@gmail.com>
140  */
141 class SearchUI extends MetalComboBoxUI {
142
143     /**
144      * Default constructor
145      */
146     public SearchUI() {
147         super();
148     }
149
150     /**
151      * Hides the arrow button
152      * @return JButton
153      */
154     @SuppressWarnings("serial")
155     protected JButton createArrowButton() {
156         JButton btn = new JButton() {
157             @Override
158             public int getWidth() {
159                 return 0;
160             }
161         };
162         btn.setVisible(false);
163         return btn;
164     }
165
166     /**
167      * Create the box editor
168      * @return ComboBoxEditor
169      */
170     protected ComboBoxEditor createEditor() {
171         BasicComboBoxEditor editor = (BasicComboBoxEditor) super.createEditor();
172         JTextField txtField = (JTextField) editor.getEditorComponent();
173
174         comboBox.setEditable(true);
175
176         final int height = txtField.getPreferredSize().height - 2;
177         final int width = 150;
178         Dimension dim = new Dimension(width, height);
179         comboBox.setPreferredSize(dim);
180         txtField.setPreferredSize(dim);
181
182         final int tb = 0;
183         final int lr = 5;
184         txtField.setBorder(new EmptyBorder(tb, lr, tb, lr));
185
186         txtField.addKeyListener(new KeyAdapter() {
187             @Override
188             public void keyReleased(KeyEvent e) {
189                 if (e.getKeyCode() == KeyEvent.VK_ENTER) {
190                     SearchAction.search();
191                 } else if (e.getKeyCode() != KeyEvent.VK_UP
192                         && e.getKeyCode() != KeyEvent.VK_DOWN) {
193                     comboBox.hidePopup();
194                 }
195             }
196         });
197         return editor;
198     }
199 }