source: josm/trunk/src/org/openstreetmap/josm/gui/MainApplication.java@ 999

Last change on this file since 999 was 999, checked in by stoecker, 16 years ago

close bug #1588, code cleanup by bruce89

  • Property svn:eol-style set to native
File size: 7.3 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2//Licence: GPL
3package org.openstreetmap.josm.gui;
4
5import static org.openstreetmap.josm.tools.I18n.tr;
6
7import java.awt.EventQueue;
8import java.awt.Toolkit;
9import java.awt.event.WindowAdapter;
10import java.awt.event.WindowEvent;
11import java.io.File;
12import java.io.IOException;
13import java.util.Arrays;
14import java.util.Collection;
15import java.util.HashMap;
16import java.util.LinkedList;
17import java.util.List;
18import java.util.Map;
19
20import javax.swing.JFrame;
21import javax.swing.JOptionPane;
22
23import org.openstreetmap.josm.Main;
24import org.openstreetmap.josm.plugins.PluginDownloader;
25import org.openstreetmap.josm.tools.BugReportExceptionHandler;
26/**
27 * Main window class application.
28 *
29 * @author imi
30 */
31public class MainApplication extends Main {
32 /**
33 * Construct an main frame, ready sized and operating. Does not
34 * display the frame.
35 */
36 public MainApplication(JFrame mainFrame) {
37 super();
38 mainFrame.setContentPane(contentPane);
39 mainFrame.setJMenuBar(menu);
40 mainFrame.setBounds(bounds);
41 mainFrame.addWindowListener(new WindowAdapter(){
42 @Override public void windowClosing(final WindowEvent arg0) {
43 if (Main.breakBecauseUnsavedChanges())
44 return;
45 System.exit(0);
46 }
47 });
48 mainFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
49 }
50
51 /**
52 * Main application Startup
53 */
54 public static void main(final String[] argArray) {
55 /////////////////////////////////////////////////////////////////////////
56 // TO ALL TRANSLATORS
57 /////////////////////////////////////////////////////////////////////////
58 // Do not translate the early strings below until the locale is set up.
59 // (By the eager loaded plugins)
60 //
61 // These strings cannot be translated. That's life. Really. Sorry.
62 //
63 // Imi.
64 /////////////////////////////////////////////////////////////////////////
65
66 Thread.setDefaultUncaughtExceptionHandler(new BugReportExceptionHandler());
67
68 // construct argument table
69 List<String> argList = Arrays.asList(argArray);
70 final Map<String, Collection<String>> args = new HashMap<String, Collection<String>>();
71 for (String arg : argArray) {
72 if (!arg.startsWith("--"))
73 arg = "--download="+arg;
74 int i = arg.indexOf('=');
75 String key = i == -1 ? arg.substring(2) : arg.substring(2,i);
76 String value = i == -1 ? "" : arg.substring(i+1);
77 Collection<String> v = args.get(key);
78 if (v == null)
79 v = new LinkedList<String>();
80 v.add(value);
81 args.put(key, v);
82 }
83
84 // Only show the splash screen if we don't print the help and exit
85 SplashScreen splash;
86 if (!argList.contains("--help") && !argList.contains("-?") && !argList.contains("-h")) {
87 splash = new SplashScreen();
88 } else {
89 splash = null;
90 }
91
92 splash.setStatus(tr("Reading preferences"));
93 // get the preferences.
94 final File prefDir = new File(Main.pref.getPreferencesDir());
95 // check if preferences directory has moved (TODO: Update code. Remove this after some time)
96 File oldPrefDir = new File(System.getProperty("user.home"), ".josm");
97 if (!prefDir.isDirectory() && oldPrefDir.isDirectory()) {
98 if (oldPrefDir.renameTo(prefDir)) {
99 // do not translate this
100 JOptionPane.showMessageDialog(null, "The preference directory has been moved to "+prefDir);
101 } else {
102 JOptionPane.showMessageDialog(null, "The preference directory location has changed. Please move "+oldPrefDir+" to "+prefDir);
103 }
104 }
105
106 if (prefDir.exists() && !prefDir.isDirectory()) {
107 JOptionPane.showMessageDialog(null, "Cannot open preferences directory: "+Main.pref.getPreferencesDir());
108 return;
109 }
110 if (!prefDir.exists())
111 prefDir.mkdirs();
112
113 if (!new File(Main.pref.getPreferencesDir()+"preferences").exists()) {
114 Main.pref.resetToDefault();
115 }
116
117 try {
118 if (args.containsKey("reset-preferences")) {
119 Main.pref.resetToDefault();
120 } else {
121 Main.pref.load();
122 }
123 } catch (final IOException e1) {
124 e1.printStackTrace();
125 String backup = Main.pref.getPreferencesDir() + "preferences.bak";
126 JOptionPane.showMessageDialog(null, "Preferences file had errors. Making backup of old one to " + backup);
127 new File(Main.pref.getPreferencesDir() + "preferences").renameTo(new File(backup));
128 Main.pref.save();
129 }
130 String language = null;
131 if(args.containsKey("language"))
132 language = (String)(args.get("language").toArray()[0]);
133
134 splash.setStatus(tr("Activating updated plugins"));
135 if (!PluginDownloader.moveUpdatedPlugins()) {
136 JOptionPane.showMessageDialog(null,
137 tr("Activating the updated plugins failed. Check if JOSM has the permission to overwrite the existing ones."),
138 tr("Plugins"), JOptionPane.ERROR_MESSAGE);
139 }
140
141 // load the early plugins
142 splash.setStatus(tr("Loading early plugins"));
143 Main.loadPlugins(true, language);
144
145 if (argList.contains("--help") || argList.contains("-?") || argList.contains("-h")) {
146 System.out.println(tr("Java OpenStreetMap Editor")+"\n\n"+
147 tr("usage")+":\n"+
148 "\tjava -jar josm.jar <option> <option> <option>...\n\n"+
149 tr("options")+":\n"+
150 "\t--help|-?|-h "+tr("Show this help")+"\n"+
151 "\t--geometry=widthxheight(+|-)x(+|-)y "+tr("Standard unix geometry argument")+"\n"+
152 "\t[--download=]minlat,minlon,maxlat,maxlon "+tr("Download the bounding box")+"\n"+
153 "\t[--download=]<url> "+tr("Download the location at the url (with lat=x&lon=y&zoom=z)")+"\n"+
154 "\t[--download=]<filename> "+tr("Open file (as raw gps, if .gpx)")+"\n"+
155 "\t--downloadgps=minlat,minlon,maxlat,maxlon "+tr("Download the bounding box as raw gps")+"\n"+
156 "\t--selection=<searchstring> "+tr("Select with the given search")+"\n"+
157 "\t--no-fullscreen "+tr("Don't launch in fullscreen mode")+"\n"+
158 "\t--reset-preferences "+tr("Reset the preferences to default")+"\n\n"+
159 "\t--language=<language> "+tr("Set the language.")+"\n\n"+
160 tr("examples")+":\n"+
161 "\tjava -jar josm.jar track1.gpx track2.gpx london.osm\n"+
162 "\tjava -jar josm.jar http://www.openstreetmap.org/index.html?lat=43.2&lon=11.1&zoom=13\n"+
163 "\tjava -jar josm.jar london.osm --selection=http://www.ostertag.name/osm/OSM_errors_node-duplicate.xml\n"+
164 "\tjava -jar josm.jar 43.2,11.1,43.4,11.4\n\n"+
165
166 tr("Parameters are read in the order they are specified, so make sure you load\n"+
167 "some data before --selection")+"\n\n"+
168 tr("Instead of --download=<bbox> you may specify osm://<bbox>\n"));
169 System.exit(0);
170 }
171
172 splash.setStatus(tr("Setting defaults"));
173 preConstructorInit(args);
174 splash.setStatus(tr("Creating main GUI"));
175 JFrame mainFrame = new JFrame(tr("Java OpenStreetMap - Editor"));
176 Main.parent = mainFrame;
177 final Main main = new MainApplication(mainFrame);
178 splash.setStatus(tr("Loading plugins"));
179 Main.loadPlugins(false, null);
180 toolbar.refreshToolbarControl();
181
182 mainFrame.setVisible(true);
183 splash.closeSplash();
184
185 if (!args.containsKey("no-fullscreen") && !args.containsKey("geometry") && Toolkit.getDefaultToolkit().isFrameStateSupported(JFrame.MAXIMIZED_BOTH))
186 mainFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
187
188 EventQueue.invokeLater(new Runnable(){
189 public void run() {
190 main.postConstructorProcessCmdLine(args);
191 }
192 });
193 }
194}
Note: See TracBrowser for help on using the repository browser.