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-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.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 
123  keyStroke = new KeyStrokeField(ActionUtils.getShortcut(action));
124  final KeyStrokeFieldListener keyStrokeFieldListener = new KeyStrokeFieldListener() {
125 
126  @Override
127  public void keyStrokeChanged(@NotNull final KeyStroke keyStroke) {
128  updateKeyStroke();
129  }
130 
131  };
132  keyStroke.addKeyStrokeListener(keyStrokeFieldListener);
133 
134  conflictsTextArea.setEditable(false);
135  conflictsTextArea.setLineWrap(true);
136  conflictsTextArea.setWrapStyleWord(true);
137  conflictsTextArea.setFocusable(false);
138  conflictsTextArea.setRows(3);
139 
140  setMessage(createPanel());
141 
142  dialog = createDialog(parentComponent, ActionBuilderUtils.getString(ACTION_BUILDER, "keyStroke.title"));
143  dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
144  dialog.getRootPane().setDefaultButton(okButton);
145  dialog.setModal(true);
146  dialog.pack();
147  }
148 
154  public boolean showDialog(@NotNull final Component parentComponent) {
155  dialog.setLocationRelativeTo(parentComponent);
156  updateKeyStroke();
157  keyStroke.requestFocusInWindow();
158  setInitialValue(keyStroke);
159  dialog.setVisible(true);
160  return getValue() == okButton;
161  }
162 
167  @NotNull
168  private JPanel createPanel() {
169  final GridBagConstraints gbc = new GridBagConstraints();
170 
171  final JComponent keyStrokePanel = new JPanel(new GridBagLayout());
172  keyStrokePanel.setBorder(new TitledBorder(ActionBuilderUtils.getString(ACTION_BUILDER, "keyStroke.borderKeyStroke")));
173  gbc.gridx = 0;
174  gbc.gridy = 0;
175  gbc.fill = GridBagConstraints.HORIZONTAL;
176  gbc.weightx = 1.0;
177  keyStrokePanel.add(keyStroke, gbc);
178 
179  final JComponent conflictsPanel = new JPanel(new GridBagLayout());
180  conflictsPanel.setBorder(new TitledBorder(ActionBuilderUtils.getString(ACTION_BUILDER, "keyStroke.borderConflicts")));
181  gbc.gridx = 0;
182  gbc.gridy = 0;
183  gbc.fill = GridBagConstraints.BOTH;
184  gbc.weightx = 1.0;
185  gbc.weighty = 1.0;
186  conflictsPanel.add(conflictsTextArea, gbc);
187 
188  final JPanel panel = new JPanel(new GridBagLayout());
189  gbc.gridx = 0;
190  gbc.gridy = 0;
191  gbc.fill = GridBagConstraints.HORIZONTAL;
192  gbc.weightx = 1.0;
193  gbc.weighty = 0.0;
194  panel.add(keyStrokePanel, gbc);
195  gbc.gridy++;
196  gbc.fill = GridBagConstraints.BOTH;
197  gbc.weighty = 1.0;
198  panel.add(conflictsPanel, gbc);
199 
200  panel.setBorder(GUIConstants.DIALOG_BORDER);
201  return panel;
202  }
203 
207  @ActionMethod
208  public void keyStrokeOkay() {
209  setValue(okButton);
210  }
211 
215  @ActionMethod
216  public void keyStrokeCancel() {
217  setValue(cancelButton);
218  }
219 
220  @Override
221  public void setValue(@Nullable final Object newValue) {
222  super.setValue(newValue);
223  if (newValue != UNINITIALIZED_VALUE) {
224  dialog.dispose();
225  }
226  }
227 
231  private void updateKeyStroke() {
232  final KeyStroke newKeyStroke = getKeyStroke();
233  Action conflictAction = null;
234  if (newKeyStroke != null) {
235  for (final Action tmp : shortcutsManager) {
236  if (tmp != action) {
237  final KeyStroke tmpKeyStroke = ActionUtils.getShortcut(tmp);
238  if (newKeyStroke.equals(tmpKeyStroke)) {
239  conflictAction = tmp;
240  break;
241  }
242  }
243  }
244  }
245  if (conflictAction == null) {
246  conflictsTextArea.setText("");
247  okButton.setEnabled(true);
248  } else {
249  conflictsTextArea.setText(ACTION_BUILDER.format("keyStroke.borderConflictAssigned", ActionUtils.getActionName(conflictAction), ActionUtils.getActionCategory(conflictAction)));
250  okButton.setEnabled(false);
251  }
252  }
253 
258  @Nullable
259  @SuppressWarnings("NullableProblems")
260  public KeyStroke getKeyStroke() {
261  return keyStroke.getKeyStroke();
262  }
263 
264 }
KeyStroke getKeyStroke()
Returns the currently shown key stroke.
final ShortcutsManager shortcutsManager
The ShortcutsManager for checking conflicts.
Graphical User Interface of Gridarta.
final JTextArea conflictsTextArea
The text area showing conflicts between the new key stroke and assigned key strokes.
boolean showDialog(@NotNull final Component parentComponent)
Opens the dialog.
static String getActionCategory(@NotNull final Action action)
Returns an Action's category.
Utility class implementing Action related functions.
KeyStrokeDialog(@NotNull final Component parentComponent, @NotNull final ShortcutsManager shortcutsManager, @NotNull final Action action)
Creates a new instance.
Interface for listeners interested in KeyStrokeField related events.
static final long serialVersionUID
The serial Version UID.
void updateKeyStroke()
Updates the information shown for the selected action.
static String getString(@NotNull final ActionBuilder actionBuilder, @NotNull final String key, @NotNull final String defaultValue)
Returns the value of a key.
Base package of all Gridarta classes.
KeyStroke getKeyStroke()
Returns the currently shown KeyStroke.
final JButton cancelButton
The JButton for cancel.
final KeyStrokeField keyStroke
The key stroke input field.
Manager for shortcuts of all Actions in an ActionBuilder instance.
A javax.swing.JComponent for selecting a KeyStroke.
static KeyStroke getShortcut(@NotNull final Action action)
Returns the shortcut of an Action.
static final ActionBuilder ACTION_BUILDER
The ActionBuilder.
Utility class for ActionBuilder related functions.
Border DIALOG_BORDER
The Border object to be used when creating dialogs.
static String getActionName(@NotNull final Action action)
Returns the name of an Action.
final Action action
The Action being redefined.
void setValue(@Nullable final Object newValue)
Defines common UI constants used in different dialogs.