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