1 | //Licence: GPL
|
---|
2 | package org.openstreetmap.josm.gui;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Toolkit;
|
---|
7 | import java.awt.event.WindowAdapter;
|
---|
8 | import java.awt.event.WindowEvent;
|
---|
9 | import java.io.File;
|
---|
10 | import java.io.IOException;
|
---|
11 | import java.util.Arrays;
|
---|
12 | import java.util.Collection;
|
---|
13 | import java.util.HashMap;
|
---|
14 | import java.util.LinkedList;
|
---|
15 | import java.util.List;
|
---|
16 | import java.util.Map;
|
---|
17 |
|
---|
18 | import javax.swing.JFrame;
|
---|
19 | import javax.swing.JOptionPane;
|
---|
20 |
|
---|
21 | import org.openstreetmap.josm.Main;
|
---|
22 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
23 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
24 | import org.openstreetmap.josm.plugins.PluginException;
|
---|
25 | import org.openstreetmap.josm.plugins.PluginInformation;
|
---|
26 | import org.openstreetmap.josm.tools.BugReportExceptionHandler;
|
---|
27 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
28 | /**
|
---|
29 | * Main window class application.
|
---|
30 | *
|
---|
31 | * @author imi
|
---|
32 | */
|
---|
33 | public class MainApplication extends Main {
|
---|
34 | /**
|
---|
35 | * Construct an main frame, ready sized and operating. Does not
|
---|
36 | * display the frame.
|
---|
37 | */
|
---|
38 | public MainApplication(JFrame mainFrame) {
|
---|
39 | mainFrame.setContentPane(contentPane);
|
---|
40 | mainFrame.setJMenuBar(menu);
|
---|
41 | mainFrame.setBounds(bounds);
|
---|
42 | mainFrame.addWindowListener(new WindowAdapter(){
|
---|
43 | @Override public void windowClosing(final WindowEvent arg0) {
|
---|
44 | if (Main.map != null) {
|
---|
45 | boolean modified = false;
|
---|
46 | boolean uploadedModified = false;
|
---|
47 | for (final Layer l : Main.map.mapView.getAllLayers()) {
|
---|
48 | if (l instanceof OsmDataLayer && ((OsmDataLayer)l).isModified()) {
|
---|
49 | modified = true;
|
---|
50 | uploadedModified = ((OsmDataLayer)l).uploadedModified;
|
---|
51 | break;
|
---|
52 | }
|
---|
53 | }
|
---|
54 | if (modified) {
|
---|
55 | final String msg = uploadedModified ? "\n"+tr("Hint: Some changes came from uploading new data to the server.") : "";
|
---|
56 | final int answer = JOptionPane.showConfirmDialog(
|
---|
57 | Main.parent, tr("There are unsaved changes. Really quit?")+msg,
|
---|
58 | tr("Unsaved Changes"), JOptionPane.YES_NO_OPTION);
|
---|
59 | if (answer != JOptionPane.YES_OPTION)
|
---|
60 | return;
|
---|
61 | }
|
---|
62 | }
|
---|
63 | System.exit(0);
|
---|
64 | }
|
---|
65 | });
|
---|
66 | mainFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
|
---|
67 | }
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * Main application Startup
|
---|
71 | */
|
---|
72 | public static void main(final String[] argArray) {
|
---|
73 | /////////////////////////////////////////////////////////////////////////
|
---|
74 | // TO ALL TRANSLATORS
|
---|
75 | /////////////////////////////////////////////////////////////////////////
|
---|
76 | // Do not translate the early strings below until the locale is set up.
|
---|
77 | // (By the eager loaded plugins)
|
---|
78 | //
|
---|
79 | // These strings cannot be translated. That's live. Really. Sorry.
|
---|
80 | //
|
---|
81 | // Imi.
|
---|
82 | /////////////////////////////////////////////////////////////////////////
|
---|
83 |
|
---|
84 | Thread.setDefaultUncaughtExceptionHandler(new BugReportExceptionHandler());
|
---|
85 |
|
---|
86 | // construct argument table
|
---|
87 | List<String> argList = Arrays.asList(argArray);
|
---|
88 | 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 | // get the preferences.
|
---|
103 | final File prefDir = new File(Main.pref.getPreferencesDir());
|
---|
104 | if (prefDir.exists() && !prefDir.isDirectory()) {
|
---|
105 | JOptionPane.showMessageDialog(null, "Cannot open preferences directory: "+Main.pref.getPreferencesDir());
|
---|
106 | return;
|
---|
107 | }
|
---|
108 | if (!prefDir.exists())
|
---|
109 | prefDir.mkdirs();
|
---|
110 | try {
|
---|
111 | if (args.containsKey("reset-preferences")) {
|
---|
112 | Main.pref.resetToDefault();
|
---|
113 | } else {
|
---|
114 | Main.pref.load();
|
---|
115 | }
|
---|
116 | } catch (final IOException e1) {
|
---|
117 | e1.printStackTrace();
|
---|
118 | JOptionPane.showMessageDialog(null, "Preferences could not be loaded. Writing default preference file to "+pref.getPreferencesDir()+"preferences");
|
---|
119 | Main.pref.resetToDefault();
|
---|
120 | }
|
---|
121 |
|
---|
122 | // load the early plugins
|
---|
123 | if (Main.pref.hasKey("plugins")) {
|
---|
124 | for (String pluginName : Main.pref.get("plugins").split(",")) {
|
---|
125 | try {
|
---|
126 | File pluginFile = new File(pref.getPreferencesDir()+"plugins/"+pluginName+".jar");
|
---|
127 | if (pluginFile.exists()) {
|
---|
128 | PluginInformation info = new PluginInformation(pluginFile);
|
---|
129 | if (!info.early)
|
---|
130 | continue;
|
---|
131 | Class<?> klass = info.loadClass();
|
---|
132 | ImageProvider.sources.add(0, klass);
|
---|
133 | Main.plugins.add(info.load(klass));
|
---|
134 | } else
|
---|
135 | System.out.println("Plugin not found: "+pluginName);
|
---|
136 | } catch (PluginException e) {
|
---|
137 | System.out.println("Could not load plugin "+pluginName);
|
---|
138 | e.printStackTrace();
|
---|
139 | }
|
---|
140 | }
|
---|
141 | }
|
---|
142 |
|
---|
143 | if (argList.contains("--help") || argList.contains("-?") || argList.contains("-h")) {
|
---|
144 | System.out.println(tr("Java OpenStreetMap Editor")+"\n\n"+
|
---|
145 | tr("usage")+":\n"+
|
---|
146 | "\tjava -jar josm.jar <option> <option> <option>...\n\n"+
|
---|
147 | tr("options")+":\n"+
|
---|
148 | "\t--help|-?|-h "+tr("Show this help")+"\n"+
|
---|
149 | "\t--geometry=widthxheight(+|-)x(+|-)y "+tr("Standard unix geometry argument")+"\n"+
|
---|
150 | "\t[--download=]minlat,minlon,maxlat,maxlon "+tr("Download the bounding box")+"\n"+
|
---|
151 | "\t[--download=]<url> "+tr("Download the location at the url (with lat=x&lon=y&zoom=z)")+"\n"+
|
---|
152 | "\t[--download=]<filename> "+tr("Open file (as raw gps, if .gpx or .csv)")+"\n"+
|
---|
153 | "\t--downloadgps=minlat,minlon,maxlat,maxlon "+tr("Download the bounding box as raw gps")+"\n"+
|
---|
154 | "\t--selection=<searchstring> "+tr("Select with the given search")+"\n"+
|
---|
155 | "\t--no-fullscreen "+tr("Don't launch in fullscreen mode")+"\n"+
|
---|
156 | "\t--reset-preferences "+tr("Reset the preferences to default")+"\n\n"+
|
---|
157 | "\t--language=<language> "+tr("Set the language. Example: ")+"\n\n"+
|
---|
158 | tr("examples")+":\n"+
|
---|
159 | "\tjava -jar josm.jar track1.gpx track2.gpx london.osm\n"+
|
---|
160 | "\tjava -jar josm.jar http://www.openstreetmap.org/index.html?lat=43.2&lon=11.1&zoom=13\n"+
|
---|
161 | "\tjava -jar josm.jar london.osm --selection=http://www.ostertag.name/osm/OSM_errors_node-duplicate.xml\n"+
|
---|
162 | "\tjava -jar josm.jar 43.2,11.1,43.4,11.4\n\n"+
|
---|
163 |
|
---|
164 | tr("Parameters are read in the order they are specified, so make sure you load\n"+
|
---|
165 | "some data before --selection")+"\n\n"+
|
---|
166 | tr("Instead of --download=<bbox> you may specify osm://<bbox>\n"));
|
---|
167 | System.exit(0);
|
---|
168 | }
|
---|
169 |
|
---|
170 | preConstructorInit(args);
|
---|
171 | JFrame mainFrame = new JFrame(tr("Java Open Street Map - Editor"));
|
---|
172 | Main.parent = mainFrame;
|
---|
173 | Main main = new MainApplication(mainFrame);
|
---|
174 | main.loadPlugins();
|
---|
175 |
|
---|
176 | mainFrame.setVisible(true);
|
---|
177 |
|
---|
178 | if (!args.containsKey("no-fullscreen") && !args.containsKey("geometry") && Toolkit.getDefaultToolkit().isFrameStateSupported(JFrame.MAXIMIZED_BOTH))
|
---|
179 | mainFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
|
---|
180 |
|
---|
181 | main.postConstructorProcessCmdLine(args);
|
---|
182 | }
|
---|
183 | }
|
---|