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-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.plugin;
21 
22 import java.io.File;
23 import java.io.FileOutputStream;
24 import java.io.IOException;
25 import java.util.Collection;
26 import java.util.Collections;
27 import java.util.Iterator;
28 import java.util.Map;
29 import java.util.TreeMap;
30 import java.util.concurrent.CopyOnWriteArrayList;
35 import nu.xom.Document;
36 import nu.xom.Element;
37 import nu.xom.ParsingException;
38 import nu.xom.Serializer;
39 import org.apache.log4j.Category;
40 import org.apache.log4j.Logger;
41 import org.jetbrains.annotations.NotNull;
42 import org.jetbrains.annotations.Nullable;
43 import org.xml.sax.SAXException;
44 
49 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>> {
50 
54  @NotNull
55  private static final Category LOG = Logger.getLogger(PluginModel.class);
56 
57  @NotNull
59 
60  @NotNull
62 
63  @NotNull
64  private final Map<String, Plugin<G, A, R>> plugins = new TreeMap<>();
65 
70  @NotNull
71  private final Collection<PluginModelListener<G, A, R>> listeners = new CopyOnWriteArrayList<>();
72 
74  this.pluginParameterFactory = pluginParameterFactory;
75  this.pluginModelParser = pluginModelParser;
76  }
77 
78  @Nullable
79  public Plugin<G, A, R> getPlugin(@NotNull final String name) {
80  return plugins.get(name);
81  }
82 
83  public int getPluginCount() {
84  return plugins.size();
85  }
86 
87  @Nullable
88  public Plugin<G, A, R> getPlugin(final int index) {
89  final Iterator<Plugin<G, A, R>> it = plugins.values().iterator();
90  Plugin<G, A, R> m = null;
91  int i = index;
92  while (it.hasNext() && i-- >= 0) {
93  m = it.next();
94  }
95  return i >= 0 ? null : m;
96  }
97 
104  public boolean addPlugin(@NotNull final Plugin<G, A, R> plugin) {
105  if (plugins.containsKey(plugin.getName())) {
106  return false;
107  }
108 
109  plugins.put(plugin.getName(), plugin);
110  firePluginCreatedEvent(plugin);
112  return true;
113  }
114 
115  public void addPlugin(@NotNull final File file) {
116  try {
118  } catch (final IOException ex) {
119  LOG.warn("can't load plugin: " + ex.getMessage());
120  } catch (final ParsingException | SAXException ex) {
121  LOG.warn("can't parse plugin: " + ex.getMessage());
122  }
123  }
124 
125  public void removePlugin(@NotNull final Plugin<G, A, R> plugin) {
126  if (!plugins.containsKey(plugin.getName())) {
127  throw new IllegalArgumentException("plugin does not exist: " + plugin.getName());
128  }
129 
131  firePluginDeletedEvent(plugin);
132  plugins.remove(plugin.getName());
133  }
134 
141  public void savePlugin(@NotNull final Plugin<G, A, R> plugin, @NotNull final File file) throws IOException {
142  try (FileOutputStream fos = new FileOutputStream(file)) {
143  final Element root = pluginModelParser.toXML(plugin);
144  final Document d = new Document(root);
145  final Serializer serializer = new Serializer(fos, "UTF-8");
146  serializer.setIndent(2);
147  serializer.write(d);
148  }
149  }
150 
155  public void addPluginModelListener(@NotNull final PluginModelListener<G, A, R> listener) {
156  listeners.add(listener);
157  }
158 
163  public void removePluginModelListener(@NotNull final PluginModelListener<G, A, R> listener) {
164  listeners.remove(listener);
165  }
166 
171  private void firePluginCreatedEvent(@NotNull final Plugin<G, A, R> plugin) {
172  for (final PluginModelListener<G, A, R> listener : listeners) {
173  listener.pluginCreated(plugin);
174  }
175  }
176 
181  private void firePluginDeletedEvent(@NotNull final Plugin<G, A, R> plugin) {
182  for (final PluginModelListener<G, A, R> listener : listeners) {
183  listener.pluginDeleted(plugin);
184  }
185  }
186 
191  private void firePluginRegisteredEvent(@NotNull final Plugin<G, A, R> plugin) {
192  for (final PluginModelListener<G, A, R> listener : listeners) {
193  listener.pluginRegistered(plugin);
194  }
195  }
196 
201  private void firePluginUnregisteredEvent(@NotNull final Plugin<G, A, R> plugin) {
202  for (final PluginModelListener<G, A, R> listener : listeners) {
203  listener.pluginUnregistered(plugin);
204  }
205  }
206 
207  @NotNull
208  @Override
209  public Iterator<Plugin<G, A, R>> iterator() {
210  return Collections.unmodifiableCollection(plugins.values()).iterator();
211  }
212 
213  public void reRegister(@NotNull final Plugin<G, A, R> plugin) {
216  }
217 
218  @Nullable
219  public Plugin<G, A, R> newPlugin(@NotNull final String name, @NotNull final String code) {
221  plugin.setCode(code);
222  return addPlugin(plugin) ? plugin : null;
223  }
224 
225 }
name
name
Definition: ArchetypeTypeSetParserTest-ignoreDefaultAttribute1-result.txt:2
net.sf.gridarta.plugin
Definition: BshRunnable.java:20
net.sf.gridarta.plugin.PluginModelParser.toXML
Element toXML(@NotNull final Plugin< G, A, R > plugin)
Converts a Plugin instance to XML representation.
Definition: PluginModelParser.java:135
net.sf.gridarta.plugin.PluginModel.newPlugin
Plugin< G, A, R > newPlugin(@NotNull final String name, @NotNull final String code)
Definition: PluginModel.java:219
net.sf.gridarta
Base package of all Gridarta classes.
net.sf.gridarta.plugin.PluginModel.addPluginModelListener
void addPluginModelListener(@NotNull final PluginModelListener< G, A, R > listener)
Adds a listener to be informed of changes.
Definition: PluginModel.java:155
net.sf.gridarta.plugin.PluginModel.removePlugin
void removePlugin(@NotNull final Plugin< G, A, R > plugin)
Definition: PluginModel.java:125
net.sf.gridarta.plugin.PluginModel.removePluginModelListener
void removePluginModelListener(@NotNull final PluginModelListener< G, A, R > listener)
Removes a listener to be informed of changes.
Definition: PluginModel.java:163
net.sf.gridarta.plugin.PluginModelListener< G, A, R >
net.sf.gridarta.plugin.PluginModelLoader
Utility class for loading plugins.
Definition: PluginModelLoader.java:47
net.sf.gridarta.plugin.PluginModelLoader.loadXML
static< G extends GameObject< G, A, R > A extends R extends Archetype< G, A, R > Plugin< G, A, R > loadXML(@NotNull final PluginModelParser< G, A, R > pluginModelParser, @NotNull final File file)
Definition: PluginModelLoader.java:101
net.sf.gridarta.plugin.PluginModel.addPlugin
boolean addPlugin(@NotNull final Plugin< G, A, R > plugin)
Add a new plugin.
Definition: PluginModel.java:104
net.sf
net.sf.gridarta.plugin.PluginModelParser< G, A, R >
net.sf.gridarta.plugin.PluginModel.listeners
final Collection< PluginModelListener< G, A, R > > listeners
The PluginModelListeners to inform of changes.
Definition: PluginModel.java:71
net.sf.gridarta.plugin.PluginModel.LOG
static final Category LOG
The Logger for printing log messages.
Definition: PluginModel.java:55
net.sf.gridarta.plugin.Plugin.setCode
void setCode(@NotNull final String code)
Sets the executable code of this plugin.
Definition: Plugin.java:165
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.plugin.PluginModel.iterator
Iterator< Plugin< G, A, R > > iterator()
Definition: PluginModel.java:209
net.sf.gridarta.plugin.PluginModel.firePluginUnregisteredEvent
void firePluginUnregisteredEvent(@NotNull final Plugin< G, A, R > plugin)
Notifies all listeners about an unregistered plugin.
Definition: PluginModel.java:201
net.sf.gridarta.model.gameobject
GameObjects are the objects based on Archetypes found on maps.
Definition: AbstractGameObject.java:20
net
net.sf.gridarta.plugin.PluginModel.plugins
final Map< String, Plugin< G, A, R > > plugins
Definition: PluginModel.java:64
net.sf.gridarta.plugin.PluginModel.pluginModelParser
final PluginModelParser< G, A, R > pluginModelParser
Definition: PluginModel.java:61
net.sf.gridarta.plugin.PluginModel.reRegister
void reRegister(@NotNull final Plugin< G, A, R > plugin)
Definition: PluginModel.java:213
net.sf.gridarta.model.maparchobject.MapArchObject
Interface for MapArchObjects.
Definition: MapArchObject.java:40
net.sf.gridarta.plugin.PluginModel.getPlugin
Plugin< G, A, R > getPlugin(@NotNull final String name)
Definition: PluginModel.java:79
net.sf.gridarta.plugin.PluginModel.savePlugin
void savePlugin(@NotNull final Plugin< G, A, R > plugin, @NotNull final File file)
Saves a plugin to a given file.
Definition: PluginModel.java:141
net.sf.gridarta.plugin.PluginModel.PluginModel
PluginModel(@NotNull final PluginParameterFactory< G, A, R > pluginParameterFactory, @NotNull final PluginModelParser< G, A, R > pluginModelParser)
Definition: PluginModel.java:73
net.sf.gridarta.plugin.PluginModel.getPlugin
Plugin< G, A, R > getPlugin(final int index)
Definition: PluginModel.java:88
net.sf.gridarta.plugin.PluginModel.addPlugin
void addPlugin(@NotNull final File file)
Definition: PluginModel.java:115
net.sf.gridarta.plugin.PluginModel.firePluginCreatedEvent
void firePluginCreatedEvent(@NotNull final Plugin< G, A, R > plugin)
Notify all listeners about an added plugin.
Definition: PluginModel.java:171
net.sf.gridarta.model
net.sf.gridarta.model.archetype.Archetype
Reflects an Archetype.
Definition: Archetype.java:41
net.sf.gridarta.plugin.Plugin< G, A, R >
net.sf.gridarta.plugin.PluginModel.pluginParameterFactory
final PluginParameterFactory< G, A, R > pluginParameterFactory
Definition: PluginModel.java:58
net.sf.gridarta.plugin.PluginModel.firePluginRegisteredEvent
void firePluginRegisteredEvent(@NotNull final Plugin< G, A, R > plugin)
Notifies all listeners about a registered plugin.
Definition: PluginModel.java:191
net.sf.gridarta.plugin.parameter.PluginParameterFactory
Factory for Plugin Parameters.
Definition: PluginParameterFactory.java:37
net.sf.gridarta.model.maparchobject
Definition: AbstractMapArchObject.java:20
net.sf.gridarta.plugin.PluginModel.firePluginDeletedEvent
void firePluginDeletedEvent(@NotNull final Plugin< G, A, R > plugin)
Notifies all listeners about a removed plugin.
Definition: PluginModel.java:181
net.sf.gridarta.plugin.parameter
Definition: AbstractPathParameter.java:20
net.sf.gridarta.plugin.PluginModel.getPluginCount
int getPluginCount()
Definition: PluginModel.java:83
it
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 before considering any fundamental changes About code DO NOT USE TABS No matter what Java development platform you are please configure insert indent Tabs are displayed totally different in every editor and there are millions of different editors out there The insertion of tabs in the source code is messing up the syntax formatting in a way that is UNREPAIRABLE Apart from please keep code indentation accurate This is not just good it helps to keep code readable and in that way dramatically decreases the chance for overlooked bugs Everyone is welcomed to correct indentation errors wherever they are spotted Before you start to do this please double check that your editor is really configured to insert spaces Line feeds may be checked in either in windows or in unix linux style All reasonable text and java editors can deal with both linefeed formats Converting line feeds is but in this case please make sure that only linefeed characters are changed and nothing else is affected Due to the platform independent nature of the editor has the potential to run on almost any given operating system the build process differs greatly between systems as well as java environments In the several people have attempted to add build scripts along with structural changes to optimize the setup on one particular system environment which has led to conflict Please do *not *attempt to change the structure or any directories for the mere purpose of improving a build process or performance in a java environment Build scripts may be placed in the root it would be especially fine if it is just one or two files but the latter is not required Please excuse me for placing such restriction I and many users of the editor greatly appreciate build scripts We just had some real troubles over this issue in the past and I don t want to have them repeated the editor has relatively high performance requirements I ve spent a lot of extra work to keep everything as fast and memory efficient as possible when you add new data fields or calculations in the archetype please make sure they are as efficient as possible and worth both the time and space they consume Now don t be afraid too much No development would be possible without adding calculations and data at all Just bear in mind unlike for many other open source performance does make a difference for the CrossfireEditor The for as many systems as possible In case you are unexperienced with java and note that the graphics look different on every and with every font They also have different sizes proportions and behave different A seemingly trivial and effectless change can wreck havoc for the same GUI run on another system please don t be totally afraid of it
Definition: Developer_README.txt:76
net.sf.gridarta.plugin.PluginModel
Definition: PluginModel.java:49