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