Xcos src: Java cleanup
[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     @Override
113     public void actionPerformed(ActionEvent e) {
114         search();
115     }
116
117     @Override
118     public void callBack() {
119     }
120
121     /**
122      * Search
123      */
124     protected static void search() {
125         String query = txtSearch.getSelectedItem().toString();
126         if (query.isEmpty()) {
127             return;
128         }
129
130         PaletteManagerView.get().getSearchManager().search(query);
131         Object lastItem = txtSearch.getItemAt(0);
132         if (lastItem == null || !lastItem.toString().equals(query)) {
133             txtSearch.insertItemAt(query, 0); // insert on top
134         }
135     }
136 }
137
138 /**
139  * Implements a comboboxui
140  * @author Marcos CARDINOT <mcardinot@gmail.com>
141  */
142 class SearchUI extends MetalComboBoxUI {
143
144     /**
145      * Default constructor
146      */
147     public SearchUI() {
148         super();
149     }
150
151     /**
152      * Hides the arrow button
153      * @return JButton
154      */
155     @Override
156     @SuppressWarnings("serial")
157     protected JButton createArrowButton() {
158         JButton btn = new JButton() {
159             @Override
160             public int getWidth() {
161                 return 0;
162             }
163         };
164         btn.setVisible(false);
165         return btn;
166     }
167
168     /**
169      * Create the box editor
170      * @return ComboBoxEditor
171      */
172     @Override
173     protected ComboBoxEditor createEditor() {
174         BasicComboBoxEditor editor = (BasicComboBoxEditor) super.createEditor();
175         JTextField txtField = (JTextField) editor.getEditorComponent();
176
177         comboBox.setEditable(true);
178
179         final int height = txtField.getPreferredSize().height - 2;
180         final int width = 150;
181         Dimension dim = new Dimension(width, height);
182         comboBox.setPreferredSize(dim);
183         txtField.setPreferredSize(dim);
184
185         final int tb = 0;
186         final int lr = 5;
187         txtField.setBorder(new EmptyBorder(tb, lr, tb, lr));
188
189         txtField.addKeyListener(new KeyAdapter() {
190             @Override
191             public void keyReleased(KeyEvent e) {
192                 if (e.getKeyCode() == KeyEvent.VK_ENTER) {
193                     SearchAction.search();
194                 } else if (e.getKeyCode() != KeyEvent.VK_UP
195                            && e.getKeyCode() != KeyEvent.VK_DOWN) {
196                     comboBox.hidePopup();
197                 }
198             }
199         });
200         return editor;
201     }
202 }