Gridarta Editor
SelectedSquareModel.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.panel.selectedsquare;
21 
27 import org.jetbrains.annotations.NotNull;
28 import org.jetbrains.annotations.Nullable;
29 
34 public class SelectedSquareModel<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> {
35 
39  @NotNull
41 
45  @Nullable
47 
51  @Nullable
52  private G selectedGameObject;
53 
59  @Nullable
61  return selectedMapSquare;
62  }
63 
69  @Nullable
70  public G getSelectedGameObject() {
71  return selectedGameObject;
72  }
73 
78  public void addSelectedSquareListener(@NotNull final SelectedSquareModelListener<G, A, R> listener) {
79  listenerList.add(listener);
80  }
81 
87  listenerList.remove(listener);
88  }
89 
93  private void fireSelectionChangedEvent() {
94  final G gameObject = selectedGameObject;
95  final G head = gameObject == null ? null : gameObject.getHead();
96  for (final SelectedSquareModelListener<G, A, R> listener : listenerList.getListeners()) {
97  listener.selectionChanged(selectedMapSquare, head);
98  }
99  }
100 
101  public boolean isSelectedMapSquares(@NotNull final Iterable<MapSquare<G, A, R>> mapSquares) {
102  final MapSquare<G, A, R> selectedMapSquare = this.selectedMapSquare;
103  if (selectedMapSquare == null) {
104  return false;
105  }
106 
107  for (final MapSquare<G, A, R> mapSquare : mapSquares) {
108  if (selectedMapSquare == mapSquare) {
109  return true;
110  }
111  }
112 
113  return false;
114  }
115 
116  public boolean isSelectedGameObjects(@NotNull final Iterable<G> gameObjects) {
117  final MapSquare<G, A, R> selectedMapSquare = this.selectedMapSquare;
118  if (selectedMapSquare == null) {
119  return false;
120  }
121 
122  for (final GameObject<G, A, R> gameObject : gameObjects) {
123  for (GameObject<G, A, R> gameObjectPart = gameObject; gameObjectPart != null; gameObjectPart = gameObjectPart.getMultiNext()) {
124  if (selectedMapSquare == gameObjectPart.getMapSquare()) {
125  return true;
126  }
127  }
128  }
129 
130  return false;
131  }
132 
140  public boolean setSelectedMapSquare(@Nullable final MapSquare<G, A, R> mapSquare, @Nullable final G gameObject) {
141  if (selectedMapSquare == mapSquare && selectedGameObject == gameObject) {
142  return false;
143  }
144 
145  selectedMapSquare = mapSquare;
146  selectedGameObject = gameObject;
148  return true;
149  }
150 
155  public void setSelectedGameObject(@Nullable final G gameObject) {
156  if (selectedMapSquare != null && selectedGameObject != gameObject) {
157  selectedGameObject = gameObject;
159  }
160  }
161 
162 }
G getSelectedGameObject()
Returns the currently selected GameObject within this list (currently selected MapSquare).
boolean isSelectedMapSquares(@NotNull final Iterable< MapSquare< G, A, R >> mapSquares)
T [] getListeners()
Returns an array of all the listeners.
T getMultiNext()
Returns the next of this multi-part object.
MapSquare< G, A, R > getSelectedMapSquare()
Returns the currently selected map square.
Interface for listeners interested in events of SelectedSquareModel instances.
Base package of all Gridarta classes.
Reflects a game object (object on a map).
Definition: GameObject.java:36
void remove(@NotNull final T listener)
Removes a listener.
GameObjects are the objects based on Archetypes found on maps.
void add(@NotNull final T listener)
Adds a listener.
boolean setSelectedMapSquare(@Nullable final MapSquare< G, A, R > mapSquare, @Nullable final G gameObject)
Sets the currently selected map square.
MapSquare< G, A, R > selectedMapSquare
The currently selected MapSquare.
void removeSelectedSquareListener(@NotNull final SelectedSquareModelListener< G, A, R > listener)
Removes a SelectedSquareModelListener to be notified.
void addSelectedSquareListener(@NotNull final SelectedSquareModelListener< G, A, R > listener)
Adds a SelectedSquareModelListener to be notified.
Type-safe version of EventListenerList.
void setSelectedGameObject(@Nullable final G gameObject)
Sets the currently selected GameObject.
void fireSelectionChangedEvent()
Notifies all listeners that the selected map square has changed.
The model component of the selected square control.
final EventListenerList2< SelectedSquareModelListener< G, A, R > > listenerList
The listeners to inform of changes.
boolean isSelectedGameObjects(@NotNull final Iterable< G > gameObjects)