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

Last change on this file since 205 was 205, checked in by imi, 18 years ago
  • added "addToggleDialog" to MapFrame to give plugins the chance of providing new toggle dialogs
  • fixed display of <different> in the property dialog when selecting different non-empty values
  • fixed the preferences directory under Windows (JOSM now uses environment variable APPDATA)
  • added LiveGPS plugin to exception detection heuristic
File size: 7.2 KB
Line 
1//Licence: GPL
2package org.openstreetmap.josm.gui;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.EventQueue;
7import java.awt.Toolkit;
8import java.awt.event.WindowAdapter;
9import java.awt.event.WindowEvent;
10import java.io.File;
11import java.io.IOException;
12import java.util.Arrays;
13import java.util.Collection;
14import java.util.HashMap;
15import java.util.LinkedList;
16import java.util.List;
17import java.util.Map;
18
19import javax.swing.JFrame;
20import javax.swing.JOptionPane;
21
22import org.openstreetmap.josm.Main;
23import org.openstreetmap.josm.plugins.PluginException;
24import org.openstreetmap.josm.plugins.PluginInformation;
25import org.openstreetmap.josm.tools.BugReportExceptionHandler;
26import org.openstreetmap.josm.tools.ImageProvider;
27/**
28 * Main window class application.
29 *
30 * @author imi
31 */
32public class MainApplication extends Main {
33 /**
34 * Construct an main frame, ready sized and operating. Does not
35 * display the frame.
36 */
37 public MainApplication(JFrame mainFrame) {
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 live. 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 // get the preferences.
85 final File prefDir = new File(Main.pref.getPreferencesDir());
86
87 // check if preferences directory has moved (TODO: Update code. Remove this after some time)
88 File oldPrefDir = new File(System.getProperty("user.home")+"/.josm");
89 if (!prefDir.isDirectory() && oldPrefDir.isDirectory()) {
90 if (oldPrefDir.renameTo(prefDir)) {
91 // do not translate this
92 JOptionPane.showMessageDialog(null, "The preference directory has been moved to "+prefDir);
93 } else {
94 JOptionPane.showMessageDialog(null, "The preference directory location has changed. Please move "+oldPrefDir+" to "+prefDir);
95 }
96 }
97
98 if (prefDir.exists() && !prefDir.isDirectory()) {
99 JOptionPane.showMessageDialog(null, "Cannot open preferences directory: "+Main.pref.getPreferencesDir());
100 return;
101 }
102 if (!prefDir.exists())
103 prefDir.mkdirs();
104 try {
105 if (args.containsKey("reset-preferences")) {
106 Main.pref.resetToDefault();
107 } else {
108 Main.pref.load();
109 }
110 } catch (final IOException e1) {
111 e1.printStackTrace();
112 JOptionPane.showMessageDialog(null, "Preferences could not be loaded. Writing default preference file to "+pref.getPreferencesDir()+"preferences");
113 Main.pref.resetToDefault();
114 }
115
116 // determine what classloader to be used for plugins
117 if (args.containsKey("default-classloader"))
118 PluginInformation.useJosmClassloader = true;
119
120 // load the early plugins
121 if (Main.pref.hasKey("plugins")) {
122 for (String pluginName : Main.pref.get("plugins").split(",")) {
123 try {
124 File pluginFile = new File(pref.getPreferencesDir()+"plugins/"+pluginName+".jar");
125 if (pluginFile.exists()) {
126 PluginInformation info = new PluginInformation(pluginFile);
127 if (!info.early)
128 continue;
129 Class<?> klass = info.loadClass();
130 ImageProvider.sources.add(0, klass);
131 Main.plugins.add(info.load(klass));
132 } else
133 System.out.println("Plugin not found: "+pluginName);
134 } catch (PluginException e) {
135 System.out.println("Could not load plugin "+pluginName);
136 e.printStackTrace();
137 }
138 }
139 }
140
141 if (argList.contains("--help") || argList.contains("-?") || argList.contains("-h")) {
142 System.out.println(tr("Java OpenStreetMap Editor")+"\n\n"+
143 tr("usage")+":\n"+
144 "\tjava -jar josm.jar <option> <option> <option>...\n\n"+
145 tr("options")+":\n"+
146 "\t--help|-?|-h "+tr("Show this help")+"\n"+
147 "\t--geometry=widthxheight(+|-)x(+|-)y "+tr("Standard unix geometry argument")+"\n"+
148 "\t[--download=]minlat,minlon,maxlat,maxlon "+tr("Download the bounding box")+"\n"+
149 "\t[--download=]<url> "+tr("Download the location at the url (with lat=x&lon=y&zoom=z)")+"\n"+
150 "\t[--download=]<filename> "+tr("Open file (as raw gps, if .gpx or .csv)")+"\n"+
151 "\t--downloadgps=minlat,minlon,maxlat,maxlon "+tr("Download the bounding box as raw gps")+"\n"+
152 "\t--selection=<searchstring> "+tr("Select with the given search")+"\n"+
153 "\t--default-classloader "+tr("Load all plugins with the default class loader")+"\n"+
154 "\t--no-fullscreen "+tr("Don't launch in fullscreen mode")+"\n"+
155 "\t--reset-preferences "+tr("Reset the preferences to default")+"\n\n"+
156 "\t--language=<language> "+tr("Set the language. Example: ")+"\n\n"+
157 tr("examples")+":\n"+
158 "\tjava -jar josm.jar track1.gpx track2.gpx london.osm\n"+
159 "\tjava -jar josm.jar http://www.openstreetmap.org/index.html?lat=43.2&lon=11.1&zoom=13\n"+
160 "\tjava -jar josm.jar london.osm --selection=http://www.ostertag.name/osm/OSM_errors_node-duplicate.xml\n"+
161 "\tjava -jar josm.jar 43.2,11.1,43.4,11.4\n\n"+
162
163 tr("Parameters are read in the order they are specified, so make sure you load\n"+
164 "some data before --selection")+"\n\n"+
165 tr("Instead of --download=<bbox> you may specify osm://<bbox>\n"));
166 System.exit(0);
167 }
168
169 preConstructorInit(args);
170 JFrame mainFrame = new JFrame(tr("Java Open Street Map - Editor"));
171 Main.parent = mainFrame;
172 final Main main = new MainApplication(mainFrame);
173 main.loadPlugins();
174
175 mainFrame.setVisible(true);
176
177 if (!args.containsKey("no-fullscreen") && !args.containsKey("geometry") && Toolkit.getDefaultToolkit().isFrameStateSupported(JFrame.MAXIMIZED_BOTH))
178 mainFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
179
180 EventQueue.invokeLater(new Runnable(){
181 public void run() {
182 main.postConstructorProcessCmdLine(args);
183 }
184 });
185 }
186}
Note: See TracBrowser for help on using the repository browser.