Gridarta Editor
GoExitDialog.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.goexit;
21 
22 import java.awt.BorderLayout;
23 import java.awt.Component;
24 import java.awt.event.MouseEvent;
25 import java.awt.event.MouseListener;
26 import java.util.Collection;
27 import java.util.TreeSet;
28 import javax.swing.DefaultListModel;
29 import javax.swing.JButton;
30 import javax.swing.JDialog;
31 import javax.swing.JList;
32 import javax.swing.JOptionPane;
33 import javax.swing.JPanel;
34 import javax.swing.JScrollPane;
35 import javax.swing.ListSelectionModel;
36 import javax.swing.ScrollPaneConstants;
37 import javax.swing.WindowConstants;
51 import net.sf.japi.swing.action.ActionBuilder;
52 import net.sf.japi.swing.action.ActionBuilderFactory;
53 import net.sf.japi.swing.action.ActionMethod;
54 import org.jetbrains.annotations.NotNull;
55 
60 public class GoExitDialog<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> {
61 
65  @NotNull
66  private static final ActionBuilder ACTION_BUILDER = ActionBuilderFactory.getInstance().getActionBuilder("net.sf.gridarta");
67 
71  @NotNull
72  private static final JButton @NotNull [] EMPTY_BUTTON_ARRAY = new JButton[0];
73 
77  @NotNull
78  private final MapView<G, A, R> mapView;
79 
83  @NotNull
85 
89  @NotNull
90  private final EnterMap<G, A, R> enterMap;
91 
95  @NotNull
96  private final JDialog dialog;
97 
101  @NotNull
102  private final DefaultListModel<G> listModel = new DefaultListModel<>();
103 
107  @NotNull
108  private final JList<G> list = new JList<>(listModel);
109 
113  @NotNull
115 
125  public GoExitDialog(@NotNull final Component parent, @NotNull final MapView<G, A, R> mapView, @NotNull final GameObjectMatcher exitGameObjectMatcher, @NotNull final EnterMap<G, A, R> enterMap, @NotNull final FaceObjectProviders faceObjectProviders) {
126  this.mapView = mapView;
127  this.exitGameObjectMatcher = exitGameObjectMatcher;
128  this.enterMap = enterMap;
129  SwingUtils.addAction(list, ACTION_BUILDER.createAction(false, "goExitApply", this));
130  list.setFocusable(false);
131  final Component scrollPane = new JScrollPane(list, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
132  mapListCellRenderer = new MapListCellRenderer(faceObjectProviders);
133  list.setCellRenderer(mapListCellRenderer);
134  list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
135  final MouseListener mouseListener = new MouseListener() {
136 
137  @Override
138  public void mouseClicked(final MouseEvent e) {
139  // ignore
140  }
141 
142  @Override
143  public void mousePressed(final MouseEvent e) {
144  if (e.getClickCount() > 1) {
145  goExitApply();
146  }
147  }
148 
149  @Override
150  public void mouseReleased(final MouseEvent e) {
151  // ignore
152  }
153 
154  @Override
155  public void mouseEntered(final MouseEvent e) {
156  // ignore
157  }
158 
159  @Override
160  public void mouseExited(final MouseEvent e) {
161  // ignore
162  }
163 
164  };
165  list.addMouseListener(mouseListener);
166  list.setFocusable(true);
167 
168  final JPanel panel = new JPanel(new BorderLayout());
169  panel.add(scrollPane, BorderLayout.CENTER);
170  final JOptionPane optionPane = new JOptionPane(panel, JOptionPane.PLAIN_MESSAGE, JOptionPane.DEFAULT_OPTION, null, EMPTY_BUTTON_ARRAY, list);
171  dialog = optionPane.createDialog(parent, ActionBuilderUtils.getString(ACTION_BUILDER, "goExitTitle"));
172  dialog.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
173  dialog.setResizable(true);
174  dialog.setSize(500, 250);
175  dialog.setLocationRelativeTo(parent);
176  }
177 
181  public void showDialog() {
182  final MapControl<G, A, R> mapControl = mapView.getMapControl();
183  final MapModel<G, A, R> mapModel = mapControl.getMapModel();
184  final Collection<G> exits = new TreeSet<>(mapListCellRenderer);
185  for (final Iterable<G> mapSquare : mapModel) {
186  for (final G gameObject : mapSquare) {
187  if (exitGameObjectMatcher.isMatching(gameObject)) {
188  try {
189  MapLocation.newAbsoluteMapLocation(gameObject, true);
190  exits.add(gameObject);
191  } catch (final NoExitPathException ignored) {
192  // ignore
193  }
194  }
195  }
196  }
197  for (final G exit : exits) {
198  listModel.addElement(exit);
199  }
200 
201  list.setSelectedIndex(0);
202  list.ensureIndexIsVisible(0);
203  dialog.setVisible(true);
204  }
205 
209  @ActionMethod
210  public void goExitApply() {
211  if (goExit()) {
212  dialog.dispose();
213  }
214  }
215 
219  @ActionMethod
220  public void goExitCancel() {
221  dialog.dispose();
222  }
223 
227  @ActionMethod
228  public void goExitScrollUp() {
229  final int index = list.getMinSelectionIndex();
230  final int newIndex = (index > 0 ? index : listModel.size()) - 1;
231  list.setSelectedIndex(newIndex);
232  list.ensureIndexIsVisible(newIndex);
233  }
234 
238  @ActionMethod
239  public void goExitScrollDown() {
240  final int index = list.getMaxSelectionIndex() + 1;
241  final int newIndex = index < listModel.size() ? index : 0;
242  list.setSelectedIndex(newIndex);
243  list.ensureIndexIsVisible(newIndex);
244  }
245 
249  @ActionMethod
250  public void goExitScrollPageUp() {
251  final int index = list.getMinSelectionIndex();
252  final int firstIndex = list.getFirstVisibleIndex();
253  final int newIndex;
254  if (firstIndex == -1) {
255  newIndex = -1;
256  } else if (index == -1) {
257  newIndex = firstIndex;
258  } else if (index > firstIndex) {
259  newIndex = firstIndex;
260  } else {
261  newIndex = Math.max(firstIndex - (list.getLastVisibleIndex() - firstIndex), 0);
262  }
263  list.setSelectedIndex(newIndex);
264  list.ensureIndexIsVisible(newIndex);
265  }
266 
270  @ActionMethod
271  public void goExitScrollPageDown() {
272  final int index = list.getMaxSelectionIndex();
273  final int lastIndex = list.getLastVisibleIndex();
274  final int newIndex;
275  if (lastIndex == -1) {
276  newIndex = -1;
277  } else if (index == -1) {
278  newIndex = lastIndex;
279  } else if (index < lastIndex) {
280  newIndex = lastIndex;
281  } else {
282  newIndex = Math.min(lastIndex + (lastIndex - list.getFirstVisibleIndex()), listModel.size() - 1);
283  }
284  list.setSelectedIndex(newIndex);
285  list.ensureIndexIsVisible(newIndex);
286  }
287 
291  @ActionMethod
292  public void goExitScrollTop() {
293  final int newIndex = 0;
294  list.setSelectedIndex(newIndex);
295  list.ensureIndexIsVisible(newIndex);
296  }
297 
301  @ActionMethod
302  public void goExitScrollBottom() {
303  final int newIndex = listModel.size() - 1;
304  list.setSelectedIndex(newIndex);
305  list.ensureIndexIsVisible(newIndex);
306  }
307 
311  @ActionMethod
312  public void goExitSelectUp() {
313  final int index = list.getMinSelectionIndex();
314  if (index != 0) {
315  final int newIndex = (index > 0 ? index : listModel.size()) - 1;
316  list.addSelectionInterval(newIndex, newIndex);
317  list.ensureIndexIsVisible(newIndex);
318  }
319  }
320 
324  @ActionMethod
325  public void goExitSelectDown() {
326  final int index = list.getMaxSelectionIndex();
327  if (index + 1 < listModel.size()) {
328  final int newIndex = index + 1;
329  list.addSelectionInterval(newIndex, newIndex);
330  list.ensureIndexIsVisible(newIndex);
331  }
332  }
333 
338  private boolean goExit() {
339  final G selectedValue = list.getSelectedValue();
340  return selectedValue != null && enterMap.enterExit(mapView, selectedValue, true);
341  }
342 
343 }
net.sf.gridarta.model.mapmodel.MapModel
A MapModel reflects the data of a map.
Definition: MapModel.java:75
net.sf.gridarta.gui.dialog.goexit.MapListCellRenderer
A net.sf.gridarta.gui.panel.connectionview.CellRenderer for the locked items view.
Definition: MapListCellRenderer.java:39
net.sf.gridarta.gui.map.mapview.MapView.getMapControl
MapControl< G, A, R > getMapControl()
Return the controller of this view.
net.sf.gridarta
Base package of all Gridarta classes.
net.sf.gridarta.gui.map.mapactions
Definition: EnterMap.java:20
net.sf.gridarta.gui.dialog.goexit.GoExitDialog.ACTION_BUILDER
static final ActionBuilder ACTION_BUILDER
The ActionBuilder.
Definition: GoExitDialog.java:66
net.sf
net.sf.gridarta.model.mapmodel
Definition: AboveFloorInsertionMode.java:20
net.sf.gridarta.gui.dialog.goexit.GoExitDialog.dialog
final JDialog dialog
The JDialog instance.
Definition: GoExitDialog.java:96
net.sf.gridarta.model.match.GameObjectMatcher
Interface for classes that match GameObjects.
Definition: GameObjectMatcher.java:30
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.model.mapcontrol
Definition: DefaultMapControl.java:20
net.sf.gridarta.gui.dialog.goexit.GoExitDialog.listModel
final DefaultListModel< G > listModel
The list model containing the search results.
Definition: GoExitDialog.java:102
net.sf.gridarta.model.match.GameObjectMatcher.isMatching
boolean isMatching(@NotNull GameObject<?, ?, ?> gameObject)
Matches a GameObject.
net.sf.gridarta.gui.dialog.goexit.GoExitDialog.showDialog
void showDialog()
Opens the dialog.
Definition: GoExitDialog.java:181
net.sf.gridarta.gui.dialog.goexit.GoExitDialog.goExit
boolean goExit()
Opens the selected maps.
Definition: GoExitDialog.java:338
net.sf.gridarta.gui.dialog.goexit.GoExitDialog.goExitScrollBottom
void goExitScrollBottom()
Action method for scroll bottom.
Definition: GoExitDialog.java:302
net.sf.gridarta.gui.utils.SwingUtils.addAction
static void addAction(@NotNull final JComponent textComponent, @NotNull final Action action)
Adds an accelerator key for a component.
Definition: SwingUtils.java:71
net.sf.gridarta.gui.map.mapactions.EnterMap
Helper class for entering maps.
Definition: EnterMap.java:52
net.sf.gridarta.gui
Graphical User Interface of Gridarta.
net.sf.gridarta.gui.dialog.goexit.GoExitDialog.goExitScrollUp
void goExitScrollUp()
Action method for scroll up.
Definition: GoExitDialog.java:228
net.sf.gridarta.gui.dialog.goexit.GoExitDialog.goExitSelectUp
void goExitSelectUp()
Action method for select up.
Definition: GoExitDialog.java:312
net.sf.gridarta.gui.dialog.goexit.GoExitDialog.enterMap
final EnterMap< G, A, R > enterMap
The EnterMap instance for entering maps.
Definition: GoExitDialog.java:90
net.sf.gridarta.model.gameobject
GameObjects are the objects based on Archetypes found on maps.
Definition: AbstractGameObject.java:20
net
net.sf.gridarta.model.maplocation.MapLocation.newAbsoluteMapLocation
static MapLocation newAbsoluteMapLocation(@NotNull final GameObject<?, ?, ?> gameObject, final boolean allowRandomMapParameters)
Creates a new instance from a BaseObject instance.
Definition: MapLocation.java:94
net.sf.gridarta.model.match
Classes related to matching {GameObjects}, so called { net.sf.gridarta.model.match....
Definition: AndGameObjectMatcher.java:20
net.sf.gridarta.gui.dialog.goexit.GoExitDialog.GoExitDialog
GoExitDialog(@NotNull final Component parent, @NotNull final MapView< G, A, R > mapView, @NotNull final GameObjectMatcher exitGameObjectMatcher, @NotNull final EnterMap< G, A, R > enterMap, @NotNull final FaceObjectProviders faceObjectProviders)
Creates a new instance.
Definition: GoExitDialog.java:125
net.sf.gridarta.gui.utils.SwingUtils
Utility class for Swing related functions.
Definition: SwingUtils.java:37
net.sf.gridarta.model.maparchobject.MapArchObject
Interface for MapArchObjects.
Definition: MapArchObject.java:40
net.sf.gridarta.gui.dialog.goexit.GoExitDialog.exitGameObjectMatcher
final GameObjectMatcher exitGameObjectMatcher
The GameObjectMatcher for selecting exits.
Definition: GoExitDialog.java:84
net.sf.gridarta.gui.dialog.goexit.GoExitDialog.goExitScrollPageDown
void goExitScrollPageDown()
Action method for scroll page down.
Definition: GoExitDialog.java:271
net.sf.gridarta.model.maplocation.NoExitPathException
Exception thrown if a game object does not specify a valid exit path.
Definition: NoExitPathException.java:29
net.sf.gridarta.gui.map.mapview
Definition: AbstractMapView.java:20
net.sf.gridarta.gui.map.mapview.MapView
A map view consists of a map grid and a map cursor, and is attached to a map control.
Definition: MapView.java:43
net.sf.gridarta.model.maplocation.MapLocation
Represents a location on a map consisting of a map path and a map coordinate.
Definition: MapLocation.java:42
net.sf.gridarta.gui.map.mapactions.EnterMap.enterExit
boolean enterExit(@NotNull final MapView< G, A, R > mapView, @NotNull final GameObject< G, A, R > exit, final boolean allowRandomMapParameters)
Opens the map an exit game object points to.
Definition: EnterMap.java:176
net.sf.gridarta.utils.ActionBuilderUtils.getString
static String getString(@NotNull final ActionBuilder actionBuilder, @NotNull final String key, @NotNull final String defaultValue)
Returns the value of a key.
Definition: ActionBuilderUtils.java:71
net.sf.gridarta.gui.dialog.goexit.GoExitDialog.goExitScrollDown
void goExitScrollDown()
Action method for scroll down.
Definition: GoExitDialog.java:239
net.sf.gridarta.gui.dialog.goexit.GoExitDialog.goExitApply
void goExitApply()
Action method for apply.
Definition: GoExitDialog.java:210
net.sf.gridarta.gui.dialog.goexit.GoExitDialog.EMPTY_BUTTON_ARRAY
static final JButton[] EMPTY_BUTTON_ARRAY
An empty array of JButton instances.
Definition: GoExitDialog.java:72
net.sf.gridarta.gui.dialog.goexit.GoExitDialog
A dialog to ask the user for a map to open.
Definition: GoExitDialog.java:60
net.sf.gridarta.model
net.sf.gridarta.model.archetype.Archetype
Reflects an Archetype.
Definition: Archetype.java:41
net.sf.gridarta.gui.dialog.goexit.GoExitDialog.goExitScrollTop
void goExitScrollTop()
Action method for scroll top.
Definition: GoExitDialog.java:292
net.sf.gridarta.gui.dialog.goexit.GoExitDialog.mapListCellRenderer
final MapListCellRenderer mapListCellRenderer
The MapListCellRenderer for list.
Definition: GoExitDialog.java:114
net.sf.gridarta.gui.map
Base classes for rendering maps.
Definition: AbstractPerMapDialogManager.java:20
net.sf.gridarta.model.face
The face is the appearance of an object.
Definition: AbstractFaceObjects.java:20
net.sf.gridarta.gui.dialog.goexit.GoExitDialog.goExitSelectDown
void goExitSelectDown()
Action method for select down.
Definition: GoExitDialog.java:325
net.sf.gridarta.gui.dialog.goexit.GoExitDialog.goExitCancel
void goExitCancel()
Action method for cancel.
Definition: GoExitDialog.java:220
net.sf.gridarta.model.mapcontrol.MapControl
Currently nothing more than a marker interface for unification.
Definition: MapControl.java:35
net.sf.gridarta.utils.ActionBuilderUtils
Utility class for ActionBuilder related functions.
Definition: ActionBuilderUtils.java:31
net.sf.gridarta.model.mapcontrol.MapControl.getMapModel
MapModel< G, A, R > getMapModel()
Returns the map model.
net.sf.gridarta.gui.dialog.goexit.GoExitDialog.goExitScrollPageUp
void goExitScrollPageUp()
Action method for scroll page up.
Definition: GoExitDialog.java:250
net.sf.gridarta.gui.dialog.goexit.GoExitDialog.mapView
final MapView< G, A, R > mapView
The MapView for this dialog.
Definition: GoExitDialog.java:78
net.sf.gridarta.model.maparchobject
Definition: AbstractMapArchObject.java:20
net.sf.gridarta.gui.utils
Definition: AnimationComponent.java:20
net.sf.gridarta.gui.dialog.goexit.GoExitDialog.list
final JList< G > list
The JList showing the matching maps.
Definition: GoExitDialog.java:108
net.sf.gridarta.utils
Definition: ActionBuilderUtils.java:20
net.sf.gridarta.model.maplocation
Definition: MapLocation.java:20