OSDN Git Service

97b51f913ded89f017c5527a6c052421fc2ac2d0
[sawarabi-fonts/sawarabi-fonts.git] / chartool / src / jp / sourceforge / sawarabifonts / chartool / SearchPanel.java
1 /*
2  * Copyright (C) 2010, mshio <mshio@users.sourceforge.jp>
3  *
4  * This program is free software: you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  * ---
17  * Require JDK 1.5 (or later)
18  */
19 package jp.sourceforge.sawarabifonts.chartool;
20
21 import java.awt.BorderLayout;
22 import java.awt.Component;
23 import java.awt.Toolkit;
24 import java.awt.datatransfer.Clipboard;
25 import java.awt.datatransfer.DataFlavor;
26 import java.awt.event.ActionEvent;
27 import java.awt.event.KeyEvent;
28 import java.awt.event.MouseAdapter;
29 import java.awt.event.MouseEvent;
30
31 import javax.swing.Action;
32 import javax.swing.ActionMap;
33 import javax.swing.JButton;
34 import javax.swing.JMenuItem;
35 import javax.swing.JPanel;
36 import javax.swing.JPopupMenu;
37 import javax.swing.JTextField;
38 import javax.swing.KeyStroke;
39 import javax.swing.text.DefaultEditorKit;
40 import javax.swing.text.JTextComponent;
41 import javax.swing.text.Keymap;
42 import javax.swing.text.TextAction;
43
44 public class SearchPanel extends JPanel {
45         private static final long serialVersionUID = 1971690273660748828L;
46
47         private final JTextField searchField;
48         private final HistoryBuffer buffer = new HistoryBuffer();
49         private final SearchFieldPopup popup;
50
51         public SearchPanel(final GuiController controller) {
52                 setLayout(new BorderLayout());
53
54                 searchField = new JTextField();
55                 popup = new SearchFieldPopup();
56                 searchField.addMouseListener(new MouseAdapter() {
57                         @Override
58                         public void mouseClicked(MouseEvent e) {
59                                 if (e.getButton() == MouseEvent.BUTTON3) {
60                                         popup.show(searchField, e.getX(), e.getY());
61                                 }
62                         }
63                 });
64                 setupKeymap(controller);
65                 
66                 add(searchField, BorderLayout.CENTER);
67                 add(makeSearchButton(), BorderLayout.EAST);
68
69                 controller.setSearchPanel(this);
70         }
71
72         private JButton makeSearchButton() {
73                 JButton b = new JButton("Search");
74                 Keymap k = searchField.getKeymap();
75                 Action a = k.getAction(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0));
76                 b.addActionListener(a);
77                 b.setFocusable(false);
78
79                 return b;
80         }
81
82         public void setFocus() {
83                 searchField.requestFocusInWindow();
84         }
85
86         public void setCaretPosition(int position, int length) {
87                 if (searchField.getText().length() > position) {
88                         searchField.setSelectionStart(position);
89                         searchField.setSelectionEnd(position + length); 
90                 } else {
91                         searchField.setSelectionStart(0);
92                         searchField.setSelectionEnd(length);
93                 }
94         }
95
96         private void setupKeymap(final GuiController controller) {
97                 Keymap map = JTextComponent.addKeymap("charpalette", searchField.getKeymap());
98                 searchField.setKeymap(map);
99
100                 setupSearchKeymap(controller);
101                 setupHistoryUpKeymap();
102                 setupHistoryDownKeymap();
103         }
104
105         private void setupHistoryUpKeymap() {
106                 Keymap map = searchField.getKeymap();
107                 KeyStroke key0 = KeyStroke.getKeyStroke(KeyEvent.VK_P, KeyEvent.CTRL_MASK);
108                 KeyStroke key1 = KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0);
109                 Action a = new TextAction("history-up") {
110                         private static final long serialVersionUID = 8417010810041845179L;
111                         public void actionPerformed(ActionEvent e) {
112                                 String s = buffer.getPrev();
113                                 if (s != null) {
114                                         searchField.setText(s);
115                                         searchField.setCaretPosition(0);
116                                 }
117                         }
118                 }; 
119                 map.addActionForKeyStroke(key0, a);
120                 map.addActionForKeyStroke(key1, a);
121         }
122
123         private void setupHistoryDownKeymap() {
124                 Keymap map = searchField.getKeymap();
125                 KeyStroke key0 = KeyStroke.getKeyStroke(KeyEvent.VK_N, KeyEvent.CTRL_MASK);
126                 KeyStroke key1 = KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0);
127                 Action a = new TextAction("history-down") {
128                         private static final long serialVersionUID = 2292000606648845301L;
129                         public void actionPerformed(ActionEvent e) {
130                                 String s = buffer.getNext();
131                                 if (s != null) {
132                                         searchField.setText(s);
133                                         searchField.setCaretPosition(0);
134                                 }
135                         }
136                 }; 
137                 map.addActionForKeyStroke(key0, a);
138                 map.addActionForKeyStroke(key1, a);
139         }
140
141         private void setupSearchKeymap(final GuiController controller) {
142                 KeyStroke key = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
143                 Action a = new TextAction("search") {
144                         private static final long serialVersionUID = 7670487950463928408L;
145                         public void actionPerformed(ActionEvent e) {
146                                 String st = searchField.getSelectedText();
147                                 int p = searchField.getCaretPosition();
148                                 controller.onSearch(searchField.getText(), p, st);
149                                 String s = buffer.getCurrent();
150                                 String t = searchField.getText();
151                                 if (s == null || ! s.equals(t)) {
152                                         buffer.add(t);
153                                 }
154                         }
155                 };
156                 searchField.getKeymap().addActionForKeyStroke(key, a);
157         }
158
159
160         class SearchFieldPopup extends JPopupMenu {
161                 private static final long serialVersionUID = 5225317856689478836L;
162
163                 final JMenuItem cut;
164                 final JMenuItem copy;
165                 final JMenuItem paste;
166                 final JMenuItem clear;
167
168                 SearchFieldPopup() {
169                         cut = makeMenuItem("Cut", DefaultEditorKit.cutAction);
170                         copy = makeMenuItem("Copy", DefaultEditorKit.copyAction);
171                         paste = makeMenuItem("Paste", DefaultEditorKit.pasteAction);
172                         clear = new JMenuItem(new TextAction("Clear") {
173                                 private static final long serialVersionUID = 3995545953271258486L;
174                                 public void actionPerformed(ActionEvent e) {
175                                         searchField.setText("");
176                                 }
177                         });
178                         add(cut);
179                         add(copy);
180                         add(paste);
181                         add(clear);
182                 }
183
184                 private JMenuItem makeMenuItem(String name, String action) {
185                         JMenuItem ret = null;
186                         Action a = getActionFromTextField(action);
187                         if (a != null) {
188                                 ret = new JMenuItem();
189                                 ret.setAction(a);
190                                 ret.setText(name);
191                         }
192                         return ret;
193                 }
194
195                 private Action getActionFromTextField(String name) {
196                         ActionMap m = searchField.getActionMap();
197                         return m.get(name);
198                 }
199
200                 private boolean isClipboardContentsString(Clipboard c) {
201                         boolean ret = false;
202                         try {
203                                 ret = c.isDataFlavorAvailable(DataFlavor.stringFlavor);
204                         } catch (IllegalStateException e) {
205                                 e.printStackTrace();
206                         }
207                         return ret;
208                 }
209
210                 private Clipboard getClipboard() {
211                         Toolkit k = Toolkit.getDefaultToolkit();
212                         return k.getSystemClipboard();
213                 }
214
215                 @Override
216                 public void show(Component invoker, int x, int y) {
217                         boolean s = searchField.getSelectedText() != null;
218                         cut.setEnabled(s);
219                         copy.setEnabled(s);
220                         paste.setEnabled(isClipboardContentsString(getClipboard()));
221                         clear.setEnabled(searchField.getText().length() != 0);
222                         super.show(invoker, x, y);
223                 }
224         }
225
226 }