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-2023 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 ? textToFind : selectedText);
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 }
net.sf.gridarta.textedit.textarea.actions.Find.textToFind
String textToFind
The text that was previously selected.
Definition: Find.java:49
net.sf.gridarta.textedit.textarea.JEditTextArea
jEdit's text area component.
Definition: JEditTextArea.java:91
net.sf.gridarta.textedit.scripteditor
Classes for the script editor used within the editor to create and modify Python and Lua scripts.
Definition: Actions.java:20
net.sf.gridarta.textedit.textarea
This package contains the other part of the script editor.
net.sf.gridarta
Base package of all Gridarta classes.
net.sf.gridarta.textedit.textarea.actions.Find.actionPerformed
void actionPerformed(final ActionEvent e)
Definition: Find.java:52
net.sf
net.sf.gridarta.textedit.scripteditor.Actions.FIND_AGAIN
static final FindAgain FIND_AGAIN
The "find again" action listener.
Definition: Actions.java:74
net.sf.gridarta.textedit.textarea.actions.FindType.FIND
FIND
Find again.
Definition: FindType.java:36
net.sf.gridarta.textedit.textarea.InputHandler
An input handler converts the user's key strokes into concrete actions.
Definition: InputHandler.java:36
net.sf.gridarta.textedit
net.sf.gridarta.textedit.textarea.JEditTextArea.getSelectedText
String getSelectedText()
Returns the selected text, or null if no selection is active.
Definition: JEditTextArea.java:709
net.sf.gridarta.textedit.textarea.actions.Find.ACTION_BUILDER
static final ActionBuilder ACTION_BUILDER
Action Builder.
Definition: Find.java:43
net
net.sf.gridarta.textedit.textarea.InputHandler.getTextArea
static JEditTextArea getTextArea(final EventObject evt)
Returns the text area that fired the specified event.
Definition: InputHandler.java:144
net.sf.gridarta.textedit.scripteditor.Actions
Actions used by the script editor.
Definition: Actions.java:41
net.sf.gridarta.textedit.textarea.actions.Find.find
void find(@NotNull final JEditTextArea textArea)
Find the next occurrence of textToFind.
Definition: Find.java:67
net.sf.gridarta.textedit.textarea.actions.FindAgain.setType
void setType(@NotNull final FindType type)
Sets the operation to perform.
Definition: FindAgain.java:80
net.sf.gridarta.utils.ActionBuilderUtils.getString
static String getString(@NotNull final ActionBuilder actionBuilder, @NotNull final String key, @NotNull final String defaultValue)
Returns the value of a key.
Definition: ActionBuilderUtils.java:71
net.sf.gridarta.textedit.textarea.actions.Find
Action listener for "find".
Definition: Find.java:37
net.sf.gridarta.utils.ActionBuilderUtils
Utility class for ActionBuilder related functions.
Definition: ActionBuilderUtils.java:31
net.sf.gridarta.textedit.textarea.actions.FindType
The action to perform.
Definition: FindType.java:26
net.sf.gridarta.utils
Definition: ActionBuilderUtils.java:20