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

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

fix bug #1561 and #5160

  • 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 @SuppressWarnings("deprecation")
55 public static void main(final String[] argArray) {
56 /////////////////////////////////////////////////////////////////////////
57 // TO ALL TRANSLATORS
58 /////////////////////////////////////////////////////////////////////////
59 // Do not translate the early strings below until the locale is set up.
60 // (By the eager loaded plugins)
61 //
62 // These strings cannot be translated. That's life. Really. Sorry.
63 //
64 // Imi.
65 /////////////////////////////////////////////////////////////////////////
66
67 Thread.setDefaultUncaughtExceptionHandler(new BugReportExceptionHandler());
68
69 // construct argument table
70 List<String> argList = Arrays.asList(argArray);
71 final Map<String, Collection<String>> args = new HashMap<String, Collection<String>>();
72 for (String arg : argArray) {
73 if (!arg.startsWith("--"))
74 arg = "--download="+arg;
75 int i = arg.indexOf('=');
76 String key = i == -1 ? arg.substring(2) : arg.substring(2,i);
77 String value = i == -1 ? "" : arg.substring(i+1);
78 Collection<String> v = args.get(key);
79 if (v == null)
80 v = new LinkedList<String>();
81 v.add(value);
82 args.put(key, v);
83 }
84
85 // Only show the splash screen if we don't print the help and exit
86 SplashScreen splash;
87 if (!argList.contains("--help") && !argList.contains("-?") && !argList.contains("-h")) {
88 splash = new SplashScreen();
89 } else {
90 splash = null;
91 }
92
93 splash.setStatus(tr("Reading preferences"));
94 // get the preferences.
95 final File prefDir = new File(Main.pref.getPreferencesDir());
96 // check if preferences directory has moved (TODO: Update code. Remove this after some time)
97 File oldPrefDir = new File(System.getProperty("user.home"), ".josm");
98 if (!prefDir.isDirectory() && oldPrefDir.isDirectory()) {
99 if (oldPrefDir.renameTo(prefDir)) {
100 // do not translate this
101 JOptionPane.showMessageDialog(null, "The preference directory has been moved to "+prefDir);
102 } else {
103 JOptionPane.showMessageDialog(null, "The preference directory location has changed. Please move "+oldPrefDir+" to "+prefDir);
104 }
105 }
106
107 if (prefDir.exists() && !prefDir.isDirectory()) {
108 JOptionPane.showMessageDialog(null, "Cannot open preferences directory: "+Main.pref.getPreferencesDir());
109 return;
110 }
111 if (!prefDir.exists())
112 prefDir.mkdirs();
113
114 if (!new File(Main.pref.getPreferencesDir()+"preferences").exists()) {
115 Main.pref.resetToDefault();
116 }
117
118 try {
119 if (args.containsKey("reset-preferences")) {
120 Main.pref.resetToDefault();
121 } else {
122 Main.pref.load();
123 }
124 } catch (final IOException e1) {
125 e1.printStackTrace();
126 String backup = Main.pref.getPreferencesDir() + "preferences.bak";
127 JOptionPane.showMessageDialog(null, "Preferences file had errors. Making backup of old one to " + backup);
128 new File(Main.pref.getPreferencesDir() + "preferences").renameTo(new File(backup));
129 Main.pref.save();
130 }
131 String language = null;
132 if(args.containsKey("language"))
133 language = (String)(args.get("language").toArray()[0]);
134
135 splash.setStatus(tr("Activating updated plugins"));
136 if (!PluginDownloader.moveUpdatedPlugins()) {
137 JOptionPane.showMessageDialog(null,
138 tr("Activating the updated plugins failed. Check if JOSM has the permission to overwrite the existing ones."),
139 tr("Plugins"), JOptionPane.ERROR_MESSAGE);
140 }
141
142 // load the early plugins
143 splash.setStatus(tr("Loading early plugins"));
144 Main.loadPlugins(true, language);
145
146 if (argList.contains("--help") || argList.contains("-?") || argList.contains("-h")) {
147 System.out.println(tr("Java OpenStreetMap Editor")+"\n\n"+
148 tr("usage")+":\n"+
149 "\tjava -jar josm.jar <option> <option> <option>...\n\n"+
150 tr("options")+":\n"+
151 "\t--help|-?|-h "+tr("Show this help")+"\n"+
152 "\t--geometry=widthxheight(+|-)x(+|-)y "+tr("Standard unix geometry argument")+"\n"+
153 "\t[--download=]minlat,minlon,maxlat,maxlon "+tr("Download the bounding box")+"\n"+
154 "\t[--download=]<url> "+tr("Download the location at the url (with lat=x&lon=y&zoom=z)")+"\n"+
155 "\t[--download=]<filename> "+tr("Open file (as raw gps, if .gpx)")+"\n"+
156 "\t--downloadgps=minlat,minlon,maxlat,maxlon "+tr("Download the bounding box as raw gps")+"\n"+
157 "\t--selection=<searchstring> "+tr("Select with the given search")+"\n"+
158 "\t--no-fullscreen "+tr("Don't launch in fullscreen mode")+"\n"+
159 "\t--reset-preferences "+tr("Reset the preferences to default")+"\n\n"+
160 "\t--language=<language> "+tr("Set the language.")+"\n\n"+
161 tr("examples")+":\n"+
162 "\tjava -jar josm.jar track1.gpx track2.gpx london.osm\n"+
163 "\tjava -jar josm.jar http://www.openstreetmap.org/index.html?lat=43.2&lon=11.1&zoom=13\n"+
164 "\tjava -jar josm.jar london.osm --selection=http://www.ostertag.name/osm/OSM_errors_node-duplicate.xml\n"+
165 "\tjava -jar josm.jar 43.2,11.1,43.4,11.4\n\n"+
166
167 tr("Parameters are read in the order they are specified, so make sure you load\n"+
168 "some data before --selection")+"\n\n"+
169 tr("Instead of --download=<bbox> you may specify osm://<bbox>\n"));
170 System.exit(0);
171 }
172
173 splash.setStatus(tr("Setting defaults"));
174 preConstructorInit(args);
175 splash.setStatus(tr("Creating main GUI"));
176 JFrame mainFrame = new JFrame(tr("Java OpenStreetMap - Editor"));
177 Main.parent = mainFrame;
178 final Main main = new MainApplication(mainFrame);
179 splash.setStatus(tr("Loading plugins"));
180 Main.loadPlugins(false, null);
181 toolbar.refreshToolbarControl();
182
183 mainFrame.setVisible(true);
184 splash.closeSplash();
185
186 if (!args.containsKey("no-fullscreen") && !args.containsKey("geometry") && Toolkit.getDefaultToolkit().isFrameStateSupported(JFrame.MAXIMIZED_BOTH))
187 mainFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
188
189 EventQueue.invokeLater(new Runnable(){
190 public void run() {
191 main.postConstructorProcessCmdLine(args);
192 }
193 });
194 }
195}
Note: See TracBrowser for help on using the repository browser.