Gridarta Editor
ModelUpdater.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.panel.selectedsquare;
21 
22 import javax.swing.DefaultListModel;
28 import org.jetbrains.annotations.NotNull;
29 import org.jetbrains.annotations.Nullable;
30 
39 public class ModelUpdater<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> {
40 
44  @NotNull
45  private final DefaultListModel<G> model;
46 
50  @NotNull
52 
56  private int updateIndex;
57 
63  public ModelUpdater(@NotNull final DefaultListModel<G> model, @NotNull final MapViewSettings mapViewSettings) {
64  this.model = model;
65  this.mapViewSettings = mapViewSettings;
66  }
67 
74  public int update(@Nullable final GameObjectContainer<G, A, R> mapSquare, @Nullable final G gameObject) {
75  if (mapSquare == null) {
76  model.removeAllElements();
77  return -1;
78  }
79 
80  updateIndex = 0;
81  int currentSelectionIndex = -1;
82  int firstVisibleIndex = -1;
83 
84  // Now go through the list backwards and put all game object on the
85  // panel in this order
86  boolean foundVisibleIndex = false;
87  for (final G node : mapSquare.reverse()) {
88  // add the node
89  if (node == gameObject) {
90  currentSelectionIndex = updateIndex;
91  }
92  addElement(node);
93 
94  // if view-settings are applied, mark topmost "visible" square for selection
95  if (mapViewSettings.isEditTypeSet() && !foundVisibleIndex && mapViewSettings.isEditType(node.getEditType())) {
96  firstVisibleIndex = updateIndex - 1; // select this square
97  foundVisibleIndex = true; // this is it - don't select any other square
98  }
99 
100  final int tmpSelect = addInvObjects(node, gameObject);
101  if (currentSelectionIndex == -1 && tmpSelect != -1) {
102  currentSelectionIndex = tmpSelect;
103  }
104  }
105  if (updateIndex < model.size()) {
106  model.removeRange(updateIndex, model.size() - 1);
107  }
108 
109  final int selectedIndex;
110  if (currentSelectionIndex != -1) {
111  selectedIndex = currentSelectionIndex;
112  } else if (firstVisibleIndex != -1) {
113  selectedIndex = firstVisibleIndex;
114  } else {
115  selectedIndex = -1;
116  }
117  return selectedIndex;
118  }
119 
127  private int addInvObjects(@NotNull final G node, @Nullable final G gameObject) {
128  int selListCounter = -1;
129  for (final G invObject : node.getHead().reverse()) {
130  if (invObject == gameObject) {
131  selListCounter = updateIndex;
132  }
133  addElement(invObject);
134 
135  final int tmpListCounter = addInvObjects(invObject, gameObject);
136  if (tmpListCounter != -1) {
137  selListCounter = tmpListCounter;
138  }
139  }
140  return selListCounter;
141  }
142 
147  private void addElement(final G gameObject) {
148  if (updateIndex < model.size()) {
149  if (model.get(updateIndex) != gameObject) {
150  model.set(updateIndex, gameObject);
151  }
152  } else {
153  model.addElement(gameObject);
154  }
155  updateIndex++;
156  }
157 
158 }
net.sf.gridarta.gui.panel.selectedsquare.ModelUpdater.updateIndex
int updateIndex
The next index to update.
Definition: ModelUpdater.java:56
net.sf.gridarta
Base package of all Gridarta classes.
net.sf.gridarta.model.mapviewsettings
Definition: AbstractMapViewSettings.java:20
net.sf
net.sf.gridarta.model.archetype
Definition: AbstractArchetype.java:20
net.sf.gridarta.model.gameobject.GameObject
Reflects a game object (object on a map).
Definition: GameObject.java:36
net.sf.gridarta.model.mapviewsettings.MapViewSettings
Container for settings that affect the rendering of maps.
Definition: MapViewSettings.java:30
net.sf.gridarta.gui.panel.selectedsquare.ModelUpdater.update
int update(@Nullable final GameObjectContainer< G, A, R > mapSquare, @Nullable final G gameObject)
Updates the model to reflect a net.sf.gridarta.model.mapmodel.MapSquare.
Definition: ModelUpdater.java:74
net.sf.gridarta.model.gameobject
GameObjects are the objects based on Archetypes found on maps.
Definition: AbstractGameObject.java:20
net
net.sf.gridarta.model.maparchobject.MapArchObject
Interface for MapArchObjects.
Definition: MapArchObject.java:40
net.sf.gridarta.model.mapviewsettings.MapViewSettings.isEditType
boolean isEditType(int editType)
Get information on the current state of edit type.
net.sf.gridarta.gui.panel.selectedsquare.ModelUpdater.addElement
void addElement(final G gameObject)
Adds one GameObject to model.
Definition: ModelUpdater.java:147
net.sf.gridarta.gui.panel.selectedsquare.ModelUpdater.model
final DefaultListModel< G > model
The DefaultListModel to update.
Definition: ModelUpdater.java:45
net.sf.gridarta.gui.panel.selectedsquare.ModelUpdater.addInvObjects
int addInvObjects(@NotNull final G node, @Nullable final G gameObject)
Add inventory objects to an arch in the SelectedSquareView recursively.
Definition: ModelUpdater.java:127
net.sf.gridarta.model
net.sf.gridarta.model.archetype.Archetype
Reflects an Archetype.
Definition: Archetype.java:41
net.sf.gridarta.gui.panel.selectedsquare.ModelUpdater.ModelUpdater
ModelUpdater(@NotNull final DefaultListModel< G > model, @NotNull final MapViewSettings mapViewSettings)
Creates a new instance.
Definition: ModelUpdater.java:63
net.sf.gridarta.model.baseobject
Definition: AbstractBaseObject.java:20
net.sf.gridarta.model.maparchobject
Definition: AbstractMapArchObject.java:20
net.sf.gridarta.model.baseobject.GameObjectContainer
Base class for classes that contain GameObjects as children in the sense of containment.
Definition: GameObjectContainer.java:50
net.sf.gridarta.gui.panel.selectedsquare.ModelUpdater
Updates a DefaultListModel instance to reflect the contents of a net.sf.gridarta.model....
Definition: ModelUpdater.java:39
net.sf.gridarta.gui.panel.selectedsquare.ModelUpdater.mapViewSettings
final MapViewSettings mapViewSettings
The MapViewSettings to use.
Definition: ModelUpdater.java:51
net.sf.gridarta.model.mapviewsettings.MapViewSettings.isEditTypeSet
boolean isEditTypeSet()
Returns whether a editType value is set so that not all squares are displayed.