Gridarta Editor
Find.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2015 The Gridarta Developers.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 package net.sf.gridarta.textedit.textarea.actions;
21 
22 import java.awt.event.ActionEvent;
23 import java.awt.event.ActionListener;
24 import javax.swing.JOptionPane;
29 import net.sf.japi.swing.action.ActionBuilder;
30 import net.sf.japi.swing.action.ActionBuilderFactory;
31 import org.jetbrains.annotations.NotNull;
32 
37 public class Find implements ActionListener {
38 
42  @NotNull
43  private static final ActionBuilder ACTION_BUILDER = ActionBuilderFactory.getInstance().getActionBuilder("net.sf.gridarta");
44 
48  @NotNull
49  private String textToFind = "";
50 
51  @Override
52  public void actionPerformed(final ActionEvent e) {
54  final JEditTextArea textArea = InputHandler.getTextArea(e);
55  final String selectedText = textArea.getSelectedText();
56  final String text = (String) JOptionPane.showInputDialog(textArea, ActionBuilderUtils.getString(ACTION_BUILDER, "scriptEdit.find.text"), ActionBuilderUtils.getString(ACTION_BUILDER, "scriptEdit.find.title"), JOptionPane.PLAIN_MESSAGE, null, null, selectedText != null ? selectedText : textToFind);
57  if (text != null && !text.isEmpty()) {
58  textToFind = text;
59  find(textArea);
60  }
61  }
62 
67  public void find(@NotNull final JEditTextArea textArea) {
68  if (textToFind.isEmpty()) {
69  return;
70  }
71 
72  final int startPos = textArea.getCaretPosition();
73  final String text = textArea.getText();
74  final int foundIndex = text.indexOf(textToFind, startPos + 1);
75  if (foundIndex == -1) {
76  JOptionPane.showMessageDialog(textArea, ActionBuilderUtils.getString(ACTION_BUILDER, "scriptEdit.find.notFound"));
77  return;
78  }
79 
80  textArea.select(foundIndex, foundIndex + textToFind.length());
81  }
82 
83 }
void find(@NotNull final JEditTextArea textArea)
Find the next occurrence of textToFind.
Definition: Find.java:67
Action listener for "find".
Definition: Find.java:37
static String getString(@NotNull final ActionBuilder actionBuilder, @NotNull final String key, @NotNull final String defaultValue)
Returns the value of a key.
An input handler converts the user's key strokes into concrete actions.
Base package of all Gridarta classes.
This package contains the classes for the script editor used within the editor to create and modify P...
Definition: Actions.java:20
Actions used by the script editor.
Definition: Actions.java:41
String textToFind
The text that was previously selected.
Definition: Find.java:49
String getSelectedText()
Returns the selected text, or null if no selection is active.
This package contains the other part of the script editor.
void setType(@NotNull final FindType type)
Set the operation to perform.
Definition: FindAgain.java:80
static JEditTextArea getTextArea(final EventObject evt)
Returns the text area that fired the specified event.
Utility class for ActionBuilder related functions.
void actionPerformed(final ActionEvent e)
Definition: Find.java:52
static final FindAgain FIND_AGAIN
The "find again" action listener.
Definition: Actions.java:74
static final ActionBuilder ACTION_BUILDER
Action Builder.
Definition: Find.java:43