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

Last change on this file since 138 was 138, checked in by imi, 18 years ago
  • added feature "Save" (old save is now "Save as")
  • added feature to rename a layer
  • fixed bug that geo-images pop up more than once
  • fixed bug where you could try to change properties for 0 objects
File size: 7.1 KB
Line 
1//Licence: GPL
2package org.openstreetmap.josm.gui;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Toolkit;
7import java.awt.event.WindowAdapter;
8import java.awt.event.WindowEvent;
9import java.io.File;
10import java.io.IOException;
11import java.util.Arrays;
12import java.util.Collection;
13import java.util.HashMap;
14import java.util.LinkedList;
15import java.util.List;
16import java.util.Locale;
17import java.util.Map;
18
19import javax.swing.JFrame;
20import javax.swing.JOptionPane;
21
22import org.openstreetmap.josm.Main;
23import org.openstreetmap.josm.gui.layer.Layer;
24import org.openstreetmap.josm.gui.layer.OsmDataLayer;
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 mainFrame.setContentPane(contentPane);
38 mainFrame.setJMenuBar(mainMenu);
39 mainFrame.setBounds(bounds);
40 mainFrame.addWindowListener(new WindowAdapter(){
41 @Override public void windowClosing(final WindowEvent arg0) {
42 if (Main.map != null) {
43 boolean modified = false;
44 boolean uploadedModified = false;
45 for (final Layer l : Main.map.mapView.getAllLayers()) {
46 if (l instanceof OsmDataLayer && ((OsmDataLayer)l).isModified()) {
47 modified = true;
48 uploadedModified = ((OsmDataLayer)l).uploadedModified;
49 break;
50 }
51 }
52 if (modified) {
53 final String msg = uploadedModified ? "\n"+tr("Hint: Some changes came from uploading new data to the server.") : "";
54 final int answer = JOptionPane.showConfirmDialog(
55 Main.parent, tr("There are unsaved changes. Really quit?")+msg,
56 tr("Unsaved Changes"), JOptionPane.YES_NO_OPTION);
57 if (answer != JOptionPane.YES_OPTION)
58 return;
59 }
60 }
61 System.exit(0);
62 }
63 });
64 mainFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
65 }
66
67 /**
68 * Main application Startup
69 * @param args No parameters accepted.
70 */
71 public static void main(final String[] argArray) {
72 /////////////////////////////////////////////////////////////////////////
73 // TO ALL TRANSLATORS
74 /////////////////////////////////////////////////////////////////////////
75 // Do not translate the early strings below until the locale is set up.
76 // The cannot be translated. That's live. Really. Sorry.
77 //
78 // The next sending me a patch translating these strings owe me a beer!
79 //
80 // Imi.
81 /////////////////////////////////////////////////////////////////////////
82
83 Thread.setDefaultUncaughtExceptionHandler(new BugReportExceptionHandler());
84
85 // construct argument table
86 List<String> argList = Arrays.asList(argArray);
87 Map<String, Collection<String>> args = new HashMap<String, Collection<String>>();
88 for (String arg : argArray) {
89 if (!arg.startsWith("--"))
90 arg = "--download="+arg;
91 int i = arg.indexOf('=');
92 String key = i == -1 ? arg.substring(2) : arg.substring(2,i);
93 String value = i == -1 ? "" : arg.substring(i+1);
94 Collection<String> v = args.get(key);
95 if (v == null)
96 v = new LinkedList<String>();
97 v.add(value);
98 args.put(key, v);
99 }
100
101 // get the preferences.
102 final File prefDir = new File(Main.pref.getPreferencesDir());
103 if (prefDir.exists() && !prefDir.isDirectory()) {
104 JOptionPane.showMessageDialog(null, "Cannot open preferences directory: "+Main.pref.getPreferencesDir());
105 return;
106 }
107 if (!prefDir.exists())
108 prefDir.mkdirs();
109 try {
110 if (args.containsKey("reset-preferences")) {
111 Main.pref.resetToDefault();
112 } else {
113 Main.pref.load();
114 }
115 } catch (final IOException e1) {
116 e1.printStackTrace();
117 JOptionPane.showMessageDialog(null, "Preferences could not be loaded. Writing default preference file to "+pref.getPreferencesDir()+"preferences");
118 Main.pref.resetToDefault();
119 }
120
121 // setup the locale
122 if (args.containsKey("language") && !args.get("language").isEmpty() && args.get("language").iterator().next().length() >= 2) {
123 String s = args.get("language").iterator().next();
124 Locale l = null;
125 if (s.length() <= 2 || s.charAt(2) != '_')
126 l = new Locale(s);
127 else if (s.length() <= 5 || s.charAt(5) != '.')
128 l = new Locale(s.substring(0,2), s.substring(3));
129 else
130 l = new Locale(s.substring(0,2), s.substring(3,5), s.substring(6));
131 Locale.setDefault(l);
132 } else if (!Main.pref.get("language").equals("")) {
133 String lang = Main.pref.get("language");
134 for (Locale l : Locale.getAvailableLocales()) {
135 if (l.toString().equals(lang)) {
136 Locale.setDefault(l);
137 break;
138 }
139 }
140 }
141
142 // Locale is set. From now on, tr(), trn() and trc() may be called.
143
144 if (argList.contains("--help") || argList.contains("-?") || argList.contains("-h")) {
145 System.out.println(tr("Java OpenStreetMap Editor")+"\n\n"+
146 tr("usage")+":\n"+
147 "\tjava -jar josm.jar <option> <option> <option>...\n\n"+
148 tr("options")+":\n"+
149 "\t--help|-?|-h "+tr("Show this help")+"\n"+
150 "\t--geometry=widthxheight(+|-)x(+|-)y "+tr("Standard unix geometry argument")+"\n"+
151 "\t[--download=]minlat,minlon,maxlat,maxlon "+tr("Download the bounding box")+"\n"+
152 "\t[--download=]<url> "+tr("Download the location at the url (with lat=x&lon=y&zoom=z)")+"\n"+
153 "\t[--download=]<filename> "+tr("Open file (as raw gps, if .gpx or .csv)")+"\n"+
154 "\t--downloadgps=minlat,minlon,maxlat,maxlon "+tr("Download the bounding box as raw gps")+"\n"+
155 "\t--selection=<searchstring> "+tr("Select with the given search")+"\n"+
156 "\t--no-fullscreen "+tr("Don't launch in fullscreen mode")+"\n"+
157 "\t--reset-preferences "+tr("Reset the preferences to default")+"\n\n"+
158 "\t--language=<language> "+tr("Set the language. Example: ")+"\n\n"+
159 tr("examples")+":\n"+
160 "\tjava -jar josm.jar track1.gpx track2.gpx london.osm\n"+
161 "\tjava -jar josm.jar http://www.openstreetmap.org/index.html?lat=43.2&lon=11.1&zoom=13\n"+
162 "\tjava -jar josm.jar london.osm --selection=http://www.ostertag.name/osm/OSM_errors_node-duplicate.xml\n"+
163 "\tjava -jar josm.jar 43.2,11.1,43.4,11.4\n\n"+
164
165 tr("Parameters are read in the order they are specified, so make sure you load\n"+
166 "some data before --selection")+"\n\n"+
167 tr("Instead of --download=<bbox> you may specify osm://<bbox>\n"));
168 System.exit(0);
169 }
170
171 preConstructorInit(args);
172 JFrame mainFrame = new JFrame(tr("Java Open Street Map - Editor"));
173 Main.parent = mainFrame;
174 Main main = new MainApplication(mainFrame);
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}
Note: See TracBrowser for help on using the repository browser.