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-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.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[] 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 - 1 : 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 - 1 : 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  if (selectedValue == null) {
341  return false;
342  }
343  return enterMap.enterExit(mapView, selectedValue, true);
344  }
345 
346 }
void goExitScrollUp()
Action method for scroll up.
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.
void goExitScrollTop()
Action method for scroll top.
A MapModel reflects the data of a map.
Definition: MapModel.java:75
Graphical User Interface of Gridarta.
Interface for classes that match GameObjects.
This package contains classes related to matching GameObjects, so called GameObjectMatchers.
Exception thrown if a game object does not specify a valid exit path.
final GameObjectMatcher exitGameObjectMatcher
The GameObjectMatcher for selecting exits.
void goExitScrollDown()
Action method for scroll down.
void goExitApply()
Action method for apply.
final MapListCellRenderer mapListCellRenderer
The MapListCellRenderer for list.
void goExitSelectDown()
Action method for select down.
void goExitCancel()
Action method for cancel.
void goExitScrollPageUp()
Action method for scroll page up.
MapControl< G, A, R > getMapControl()
Return the controller of this view.
static final JButton [] EMPTY_BUTTON_ARRAY
An empty array of JButton instances.
Utility class for Swing related functions.
Definition: SwingUtils.java:37
MapModel< G, A, R > getMapModel()
Returns the map model.
static String getString(@NotNull final ActionBuilder actionBuilder, @NotNull final String key, @NotNull final String defaultValue)
Returns the value of a key.
Base package of all Gridarta classes.
Reflects a game object (object on a map).
Definition: GameObject.java:36
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:179
final JList< G > list
The JList showing the matching maps.
final MapView< G, A, R > mapView
The MapView for this dialog.
GameObjects are the objects based on Archetypes found on maps.
A net.sf.gridarta.gui.panel.connectionview.CellRenderer for the locked items view.
Base classes for rendering maps.
Utility class for ActionBuilder related functions.
static final ActionBuilder ACTION_BUILDER
The ActionBuilder.
Provider for faces of GameObjects and Archetypes.
The face is the appearance of an object.
static MapLocation newAbsoluteMapLocation(@NotNull final GameObject<?, ?, ?> gameObject, final boolean allowRandomMapParameters)
Creates a new instance from a BaseObject instance.
Currently nothing more than a marker interface for unification.
Definition: MapControl.java:35
final EnterMap< G, A, R > enterMap
The EnterMap instance for entering maps.
A map view consists of a map grid and a map cursor, and is attached to a map control.
Definition: MapView.java:43
Represents a location on a map consisting of a map path and a map coordinate.
void goExitScrollBottom()
Action method for scroll bottom.
Helper class for entering maps.
Definition: EnterMap.java:52
final DefaultListModel< G > listModel
The list model containing the search results.
void goExitSelectUp()
Action method for select up.
boolean goExit()
Opens the selected maps.
final JDialog dialog
The JDialog instance.
boolean isMatching(@NotNull GameObject<?, ?, ?> gameObject)
Matches an GameObject.
A dialog to ask the user for a map to open.
void goExitScrollPageDown()
Action method for scroll page down.
static void addAction(@NotNull final JComponent textComponent, @NotNull final Action action)
Adds an accelerator key for a component.
Definition: SwingUtils.java:71