1 | // Licence: GPL
|
---|
2 | package org.openstreetmap.josm;
|
---|
3 |
|
---|
4 | import java.awt.BorderLayout;
|
---|
5 | import java.awt.Dimension;
|
---|
6 | import java.awt.Point;
|
---|
7 | import java.awt.Toolkit;
|
---|
8 | import java.awt.event.WindowAdapter;
|
---|
9 | import java.awt.event.WindowEvent;
|
---|
10 | import java.io.File;
|
---|
11 | import java.io.IOException;
|
---|
12 | import java.util.Arrays;
|
---|
13 | import java.util.Iterator;
|
---|
14 | import java.util.LinkedList;
|
---|
15 | import java.util.StringTokenizer;
|
---|
16 |
|
---|
17 | import javax.swing.JFrame;
|
---|
18 | import javax.swing.JMenu;
|
---|
19 | import javax.swing.JMenuBar;
|
---|
20 | import javax.swing.JOptionPane;
|
---|
21 | import javax.swing.JPanel;
|
---|
22 | import javax.swing.JSeparator;
|
---|
23 | import javax.swing.JToolBar;
|
---|
24 | import javax.swing.UIManager;
|
---|
25 |
|
---|
26 | import org.openstreetmap.josm.actions.AboutAction;
|
---|
27 | import org.openstreetmap.josm.actions.DownloadAction;
|
---|
28 | import org.openstreetmap.josm.actions.ExitAction;
|
---|
29 | import org.openstreetmap.josm.actions.OpenAction;
|
---|
30 | import org.openstreetmap.josm.actions.PreferencesAction;
|
---|
31 | import org.openstreetmap.josm.actions.RedoAction;
|
---|
32 | import org.openstreetmap.josm.actions.SaveAction;
|
---|
33 | import org.openstreetmap.josm.actions.UndoAction;
|
---|
34 | import org.openstreetmap.josm.actions.UploadAction;
|
---|
35 | import org.openstreetmap.josm.data.Preferences;
|
---|
36 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
37 | import org.openstreetmap.josm.data.projection.Epsg4263;
|
---|
38 | import org.openstreetmap.josm.data.projection.Projection;
|
---|
39 | import org.openstreetmap.josm.gui.MapFrame;
|
---|
40 | import org.openstreetmap.josm.gui.ShowModifiers;
|
---|
41 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
42 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
43 | import org.openstreetmap.josm.tools.BugReportExceptionHandler;
|
---|
44 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * Main window class application.
|
---|
48 | *
|
---|
49 | * @author imi
|
---|
50 | */
|
---|
51 | public class Main extends JFrame {
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * Global application window. Use this as JOPtionPane-parent to center on application.
|
---|
55 | */
|
---|
56 | public static Main main;
|
---|
57 |
|
---|
58 | public static Projection proj;
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * Global application preferences
|
---|
62 | */
|
---|
63 | public final static Preferences pref = new Preferences();
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * The global dataset.
|
---|
67 | */
|
---|
68 | public DataSet ds = new DataSet();
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * The main panel.
|
---|
72 | */
|
---|
73 | public JPanel panel;
|
---|
74 | /**
|
---|
75 | * The mapFrame currently loaded.
|
---|
76 | */
|
---|
77 | private MapFrame mapFrame;
|
---|
78 |
|
---|
79 | public final UndoAction undoAction;
|
---|
80 | public final RedoAction redoAction;
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * This directory is used for all disc IO access as starting directory. Should
|
---|
84 | * be set accordingly after the IO action.
|
---|
85 | */
|
---|
86 | public File currentDirectory = new File(".");
|
---|
87 |
|
---|
88 | private OpenAction openAction;
|
---|
89 |
|
---|
90 | private DownloadAction downloadAction;
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * Construct an main frame, ready sized and operating. Does not
|
---|
94 | * display the frame.
|
---|
95 | */
|
---|
96 | public Main() {
|
---|
97 | super("Java Open Street Map - Editor");
|
---|
98 | Main.main = this;
|
---|
99 | setLayout(new BorderLayout());
|
---|
100 | panel = new JPanel(new BorderLayout());
|
---|
101 | getContentPane().add(panel, BorderLayout.CENTER);
|
---|
102 | setSize(1000,740); // some strange default size
|
---|
103 | setVisible(true);
|
---|
104 |
|
---|
105 | downloadAction = new DownloadAction();
|
---|
106 | UploadAction uploadAction = new UploadAction();
|
---|
107 | openAction = new OpenAction();
|
---|
108 | SaveAction saveAction = new SaveAction();
|
---|
109 | ExitAction exitAction = new ExitAction();
|
---|
110 | undoAction = new UndoAction();
|
---|
111 | redoAction = new RedoAction();
|
---|
112 | PreferencesAction preferencesAction = new PreferencesAction();
|
---|
113 | AboutAction aboutAction = new AboutAction();
|
---|
114 |
|
---|
115 | // creating menu
|
---|
116 | JMenuBar mainMenu = new JMenuBar();
|
---|
117 | setJMenuBar(mainMenu);
|
---|
118 |
|
---|
119 | JMenu fileMenu = new JMenu("Files");
|
---|
120 | fileMenu.setMnemonic('F');
|
---|
121 | fileMenu.add(openAction);
|
---|
122 | fileMenu.add(saveAction);
|
---|
123 | fileMenu.addSeparator();
|
---|
124 | fileMenu.add(exitAction);
|
---|
125 | mainMenu.add(fileMenu);
|
---|
126 |
|
---|
127 |
|
---|
128 | JMenu connectionMenu = new JMenu("Connection");
|
---|
129 | connectionMenu.setMnemonic('C');
|
---|
130 | connectionMenu.add(downloadAction);
|
---|
131 | connectionMenu.add(uploadAction);
|
---|
132 | mainMenu.add(connectionMenu);
|
---|
133 |
|
---|
134 | JMenu editMenu = new JMenu("Edit");
|
---|
135 | editMenu.setMnemonic('E');
|
---|
136 | editMenu.add(undoAction);
|
---|
137 | editMenu.add(redoAction);
|
---|
138 | editMenu.addSeparator();
|
---|
139 | editMenu.add(preferencesAction);
|
---|
140 | mainMenu.add(editMenu);
|
---|
141 |
|
---|
142 | mainMenu.add(new JSeparator());
|
---|
143 | JMenu helpMenu = new JMenu("Help");
|
---|
144 | helpMenu.setMnemonic('H');
|
---|
145 | helpMenu.add(aboutAction);
|
---|
146 | mainMenu.add(helpMenu);
|
---|
147 |
|
---|
148 | // creating toolbar
|
---|
149 | JToolBar toolBar = new JToolBar();
|
---|
150 | toolBar.setFloatable(false);
|
---|
151 | toolBar.add(downloadAction);
|
---|
152 | toolBar.add(uploadAction);
|
---|
153 | toolBar.add(openAction);
|
---|
154 | toolBar.add(saveAction);
|
---|
155 | toolBar.addSeparator();
|
---|
156 | toolBar.add(undoAction);
|
---|
157 | toolBar.add(redoAction);
|
---|
158 | toolBar.addSeparator();
|
---|
159 | toolBar.add(preferencesAction);
|
---|
160 |
|
---|
161 | getContentPane().add(toolBar, BorderLayout.NORTH);
|
---|
162 |
|
---|
163 | addWindowListener(new WindowAdapter(){
|
---|
164 | @Override
|
---|
165 | public void windowClosing(WindowEvent arg0) {
|
---|
166 | if (mapFrame != null) {
|
---|
167 | boolean modified = false;
|
---|
168 | boolean uploadedModified = false;
|
---|
169 | for (Layer l : mapFrame.mapView.getAllLayers()) {
|
---|
170 | if (l instanceof OsmDataLayer && ((OsmDataLayer)l).isModified()) {
|
---|
171 | modified = true;
|
---|
172 | uploadedModified = ((OsmDataLayer)l).uploadedModified;
|
---|
173 | break;
|
---|
174 | }
|
---|
175 | }
|
---|
176 | if (modified) {
|
---|
177 | String msg = uploadedModified ? "\nHint: Some changes came from uploading new data to the server." : "";
|
---|
178 | int answer = JOptionPane.showConfirmDialog(
|
---|
179 | Main.this, "There are unsaved changes. Really quit?"+msg,
|
---|
180 | "Unsaved Changes", JOptionPane.YES_NO_OPTION);
|
---|
181 | if (answer != JOptionPane.YES_OPTION)
|
---|
182 | return;
|
---|
183 | }
|
---|
184 | }
|
---|
185 | System.exit(0);
|
---|
186 | }
|
---|
187 | });
|
---|
188 | setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
|
---|
189 | }
|
---|
190 |
|
---|
191 | /**
|
---|
192 | * Main application Startup
|
---|
193 | * @param args No parameters accepted.
|
---|
194 | */
|
---|
195 | public static void main(String[] args) {
|
---|
196 | setupExceptionHandler();
|
---|
197 | setupUiDefaults();
|
---|
198 |
|
---|
199 | LinkedList<String> arguments = new LinkedList<String>(Arrays.asList(args));
|
---|
200 |
|
---|
201 | if (arguments.contains("--help")) {
|
---|
202 | System.out.println("Java OpenStreetMap Editor");
|
---|
203 | System.out.println();
|
---|
204 | System.out.println("usage:");
|
---|
205 | System.out.println("\tjava -jar josm.jar <options>");
|
---|
206 | System.out.println();
|
---|
207 | System.out.println("options:");
|
---|
208 | System.out.println("\t--help Show this help");
|
---|
209 | System.out.println("\t--download=minlat,minlon,maxlat,maxlon Download the bounding box");
|
---|
210 | System.out.println("\t--open=file(.osm|.xml|.gpx|.txt|.csv) Open the specific file");
|
---|
211 | System.out.println("\t--no-fullscreen Don't launch in fullscreen mode");
|
---|
212 | System.out.println("\t--reset-preferences Reset the preferences to default");
|
---|
213 | System.exit(0);
|
---|
214 | }
|
---|
215 |
|
---|
216 | File prefDir = new File(Preferences.getPreferencesDir());
|
---|
217 | if (prefDir.exists() && !prefDir.isDirectory()) {
|
---|
218 | JOptionPane.showMessageDialog(null, "Cannot open preferences directory: "+Preferences.getPreferencesDir());
|
---|
219 | return;
|
---|
220 | }
|
---|
221 | if (!prefDir.exists())
|
---|
222 | prefDir.mkdirs();
|
---|
223 |
|
---|
224 | // load preferences
|
---|
225 | String errMsg = null;
|
---|
226 | try {
|
---|
227 | if (arguments.remove("--reset-preferences")) {
|
---|
228 | pref.resetToDefault();
|
---|
229 | } else
|
---|
230 | pref.load();
|
---|
231 | } catch (IOException e1) {
|
---|
232 | e1.printStackTrace();
|
---|
233 | errMsg = "Preferences could not be loaded. Write default preference file to '"+Preferences.getPreferencesDir()+"preferences'.";
|
---|
234 | pref.resetToDefault();
|
---|
235 | }
|
---|
236 | if (errMsg != null)
|
---|
237 | JOptionPane.showMessageDialog(null, errMsg);
|
---|
238 |
|
---|
239 | try {
|
---|
240 | proj = (Projection)Class.forName(pref.get("projection")).newInstance();
|
---|
241 | } catch (Exception e) {
|
---|
242 | e.printStackTrace();
|
---|
243 | JOptionPane.showMessageDialog(null, "The projection could not be read from preferences. Using EPSG:4263.");
|
---|
244 | proj = new Epsg4263();
|
---|
245 | }
|
---|
246 |
|
---|
247 | try {
|
---|
248 | UIManager.setLookAndFeel(pref.get("laf"));
|
---|
249 | } catch (Exception e) {
|
---|
250 | e.printStackTrace();
|
---|
251 | }
|
---|
252 |
|
---|
253 | new Main();
|
---|
254 | main.setVisible(true);
|
---|
255 |
|
---|
256 |
|
---|
257 | if (arguments.remove("--show-modifiers")) {
|
---|
258 | Point p = main.getLocationOnScreen();
|
---|
259 | Dimension s = main.getSize();
|
---|
260 | new ShowModifiers(p.x + s.width - 3, p.y + s.height - 32);
|
---|
261 | main.setVisible(true);
|
---|
262 | }
|
---|
263 |
|
---|
264 | if (!arguments.remove("--no-fullscreen")) {
|
---|
265 | if (Toolkit.getDefaultToolkit().isFrameStateSupported(MAXIMIZED_BOTH))
|
---|
266 | main.setExtendedState(MAXIMIZED_BOTH); // some platform are able to maximize
|
---|
267 | else {
|
---|
268 | Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
|
---|
269 | main.setSize(d);
|
---|
270 | }
|
---|
271 | }
|
---|
272 |
|
---|
273 | for (Iterator<String> it = arguments.iterator(); it.hasNext();) {
|
---|
274 | String s = it.next();
|
---|
275 | if (s.startsWith("--open=")) {
|
---|
276 | main.openAction.openFile(new File(s.substring(7)));
|
---|
277 | it.remove();
|
---|
278 | } else if (s.startsWith("--download=")) {
|
---|
279 | downloadFromParamString(false, s.substring(11));
|
---|
280 | it.remove();
|
---|
281 | } else if (s.startsWith("--downloadgps=")) {
|
---|
282 | downloadFromParamString(true, s.substring(14));
|
---|
283 | it.remove();
|
---|
284 | }
|
---|
285 | }
|
---|
286 |
|
---|
287 | if (!arguments.isEmpty()) {
|
---|
288 | String s = "Unknown Parameter:\n";
|
---|
289 | for (String arg : arguments)
|
---|
290 | s += arg+"\n";
|
---|
291 | JOptionPane.showMessageDialog(main, s);
|
---|
292 | }
|
---|
293 | }
|
---|
294 |
|
---|
295 |
|
---|
296 | private static void downloadFromParamString(boolean rawGps, String s) {
|
---|
297 | StringTokenizer st = new StringTokenizer(s, ",");
|
---|
298 | if (st.countTokens() != 4) {
|
---|
299 | JOptionPane.showMessageDialog(main, "Download option does not take "+st.countTokens()+" bounding parameter.");
|
---|
300 | return;
|
---|
301 | }
|
---|
302 |
|
---|
303 | try {
|
---|
304 | main.downloadAction.download(rawGps, Double.parseDouble(st.nextToken()), Double.parseDouble(st.nextToken()), Double.parseDouble(st.nextToken()), Double.parseDouble(st.nextToken()));
|
---|
305 | } catch (NumberFormatException e) {
|
---|
306 | JOptionPane.showMessageDialog(main, "Could not parse the Coordinates.");
|
---|
307 | }
|
---|
308 | }
|
---|
309 |
|
---|
310 | //TODO: should be solved better.
|
---|
311 | public void setMapFrame(MapFrame mapFrame) {
|
---|
312 | if (this.mapFrame != null)
|
---|
313 | this.mapFrame.setVisible(false);
|
---|
314 | this.mapFrame = mapFrame;
|
---|
315 | panel.setVisible(false);
|
---|
316 | panel.removeAll();
|
---|
317 | if (mapFrame != null) {
|
---|
318 | mapFrame.fillPanel(panel);
|
---|
319 | panel.setVisible(true);
|
---|
320 | mapFrame.setVisible(true);
|
---|
321 | }
|
---|
322 | }
|
---|
323 | /**
|
---|
324 | * @return Returns the mapFrame.
|
---|
325 | */
|
---|
326 | public MapFrame getMapFrame() {
|
---|
327 | return mapFrame;
|
---|
328 | }
|
---|
329 |
|
---|
330 |
|
---|
331 | /**
|
---|
332 | * Sets some icons to the ui.
|
---|
333 | */
|
---|
334 | private static void setupUiDefaults() {
|
---|
335 | UIManager.put("OptionPane.okIcon", ImageProvider.get("ok"));
|
---|
336 | UIManager.put("OptionPane.yesIcon", UIManager.get("OptionPane.okIcon"));
|
---|
337 | UIManager.put("OptionPane.cancelIcon", ImageProvider.get("cancel"));
|
---|
338 | UIManager.put("OptionPane.noIcon", UIManager.get("OptionPane.cancelIcon"));
|
---|
339 | }
|
---|
340 |
|
---|
341 | /**
|
---|
342 | * Setup an exception handler that displays a sorry message and the possibility
|
---|
343 | * to do a bug report.
|
---|
344 | */
|
---|
345 | private static void setupExceptionHandler() {
|
---|
346 | Thread.setDefaultUncaughtExceptionHandler(new BugReportExceptionHandler());
|
---|
347 | }
|
---|
348 | }
|
---|