Gridarta Editor
AbstractScriptArchUtils.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.scripts;
21 
22 import java.util.ArrayList;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Vector;
27 import javax.swing.JList;
31 import net.sf.gridarta.utils.Pair;
32 import org.jetbrains.annotations.NotNull;
33 import org.jetbrains.annotations.Nullable;
34 
35 public abstract class AbstractScriptArchUtils implements ScriptArchUtils {
36 
41  @NotNull
42  private final String subtypeAttribute;
43 
48  private final int eventTypeNo;
49 
53  @NotNull
54  private final Map<Integer, String> allEventTypes = new HashMap<>();
55 
59  @NotNull
60  private final Map<Integer, Integer> indexToEventType = new HashMap<>();
61 
65  @NotNull
66  private final List<String> eventNames = new ArrayList<>();
67 
74  protected AbstractScriptArchUtils(@NotNull final Iterable<Pair<Integer, String>> eventTypes, @NotNull final String subtypeAttribute, final int eventTypeNo) {
75  this.subtypeAttribute = subtypeAttribute;
76  this.eventTypeNo = eventTypeNo;
77  for (final Pair<Integer, String> pair : eventTypes) {
78  final int eventType = pair.getFirst();
79  final String eventName = pair.getSecond();
80  add(eventType, eventName);
81  }
82  }
83 
89  private void add(final int eventType, @NotNull final String eventName) {
90  assert !allEventTypes.containsKey(eventType);
91  allEventTypes.put(eventType, eventName);
92  indexToEventType.put(eventNames.size(), eventType);
93  eventNames.add(eventName);
94  }
95 
96  @NotNull
97  @Override
98  public String typeName(final int eventType) {
99  final String typeName = allEventTypes.get(eventType);
100  return typeName == null ? "<invalid type>" : typeName;
101  }
102 
108  @Nullable
109  protected String getEventType(final int eventType) {
110  return allEventTypes.get(eventType);
111  }
112 
113  @Override
114  public int indexToEventType(final int index) {
115  //noinspection ProhibitedExceptionCaught
116  try {
117  return indexToEventType.get(index);
118  } catch (final NullPointerException ignored) {
119  return 0;
120  }
121  }
122 
123  @NotNull
124  @Override
125  public String @NotNull [] getEventNames() {
126  return eventNames.toArray(new String[0]);
127  }
128 
129  @Override
130  public void addEventsToJList(@NotNull final JList<String> list, @NotNull final Iterable<? extends GameObject<?, ?, ?>> gameObject) {
131  //cher: JList expects Vector, so we MUST use an obsolete concrete collection.
132  final Vector<String> content = new Vector<>();
133  for (final BaseObject<?, ?, ?, ?> tmp : gameObject) {
134  if (tmp.getTypeNo() == eventTypeNo) {
135  content.add(" " + typeName(tmp.getAttributeInt(subtypeAttribute)));
136  }
137  }
138 
139  list.setListData(content);
140  list.setSelectedIndex(0);
141  }
142 
143 }
net.sf.gridarta.gui.scripts.AbstractScriptArchUtils.allEventTypes
final Map< Integer, String > allEventTypes
Maps event type to event name.
Definition: AbstractScriptArchUtils.java:54
net.sf.gridarta
Base package of all Gridarta classes.
net.sf.gridarta.gui.scripts.AbstractScriptArchUtils.addEventsToJList
void addEventsToJList(@NotNull final JList< String > list, @NotNull final Iterable<? extends GameObject<?, ?, ?>> gameObject)
Set all ScriptedEvents to appear in the given JList This method should be fast because it may be exec...
Definition: AbstractScriptArchUtils.java:130
net.sf
net.sf.gridarta.model.scripts
Common types for event archetypes.
Definition: AbstractScriptedEvent.java:20
net.sf.gridarta.gui.scripts.AbstractScriptArchUtils.subtypeAttribute
final String subtypeAttribute
The attribute name for the subtype field.
Definition: AbstractScriptArchUtils.java:42
net.sf.gridarta.model.gameobject.GameObject
Reflects a game object (object on a map).
Definition: GameObject.java:36
net.sf.gridarta.model.scripts.ScriptArchUtils
Definition: ScriptArchUtils.java:26
net.sf.gridarta.gui.scripts.AbstractScriptArchUtils.indexToEventType
final Map< Integer, Integer > indexToEventType
Maps index into eventNames to event type.
Definition: AbstractScriptArchUtils.java:60
net.sf.gridarta.gui.scripts.AbstractScriptArchUtils.getEventType
String getEventType(final int eventType)
Returns the type name for an event type.
Definition: AbstractScriptArchUtils.java:109
net.sf.gridarta.gui.scripts.AbstractScriptArchUtils.getEventNames
String[] getEventNames()
Returns all event names.
Definition: AbstractScriptArchUtils.java:125
net.sf.gridarta.gui.scripts.AbstractScriptArchUtils.eventNames
final List< String > eventNames
All event names.
Definition: AbstractScriptArchUtils.java:66
net.sf.gridarta.model.gameobject
GameObjects are the objects based on Archetypes found on maps.
Definition: AbstractGameObject.java:20
net
net.sf.gridarta.gui.scripts.AbstractScriptArchUtils.indexToEventType
int indexToEventType(final int index)
Converts a combo box index to an event type.
Definition: AbstractScriptArchUtils.java:114
net.sf.gridarta.utils.Pair
Stores a pair of values.
Definition: Pair.java:26
list
This document describes some hints and requirements for general development on the CrossfireEditor If you plan to make changes to the editor code or setup please read the following and keep it in derived from a basic editor application called Gridder by Pasi Ker�nen so please communicate with best through the cf devel mailing list
Definition: Developer_README.txt:13
net.sf.gridarta.gui.scripts.AbstractScriptArchUtils.AbstractScriptArchUtils
AbstractScriptArchUtils(@NotNull final Iterable< Pair< Integer, String >> eventTypes, @NotNull final String subtypeAttribute, final int eventTypeNo)
Creates a new instance.
Definition: AbstractScriptArchUtils.java:74
net.sf.gridarta.model.baseobject.BaseObject
Definition: BaseObject.java:34
net.sf.gridarta.gui.scripts.AbstractScriptArchUtils.add
void add(final int eventType, @NotNull final String eventName)
Adds on event description.
Definition: AbstractScriptArchUtils.java:89
net.sf.gridarta.gui.scripts.AbstractScriptArchUtils.typeName
String typeName(final int eventType)
Returns a human readable name for an event type.
Definition: AbstractScriptArchUtils.java:98
net.sf.gridarta.model
net.sf.gridarta.model.baseobject
Definition: AbstractBaseObject.java:20
net.sf.gridarta.gui.scripts.AbstractScriptArchUtils
Definition: AbstractScriptArchUtils.java:35
net.sf.gridarta.var.crossfire.model.gameobject.GameObject<?, ?, ?>
net.sf.gridarta.gui.scripts.AbstractScriptArchUtils.eventTypeNo
final int eventTypeNo
The object type for event objects.
Definition: AbstractScriptArchUtils.java:48
net.sf.gridarta.utils
Definition: ActionBuilderUtils.java:20