Gridarta Editor
PluginEditorRow.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.plugin;
21 
22 import java.awt.Component;
23 import java.awt.event.FocusListener;
24 import java.awt.event.ItemEvent;
25 import java.awt.event.ItemListener;
26 import java.awt.event.MouseListener;
27 import javax.swing.JComboBox;
28 import javax.swing.JTextField;
29 import javax.swing.event.DocumentEvent;
30 import javax.swing.event.DocumentListener;
31 import javax.swing.text.JTextComponent;
37 import net.sf.gridarta.plugin.Plugin;
41 import org.apache.log4j.Category;
42 import org.apache.log4j.Logger;
43 import org.jetbrains.annotations.NotNull;
44 
50 public class PluginEditorRow {
51 
55  @NotNull
56  private static final Category LOG = Logger.getLogger(PluginEditorRow.class);
57 
61  @NotNull
62  private final MouseListener mouseListener;
63 
67  @NotNull
68  private final FocusListener focusListener;
69 
73  @NotNull
74  private final Component parameterNameEditor;
75 
79  @NotNull
80  private final Component parameterDescriptionEditor;
81 
85  @NotNull
86  private final Component parameterTypeEditor;
87 
91  @NotNull
93 
106  public <G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> PluginEditorRow(@NotNull final Plugin<G, A, R> plugin, @NotNull final PluginParameter<G, A, R, ?> parameter, @NotNull final PluginParameterViewFactory<G, A, R> pluginParameterViewFactory, @NotNull final MouseListener mouseListener, @NotNull final FocusListener focusListener, @NotNull final Component parent) {
107  this.mouseListener = mouseListener;
108  this.focusListener = focusListener;
109 
110  parameterNameEditor = createNameEditor(parameter);
111  parameterDescriptionEditor = createDescriptionEditor(parameter);
112  parameterTypeEditor = createTypeEditor(plugin, parameter);
113  pluginParameterView = createView(parameter, pluginParameterViewFactory, parent);
114  }
115 
121  @NotNull
122  private <G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> Component createNameEditor(final PluginParameter<G, A, R, ?> parameter) {
123  final JTextComponent textField = new JTextField(parameter.getName());
124  textField.getDocument().addDocumentListener(new DocumentListener() {
125 
126  @Override
127  public void insertUpdate(@NotNull final DocumentEvent e) {
128  change();
129  }
130 
131  @Override
132  public void removeUpdate(@NotNull final DocumentEvent e) {
133  change();
134  }
135 
136  @Override
137  public void changedUpdate(@NotNull final DocumentEvent e) {
138  change();
139  }
140 
144  private void change() {
145  parameter.setName(textField.getText());
146  }
147 
148  });
149  attachListeners(textField);
150  return textField;
151  }
152 
158  @NotNull
159  private <G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> Component createDescriptionEditor(final PluginParameter<G, A, R, ?> parameter) {
160  final JTextComponent textField = new JTextField(parameter.getDescription());
161  textField.getDocument().addDocumentListener(new DocumentListener() {
162 
163  @Override
164  public void changedUpdate(@NotNull final DocumentEvent e) {
165  change();
166  }
167 
168  @Override
169  public void insertUpdate(@NotNull final DocumentEvent e) {
170  change();
171  }
172 
173  @Override
174  public void removeUpdate(@NotNull final DocumentEvent e) {
175  change();
176  }
177 
181  private void change() {
182  parameter.setDescription(textField.getText());
183  }
184  });
185  attachListeners(textField);
186  return textField;
187  }
188 
195  @NotNull
196  private <G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> Component createTypeEditor(final Plugin<G, A, R> plugin, final PluginParameter<G, A, R, ?> parameter) {
197  final JComboBox<?> comboBox = new JComboBox<>(PluginParameterFactory.getTypes());
198  comboBox.setSelectedItem(parameter.getParameterType());
199  comboBox.addItemListener(new ItemListener() {
200 
201  @Override
202  public void itemStateChanged(@NotNull final ItemEvent e) {
203  if (e.getStateChange() != ItemEvent.SELECTED) {
204  return;
205  }
206 
207  final String newParameterType = (String) e.getItem();
208  if (parameter.getParameterType().equals(newParameterType)) {
209  return;
210  }
211 
212  try {
213  plugin.convertType(parameter, newParameterType);
214  } catch (final NoSuchParameterException ex) {
215  LOG.warn("Cannot create parameter for " + ex.getMessage());
216  }
217  }
218 
219  });
220  attachListeners(comboBox);
221  return comboBox;
222  }
223 
232  @NotNull
233  private <G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> PluginParameterView createView(final PluginParameter<G, A, R, ?> parameter, final PluginParameterViewFactory<G, A, R> pluginParameterViewFactory, final Component parent) {
234  final PluginParameterView pluginParameterView = pluginParameterViewFactory.newPluginParameterView(parent, parameter);
235  attachListeners(pluginParameterView.getConfigComponent());
236  attachListeners(pluginParameterView.getValueComponent());
237  return pluginParameterView;
238  }
239 
244  @NotNull
245  public Component getParameterNameEditor() {
246  return parameterNameEditor;
247  }
248 
253  @NotNull
254  public Component getParameterDescriptionEditor() {
256  }
257 
262  @NotNull
263  public Component getParameterTypeEditor() {
264  return parameterTypeEditor;
265  }
266 
272  @NotNull
273  public Component getConfigComponent() {
274  return pluginParameterView.getConfigComponent();
275  }
276 
281  @NotNull
282  public Component getValueComponent() {
283  return pluginParameterView.getValueComponent();
284  }
285 
291  private void attachListeners(@NotNull final Component component) {
292  component.addFocusListener(focusListener);
293  component.addMouseListener(mouseListener);
294  }
295 
296 }
Component getParameterDescriptionEditor()
Returns the editor component for editing the parameter&#39;s description.
final Component parameterDescriptionEditor
The editor component for editing the parameter&#39;s description.
PluginParameterView newPluginParameterView(@NotNull final Component parent, @NotNull final PluginParameter< G, A, R, ?> parameter)
Graphical User Interface of Gridarta.
void setName(@NotNull String name)
Sets the name of the parameter.
Component getParameterTypeEditor()
Returns the editor component for editing the parameter&#39;s type.
Interface for views that display plugin parameters.
final Component parameterTypeEditor
The editor component for editing the parameter&#39;s type.
static final Category LOG
The Logger for printing log messages.
String getDescription()
The user-provided description of the value for the user interface.
Base package of all Gridarta classes.
Reflects a game object (object on a map).
Definition: GameObject.java:36
final MouseListener mouseListener
The MouseListener that is attached to all editor components.
GameObjects are the objects based on Archetypes found on maps.
final Component parameterNameEditor
The editor component for editing the parameter&#39;s name.
void convertType(@NotNull final PluginParameter< G, A, R, ?> pluginParameter, @NotNull final String newType)
Changes the type of a plugin parameter.
Definition: Plugin.java:377
String getParameterType()
Returns the parameter type name.
void attachListeners(@NotNull final Component component)
Attaches focusListener and mouseListener to a Component.
Model for plugins.
Definition: Plugin.java:52
Component getConfigComponent()
Returns the editor component for editing the parameter&#39;s configuration parameters.
void setDescription(@NotNull String description)
Sets the user-provided description of the value for the user interface.
final FocusListener focusListener
The FocusListener that is attached to all editor components.
A set of components for editing a PluginParameter and its configuration.
Component getParameterNameEditor()
Returns the editor component for editing the parameter&#39;s name.
Component getValueComponent()
Returns the editor component for editing the parameter&#39;s value.
String getName()
The name of the parameter.
static String [] getTypes()
Returns all available plugin parameter type names.
final PluginParameterView pluginParameterView
The PluginParameterView instance for the parameter being edited.
JComponent getValueComponent()
Returns a JComponent for editing the parameter value.
JComponent getConfigComponent()
Returns a JComponent for editing the parameter configuration.