Gridarta Editor
AbstractPerMapDialogManager.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.map;
21 
22 import java.util.HashMap;
23 import java.util.Map;
30 import org.jetbrains.annotations.NotNull;
31 import org.jetbrains.annotations.Nullable;
32 
39 public abstract class AbstractPerMapDialogManager<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>, D> {
40 
44  private final Map<MapView<G, A, R>, D> dialogs = new HashMap<>();
45 
50 
51  @Override
52  public void activeMapViewChanged(@Nullable final MapView<G, A, R> mapView) {
53  // ignore
54  }
55 
56  @Override
57  public void mapViewCreated(@NotNull final MapView<G, A, R> mapView) {
58  // ignore
59  }
60 
61  @Override
62  public void mapViewClosing(@NotNull final MapView<G, A, R> mapView) {
63  disposeDialog(mapView);
64  }
65 
66  };
67 
72  protected AbstractPerMapDialogManager(@NotNull final MapViewManager<G, A, R> mapViewManager) {
73  mapViewManager.addMapViewManagerListener(mapViewManagerListener);
74  }
75 
80  public void showDialog(final MapView<G, A, R> mapView) {
81  synchronized (dialogs) {
82  final D oldDialog = dialogs.get(mapView);
83  if (oldDialog != null) {
84  activate(oldDialog);
85  return;
86  }
87 
88  final D dialog = allocate(mapView);
89  dialogs.put(mapView, dialog);
90  }
91  }
92 
98  public void disposeDialog(final MapView<G, A, R> mapView) {
99  final D dialog;
100  synchronized (dialogs) {
101  dialog = dialogs.remove(mapView);
102  if (dialog == null) {
103  return;
104  }
105  }
106 
107  dispose(dialog);
108  }
109 
115  @NotNull
116  protected abstract D allocate(@NotNull MapView<G, A, R> mapView);
117 
122  protected abstract void activate(@NotNull D dialog);
123 
128  protected abstract void dispose(@NotNull D dialog);
129 
130 }
AbstractPerMapDialogManager(@NotNull final MapViewManager< G, A, R > mapViewManager)
Creates a new instance.
Graphical User Interface of Gridarta.
final Map< MapView< G, A, R >, D > dialogs
Dialog for each map view.
final MapViewManagerListener< G, A, R > mapViewManagerListener
The map view manager listener to detect closed map views.
abstract void activate(@NotNull D dialog)
Activates an existing instance.
Base package of all Gridarta classes.
Reflects a game object (object on a map).
Definition: GameObject.java:36
void disposeDialog(final MapView< G, A, R > mapView)
Dispose a dialog.
GameObjects are the objects based on Archetypes found on maps.
abstract D allocate(@NotNull MapView< G, A, R > mapView)
Creates a new instance.
Maintains (dialog) instance associated to map view instances.
Interface for listeners interested in events related to MapViewManager instances. ...
Base classes for rendering maps.
A map view consists of a map grid and a map cursor, and is attached to a map control.
Definition: MapView.java:43
abstract void dispose(@NotNull D dialog)
Destroys an instance.
void showDialog(final MapView< G, A, R > mapView)
Show a dialog for positioning the cursor of a map.