Gridarta Editor
KeyStrokeDialog.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.gui.dialog.shortcuts;
21 
22 import java.awt.Component;
23 import java.awt.GridBagConstraints;
24 import java.awt.GridBagLayout;
25 import javax.swing.Action;
26 import javax.swing.JButton;
27 import javax.swing.JComponent;
28 import javax.swing.JDialog;
29 import javax.swing.JOptionPane;
30 import javax.swing.JPanel;
31 import javax.swing.JTextArea;
32 import javax.swing.KeyStroke;
33 import javax.swing.WindowConstants;
34 import javax.swing.border.TitledBorder;
38 import net.sf.japi.swing.action.ActionBuilder;
39 import net.sf.japi.swing.action.ActionBuilderFactory;
40 import net.sf.japi.swing.action.ActionMethod;
41 import org.jetbrains.annotations.NotNull;
42 import org.jetbrains.annotations.Nullable;
43 
48 public class KeyStrokeDialog extends JOptionPane {
49 
53  private static final long serialVersionUID = 1L;
54 
58  @NotNull
59  private static final ActionBuilder ACTION_BUILDER = ActionBuilderFactory.getInstance().getActionBuilder("net.sf.gridarta");
60 
64  @NotNull
66 
71  @NotNull
72  private final Action action;
73 
78  @NotNull
79  private final JButton okButton = new JButton(ACTION_BUILDER.createAction(false, "keyStrokeOkay", this));
80 
85  @NotNull
86  private final JButton cancelButton = new JButton(ACTION_BUILDER.createAction(false, "keyStrokeCancel", this));
87 
92  @NotNull
93  private final JDialog dialog;
94 
99  @NotNull
100  private final KeyStrokeField keyStroke;
101 
107  @NotNull
108  private final JTextArea conflictsTextArea = new JTextArea();
109 
117  public KeyStrokeDialog(@NotNull final Component parentComponent, @NotNull final ShortcutsManager shortcutsManager, @NotNull final Action action) {
118  this.shortcutsManager = shortcutsManager;
119  this.action = action;
120  okButton.setDefaultCapable(true);
121  setOptions(new Object[] { okButton, cancelButton, });
122 
124  final KeyStrokeFieldListener keyStrokeFieldListener = keyStroke -> updateKeyStroke();
125  keyStroke.addKeyStrokeListener(keyStrokeFieldListener);
126 
127  conflictsTextArea.setEditable(false);
128  conflictsTextArea.setLineWrap(true);
129  conflictsTextArea.setWrapStyleWord(true);
130  conflictsTextArea.setFocusable(false);
131  conflictsTextArea.setRows(3);
132 
133  setMessage(createPanel());
134 
135  dialog = createDialog(parentComponent, ActionBuilderUtils.getString(ACTION_BUILDER, "keyStroke.title"));
136  dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
137  dialog.getRootPane().setDefaultButton(okButton);
138  dialog.setModal(true);
139  dialog.pack();
140  }
141 
147  public boolean showDialog(@NotNull final Component parentComponent) {
148  dialog.setLocationRelativeTo(parentComponent);
149  updateKeyStroke();
150  keyStroke.requestFocusInWindow();
151  setInitialValue(keyStroke);
152  dialog.setVisible(true);
153  return getValue() == okButton;
154  }
155 
160  @NotNull
161  private JPanel createPanel() {
162  final GridBagConstraints gbc = new GridBagConstraints();
163 
164  final JComponent keyStrokePanel = new JPanel(new GridBagLayout());
165  keyStrokePanel.setBorder(new TitledBorder(ActionBuilderUtils.getString(ACTION_BUILDER, "keyStroke.borderKeyStroke")));
166  gbc.gridx = 0;
167  gbc.gridy = 0;
168  gbc.fill = GridBagConstraints.HORIZONTAL;
169  gbc.weightx = 1.0;
170  keyStrokePanel.add(keyStroke, gbc);
171 
172  final JComponent conflictsPanel = new JPanel(new GridBagLayout());
173  conflictsPanel.setBorder(new TitledBorder(ActionBuilderUtils.getString(ACTION_BUILDER, "keyStroke.borderConflicts")));
174  gbc.gridx = 0;
175  gbc.gridy = 0;
176  gbc.fill = GridBagConstraints.BOTH;
177  gbc.weightx = 1.0;
178  gbc.weighty = 1.0;
179  conflictsPanel.add(conflictsTextArea, gbc);
180 
181  final JPanel panel = new JPanel(new GridBagLayout());
182  gbc.gridx = 0;
183  gbc.gridy = 0;
184  gbc.fill = GridBagConstraints.HORIZONTAL;
185  gbc.weightx = 1.0;
186  gbc.weighty = 0.0;
187  panel.add(keyStrokePanel, gbc);
188  gbc.gridy++;
189  gbc.fill = GridBagConstraints.BOTH;
190  gbc.weighty = 1.0;
191  panel.add(conflictsPanel, gbc);
192 
193  panel.setBorder(GUIConstants.DIALOG_BORDER);
194  return panel;
195  }
196 
200  @ActionMethod
201  public void keyStrokeOkay() {
203  }
204 
208  @ActionMethod
209  public void keyStrokeCancel() {
211  }
212 
213  @Override
214  public void setValue(@Nullable final Object newValue) {
215  super.setValue(newValue);
216  if (newValue != UNINITIALIZED_VALUE) {
217  dialog.dispose();
218  }
219  }
220 
224  private void updateKeyStroke() {
225  final KeyStroke newKeyStroke = getKeyStroke();
226  Action conflictAction = null;
227  if (newKeyStroke != null) {
228  for (final Action tmp : shortcutsManager) {
229  if (tmp != action) {
230  final KeyStroke tmpKeyStroke = ActionUtils.getShortcut(tmp);
231  if (newKeyStroke.equals(tmpKeyStroke)) {
232  conflictAction = tmp;
233  break;
234  }
235  }
236  }
237  }
238  if (conflictAction == null) {
239  conflictsTextArea.setText("");
240  okButton.setEnabled(true);
241  } else {
242  conflictsTextArea.setText(ACTION_BUILDER.format("keyStroke.borderConflictAssigned", ActionUtils.getActionName(conflictAction), ActionUtils.getActionCategory(conflictAction)));
243  okButton.setEnabled(false);
244  }
245  }
246 
251  @Nullable
252  public KeyStroke getKeyStroke() {
253  return keyStroke.getKeyStroke();
254  }
255 
256 }
net.sf.gridarta.gui.dialog.shortcuts.KeyStrokeField.getKeyStroke
KeyStroke getKeyStroke()
Returns the currently shown KeyStroke.
Definition: KeyStrokeField.java:119
net.sf.gridarta.gui.dialog.shortcuts.ShortcutsManager
Manager for shortcuts of all Actions in an {} instance.
Definition: ShortcutsManager.java:50
net.sf.gridarta
Base package of all Gridarta classes.
net.sf.gridarta.gui.utils.GUIConstants
Defines common UI constants used in different dialogs.
Definition: GUIConstants.java:33
net.sf.gridarta.gui.dialog.shortcuts.KeyStrokeDialog.setValue
void setValue(@Nullable final Object newValue)
Definition: KeyStrokeDialog.java:214
net.sf.gridarta.gui.dialog.shortcuts.KeyStrokeDialog.cancelButton
final JButton cancelButton
The JButton for cancel.
Definition: KeyStrokeDialog.java:86
net.sf
net.sf.gridarta.gui.dialog.shortcuts.KeyStrokeDialog.keyStroke
final KeyStrokeField keyStroke
The key stroke input field.
Definition: KeyStrokeDialog.java:100
net.sf.gridarta.gui.dialog.shortcuts.KeyStrokeDialog.okButton
final JButton okButton
The JButton for ok.
Definition: KeyStrokeDialog.java:79
net.sf.gridarta.utils.ActionUtils.getActionName
static String getActionName(@NotNull final Action action)
Returns the name of an Action.
Definition: ActionUtils.java:72
net.sf.gridarta.utils.ActionUtils
Utility class implementing Action related functions.
Definition: ActionUtils.java:33
net.sf.gridarta.gui.dialog.shortcuts.KeyStrokeFieldListener
Interface for listeners interested in KeyStrokeField related events.
Definition: KeyStrokeFieldListener.java:30
net.sf.gridarta.gui
Graphical User Interface of Gridarta.
net.sf.gridarta.gui.dialog.shortcuts.KeyStrokeDialog.shortcutsManager
final ShortcutsManager shortcutsManager
The ShortcutsManager for checking conflicts.
Definition: KeyStrokeDialog.java:65
net.sf.gridarta.gui.dialog.shortcuts.KeyStrokeDialog.getKeyStroke
KeyStroke getKeyStroke()
Returns the currently shown key stroke.
Definition: KeyStrokeDialog.java:252
net.sf.gridarta.gui.dialog.shortcuts.KeyStrokeDialog.keyStrokeOkay
void keyStrokeOkay()
Action method for okay.
Definition: KeyStrokeDialog.java:201
net.sf.gridarta.utils.ActionUtils.getActionCategory
static String getActionCategory(@NotNull final Action action)
Returns an Action's category.
Definition: ActionUtils.java:235
net
net.sf.gridarta.gui.dialog.shortcuts.KeyStrokeDialog.action
final Action action
The Action being redefined.
Definition: KeyStrokeDialog.java:72
net.sf.gridarta.gui.dialog.shortcuts.KeyStrokeDialog.conflictsTextArea
final JTextArea conflictsTextArea
The text area showing conflicts between the new key stroke and assigned key strokes.
Definition: KeyStrokeDialog.java:108
net.sf.gridarta.utils.ActionUtils.getShortcut
static KeyStroke getShortcut(@NotNull final Action action)
Returns the shortcut of an Action.
Definition: ActionUtils.java:120
net.sf.gridarta.gui.dialog.shortcuts.KeyStrokeDialog.dialog
final JDialog dialog
The JDialog instance.
Definition: KeyStrokeDialog.java:93
net.sf.gridarta.gui.dialog.shortcuts.KeyStrokeField.addKeyStrokeListener
void addKeyStrokeListener(@NotNull final KeyStrokeFieldListener listener)
Adds a KeyStrokeFieldListener to be notified about changes.
Definition: KeyStrokeField.java:110
net.sf.gridarta.gui.dialog.shortcuts.KeyStrokeDialog.keyStrokeCancel
void keyStrokeCancel()
Action method for cancel.
Definition: KeyStrokeDialog.java:209
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.gui.dialog.shortcuts.KeyStrokeDialog.serialVersionUID
static final long serialVersionUID
The serial Version UID.
Definition: KeyStrokeDialog.java:53
net.sf.gridarta.gui.dialog.shortcuts.KeyStrokeDialog
A dialog that asks for a KeyStroke.
Definition: KeyStrokeDialog.java:48
net.sf.gridarta.gui.dialog.shortcuts.KeyStrokeDialog.createPanel
JPanel createPanel()
Creates the GUI.
Definition: KeyStrokeDialog.java:161
net.sf.gridarta.gui.dialog.shortcuts.KeyStrokeField
A javax.swing.JComponent for selecting a KeyStroke.
Definition: KeyStrokeField.java:35
net.sf.gridarta.utils.ActionBuilderUtils
Utility class for ActionBuilder related functions.
Definition: ActionBuilderUtils.java:31
net.sf.gridarta.gui.dialog.shortcuts.KeyStrokeDialog.showDialog
boolean showDialog(@NotNull final Component parentComponent)
Opens the dialog.
Definition: KeyStrokeDialog.java:147
net.sf.gridarta.gui.utils
Definition: AnimationComponent.java:20
net.sf.gridarta.gui.dialog.shortcuts.KeyStrokeDialog.ACTION_BUILDER
static final ActionBuilder ACTION_BUILDER
The ActionBuilder.
Definition: KeyStrokeDialog.java:59
net.sf.gridarta.utils
Definition: ActionBuilderUtils.java:20
net.sf.gridarta.gui.dialog.shortcuts.KeyStrokeDialog.updateKeyStroke
void updateKeyStroke()
Updates the information shown for the selected action.
Definition: KeyStrokeDialog.java:224
net.sf.gridarta.gui.utils.GUIConstants.DIALOG_BORDER
Border DIALOG_BORDER
The Border object to be used when creating dialogs.
Definition: GUIConstants.java:38
net.sf.gridarta.gui.dialog.shortcuts.KeyStrokeDialog.KeyStrokeDialog
KeyStrokeDialog(@NotNull final Component parentComponent, @NotNull final ShortcutsManager shortcutsManager, @NotNull final Action action)
Creates a new instance.
Definition: KeyStrokeDialog.java:117