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 
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() {
210  }
211 
215  @ActionMethod
216  public void keyStrokeCancel() {
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  public KeyStroke getKeyStroke() {
260  return keyStroke.getKeyStroke();
261  }
262 
263 }
net.sf.gridarta.gui.dialog.shortcuts.KeyStrokeField.getKeyStroke
KeyStroke getKeyStroke()
Definition: KeyStrokeField.java:119
net.sf.gridarta.gui.dialog.shortcuts.ShortcutsManager
Definition: ShortcutsManager.java:50
net.sf.gridarta
net.sf.gridarta.gui.utils.GUIConstants
Definition: GUIConstants.java:33
net.sf.gridarta.gui.dialog.shortcuts.KeyStrokeDialog.setValue
void setValue(@Nullable final Object newValue)
Definition: KeyStrokeDialog.java:221
net.sf.gridarta.gui.dialog.shortcuts.KeyStrokeDialog.cancelButton
final JButton cancelButton
Definition: KeyStrokeDialog.java:86
net.sf
net.sf.gridarta.gui.dialog.shortcuts.KeyStrokeDialog.keyStroke
final KeyStrokeField keyStroke
Definition: KeyStrokeDialog.java:100
net.sf.gridarta.gui.dialog.shortcuts.KeyStrokeDialog.okButton
final JButton okButton
Definition: KeyStrokeDialog.java:79
net.sf.gridarta.utils.ActionUtils.getActionName
static String getActionName(@NotNull final Action action)
Definition: ActionUtils.java:72
net.sf.gridarta.utils.ActionUtils
Definition: ActionUtils.java:33
net.sf.gridarta.gui.dialog.shortcuts.KeyStrokeFieldListener
Definition: KeyStrokeFieldListener.java:30
net.sf.gridarta.gui
net.sf.gridarta.gui.dialog.shortcuts.KeyStrokeDialog.shortcutsManager
final ShortcutsManager shortcutsManager
Definition: KeyStrokeDialog.java:65
net.sf.gridarta.gui.dialog.shortcuts.KeyStrokeDialog.getKeyStroke
KeyStroke getKeyStroke()
Definition: KeyStrokeDialog.java:259
net.sf.gridarta.gui.dialog.shortcuts.KeyStrokeDialog.keyStrokeOkay
void keyStrokeOkay()
Definition: KeyStrokeDialog.java:208
net.sf.gridarta.utils.ActionUtils.getActionCategory
static String getActionCategory(@NotNull final Action action)
Definition: ActionUtils.java:235
net
net.sf.gridarta.gui.dialog.shortcuts.KeyStrokeDialog.action
final Action action
Definition: KeyStrokeDialog.java:72
net.sf.gridarta.gui.dialog.shortcuts.KeyStrokeDialog.conflictsTextArea
final JTextArea conflictsTextArea
Definition: KeyStrokeDialog.java:108
net.sf.gridarta.utils.ActionUtils.getShortcut
static KeyStroke getShortcut(@NotNull final Action action)
Definition: ActionUtils.java:120
net.sf.gridarta.gui.dialog.shortcuts.KeyStrokeDialog.dialog
final JDialog dialog
Definition: KeyStrokeDialog.java:93
net.sf.gridarta.gui.dialog.shortcuts.KeyStrokeField.addKeyStrokeListener
void addKeyStrokeListener(@NotNull final KeyStrokeFieldListener listener)
Definition: KeyStrokeField.java:110
net.sf.gridarta.gui.dialog.shortcuts.KeyStrokeDialog.keyStrokeCancel
void keyStrokeCancel()
Definition: KeyStrokeDialog.java:216
net.sf.gridarta.utils.ActionBuilderUtils.getString
static String getString(@NotNull final ActionBuilder actionBuilder, @NotNull final String key, @NotNull final String defaultValue)
Definition: ActionBuilderUtils.java:71
net.sf.gridarta.gui.dialog.shortcuts.KeyStrokeDialog.serialVersionUID
static final long serialVersionUID
Definition: KeyStrokeDialog.java:53
net.sf.gridarta.gui.dialog.shortcuts.KeyStrokeDialog
Definition: KeyStrokeDialog.java:48
net.sf.gridarta.gui.dialog.shortcuts.KeyStrokeDialog.createPanel
JPanel createPanel()
Definition: KeyStrokeDialog.java:168
net.sf.gridarta.gui.dialog.shortcuts.KeyStrokeField
Definition: KeyStrokeField.java:35
net.sf.gridarta.utils.ActionBuilderUtils
Definition: ActionBuilderUtils.java:31
net.sf.gridarta.gui.dialog.shortcuts.KeyStrokeDialog.showDialog
boolean showDialog(@NotNull final Component parentComponent)
Definition: KeyStrokeDialog.java:154
net.sf.gridarta.gui.utils
Definition: AnimationComponent.java:20
net.sf.gridarta.gui.dialog.shortcuts.KeyStrokeDialog.ACTION_BUILDER
static final ActionBuilder ACTION_BUILDER
Definition: KeyStrokeDialog.java:59
net.sf.gridarta.utils
Definition: ActionBuilderUtils.java:20
net.sf.gridarta.gui.dialog.shortcuts.KeyStrokeDialog.updateKeyStroke
void updateKeyStroke()
Definition: KeyStrokeDialog.java:231
net.sf.gridarta.gui.utils.GUIConstants.DIALOG_BORDER
Border DIALOG_BORDER
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)
Definition: KeyStrokeDialog.java:117