Gridarta Editor
ArchComboBoxEditor.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.archetype;
21 
22 import java.awt.Color;
23 import java.awt.Component;
24 import java.awt.Container;
25 import java.awt.Dimension;
26 import java.awt.FlowLayout;
27 import java.awt.GridBagConstraints;
28 import java.awt.GridBagLayout;
29 import java.awt.event.ActionEvent;
30 import java.awt.event.ActionListener;
31 import java.awt.event.MouseEvent;
32 import java.awt.event.MouseListener;
33 import javax.swing.AbstractButton;
34 import javax.swing.ComboBoxEditor;
35 import javax.swing.JButton;
36 import javax.swing.JComboBox;
37 import javax.swing.JLabel;
38 import javax.swing.JPanel;
39 import javax.swing.JPopupMenu;
40 import javax.swing.JTextField;
41 import javax.swing.border.LineBorder;
42 import javax.swing.event.DocumentEvent;
43 import javax.swing.event.DocumentListener;
44 import javax.swing.text.JTextComponent;
53 import org.jetbrains.annotations.NotNull;
54 import org.jetbrains.annotations.Nullable;
55 
56 public class ArchComboBoxEditor<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> implements ComboBoxEditor {
57 
58  @NotNull
59  private final JComboBox<Archetype<G, A, R>> comboBox;
60 
61  @NotNull
63 
64  @NotNull
65  private final Container editorPanel = new JPanel(new GridBagLayout());
66 
67  @NotNull
68  private final JLabel icon = new JLabel();
69 
70  @NotNull
71  private final JTextComponent editor = new JTextField();
72 
73  @NotNull
74  private final JPopupMenu popup = new JPopupMenu();
75 
76  private volatile boolean locked;
77 
81  @NotNull
83 
93  public ArchComboBoxEditor(@NotNull final JComboBox<Archetype<G, A, R>> comboBox, @NotNull final ArchComboBoxModel<G, A, R> archComboBoxModel, @NotNull final ObjectChooser<G, A, R> objectChooser, @NotNull final GameObjectAttributesModel<G, A, R> gameObjectAttributesModel, @NotNull final FaceObjectProviders faceObjectProviders) {
94  this.comboBox = comboBox;
95  this.archComboBoxModel = archComboBoxModel;
96  this.faceObjectProviders = faceObjectProviders;
97  final GridBagConstraints gbc = new GridBagConstraints();
98  gbc.gridx = 0;
99  gbc.gridy = 0;
100  gbc.weightx = 1.0;
101  gbc.gridwidth = 4;
102  gbc.fill = GridBagConstraints.HORIZONTAL;
103  editorPanel.add(editor, gbc);
104  gbc.gridy = 1;
105  gbc.gridwidth = 1;
106  gbc.weightx = 0.0;
107  gbc.gridx = 1;
108  final AbstractButton fromSelect = new JButton("From tile selection");
109  fromSelect.addActionListener(new ActionListener() {
110 
111  @Override
112  public void actionPerformed(@NotNull final ActionEvent e) {
113  final BaseObject<G, A, R, ?> gameObject = objectChooser.getSelection();
114  if (gameObject != null) {
115  final Archetype<G, A, R> ao = archComboBoxModel.getNearestMatch(gameObject.getArchetype().getArchetypeName());
116  comboBox.setSelectedItem(ao);
117  setItem(ao);
118  }
119  }
120  });
121  editorPanel.add(fromSelect, gbc);
122  final AbstractButton fromActive = new JButton("From map selection");
123  fromActive.addActionListener(new ActionListener() {
124 
125  @Override
126  public void actionPerformed(@NotNull final ActionEvent e) {
127  final BaseObject<G, A, R, ?> gameObject = gameObjectAttributesModel.getSelectedGameObject();
128  if (gameObject != null) {
129  final Archetype<G, A, R> ao = archComboBoxModel.getNearestMatch(gameObject.getArchetype().getArchetypeName());
130  comboBox.setSelectedItem(ao);
131  setItem(ao);
132  }
133  }
134  });
135  gbc.gridx = 2;
136  editorPanel.add(fromActive, gbc);
137  editor.addMouseListener(new MouseListener() {
138 
139  @Override
140  public void mouseClicked(@NotNull final MouseEvent e) {
141  }
142 
143  @Override
144  public void mouseEntered(@NotNull final MouseEvent e) {
145  //popup.setLocation(p.x, p.y+archComboBoxEditor.getHeight()+5);
146  popup.setPreferredSize(null);
147  final Dimension d = popup.getPreferredSize();
148  final Dimension p = editorPanel.getSize();
149  if (d.width < p.width) {
150  d.width = p.width;
151  }
152  if (d.height < p.height) {
153  d.height = p.height;
154  }
155  popup.setPreferredSize(d);
156  popup.show(editorPanel, 0, editorPanel.getHeight() + 5);
157  }
158 
159  @Override
160  public void mouseExited(@NotNull final MouseEvent e) {
161  popup.setVisible(false);
162  }
163 
164  @Override
165  public void mousePressed(@NotNull final MouseEvent e) {
166  }
167 
168  @Override
169  public void mouseReleased(@NotNull final MouseEvent e) {
170  }
171  });
172  editor.setEditable(true);
173  editor.getDocument().addDocumentListener(new DocumentListener() {
174 
175  @Override
176  public void changedUpdate(@NotNull final DocumentEvent e) {
178  }
179 
180  @Override
181  public void insertUpdate(@NotNull final DocumentEvent e) {
183  }
184 
185  @Override
186  public void removeUpdate(@NotNull final DocumentEvent e) {
188  }
189  });
190  popup.setLayout(new FlowLayout());
191  popup.setBackground(CommonConstants.BG_COLOR);
192  popup.setBorder(new LineBorder(Color.black));
193  popup.add(icon);
194  popup.setFocusable(false);
195  comboBox.setBackground(CommonConstants.BG_COLOR);
196  }
197 
198  public void lockEditor() {
199  locked = true;
200  }
201 
202  public void unlockEditor() {
203  locked = false;
204  }
205 
206  @Override
207  public void selectAll() {
208  // TODO Auto-generated method stub
209 
210  }
211 
212  @NotNull
213  @Override
214  public Component getEditorComponent() {
215  return editorPanel;
216  }
217 
218  @Override
219  public void addActionListener(@NotNull final ActionListener l) {
220  // TODO Auto-generated method stub
221 
222  }
223 
224  @Override
225  public void removeActionListener(@NotNull final ActionListener l) {
226  // TODO Auto-generated method stub
227 
228  }
229 
230  @Nullable
231  @Override
232  public Object getItem() {
233  // TODO Auto-generated method stub
234  return null;
235  }
236 
237  @Override
238  public void setItem(@Nullable final Object anObject) {
239  final BaseObject<?, ?, ?, ?> arch = (BaseObject<?, ?, ?, ?>) anObject;
240  if (anObject == null) {
241  icon.setIcon(null);
242  icon.setText("No item selected");
243  } else {
244  icon.setIcon(faceObjectProviders.getFace(arch));
245  }
246 
247  if (arch == null) {
248  icon.setText("");
249  if (!locked) {
250  editor.setText("");
251  }
252  } else {
253  if (!locked) {
254  editor.setText(arch.getArchetype().getArchetypeName());
255  }
256  icon.setText(arch.getArchetype().getArchetypeName());
257  }
258  }
259 
260  @NotNull
261  public JTextComponent getEditor() {
262  return editor;
263  }
264 
265  public void editorEntryChange() {
266  lockEditor();
267  final Archetype<G, A, R> nearestMatch = archComboBoxModel.getNearestMatch(editor.getText());
268  comboBox.setSelectedItem(nearestMatch);
269  setItem(nearestMatch);
270  unlockEditor();
271  }
272 
273 }
Graphical User Interface of Gridarta.
final FaceObjectProviders faceObjectProviders
The FaceObjectProviders for looking up faces.
Class with constants used in Gridarta and derivates.
static final Color BG_COLOR
Background Color (for the Panels).
Archetype< G, A, R > getNearestMatch(@NotNull final String name)
TypeMayBeWeakened
Base package of all Gridarta classes.
Reflects a game object (object on a map).
Definition: GameObject.java:36
GameObjects are the objects based on Archetypes found on maps.
ArchComboBoxEditor(@NotNull final JComboBox< Archetype< G, A, R >> comboBox, @NotNull final ArchComboBoxModel< G, A, R > archComboBoxModel, @NotNull final ObjectChooser< G, A, R > objectChooser, @NotNull final GameObjectAttributesModel< G, A, R > gameObjectAttributesModel, @NotNull final FaceObjectProviders faceObjectProviders)
Creates a new instance.
R getArchetype()
Returns the Archetype this GameObject is based on.
Provider for faces of GameObjects and Archetypes.
The face is the appearance of an object.
Common base interface for ObjectChoosers.