Gridarta Editor
IntegerParameterView.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.parameter;
21 
22 import java.awt.Component;
23 import java.awt.event.ActionEvent;
24 import java.awt.event.ActionListener;
25 import javax.swing.AbstractButton;
26 import javax.swing.JButton;
27 import javax.swing.JComponent;
28 import javax.swing.JOptionPane;
29 import javax.swing.JSpinner;
30 import javax.swing.SpinnerNumberModel;
31 import javax.swing.event.ChangeEvent;
32 import javax.swing.event.ChangeListener;
37 import org.jetbrains.annotations.NotNull;
38 
42 public class IntegerParameterView<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> implements PluginParameterView {
43 
47  @NotNull
49 
53  @NotNull
54  private final JSpinner value;
55 
59  @NotNull
60  private final AbstractButton config = new JButton("...");
61 
67  public IntegerParameterView(@NotNull final Component parent, @NotNull final IntegerParameter<G, A, R> parameter) {
68  this.parameter = parameter;
69  final SpinnerNumberModel model = new SpinnerNumberModel((int) parameter.getValue(), parameter.getMin(), parameter.getMax(), 1);
70  model.setMinimum(parameter.getMin());
71  model.setMaximum(parameter.getMax());
72  model.setValue(parameter.getValue());
73  value = new JSpinner(model);
74  value.addChangeListener(new ChangeListener() {
75 
76  @Override
77  public void stateChanged(@NotNull final ChangeEvent e) {
78  parameter.setValue((Integer) model.getNumber());
79  }
80  });
81  config.setBorderPainted(false);
82  config.addActionListener(new ActionListener() {
83 
84  @Override
85  public void actionPerformed(@NotNull final ActionEvent e) {
86  final String min = JOptionPane.showInputDialog(parent, "Minimum value:", parameter.getMin());
87  if (min == null) {
88  return;
89  }
90  final int min1;
91  try {
92  min1 = Integer.parseInt(min);
93  } catch (final NumberFormatException ignored) {
94  JOptionPane.showMessageDialog(parent, "Invalid minimum value: " + min);
95  return;
96  }
97  parameter.setMin(min1);
98  model.setMinimum(min1);
99  updateTooltip();
100 
101  final String max = JOptionPane.showInputDialog(parent, "Maximum value:", parameter.getMax());
102  if (max == null) {
103  return;
104  }
105  final int max1;
106  try {
107  max1 = Integer.parseInt(max);
108  } catch (final NumberFormatException ignored) {
109  JOptionPane.showMessageDialog(parent, "Invalid maximum value: " + max);
110  return;
111  }
112  parameter.setMax(max1);
113  model.setMaximum(max1);
114  updateTooltip();
115  }
116 
117  });
118  updateTooltip();
119  }
120 
121  @NotNull
122  @Override
123  public JComponent getConfigComponent() {
124  return config;
125  }
126 
127  @NotNull
128  @Override
129  public JComponent getValueComponent() {
130  return value;
131  }
132 
137  private void updateTooltip() {
138  final String toolTip = "[" + Integer.toString(parameter.getMin()) + "," + Integer.toString(parameter.getMax()) + "]";
139  config.setToolTipText(toolTip);
140  value.setToolTipText(toolTip);
141  }
142 
143 }
IntegerParameterView(@NotNull final Component parent, @NotNull final IntegerParameter< G, A, R > parameter)
Creates a new instance.
int getMin()
Returns the minimal allowed value.
final JSpinner value
The component for editing the parameter&#39;s value.
final IntegerParameter< G, A, R > parameter
The IntegerParameter that is shown.
A PluginParameter that holds an integer value and a range of valid values.
final AbstractButton config
The component for editing the parameter&#39;s configuration.
Interface for views that display plugin parameters.
Base package of all Gridarta classes.
Reflects a game object (object on a map).
Definition: GameObject.java:36
void updateTooltip()
Updates the tooltip text of config and value to reflect the current parameters.
GameObjects are the objects based on Archetypes found on maps.
JComponent getConfigComponent()
Returns a JComponent for editing the parameter configuration.
int getMax()
Returns the maximal allowed value.
JComponent getValueComponent()
Returns a JComponent for editing the parameter value.
void setValue(@NotNull final Integer value)