001 /*
002 * Copyright (c) 1995-2010, The University of Sheffield. See the file
003 * COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
004 *
005 * This file is part of GATE (see http://gate.ac.uk/), and is free
006 * software, licenced under the GNU Library General Public License,
007 * Version 2, June 1991 (in the distribution as file licence.html,
008 * and also available at http://gate.ac.uk/gate/licence.html).
009 *
010 * Thomas Heitz, Nov 7, 2008
011 *
012 * $Id: SchemaAnnotationEditor.java 9221 2007-11-14 17:46:37Z valyt $
013 */
014
015 package gate.gui.annedit;
016
017 import gate.util.LuckyException;
018
019 import javax.swing.*;
020 import javax.swing.text.BadLocationException;
021 import javax.swing.event.CaretListener;
022 import javax.swing.event.CaretEvent;
023 import javax.swing.event.ListSelectionListener;
024 import javax.swing.event.ListSelectionEvent;
025 import java.awt.event.ActionEvent;
026 import java.awt.event.ActionListener;
027 import java.awt.event.MouseAdapter;
028 import java.awt.event.MouseEvent;
029 import java.awt.*;
030
031 /**
032 * Dialog for building a regular expression.
033 */
034 public class SearchExpressionsAction extends AbstractAction {
035 private static final long serialVersionUID = 1L;
036 private JTextField sourceTextField;
037 private Window annotationEditorWindow;
038 private JCheckBox searchRegExpChk;
039
040 /**
041 * Shows a dialog with a list of predefined search expressions
042 * to modified the current search expression.
043 *
044 * @param sourceTextField text field that contains the search expression
045 * @param annotationEditorWindow search window
046 * @param searchRegExpChk check box for regular expression; may be null
047 */
048 public SearchExpressionsAction(JTextField sourceTextField,
049 Window annotationEditorWindow,
050 JCheckBox searchRegExpChk){
051 super("?");
052 super.putValue(SHORT_DESCRIPTION, "GATE search expression builder.");
053 this.sourceTextField = sourceTextField;
054 this.annotationEditorWindow = annotationEditorWindow;
055 this.searchRegExpChk = searchRegExpChk;
056 }
057
058 public void actionPerformed(ActionEvent arg0) {
059 String[] values1 = {
060 "Number",
061 "Person"
062 };
063 String[] values2 = {
064 "Any character",
065 "The beginning of a line",
066 "The end of a line",
067 "All letters",
068 "Letter uppercase",
069 "Letter lowercase",
070 "Letter titlecase",
071 "Letter modifier",
072 "Letter other",
073 "All Numbers",
074 "Number decimal digit",
075 "Number letter",
076 "Number other",
077 "All punctuations",
078 "Punctuation connector",
079 "Punctuation dash",
080 "Punctuation open",
081 "Punctuation close",
082 "Punctuation initial quote",
083 "Punctuation final quote",
084 "Punctuation other",
085 "All symbols",
086 "Symbol math",
087 "Symbol currency",
088 "Symbol modifier",
089 "Symbol other",
090 "All separators",
091 "Separator space",
092 "Separator line",
093 "Separator paragraph",
094 "All Marks",
095 "Mark nonspacing",
096 "Mark spacing combining",
097 "Mark enclosing",
098 "All others",
099 "Other control",
100 "Other format",
101 "Other surrogate",
102 "Other private use",
103 "Other not assigned",
104 "Any character except Category",
105 "Category1 and/or Category2",
106 "Category1 and Category2"
107 };
108 String[] values3 = {
109 "Either the selection or X",
110 "Once or not at all",
111 "Zero or more times",
112 "One or more times",
113 "Capturing group",
114 "Non-capturing group"
115 };
116 final JTextField modifiedTextField = new JTextField(25);
117 modifiedTextField.setText(sourceTextField.getText());
118 JPanel vspace1 = new JPanel();
119 vspace1.setSize(0, 5);
120 final JList list1 = new JList(values1);
121 list1.setVisibleRowCount(Math.min(10, values1.length));
122 list1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
123 JScrollPane jsp1 = new JScrollPane(list1);
124 final JButton b1 = new JButton("Replace search expression");
125 b1.setEnabled(false);
126 JPanel vspace2 = new JPanel();
127 vspace2.setSize(0, 5);
128 final JList list2 = new JList(values2);
129 list2.setVisibleRowCount(Math.min(10, values2.length));
130 list2.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
131 JScrollPane jsp2 = new JScrollPane(list2);
132 final JButton b2 = new JButton("Insert at the caret position");
133 b2.setEnabled(false);
134 JPanel vspace3 = new JPanel();
135 vspace3.setSize(0, 5);
136 final JList list3 = new JList(values3);
137 list3.setVisibleRowCount(Math.min(10, values3.length));
138 list3.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
139 JScrollPane jsp3 = new JScrollPane(list3);
140 final JButton b3 = new JButton("Modify the selection");
141 b3.setEnabled(false);
142 modifiedTextField.addCaretListener(new CaretListener() {
143 public void caretUpdate(CaretEvent e) {
144 list3.setEnabled(modifiedTextField.getSelectedText() != null);
145 }
146 });
147 Object[] messageObjects = {
148 "Current search expression:", modifiedTextField,
149 vspace1, jsp1, b1, vspace2, jsp2, b2, vspace3, jsp3, b3
150 };
151 String options[] = {"OK", "Cancel"};
152 final JOptionPane optionPane = new JOptionPane(
153 messageObjects,
154 JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION,
155 null, options, "Cancel");
156 b1.addActionListener(new ActionListener() {
157 public void actionPerformed(ActionEvent e) {
158 modifySearchExpression(list1.getSelectedValue().toString(),
159 modifiedTextField);
160 }
161 });
162 list1.addMouseListener(new MouseAdapter() {
163 public void mouseClicked(MouseEvent e) {
164 super.mouseClicked(e);
165 if (e.getClickCount() == 2) {
166 modifySearchExpression(list1.getSelectedValue().toString(),
167 modifiedTextField);
168 }
169 }
170 });
171 list1.addListSelectionListener(new ListSelectionListener() {
172 public void valueChanged(ListSelectionEvent e) {
173 if (list1.getSelectedValue() != null) {
174 b1.setEnabled(true);
175 } else {
176 b1.setEnabled(false);
177 }
178 }
179 });
180 b2.addActionListener(new ActionListener() {
181 public void actionPerformed(ActionEvent e) {
182 modifySearchExpression(list2.getSelectedValue().toString(),
183 modifiedTextField);
184 }
185 });
186 list2.addMouseListener(new MouseAdapter() {
187 public void mouseClicked(MouseEvent e) {
188 super.mouseClicked(e);
189 if (e.getClickCount() == 2) {
190 modifySearchExpression(list2.getSelectedValue().toString(),
191 modifiedTextField);
192 }
193 }
194 });
195 list2.addListSelectionListener(new ListSelectionListener() {
196 public void valueChanged(ListSelectionEvent e) {
197 if (list2.getSelectedValue() != null) {
198 b2.setEnabled(true);
199 } else {
200 b2.setEnabled(false);
201 }
202 }
203 });
204 b3.addActionListener(new ActionListener() {
205 public void actionPerformed(ActionEvent e) {
206 modifySearchExpression(list3.getSelectedValue().toString(),
207 modifiedTextField);
208 }
209 });
210 list3.addMouseListener(new MouseAdapter() {
211 public void mouseClicked(MouseEvent e) {
212 super.mouseClicked(e);
213 if (e.getClickCount() == 2 && list3.isEnabled()) {
214 modifySearchExpression(list3.getSelectedValue().toString(),
215 modifiedTextField);
216 }
217 }
218 });
219 list3.addListSelectionListener(new ListSelectionListener() {
220 public void valueChanged(ListSelectionEvent e) {
221 if (list3.getSelectedValue() != null) {
222 b3.setEnabled(true);
223 } else {
224 b3.setEnabled(false);
225 }
226 }
227 });
228 annotationEditorWindow.setVisible(false);
229 JDialog optionDialog = optionPane.createDialog(
230 gate.gui.MainFrame.getInstance(), "GATE search expression builder");
231 modifiedTextField.setCaretPosition(
232 sourceTextField.getSelectionStart()
233 == sourceTextField.getCaretPosition()?
234 sourceTextField.getSelectionEnd()
235 :sourceTextField.getSelectionStart());
236 modifiedTextField.moveCaretPosition(
237 sourceTextField.getCaretPosition());
238 modifiedTextField.requestFocus();
239 optionDialog.setVisible(true);
240 Object selectedValue = optionPane.getValue();
241 if (selectedValue != null
242 && selectedValue.equals("OK")) {
243 if (searchRegExpChk != null) {
244 searchRegExpChk.setSelected(true);
245 }
246 sourceTextField.setText(modifiedTextField.getText());
247 }
248 annotationEditorWindow.setVisible(true);
249 }
250
251 private void modifySearchExpression(String modification,
252 JTextField textField) {
253 if (modification == null
254 || !(modification instanceof String)) {
255 return;
256 }
257
258 int p = textField.getCaretPosition();
259 int s1 = textField.getSelectionStart();
260 int s2 = textField.getSelectionEnd();
261 try {
262 if (modification.equals("Number")) {
263 textField.setText("\\b[\\p{N}][\\p{N},.]*\\b");
264 } else if (modification.equals("Person")) {
265 textField.setText("\\p{Lu}\\p{L}+, \\p{Lu}\\.(?: \\p{Lu}\\.)*");
266 } else if (modification.equals("Either the selection or X")) {
267 textField.getDocument().insertString(s1, "(?:", null);
268 textField.getDocument().insertString(s2+3, ")|(?:X)", null);
269 } else if (modification.equals("Once or not at all")) {
270 textField.getDocument().insertString(s1, "(?:", null);
271 textField.getDocument().insertString(s2+3, ")?", null);
272 } else if (modification.equals("Zero or more times")) {
273 textField.getDocument().insertString(s1, "(?:", null);
274 textField.getDocument().insertString(s2+3, ")*", null);
275 } else if (modification.equals("One or more times")) {
276 textField.getDocument().insertString(s1, "(?:", null);
277 textField.getDocument().insertString(s2+3, ")+", null);
278 } else if (modification.equals("Capturing group")) {
279 textField.getDocument().insertString(s1, "(?:", null);
280 textField.getDocument().insertString(s2+3, ")", null);
281 } else if (modification.equals("Non-capturing group")) {
282 textField.getDocument().insertString(s1, "(?:", null);
283 textField.getDocument().insertString(s2+3, ")", null);
284 } else if (modification.equals("Any character")) {
285 textField.getDocument().insertString(p, ".", null);
286 } else if (modification.equals("The beginning of a line")) {
287 textField.getDocument().insertString(p, "^", null);
288 } else if (modification.equals("The end of a line")) {
289 textField.getDocument().insertString(p, "$", null);
290 } else if (modification.equals("Any character except Category")) {
291 textField.getDocument().insertString(p, "\\P{Category}", null);
292 } else if (modification.equals("Category1 and/or Category2")) {
293 textField.getDocument().insertString(p, "[\\p{Category1}\\p{Category2}]", null);
294 } else if (modification.equals("Category1 and Category2")) {
295 textField.getDocument().insertString(p, "[\\p{Category1}&&\\p{Category2}]", null);
296 } else if (modification.equals("All letters")) {
297 textField.getDocument().insertString(p, "\\p{L}", null);
298 } else if (modification.equals("Letter uppercase")) {
299 textField.getDocument().insertString(p, "\\p{Lu}", null);
300 } else if (modification.equals("Letter lowercase")) {
301 textField.getDocument().insertString(p, "\\p{Ll}", null);
302 } else if (modification.equals("Letter titlecase")) {
303 textField.getDocument().insertString(p, "\\p{Lt}", null);
304 } else if (modification.equals("Letter modifier")) {
305 textField.getDocument().insertString(p, "\\p{Lm}", null);
306 } else if (modification.equals("Letter other")) {
307 textField.getDocument().insertString(p, "\\p{Lo}", null);
308 } else if (modification.equals("All Marks")) {
309 textField.getDocument().insertString(p, "\\p{M}", null);
310 } else if (modification.equals("Mark nonspacing")) {
311 textField.getDocument().insertString(p, "\\p{Mn}", null);
312 } else if (modification.equals("Mark spacing combining")) {
313 textField.getDocument().insertString(p, "\\p{Mc}", null);
314 } else if (modification.equals("Mark enclosing")) {
315 textField.getDocument().insertString(p, "\\p{Me}", null);
316 } else if (modification.equals("All Numbers")) {
317 textField.getDocument().insertString(p, "\\p{N}", null);
318 } else if (modification.equals("Number decimal digit")) {
319 textField.getDocument().insertString(p, "\\p{Nd}", null);
320 } else if (modification.equals("Number letter")) {
321 textField.getDocument().insertString(p, "\\p{Nl}", null);
322 } else if (modification.equals("Number other")) {
323 textField.getDocument().insertString(p, "\\p{No}", null);
324 } else if (modification.equals("All separators")) {
325 textField.getDocument().insertString(p, "\\p{Z}", null);
326 } else if (modification.equals("Separator space")) {
327 textField.getDocument().insertString(p, "\\p{Zs}", null);
328 } else if (modification.equals("Separator line")) {
329 textField.getDocument().insertString(p, "\\p{Zl}", null);
330 } else if (modification.equals("Separator paragraph")) {
331 textField.getDocument().insertString(p, "\\p{Zp}", null);
332 } else if (modification.equals("All others")) {
333 textField.getDocument().insertString(p, "\\p{C}", null);
334 } else if (modification.equals("Other control")) {
335 textField.getDocument().insertString(p, "\\p{Cc}", null);
336 } else if (modification.equals("Other format")) {
337 textField.getDocument().insertString(p, "\\p{Cf}", null);
338 } else if (modification.equals("Other surrogate")) {
339 textField.getDocument().insertString(p, "\\p{Cs}", null);
340 } else if (modification.equals("Other private use")) {
341 textField.getDocument().insertString(p, "\\p{Co}", null);
342 } else if (modification.equals("Other not assigned")) {
343 textField.getDocument().insertString(p, "\\p{Cn}", null);
344 } else if (modification.equals("All punctuations")) {
345 textField.getDocument().insertString(p, "\\p{P}", null);
346 } else if (modification.equals("Punctuation connector")) {
347 textField.getDocument().insertString(p, "\\p{Pc}", null);
348 } else if (modification.equals("Punctuation dash")) {
349 textField.getDocument().insertString(p, "\\p{Pd}", null);
350 } else if (modification.equals("Punctuation open")) {
351 textField.getDocument().insertString(p, "\\p{Ps}", null);
352 } else if (modification.equals("Punctuation close")) {
353 textField.getDocument().insertString(p, "\\p{Pe}", null);
354 } else if (modification.equals("Punctuation initial quote")) {
355 textField.getDocument().insertString(p, "\\p{Pi}", null);
356 } else if (modification.equals("Punctuation final quote")) {
357 textField.getDocument().insertString(p, "\\p{Pf}", null);
358 } else if (modification.equals("Punctuation other")) {
359 textField.getDocument().insertString(p, "\\p{Po}", null);
360 } else if (modification.equals("All symbols")) {
361 textField.getDocument().insertString(p, "\\p{S}", null);
362 } else if (modification.equals("Symbol math")) {
363 textField.getDocument().insertString(p, "\\p{Sm}", null);
364 } else if (modification.equals("Symbol currency")) {
365 textField.getDocument().insertString(p, "\\p{Sc}", null);
366 } else if (modification.equals("Symbol modifier")) {
367 textField.getDocument().insertString(p, "\\p{Sk}", null);
368 } else if (modification.equals("Symbol other")) {
369 textField.getDocument().insertString(p, "\\p{So}", null);
370 }
371 } catch (BadLocationException e) {
372 // should never happend
373 throw new LuckyException(e);
374 }
375 textField.requestFocus();
376 }
377
378 }
|