Gridarta Editor
EditBookmarkDialog.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.bookmarks;
21 
22 import java.awt.Component;
23 import javax.swing.Box;
24 import javax.swing.BoxLayout;
25 import javax.swing.JButton;
26 import javax.swing.JDialog;
27 import javax.swing.JOptionPane;
28 import javax.swing.JPanel;
29 import javax.swing.JTextField;
30 import javax.swing.WindowConstants;
31 import javax.swing.event.DocumentEvent;
32 import javax.swing.event.DocumentListener;
33 import javax.swing.text.JTextComponent;
37 import net.sf.japi.swing.action.ActionBuilder;
38 import net.sf.japi.swing.action.ActionBuilderFactory;
39 import net.sf.japi.swing.action.ActionMethod;
40 import org.jetbrains.annotations.NotNull;
41 
46 public class EditBookmarkDialog extends JOptionPane {
47 
51  private static final long serialVersionUID = 1L;
52 
56  @NotNull
57  private static final ActionBuilder ACTION_BUILDER = ActionBuilderFactory.getInstance().getActionBuilder("net.sf.gridarta");
58 
63  @NotNull
64  private final JDialog dialog;
65 
70  @NotNull
71  private final JTextComponent descriptionField = new JTextField();
72 
77  @NotNull
78  private final JButton okButton = new JButton(ACTION_BUILDER.createAction(false, "editBookmarkOkay", this));
79 
84  @NotNull
85  private final JButton cancelButton = new JButton(ACTION_BUILDER.createAction(false, "editBookmarkCancel", this));
86 
92  public EditBookmarkDialog(@NotNull final Component parentComponent, @NotNull final String defaultDescription) {
93  okButton.setDefaultCapable(true);
94  setOptions(new Object[] { okButton, cancelButton });
95 
96  setMessage(createPanel());
97 
98  TextComponentUtils.setAutoSelectOnFocus(descriptionField);
99  descriptionField.setText(defaultDescription);
100 
101  final DocumentListener documentListener = new DocumentListener() {
102 
103  @Override
104  public void insertUpdate(@NotNull final DocumentEvent e) {
105  updateOkButton();
106  }
107 
108  @Override
109  public void removeUpdate(@NotNull final DocumentEvent e) {
110  updateOkButton();
111  }
112 
113  @Override
114  public void changedUpdate(@NotNull final DocumentEvent e) {
115  updateOkButton();
116  }
117 
118  };
119  descriptionField.getDocument().addDocumentListener(documentListener);
120  updateOkButton();
121 
122  dialog = createDialog(parentComponent, ActionBuilderUtils.getString(ACTION_BUILDER, "editBookmark.title"));
123  dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
124  dialog.getRootPane().setDefaultButton(okButton);
125  dialog.pack();
126  dialog.setLocationRelativeTo(parentComponent);
127  }
128 
133  public boolean showDialog() {
134  dialog.setVisible(true);
135  return getValue() == okButton;
136  }
137 
142  @NotNull
143  private JPanel createPanel() {
144  final JPanel mainPanel = new JPanel();
145  mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
146 
147  mainPanel.setBorder(GUIConstants.DIALOG_BORDER);
148 
149  mainPanel.add(ActionBuilderUtils.newLabel(ACTION_BUILDER, "editBookmark.description"));
150  mainPanel.add(Box.createVerticalStrut(5));
151 
152  mainPanel.add(descriptionField);
153  mainPanel.add(Box.createVerticalStrut(5));
154 
155  return mainPanel;
156  }
157 
161  @ActionMethod
162  public void editBookmarkOkay() {
163  if (isOkEnabled()) {
164  setValue(okButton);
165  }
166  }
167 
171  @ActionMethod
172  public void editBookmarkCancel() {
173  setValue(cancelButton);
174  }
175 
180  @NotNull
181  public String getDescription() {
182  return descriptionField.getText().trim();
183  }
184 
189  private void updateOkButton() {
190  okButton.setEnabled(isOkEnabled());
191  }
192 
197  private boolean isOkEnabled() {
198  return !getDescription().isEmpty();
199  }
200 
201 }
boolean isOkEnabled()
Returns whether the "ok" button is enabled.
Graphical User Interface of Gridarta.
static final long serialVersionUID
The serial Version UID.
static final ActionBuilder ACTION_BUILDER
The ActionBuilder.
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.
Utility class for JTextComponent related functions.
A dialog that displays one bookmark and allows to edit the values.
static void setAutoSelectOnFocus(@NotNull final JTextComponent textComponent)
Selects all text of a JTextComponent when the component gains the focus.
Utility class for ActionBuilder related functions.
EditBookmarkDialog(@NotNull final Component parentComponent, @NotNull final String defaultDescription)
Creates a new instance.
Border DIALOG_BORDER
The Border object to be used when creating dialogs.
static JLabel newLabel(@NotNull final ActionBuilder actionBuilder, @NotNull final String key)
Creates a new JLabel from a resource key.
void updateOkButton()
Sets the enabled state of okButton depending on the contents of descriptionField. ...
final JTextComponent descriptionField
The text input field for the description.
Defines common UI constants used in different dialogs.