Gridarta Editor
PluginController.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.dialog.plugin;
21 
22 import java.awt.Component;
23 import java.io.File;
24 import java.io.IOException;
25 import java.util.Map;
26 import javax.swing.Action;
27 import javax.swing.JFileChooser;
28 import javax.swing.JOptionPane;
35 import net.sf.gridarta.plugin.Plugin;
44 import net.sf.japi.swing.action.ActionBuilder;
45 import net.sf.japi.swing.action.ActionBuilderFactory;
46 import net.sf.japi.swing.action.ActionMethod;
47 import net.sf.japi.swing.action.ReflectionAction;
48 import org.jetbrains.annotations.NotNull;
49 
55 public class PluginController<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> implements EditorAction {
56 
60  @NotNull
61  private static final ActionBuilder ACTION_BUILDER = ActionBuilderFactory.getInstance().getActionBuilder("net.sf.gridarta");
62 
63  @NotNull
65 
66  @NotNull
68 
69  @NotNull
70  private final PluginView<G, A, R> view;
71 
75  @NotNull
76  private final Component parent;
77 
78  @NotNull
80 
84  @NotNull
85  private File pluginsDir;
86 
90  @NotNull
92 
93  @Override
94  public void pluginCreated(@NotNull final Plugin<G, A, R> plugin) {
95  // ignore
96  }
97 
98  @Override
99  public void pluginDeleted(@NotNull final Plugin<G, A, R> plugin) {
100  // ignore
101  }
102 
103  @Override
104  public void pluginRegistered(@NotNull final Plugin<G, A, R> plugin) {
105  final String filterName = "(s)" + plugin.getName();
106 
107  if (plugin.isFilter()) {
108  final Filter<?, ?> filter = plugin.getPluginAsFilter(pluginParameters);
109  if (filter != null) {
110  filterControl.addFilter(filterName, filter);
111  }
112  }
113 
114  if (plugin.isAutoBoot()) {
115  plugin.runPlugin(pluginParameters);
116  }
117  }
118 
119  @Override
120  public void pluginUnregistered(@NotNull final Plugin<G, A, R> plugin) {
121  final String filterName = "(s)" + plugin.getName();
122  filterControl.removeFilter(filterName);
123  }
124 
125  };
126 
130  public PluginController(@NotNull final FilterControl<G, A, R> filterControl, @NotNull final PluginModel<G, A, R> pluginModel, @NotNull final PluginParameters pluginParameters, @NotNull final Component parent, @NotNull final PluginParameterViewFactory<G, A, R> pluginParameterViewFactory, @NotNull final File pluginsDir, @NotNull final ResourceIcons resourceIcons) {
131  this.parent = parent;
132  this.filterControl = filterControl;
133  this.pluginModel = pluginModel;
134  this.pluginParameters = pluginParameters;
135  this.pluginsDir = pluginsDir;
136  view = new PluginView<>(this, pluginModel, pluginParameterViewFactory, resourceIcons);
137  pluginModel.addPluginModelListener(pluginModelListener);
138  }
139 
144  public boolean canExit() {
145  for (final Plugin<G, A, R> plugin : pluginModel) {
146  if (plugin.isModified()) {
147  final int result = ACTION_BUILDER.showConfirmDialog(parent, JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE, "pluginConfirmSaveChanges", plugin.getName());
148  if (result == JOptionPane.YES_OPTION) {
149  if (!savePlugin(plugin)) {
150  return false;
151  }
152  } else if (result == JOptionPane.CANCEL_OPTION || result == JOptionPane.CLOSED_OPTION) {
153  return false;
154  }
155  }
156  }
157 
158  return true;
159  }
160 
164  public void saveAllPlugins() {
165  for (final Plugin<G, A, R> plugin : pluginModel) {
166  if (!savePlugin(plugin)) {
167  return;
168  }
169  }
170  }
171 
178  private boolean savePlugin(@NotNull final Plugin<G, A, R> plugin) {
179  if (!plugin.isModified()) {
180  return true;
181  }
182 
183  try {
184  final File file = plugin.getFile();
185  if (file != null) {
186  pluginModel.savePlugin(plugin, file);
187  } else {
188  if (!savePluginAs(plugin, true)) {
189  return false;
190  }
191  }
192  } catch (final IOException ex) {
193  // XXX: notify user
194  return false;
195  }
196 
197  plugin.resetModified();
198  return true;
199  }
200 
209  public boolean savePluginAs(@NotNull final Plugin<G, A, R> plugin, final boolean updatePluginFile) {
210  final JFileChooser chooser = new JFileChooser();
211  final File pluginFile = plugin.getFile();
212  FileChooserUtils.setCurrentDirectory(chooser, pluginFile != null ? pluginFile : pluginsDir);
213  chooser.setDialogTitle("save plugin " + plugin.getName());
214  final int result = chooser.showSaveDialog(null);
215  if (result != JFileChooser.APPROVE_OPTION) {
216  return false;
217  }
218 
219  final File file = chooser.getSelectedFile();
220  if (updatePluginFile) {
221  plugin.setFile(file);
222  }
223  pluginsDir = file.getParentFile();
224 
225  try {
226  pluginModel.savePlugin(plugin, file);
227  } catch (final IOException ex) {
228  return false;
229  }
230 
231  return true;
232  }
233 
234  public void runPlugin(@NotNull final Plugin<G, A, R> plugin) {
235  final Plugin<G, A, R> clonedPlugin = plugin.clonePlugin();
236 
237  final Map<String, Object> runValues = view.getRunValues(clonedPlugin, parent);
238  if (runValues == null) {
239  return;
240  }
241 
242  new PluginExecutor<>(pluginModel, pluginParameters).doRunPlugin(clonedPlugin, view.createConsole(clonedPlugin.getName()), runValues); // XXX: drops thread
243  }
244 
245  @ActionMethod
246  public void runPlugin(@NotNull final String name) {
247  final Plugin<G, A, R> plugin = pluginModel.getPlugin(name);
248  if (plugin != null) {
249  runPlugin(plugin);
250  }
251  }
252 
253  @NotNull
255  return view;
256  }
257 
263  @NotNull
264  public Action createRunAction(@NotNull final Plugin<G, A, R> plugin) {
265  final Action action = ActionUtils.newAction(ACTION_BUILDER, "Plugin", this, "runPlugin");
266  action.putValue(ReflectionAction.REFLECTION_ARGUMENTS, new Object[] { plugin.getName(), });
267  action.putValue(Action.NAME, ACTION_BUILDER.format("runPlugin.text", plugin.getName()));
268  return action;
269  }
270 
271  @Override
272  public void setAction(@NotNull final Action action, @NotNull final String name) {
273  }
274 
275 }
Map< String, Object > getRunValues(@NotNull final Plugin< G, A, R > plugin, @NotNull final Component parent)
Asks the user for input parameter to run a plugin.
void saveAllPlugins()
Saves all modified plugins.
Graphical User Interface of Gridarta.
Interface for Filters.
Definition: Filter.java:33
Plugin< G, A, R > getPlugin(@NotNull final String name)
Utility class for JFileChooser related functions.
Listener interface for scripting related events.
Utility class implementing Action related functions.
final PluginModelListener< G, A, R > pluginModelListener
The PluginModelListener attached to pluginModel.
Allows execution of Plugins.
ConsoleInterface createConsole(@NotNull final String name)
Base package of all Gridarta classes.
static void setCurrentDirectory(@NotNull final JFileChooser fileChooser, @Nullable final File dir)
Calls JFileChooser#setCurrentDirectory(File).
void removeFilter(@NotNull String name)
Reflects a game object (object on a map).
Definition: GameObject.java:36
A global editor action.
boolean savePlugin(@NotNull final Plugin< G, A, R > plugin)
Saves one plugin.
String getName()
Returns the name of this plugin.
Definition: Plugin.java:147
Plugin< G, A, R > clonePlugin()
Returns a clone copy of this plugin.
Definition: Plugin.java:263
GameObjects are the objects based on Archetypes found on maps.
static final ActionBuilder ACTION_BUILDER
Action Builder.
The filter package contains the classes for Filters.
Definition: BtnPopup.java:20
void savePlugin(@NotNull final Plugin< G, A, R > plugin, @NotNull final File file)
Saves a plugin to a given file.
Action createRunAction(@NotNull final Plugin< G, A, R > plugin)
Creates an action to run a plugin plugin.
Makes basic Gridarta classes available to scripts.
void addFilter(@NotNull String name, @NotNull Filter<?, ?> filter)
Model for plugins.
Definition: Plugin.java:52
File pluginsDir
The default directory for saving plugins.
boolean canExit()
Prompts the user for all unsaved plugins.
void runPlugin(@NotNull final Plugin< G, A, R > plugin)
Creates ImageIcon instances from resources.
PluginController(@NotNull final FilterControl< G, A, R > filterControl, @NotNull final PluginModel< G, A, R > pluginModel, @NotNull final PluginParameters pluginParameters, @NotNull final Component parent, @NotNull final PluginParameterViewFactory< G, A, R > pluginParameterViewFactory, @NotNull final File pluginsDir, @NotNull final ResourceIcons resourceIcons)
Creates a new instance.
final Component parent
The parent component for dialog boxes.
boolean savePluginAs(@NotNull final Plugin< G, A, R > plugin, final boolean updatePluginFile)
Prompts the user for a file name to save a plugin.
void setAction(@NotNull final Action action, @NotNull final String name)
Sets the Action instance for this editor action.
static Action newAction(@NotNull final ActionBuilder actionBuilder, @NotNull final String category, @NotNull final EditorAction editorAction, @NotNull final String key)
Creates a new Action instance.