Gridarta Editor
PluginModel.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.plugin;
21 
22 import java.io.File;
23 import java.io.FileOutputStream;
24 import java.io.IOException;
25 import java.util.Collections;
26 import java.util.Iterator;
27 import java.util.Map;
28 import java.util.TreeMap;
34 import nu.xom.Document;
35 import nu.xom.Element;
36 import nu.xom.ParsingException;
37 import nu.xom.Serializer;
38 import org.apache.log4j.Category;
39 import org.apache.log4j.Logger;
40 import org.jetbrains.annotations.NotNull;
41 import org.jetbrains.annotations.Nullable;
42 import org.xml.sax.SAXException;
43 
48 public class PluginModel<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> implements Iterable<Plugin<G, A, R>> {
49 
53  @NotNull
54  private static final Category LOG = Logger.getLogger(PluginModel.class);
55 
56  @NotNull
58 
59  @NotNull
61 
62  @NotNull
63  private final Map<String, Plugin<G, A, R>> plugins = new TreeMap<>();
64 
69  @NotNull
71 
72  public PluginModel(@NotNull final PluginParameterFactory<G, A, R> pluginParameterFactory, @NotNull final PluginModelParser<G, A, R> pluginModelParser) {
73  this.pluginParameterFactory = pluginParameterFactory;
74  this.pluginModelParser = pluginModelParser;
75  }
76 
77  @Nullable
78  public Plugin<G, A, R> getPlugin(@NotNull final String name) {
79  return plugins.get(name);
80  }
81 
82  public int getPluginCount() {
83  return plugins.size();
84  }
85 
86  @Nullable
87  public Plugin<G, A, R> getPlugin(final int index) {
88  final Iterator<Plugin<G, A, R>> it = plugins.values().iterator();
89  Plugin<G, A, R> m = null;
90  int i = index;
91  while (it.hasNext() && i-- >= 0) {
92  m = it.next();
93  }
94  return i >= 0 ? null : m;
95  }
96 
103  public boolean addPlugin(@NotNull final Plugin<G, A, R> plugin) {
104  if (plugins.containsKey(plugin.getName())) {
105  return false;
106  }
107 
108  plugins.put(plugin.getName(), plugin);
109  firePluginCreatedEvent(plugin);
111  return true;
112  }
113 
114  public void addPlugin(@NotNull final File file) {
115  try {
116  addPlugin(PluginModelLoader.loadXML(pluginModelParser, file));
117  } catch (final IOException ex) {
118  LOG.warn("can't load plugin: " + ex.getMessage());
119  } catch (final ParsingException ex) {
120  LOG.warn("can't parse plugin: " + ex.getMessage());
121  } catch (final SAXException ex) {
122  LOG.warn("can't parse plugin: " + ex.getMessage());
123  }
124  }
125 
126  public void removePlugin(@NotNull final Plugin<G, A, R> plugin) {
127  if (!plugins.containsKey(plugin.getName())) {
128  throw new IllegalArgumentException();
129  }
130 
132  firePluginDeletedEvent(plugin);
133  plugins.remove(plugin.getName());
134  }
135 
142  public void savePlugin(@NotNull final Plugin<G, A, R> plugin, @NotNull final File file) throws IOException {
143  try (FileOutputStream fos = new FileOutputStream(file)) {
144  final Element root = pluginModelParser.toXML(plugin);
145  final Document d = new Document(root);
146  final Serializer serializer = new Serializer(fos, "UTF-8");
147  serializer.setIndent(2);
148  serializer.write(d);
149  }
150  }
151 
156  public void addPluginModelListener(@NotNull final PluginModelListener<G, A, R> listener) {
157  listeners.add(listener);
158  }
159 
164  public void removePluginModelListener(@NotNull final PluginModelListener<G, A, R> listener) {
165  listeners.remove(listener);
166  }
167 
172  private void firePluginCreatedEvent(@NotNull final Plugin<G, A, R> plugin) {
173  for (final PluginModelListener<G, A, R> listener : listeners.getListeners()) {
174  listener.pluginCreated(plugin);
175  }
176  }
177 
182  private void firePluginDeletedEvent(@NotNull final Plugin<G, A, R> plugin) {
183  for (final PluginModelListener<G, A, R> listener : listeners.getListeners()) {
184  listener.pluginDeleted(plugin);
185  }
186  }
187 
192  private void firePluginRegisteredEvent(@NotNull final Plugin<G, A, R> plugin) {
193  for (final PluginModelListener<G, A, R> listener : listeners.getListeners()) {
194  listener.pluginRegistered(plugin);
195  }
196  }
197 
202  private void firePluginUnregisteredEvent(@NotNull final Plugin<G, A, R> plugin) {
203  for (final PluginModelListener<G, A, R> listener : listeners.getListeners()) {
204  listener.pluginUnregistered(plugin);
205  }
206  }
207 
208  @NotNull
209  @Override
210  public Iterator<Plugin<G, A, R>> iterator() {
211  return Collections.unmodifiableCollection(plugins.values()).iterator();
212  }
213 
214  public void reRegister(@NotNull final Plugin<G, A, R> plugin) {
217  }
218 
219  @Nullable
220  public Plugin<G, A, R> newPlugin(@NotNull final String name, @NotNull final String code) {
221  final Plugin<G, A, R> plugin = new Plugin<>(name, pluginParameterFactory);
222  plugin.setCode(code);
223  return addPlugin(plugin) ? plugin : null;
224  }
225 
226 }
PluginModel(@NotNull final PluginParameterFactory< G, A, R > pluginParameterFactory, @NotNull final PluginModelParser< G, A, R > pluginModelParser)
void firePluginDeletedEvent(@NotNull final Plugin< G, A, R > plugin)
Notifies all listeners about a removed plugin.
final Map< String, Plugin< G, A, R > > plugins
void firePluginUnregisteredEvent(@NotNull final Plugin< G, A, R > plugin)
Notifies all listeners about an unregistered plugin.
T [] getListeners()
Returns an array of all the listeners.
Iterator< Plugin< G, A, R > > iterator()
Plugin< G, A, R > getPlugin(@NotNull final String name)
void removePlugin(@NotNull final Plugin< G, A, R > plugin)
Utility class for loading plugins.
static final Category LOG
The Logger for printing log messages.
final EventListenerList2< PluginModelListener< G, A, R > > listeners
The PluginModelListeners to inform of changes.
Listener interface for scripting related events.
void firePluginRegisteredEvent(@NotNull final Plugin< G, A, R > plugin)
Notifies all listeners about a registered plugin.
final PluginParameterFactory< G, A, R > pluginParameterFactory
void addPlugin(@NotNull final File file)
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.
void addPluginModelListener(@NotNull final PluginModelListener< G, A, R > listener)
Adds a listener to be informed of changes.
void savePlugin(@NotNull final Plugin< G, A, R > plugin, @NotNull final File file)
Saves a plugin to a given file.
Type-safe version of EventListenerList.
Iterator< PluginParameter< G, A, R, ?> > iterator()
Definition: Plugin.java:432
Plugin< G, A, R > newPlugin(@NotNull final String name, @NotNull final String code)
boolean addPlugin(@NotNull final Plugin< G, A, R > plugin)
Add a new plugin.
void removePluginModelListener(@NotNull final PluginModelListener< G, A, R > listener)
Removes a listener to be informed of changes.
void reRegister(@NotNull final Plugin< G, A, R > plugin)
Plugin< G, A, R > getPlugin(final int index)
final PluginModelParser< G, A, R > pluginModelParser
void setCode(@NotNull final String code)
Sets the executable code of this plugin.
Definition: Plugin.java:164
Element toXML(@NotNull final Plugin< G, A, R > plugin)
Converts a Plugin instance to XML representation.
void firePluginCreatedEvent(@NotNull final Plugin< G, A, R > plugin)
Notify all listeners about an added plugin.