2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2015 - Marcos CARDINOT
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
13 package org.scilab.modules.xcos.palette.actions;
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;
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;
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;
39 * Search Palettes Action
40 * @author Marcos CARDINOT <mcardinot@gmail.com>
42 public class SearchAction extends CommonCallBack {
44 private static final long serialVersionUID = 1L;
46 private static final String LABEL_SEARCH = XcosMessages.SEARCH;
47 private static final String ICON_SEARCH = FindIconHelper.findIcon("system-search");
49 private static JButton btnSearch;
50 private static JComboBox txtSearch;
51 private static JPanel searchBar;
56 public SearchAction() {
61 * Create the search bar
64 public static JPanel createSearchBar() {
65 searchBar = new JPanel();
66 searchBar.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
68 txtSearch = new JComboBox();
69 txtSearch.setUI(new SearchUI());
70 searchBar.add(txtSearch);
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);
85 * Create a new class instance
86 * @return the instance
88 private static CommonCallBack getCallBack() {
89 CommonCallBack callback = null;
91 callback = SearchAction.class.getConstructor().newInstance();
92 } catch (IllegalArgumentException e) {
94 } catch (SecurityException e) {
96 } catch (InstantiationException e) {
98 } catch (IllegalAccessException e) {
100 } catch (InvocationTargetException e) {
102 } catch (NoSuchMethodException e) {
110 * @param e ActionEvent
113 public void actionPerformed(ActionEvent e) {
118 public void callBack() {
124 protected static void search() {
125 String query = txtSearch.getSelectedItem().toString();
126 if (query.isEmpty()) {
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
139 * Implements a comboboxui
140 * @author Marcos CARDINOT <mcardinot@gmail.com>
142 class SearchUI extends MetalComboBoxUI {
145 * Default constructor
152 * Hides the arrow button
156 @SuppressWarnings("serial")
157 protected JButton createArrowButton() {
158 JButton btn = new JButton() {
160 public int getWidth() {
164 btn.setVisible(false);
169 * Create the box editor
170 * @return ComboBoxEditor
173 protected ComboBoxEditor createEditor() {
174 BasicComboBoxEditor editor = (BasicComboBoxEditor) super.createEditor();
175 JTextField txtField = (JTextField) editor.getEditorComponent();
177 comboBox.setEditable(true);
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);
187 txtField.setBorder(new EmptyBorder(tb, lr, tb, lr));
189 txtField.addKeyListener(new KeyAdapter() {
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();