20 package net.sf.gridarta.gui.dialog.gomap;
22 import java.awt.BorderLayout;
23 import java.awt.Component;
24 import java.awt.Dialog;
25 import java.awt.Window;
26 import java.awt.event.MouseEvent;
27 import java.awt.event.MouseListener;
28 import java.awt.event.WindowEvent;
29 import java.awt.event.WindowListener;
30 import java.io.IOException;
31 import java.util.Collection;
32 import java.util.Comparator;
33 import java.util.HashSet;
34 import java.util.List;
35 import java.util.TreeSet;
36 import javax.swing.DefaultListModel;
37 import javax.swing.JComponent;
38 import javax.swing.JDialog;
39 import javax.swing.JList;
40 import javax.swing.JPanel;
41 import javax.swing.JScrollPane;
42 import javax.swing.JTextField;
43 import javax.swing.ListSelectionModel;
44 import javax.swing.ScrollPaneConstants;
45 import javax.swing.SwingUtilities;
46 import javax.swing.border.EmptyBorder;
47 import javax.swing.event.DocumentEvent;
48 import javax.swing.event.DocumentListener;
49 import javax.swing.text.JTextComponent;
62 import net.
sf.japi.swing.action.ActionBuilder;
63 import net.
sf.japi.swing.action.ActionBuilderFactory;
64 import net.
sf.japi.swing.action.ActionMethod;
65 import org.jetbrains.annotations.NotNull;
77 private static final ActionBuilder
ACTION_BUILDER = ActionBuilderFactory.getInstance().getActionBuilder(
"net.sf.gridarta");
89 private final JTextComponent
input;
107 private final DefaultListModel<MapFile>
listModel =
new DefaultListModel<>();
127 public int compare(@NotNull
final MapFile o1, @NotNull
final MapFile o2) {
128 final String name1 = mapsIndex.
getName(o1);
129 final String name2 = mapsIndex.
getName(o2);
133 return name2 == null ? 0 : -1;
140 final int cmp = name1.compareToIgnoreCase(name2);
145 return o1.getFile().compareTo(o2.getFile());
159 public void valueAdded(@NotNull
final MapFile value) {
164 public void valueRemoved(@NotNull
final MapFile value) {
169 public void nameChanged() {
174 public void pendingChanged() {
179 public void indexingFinished() {
195 public void windowOpened(
final WindowEvent e) {
204 public void windowClosing(
final WindowEvent e) {
209 public void windowClosed(
final WindowEvent e) {
217 public void windowIconified(
final WindowEvent e) {
222 public void windowDeiconified(
final WindowEvent e) {
227 public void windowActivated(
final WindowEvent e) {
232 public void windowDeactivated(
final WindowEvent e) {
246 public void change() {
251 SwingUtilities.invokeLater(
new Runnable() {
278 input =
new JTextField();
281 SwingUtils.
addAction(input, ACTION_BUILDER.createAction(
false,
"goMapScrollPageUp",
this));
282 SwingUtils.
addAction(input, ACTION_BUILDER.createAction(
false,
"goMapScrollPageDown",
this));
284 SwingUtils.
addAction(input, ACTION_BUILDER.createAction(
false,
"goMapScrollBottom",
this));
289 final DocumentListener documentListener =
new DocumentListener() {
292 public void changedUpdate(@NotNull
final DocumentEvent e) {
297 public void insertUpdate(@NotNull
final DocumentEvent e) {
302 public void removeUpdate(@NotNull
final DocumentEvent e) {
307 input.getDocument().addDocumentListener(documentListener);
310 final JComponent inputPanel =
new JPanel(
new BorderLayout());
311 inputPanel.add(label, BorderLayout.NORTH);
312 inputPanel.add(input, BorderLayout.CENTER);
313 inputPanel.setBorder(
new EmptyBorder(3, 3, 3, 3));
315 list.setFocusable(
false);
316 final Component scrollPane =
new JScrollPane(list, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
318 list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
319 final MouseListener mouseListener =
new MouseListener() {
322 public void mouseClicked(
final MouseEvent e) {
327 public void mousePressed(
final MouseEvent e) {
328 if (e.getClickCount() > 1) {
334 public void mouseReleased(
final MouseEvent e) {
339 public void mouseEntered(
final MouseEvent e) {
344 public void mouseExited(
final MouseEvent e) {
349 list.addMouseListener(mouseListener);
351 dialog =
new JDialog(parent);
352 dialog.setResizable(
false);
353 dialog.setSize(600, 400);
354 dialog.setLocationRelativeTo(parent);
355 dialog.setUndecorated(
true);
356 dialog.setLayout(
new BorderLayout());
357 dialog.add(inputPanel, BorderLayout.NORTH);
358 dialog.add(scrollPane, BorderLayout.CENTER);
359 dialog.setModalityType(Dialog.ModalityType.DOCUMENT_MODAL);
360 dialog.addWindowListener(windowListener);
367 dialog.setVisible(
true);
374 final String mapName = input.getText();
375 if (mapName.isEmpty()) {
382 sortedMaps.addAll(maps);
383 final Collection<MapFile> selectedValues =
new HashSet<>(list.getSelectedValuesList());
385 int visibleIndex = -1;
386 for (
final MapFile map : sortedMaps) {
387 listModel.addElement(map);
388 if (selectedValues.contains(map)) {
389 final int index = listModel.size() - 1;
390 list.addSelectionInterval(index, index);
391 visibleIndex = index;
394 if (list.getSelectedIndex() == -1) {
395 list.setSelectedIndex(0);
398 if (visibleIndex != -1) {
399 list.ensureIndexIsVisible(visibleIndex);
409 dialog.removeWindowListener(windowListener);
419 dialog.removeWindowListener(windowListener);
428 final int index = list.getMinSelectionIndex();
429 final int newIndex = index > 0 ? index - 1 : listModel.size() - 1;
430 list.setSelectedIndex(newIndex);
431 list.ensureIndexIsVisible(newIndex);
439 final int index = list.getMaxSelectionIndex() + 1;
440 final int newIndex = index < listModel.size() ? index : 0;
441 list.setSelectedIndex(newIndex);
442 list.ensureIndexIsVisible(newIndex);
450 final int index = list.getMinSelectionIndex();
451 final int firstIndex = list.getFirstVisibleIndex();
453 if (firstIndex == -1) {
455 }
else if (index == -1) {
456 newIndex = firstIndex;
457 }
else if (index > firstIndex) {
458 newIndex = firstIndex;
460 newIndex = Math.max(firstIndex - (list.getLastVisibleIndex() - firstIndex), 0);
462 list.setSelectedIndex(newIndex);
463 list.ensureIndexIsVisible(newIndex);
471 final int index = list.getMaxSelectionIndex();
472 final int lastIndex = list.getLastVisibleIndex();
474 if (lastIndex == -1) {
476 }
else if (index == -1) {
477 newIndex = lastIndex;
478 }
else if (index < lastIndex) {
479 newIndex = lastIndex;
481 newIndex = Math.min(lastIndex + (lastIndex - list.getFirstVisibleIndex()), listModel.size() - 1);
483 list.setSelectedIndex(newIndex);
484 list.ensureIndexIsVisible(newIndex);
492 final int newIndex = 0;
493 list.setSelectedIndex(newIndex);
494 list.ensureIndexIsVisible(newIndex);
502 final int newIndex = listModel.size() - 1;
503 list.setSelectedIndex(newIndex);
504 list.ensureIndexIsVisible(newIndex);
512 final int index = list.getMinSelectionIndex();
514 final int newIndex = index > 0 ? index - 1 : listModel.size() - 1;
515 list.addSelectionInterval(newIndex, newIndex);
516 list.ensureIndexIsVisible(newIndex);
525 final int index = list.getMaxSelectionIndex();
526 if (index + 1 < listModel.size()) {
527 final int newIndex = index + 1;
528 list.addSelectionInterval(newIndex, newIndex);
529 list.ensureIndexIsVisible(newIndex);
538 final List<MapFile> selectedValues = list.getSelectedValuesList();
539 boolean result =
false;
540 for (
final MapFile selectedValue : selectedValues) {
544 }
catch (
final IOException ex) {
545 ACTION_BUILDER.showMessageDialog(dialog,
"goMapIOException", selectedValue, ex.getMessage());
Indexes maps by map name.
Helper for reducing the number of change events: calls to change() will be forwarded to DelayedChange...
final MapsIndex mapsIndex
The MapsIndex for looking up maps.
static final ActionBuilder ACTION_BUILDER
The ActionBuilder.
void removeIndexListener(@NotNull final IndexListener< V > listener)
Removes an IndexListener to be notified of changes.
void goMapScrollPageUp()
Action method for scroll page up.
void finish()
Finishes a series of change events.
Graphical User Interface of Gridarta.
final MapViewsManager< G, A, R > mapViewsManager
The MapViewsManager for opening maps.
Interface for listeners interested in Index related events.
void goMapSelectDown()
Action method for select down.
void goMapScrollPageDown()
Action method for scroll page down.
Listener for forwarded events of DelayedChangeManager.
Stores all existing MapViews.
void goMapScrollUp()
Action method for scroll up.
final JDialog dialog
The JDialog instance.
void showDialog()
Opens the dialog.
void doSearch()
Updates the maps display from the map name input field.
final JList< MapFile > list
The JList showing the matching maps.
void goMapScrollTop()
Action method for scroll top.
Utility class for Swing related functions.
void addIndexListener(@NotNull final IndexListener< V > listener)
Adds an IndexListener to be notified of changes.
Base package of all Gridarta classes.
void goMapSelectUp()
Action method for select up.
Reflects a game object (object on a map).
Utility class for JTextComponent related functions.
MapView< G, A, R > openMapFileWithView(@NotNull final MapFile mapFile, @Nullable final Point viewPosition, @Nullable final Point centerSquare)
Load a map file and create a map view.
A DefaultListCellRenderer that renders values of a MapsIndex.
final DelayedChangeManager delayedChangeManager
A DelayedChangeManager for updating search results after mapsIndex changes.
GameObjects are the objects based on Archetypes found on maps.
static void setAutoSelectOnFocus(@NotNull final JTextComponent textComponent)
Selects all text of a JTextComponent when the component gains the focus.
Collection< V > findPartialName(@NotNull final String name)
Returns all matching values for a (possibly partial) key name.
void change()
Delivers a change event to the associated DelayedChangeListener.
void goMapScrollDown()
Action method for scroll down.
void goMapScrollBottom()
Action method for scroll bottom.
Base classes for rendering maps.
GoMapDialog(@NotNull final Window parent, @NotNull final MapsIndex mapsIndex, @NotNull final MapViewsManager< G, A, R > mapViewsManager)
Creates a new instance.
final Comparator< MapFile > mapNameComparator
A Comparator that orders map files by map name.
void goMapApply()
Action method for apply.
final IndexListener< MapFile > indexListener
The IndexListener attached to mapsIndex to update search results after index changes.
String getName(@NotNull final V value)
Returns the name associated with a value.
Utility class for ActionBuilder related functions.
A dialog to ask the user for a map to open.
void goMapCancel()
Action method for cancel.
static JLabel newLabel(@NotNull final ActionBuilder actionBuilder, @NotNull final String key)
Creates a new JLabel from a resource key.
final DefaultListModel< MapFile > listModel
The list model containing the search results.
The location of a map file with a map directory.
boolean goMap()
Opens the selected maps.
Interface for MapArchObjects.
boolean dialogShown
Whether dialog is currently shown.
final WindowListener windowListener
The WindowListener attached to dialog to track opening/closing the dialog.
final JTextComponent input
The map name input field.
static void addAction(@NotNull final JComponent textComponent, @NotNull final Action action)
Adds an accelerator key for a component.