20 package net.sf.gridarta.maincontrol;
22 import gnu.getopt.Getopt;
23 import gnu.getopt.LongOpt;
24 import java.awt.GraphicsEnvironment;
26 import java.lang.reflect.InvocationTargetException;
27 import java.util.Arrays;
28 import java.util.List;
29 import java.util.Locale;
30 import java.util.MissingResourceException;
31 import java.util.ResourceBundle;
32 import java.util.prefs.Preferences;
33 import javax.swing.SwingUtilities;
34 import javax.swing.UIManager;
35 import javax.swing.UnsupportedLookAndFeelException;
66 import net.
sf.japi.swing.action.ActionBuilder;
67 import net.
sf.japi.swing.action.ActionBuilderFactory;
68 import org.apache.log4j.Category;
69 import org.apache.log4j.Logger;
70 import org.jetbrains.annotations.NotNull;
71 import org.jetbrains.annotations.Nullable;
80 @SuppressWarnings(
"UseOfSystemOutOrSystemErr")
94 System.setProperty(
"net.sf.japi.swing.tod", tipOfTheDayPackage);
96 if (LOG.isInfoEnabled()) {
97 LOG.info(
"build number: " + ResourceBundle.getBundle(
"build").getString(
"build.number"));
99 }
catch (
final MissingResourceException e) {
100 LOG.warn(
"No build number found:", e);
114 public void run(@NotNull
final String actionBuilderPackage, @NotNull
final String editorJarName, @NotNull
final EditorFactory<G, A, R> editorFactory, @Nullable
final String defaultConfig, @NotNull
final String... args) {
116 String plugin = null;
117 final LongOpt[] longOpts = {
new LongOpt(
"batchpng", LongOpt.NO_ARGUMENT, null, (
int)
'b'),
new LongOpt(
"normal", LongOpt.NO_ARGUMENT, null, (
int)
'n'),
new LongOpt(
"singlepng", LongOpt.NO_ARGUMENT, null, (
int)
's'),
new LongOpt(
"collectarches", LongOpt.NO_ARGUMENT, null, (
int)
'c'),
new LongOpt(
"collectArches", LongOpt.NO_ARGUMENT, null, (
int)
'c'),
new LongOpt(
"help", LongOpt.NO_ARGUMENT, null, (
int)
'h'),
new LongOpt(
"noexit", LongOpt.NO_ARGUMENT, null, 2),
new LongOpt(
"script", LongOpt.REQUIRED_ARGUMENT, null, 1),
new LongOpt(
"plugin", LongOpt.REQUIRED_ARGUMENT, null, 1),
new LongOpt(
"config", LongOpt.REQUIRED_ARGUMENT, null, 3), };
118 final Getopt g =
new Getopt(editorJarName, args,
"bchns", longOpts);
121 boolean doExit =
true;
123 final int ch = g.getopt();
138 usage(editorJarName, defaultConfig);
150 plugin = g.getOptarg();
158 config =
new File(g.getOptarg());
159 if (!config.exists()) {
160 System.err.println(config +
": configuration file does not exist");
175 if (config != null) {
176 System.setProperty(
"java.util.prefs.PreferencesFactory",
"net.sf.gridarta.preferences.FilePreferencesFactory");
181 final Preferences preferences = Preferences.userNodeForPackage(
MainControl.class);
183 if (locName != null) {
184 Locale.setDefault(
new Locale(locName));
187 final List<String> args2 = Arrays.asList(args).subList(g.getOptind(), args.length);
190 System.err.println(
"Running java version " + System.getProperty(
"java.version"));
192 final ActionBuilder actionBuilder = ActionBuilderFactory.getInstance().getActionBuilder(
"net.sf.gridarta");
194 actionBuilder.addParent(ActionBuilderFactory.getInstance().getActionBuilder(actionBuilderPackage));
201 final ProjectSettings projectSettings = editorFactory.newProjectSettings(editorSettings);
205 if (plugin != null) {
206 returnCode = runPlugin(plugin, errorView, args2, editorFactory, projectModel, resourceIcons, editorSettings);
211 returnCode = runNormal(args2, editorFactory, errorView, resourceIcons, configSourceFactory, projectModel, editorSettings);
219 if (args2.size() != 2) {
223 returnCode =
new SinglePngCommand(
new File(args2.get(0)),
new File(args2.get(1)),
ImageCreatorFactory.newImageCreator(resourceIcons, editorFactory, projectModel, editorSettings)).execute();
236 System.err.println(
"Usage: " + ex.getMessage());
242 System.exit(returnCode);
257 private int runPlugin(@NotNull
final String plugin,
final ErrorView errorView,
final Iterable<String> args, @NotNull
final EditorFactory<G, A, R> editorFactory, @NotNull
final ProjectModel<G, A, R> projectModel, @NotNull
final ResourceIcons resourceIcons, @NotNull
final EditorSettings editorSettings) {
258 checkForErrors(errorView);
259 waitDialog(errorView);
260 final NamedFilter defaultNamedFilterList =
new NamedFilter(projectModel.getGameObjectMatchers().getFilters());
263 final RendererFactory<G, A, R> rendererFactory = editorFactory.newRendererFactory(mapViewSettings, filterControl, projectModel.getGameObjectParser(), projectModel.getFaceObjectProviders(), resourceIcons, projectModel.getSmoothFaces());
264 return new PluginExecutor<>(projectModel.getPluginModel(), projectModel.newPluginParameters(rendererFactory)).executePlugin(plugin, args);
273 if (errorView.hasErrors()) {
274 waitDialog(errorView);
276 throw new AssertionError();
287 }
catch (
final InterruptedException ex) {
288 Thread.currentThread().interrupt();
290 throw new AssertionError(ex);
305 private int runNormal(@NotNull
final Iterable<String> args, @NotNull
final EditorFactory<G, A, R> editorFactory, @NotNull
final ErrorView errorView, @NotNull
final ResourceIcons resourceIcons, @NotNull
final ConfigSourceFactory configSourceFactory, @NotNull
final ProjectModel<G, A, R> projectModel, @NotNull
final EditorSettings editorSettings) {
307 for (
final UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
308 if (
"Nimbus".equals(info.getName())) {
309 UIManager.setLookAndFeel(info.getClassName());
313 }
catch (
final ClassNotFoundException ignored) {
314 }
catch (
final InstantiationException ignored) {
315 }
catch (
final IllegalAccessException ignored) {
316 }
catch (
final UnsupportedLookAndFeelException ignored) {
320 final Runnable runnable =
new Runnable() {
324 guiMainControl[0] =
new GUIMainControl<>(projectModel, editorSettings, errorView, resourceIcons, editorFactory, configSourceFactory);
329 SwingUtilities.invokeAndWait(runnable);
330 }
catch (
final InterruptedException ex) {
331 LOG.fatal(ex.getMessage(), ex);
332 Thread.currentThread().interrupt();
334 }
catch (
final InvocationTargetException ex) {
335 LOG.fatal(ex.getMessage(), ex);
339 checkForErrors(errorView);
340 waitDialog(errorView);
342 final Runnable runnable2 =
new Runnable() {
346 guiMainControl[0].
run(args);
351 SwingUtilities.invokeAndWait(runnable2);
352 }
catch (
final InterruptedException ex) {
353 LOG.fatal(ex.getMessage(), ex);
354 Thread.currentThread().interrupt();
356 }
catch (
final InvocationTargetException ex) {
357 LOG.fatal(ex.getMessage(), ex);
370 private static void usage(@NotNull
final String editorJarName, @Nullable
final String defaultConfig) {
371 System.out.println(
"usage: java -jar " + editorJarName +
" [option...] [map-file...]");
372 System.out.println(
"");
373 System.out.println(
" -h, --help print this help");
374 System.out.println(
" -b, --batchpng create PNG files for all given maps in their directories");
375 System.out.println(
" -c, --collectarches collect archetypes");
376 System.out.println(
" -n, --normal start editor with GUI (default)");
377 System.out.println(
" -s, --singlepng create a PNG file from the specified map");
378 System.out.println(
" --config=config-file use given config file; uses " + (defaultConfig == null ?
"Java preferences" :
"~/.gridarta/" + defaultConfig) +
" is not given");
379 System.out.println(
" --noexit do not call System.exit()");
380 System.out.println(
" --plugin=name [arg=value...]");
381 System.out.println(
" run a plugin with the given arguments");
COLLECT_ARCHES
Collect archetypes.
A Filter that aggregates named filters.
static final String PREFERENCES_LANGUAGE
Preferences key for language.
boolean isConsoleMode()
Returns whether this mode uses console-mode.
Graphical User Interface of Gridarta.
int runPlugin(@NotNull final String plugin, final ErrorView errorView, final Iterable< String > args, @NotNull final EditorFactory< G, A, R > editorFactory, @NotNull final ProjectModel< G, A, R > projectModel, @NotNull final ResourceIcons resourceIcons, @NotNull final EditorSettings editorSettings)
Executes a plugin.
Settings that apply to a project.
static File getHomeFile(@NotNull final String filename)
Return the filename to use when dealing with this application's and current users' home directory...
Default implementation of EditorSettings.
Allows execution of Plugins.
void run(@NotNull final String actionBuilderPackage, @NotNull final String editorJarName, @NotNull final EditorFactory< G, A, R > editorFactory, @Nullable final String defaultConfig, @NotNull final String... args)
Runs the editor.
GridartaEditor(@NotNull final String tipOfTheDayPackage)
Creates a new instance.
SINGLE_PNG
Single PNG: Create a PNG file from the specified map.
Default MapViewSettings implementation.
Interface for classes displaying error messages.
Base package of all Gridarta classes.
Main run mode of the editor.
Reflects a game object (object on a map).
static void checkForErrors(@NotNull final ErrorView errorView)
Checks whether a ErrorView instance contains at least one error.
Loader for loading resources the user's home directory.
A factory for creating ConfigSources.
This package contains the preferences ui modules.
Possible source locations for configuration files.
static void usage(@NotNull final String editorJarName, @Nullable final String defaultConfig)
Prints the editor's command-line options to System#out.
Container for settings that affect the rendering of maps.
GameObjects are the objects based on Archetypes found on maps.
Interface used as preferences location.
int runNormal(@NotNull final Iterable< String > args, @NotNull final EditorFactory< G, A, R > editorFactory, @NotNull final ErrorView errorView, @NotNull final ResourceIcons resourceIcons, @NotNull final ConfigSourceFactory configSourceFactory, @NotNull final ProjectModel< G, A, R > projectModel, @NotNull final EditorSettings editorSettings)
Run in normal mode.
The filter package contains the classes for Filters.
An ErrorView that reports all errors to the console.
String getConfigSourceName()
Returns the name of the configuration source.
Exception thrown for incorrect arguments.
int execute()
Executes the command.
MainControl is a central class that's used for access on global data structures / collections and glo...
static void callExit(final int returnCode)
Calls System#exit(int) or does nothing depending on the user's settings.
Base classes for rendering maps.
Creates the main GUI of Gridarta.
Utility class for creating ImageCreator instances.
Factory for creating MapRenderer instances.
A ConfigSourceFactory that is configured through action keys.
BATCH_PNG
Batch PNG: Create PNG files for all given maps in their directories.
Creates ImageIcon instances from resources.
ConfigSource getConfigSource(@NotNull String name)
Returns a ConfigSource by name.
Main class of the editor; parses command-line arguments, initializes and starts the editor...
void waitDialog()
Waits until the dialog has been dismissed.
static void initialize(@NotNull final String defaultName, @Nullable final File file)
Initialize the module.
void run(@NotNull final Iterable< String > args)
Starts the editor: makes the main window visible and opens map files.
NORMAL
Normal operation: Start editor with GUI.
Run archetype collection.
Settings that apply to the editor.
A PreferencesFactory which creates FilePreferences instances.
A dialog displaying a tree of error messages.
Interface for MapArchObjects.
static void waitDialog(final ErrorView errorView)
Waits until the error dialog has been dismissed.
Preferences Module for user interface preferences.