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-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.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 ? typeName : "<invalid type>";
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[] getEventNames() {
126  return eventNames.toArray(new String[eventNames.size()]);
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  // noinspection UseOfObsoleteCollectionType
133  final Vector<String> content = new Vector<>();
134  for (final BaseObject<?, ?, ?, ?> tmp : gameObject) {
135  if (tmp.getTypeNo() == eventTypeNo) {
136  content.add(" " + typeName(tmp.getAttributeInt(subtypeAttribute)));
137  }
138  }
139 
140  list.setListData(content);
141  list.setSelectedIndex(0);
142  }
143 
144 }
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...
String getEventType(final int eventType)
Returns the type name for an event type.
Package with common types for event archetypes.
Stores a pair of values.
Definition: Pair.java:26
int indexToEventType(final int index)
Converts a combo box index to an event type.
AbstractScriptArchUtils(@NotNull final Iterable< Pair< Integer, String >> eventTypes, @NotNull final String subtypeAttribute, final int eventTypeNo)
Creates a new instance.
Base package of all Gridarta classes.
Reflects a game object (object on a map).
Definition: GameObject.java:36
final Map< Integer, Integer > indexToEventType
Maps index into eventNames to event type.
GameObjects are the objects based on Archetypes found on maps.
final int eventTypeNo
The object type for event objects.
final Map< Integer, String > allEventTypes
Maps event type to event name.
String typeName(final int eventType)
Returns a human readable name for an event type.
void add(final int eventType, @NotNull final String eventName)
Adds on event description.
final String subtypeAttribute
The attribute name for the subtype field.