Gridarta Editor
Replace.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.Container;
23 import java.awt.FlowLayout;
24 import java.awt.event.ActionEvent;
25 import java.awt.event.ActionListener;
26 import javax.swing.Box;
27 import javax.swing.BoxLayout;
28 import javax.swing.JButton;
29 import javax.swing.JDialog;
30 import javax.swing.JOptionPane;
31 import javax.swing.JPanel;
32 import javax.swing.JTextField;
33 import javax.swing.WindowConstants;
34 import javax.swing.text.JTextComponent;
40 import net.sf.japi.swing.action.ActionBuilder;
41 import net.sf.japi.swing.action.ActionBuilderFactory;
42 import net.sf.japi.swing.action.ActionMethod;
43 import org.jetbrains.annotations.NotNull;
44 import org.jetbrains.annotations.Nullable;
45 
50 public class Replace implements ActionListener {
51 
55  @NotNull
56  private static final ActionBuilder ACTION_BUILDER = ActionBuilderFactory.getInstance().getActionBuilder("net.sf.gridarta");
57 
61  @NotNull
62  private String textToFind = "";
63 
67  @NotNull
68  private String textToReplace = "";
69 
70  @Override
71  public void actionPerformed(final ActionEvent e) {
73  final JEditTextArea textArea = InputHandler.getTextArea(e);
74  final ReplaceDialog pane = new ReplaceDialog(textArea);
75  pane.dialog = pane.createDialog(textArea, ActionBuilderUtils.getString(ACTION_BUILDER, "scriptEdit.replace.title"));
76  pane.dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
77  pane.dialog.getRootPane().setDefaultButton(pane.replaceButton);
78  pane.dialog.setModal(false);
79  pane.dialog.setVisible(true);
80  pane.findTextField.selectAll();
81  pane.replaceTextField.selectAll();
82  }
83 
91  public boolean replace(@NotNull final JEditTextArea textArea) {
92  final int startPos = textArea.getCaretPosition();
93  final String text = textArea.getText();
94  final int foundIndex = text.indexOf(textToFind, startPos + 1);
95  if (foundIndex == -1) {
96  JOptionPane.showMessageDialog(textArea, ActionBuilderUtils.getString(ACTION_BUILDER, "scriptEdit.find.notFound"));
97  return false;
98  }
99 
101  try {
102  textArea.select(foundIndex, foundIndex + textToFind.length());
103  textArea.setSelectedText(textToReplace);
104  textArea.select(foundIndex, foundIndex + textToReplace.length());
105  } finally {
107  }
108 
109  return true;
110  }
111 
115  public class ReplaceDialog extends JOptionPane {
116 
120  private static final long serialVersionUID = 1L;
121 
126  @NotNull
127  private final JButton replaceButton = new JButton(ACTION_BUILDER.createAction(false, "replaceButton", this));
128 
133  @NotNull
134  private final JButton cancelButton = new JButton(ACTION_BUILDER.createAction(false, "cancelButton", this));
135 
140  @NotNull
141  private final JTextComponent findTextField = new JTextField(32);
142 
147  @NotNull
148  private final JTextComponent replaceTextField = new JTextField(32);
149 
154  @Nullable
155  private JDialog dialog;
156 
161  @NotNull
162  private final JEditTextArea textArea;
163 
168  private ReplaceDialog(@NotNull final JEditTextArea textArea) {
169  this.textArea = textArea;
170  replaceButton.setDefaultCapable(true);
171  setOptions(new Object[] { replaceButton, cancelButton, });
172  setMessage(createPanel());
173  }
174 
178  @ActionMethod
179  public void replaceButton() {
180  textToFind = findTextField.getText();
181  textToReplace = replaceTextField.getText();
182  if (replace(textArea)) {
183  setValue(replaceButton);
184  dialog.dispose();
185  }
186  }
187 
191  @ActionMethod
192  public void cancelButton() {
193  setValue(cancelButton);
194  dialog.dispose();
195  }
196 
201  @NotNull
202  private JPanel createPanel() {
203  final JPanel replacePanel = new JPanel();
204  replacePanel.setLayout(new BoxLayout(replacePanel, BoxLayout.Y_AXIS));
205 
206  // find text field
207  final Container findTextPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
208  findTextPanel.add(ActionBuilderUtils.newLabel(ACTION_BUILDER, "scriptEdit.replace.find"));
209  findTextPanel.add(findTextField);
210  final String selectedText = textArea.getSelectedText();
211  findTextField.setText(selectedText != null ? selectedText : textToFind);
212  replacePanel.add(findTextPanel);
213  replacePanel.add(Box.createVerticalStrut(5));
214 
215  // replacement text field
216  final Container replaceTextPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
217  replaceTextPanel.add(ActionBuilderUtils.newLabel(ACTION_BUILDER, "scriptEdit.replace.replace"));
218  replaceTextPanel.add(replaceTextField);
219  replaceTextField.setText(textToReplace);
220  replacePanel.add(replaceTextPanel);
221  replacePanel.add(Box.createVerticalStrut(5));
222 
223  return replacePanel;
224  }
225 
226  }
227 
228 }
static final ActionBuilder ACTION_BUILDER
Action Builder.
Definition: Replace.java:56
A document implementation that can be tokenized by the syntax highlighting system.
String textToReplace
The text that was previously selected as replacement.
Definition: Replace.java:68
final JTextComponent replaceTextField
Textfield for the text to replace with.
Definition: Replace.java:148
ReplaceDialog(@NotNull final JEditTextArea textArea)
Create a new instance.
Definition: Replace.java:168
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
Action listener for "replace".
Definition: Replace.java:50
Actions used by the script editor.
Definition: Actions.java:41
final JTextComponent findTextField
Textfield for the text to find.
Definition: Replace.java:141
final JEditTextArea textArea
The text area to replace in.
Definition: Replace.java:162
static void beginCompoundEdit()
Starts a compound edit that can be undone in one operation.
String textToFind
The text that was previously selected to find.
Definition: Replace.java:62
JPanel createPanel()
Creates the text field panel.
Definition: Replace.java:202
String getSelectedText()
Returns the selected text, or null if no selection is active.
static final long serialVersionUID
The serial version UID.
Definition: Replace.java:120
This package contains the other part of the script editor.
static void endCompoundEdit()
Ends a compound edit that can be undone in one operation.
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.
static JLabel newLabel(@NotNull final ActionBuilder actionBuilder, @NotNull final String key)
Creates a new JLabel from a resource key.
static final FindAgain FIND_AGAIN
The "find again" action listener.
Definition: Actions.java:74
boolean replace(@NotNull final JEditTextArea textArea)
Replace the next occurrence of textToFind with textToReplace.
Definition: Replace.java:91