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.ArrayList;
28 import java.util.Arrays;
29 import java.util.List;
30 import java.util.Locale;
31 import java.util.MissingResourceException;
32 import java.util.ResourceBundle;
33 import java.util.prefs.Preferences;
34 import javax.swing.SwingUtilities;
35 import javax.swing.UIManager;
36 import javax.swing.UIManager.LookAndFeelInfo;
37 import javax.swing.UnsupportedLookAndFeelException;
68 import net.
sf.japi.swing.action.ActionBuilder;
69 import net.
sf.japi.swing.action.ActionBuilderFactory;
70 import org.apache.log4j.Category;
71 import org.apache.log4j.Logger;
72 import org.jetbrains.annotations.NotNull;
73 import org.jetbrains.annotations.Nullable;
82 @SuppressWarnings(
"UseOfSystemOutOrSystemErr")
96 System.setProperty(
"net.sf.japi.swing.tod", tipOfTheDayPackage);
98 if (LOG.isInfoEnabled()) {
99 LOG.info(
"build number: " + ResourceBundle.getBundle(
"build").getString(
"build.number"));
101 }
catch (
final MissingResourceException e) {
102 LOG.warn(
"No build number found:", e);
116 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) {
118 String plugin =
null;
119 final LongOpt[] longOpts = {
121 new LongOpt(
"batchpng", LongOpt.NO_ARGUMENT,
null,
'b'),
122 new LongOpt(
"normal", LongOpt.NO_ARGUMENT,
null,
'n'),
123 new LongOpt(
"singlepng", LongOpt.NO_ARGUMENT,
null,
's'),
124 new LongOpt(
"collectarches", LongOpt.NO_ARGUMENT,
null,
'c'),
125 new LongOpt(
"collectArches", LongOpt.NO_ARGUMENT,
null,
'c'),
126 new LongOpt(
"help", LongOpt.NO_ARGUMENT,
null,
'h'),
127 new LongOpt(
"noexit", LongOpt.NO_ARGUMENT,
null, 2),
128 new LongOpt(
"script", LongOpt.REQUIRED_ARGUMENT,
null, 1),
129 new LongOpt(
"plugin", LongOpt.REQUIRED_ARGUMENT,
null, 1),
130 new LongOpt(
"config", LongOpt.REQUIRED_ARGUMENT,
null, 3),
131 new LongOpt(
"use-mapdir", LongOpt.NO_ARGUMENT,
null, 4),
134 final Getopt g =
new Getopt(editorJarName, args,
"bchns", longOpts);
137 boolean useMapdir =
false;
138 boolean doExit =
true;
140 final int ch = g.getopt();
155 usage(editorJarName, defaultConfig);
167 plugin = g.getOptarg();
175 config =
new File(g.getOptarg());
176 if (!config.exists()) {
177 System.err.println(config +
": configuration file does not exist");
196 if (config !=
null) {
197 System.setProperty(
"java.util.prefs.PreferencesFactory",
"net.sf.gridarta.preferences.FilePreferencesFactory");
202 final Preferences preferences = Preferences.userNodeForPackage(
MainControl.class);
204 if (locName !=
null) {
205 Locale.setDefault(
new Locale(locName));
208 final List<String> args2 = Arrays.asList(args).subList(g.getOptind(), args.length);
211 final ActionBuilder actionBuilder = ActionBuilderFactory.getInstance().getActionBuilder(
"net.sf.gridarta");
213 actionBuilder.addParent(ActionBuilderFactory.getInstance().getActionBuilder(actionBuilderPackage));
220 final ProjectSettings projectSettings = editorFactory.newProjectSettings(editorSettings);
224 if (plugin ==
null) {
225 final List<File> args3 =
new ArrayList<>();
226 for (
final String arg: args2) {
227 args3.add(useMapdir ?
new File(projectSettings.
getMapsDirectory(), arg) :
new File(arg));
232 returnCode = runNormal(args3, editorFactory, errorView, resourceIcons, configSourceFactory, projectModel, editorSettings);
240 if (args3.size() != 2) {
257 System.err.println(
"Usage: " + ex.getMessage());
261 returnCode = runPlugin(plugin, errorView, args2, editorFactory, projectModel, resourceIcons, editorSettings);
265 System.exit(returnCode);
280 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) {
281 checkForErrors(errorView);
282 waitDialog(errorView);
283 final NamedFilter defaultNamedFilterList =
new NamedFilter(projectModel.getGameObjectMatchers().getFilters());
286 final RendererFactory<G, A, R> rendererFactory = editorFactory.newRendererFactory(mapViewSettings, filterControl, projectModel.getGameObjectParser(), projectModel.getFaceObjectProviders(), resourceIcons, projectModel.getSmoothFaces());
296 if (errorView.hasErrors()) {
297 waitDialog(errorView);
299 throw new AssertionError();
310 }
catch (
final InterruptedException ex) {
311 Thread.currentThread().interrupt();
313 throw new AssertionError(ex);
328 private int runNormal(@NotNull
final List<File> 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) {
330 for (
final LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
331 if (
"Nimbus".equals(info.getName())) {
332 UIManager.setLookAndFeel(info.getClassName());
336 }
catch (
final ClassNotFoundException | IllegalAccessException | InstantiationException | UnsupportedLookAndFeelException ignored) {
340 final Runnable runnable = () -> guiMainControl[0] =
new GUIMainControl<>(projectModel, editorSettings, errorView, resourceIcons, editorFactory, configSourceFactory);
342 SwingUtilities.invokeAndWait(runnable);
343 }
catch (
final InterruptedException ex) {
344 LOG.fatal(ex.getMessage(), ex);
345 Thread.currentThread().interrupt();
347 }
catch (
final InvocationTargetException ex) {
348 LOG.fatal(ex.getMessage(), ex);
352 checkForErrors(errorView);
353 waitDialog(errorView);
355 final Runnable runnable2 = () -> guiMainControl[0].run(args);
357 SwingUtilities.invokeAndWait(runnable2);
358 }
catch (
final InterruptedException ex) {
359 LOG.fatal(ex.getMessage(), ex);
360 Thread.currentThread().interrupt();
362 }
catch (
final InvocationTargetException ex) {
363 LOG.fatal(ex.getMessage(), ex);
376 private static void usage(@NotNull
final String editorJarName, @Nullable
final String defaultConfig) {
377 System.out.println(
"usage: java -jar " + editorJarName +
" [option...] [map-file...]");
378 System.out.println();
379 System.out.println(
" -h, --help print this help");
380 System.out.println(
" -b, --batchpng create PNG files for all given maps in their directories");
381 System.out.println(
" -c, --collectarches collect archetypes");
382 System.out.println(
" -n, --normal start editor with GUI (default)");
383 System.out.println(
" -s, --singlepng create a PNG file from the specified map");
384 System.out.println(
" --config=config-file use given config file; uses " + (defaultConfig ==
null ?
"Java preferences" :
"~/.gridarta/" + defaultConfig) +
" is not given");
385 System.out.println(
" --noexit do not call System.exit()");
386 System.out.println(
" --plugin=name [arg=value...]");
387 System.out.println(
" run a plugin with the given arguments");
388 System.out.println(
" --use-mapdir prepend the configured maps directory to all map-file arguments");