source: josm/trunk/src/org/openstreetmap/josm/gui/MainApplet.java@ 6889

Last change on this file since 6889 was 6889, checked in by Don-vip, 11 years ago

fix some Sonar issues (JLS order)

  • Property svn:eol-style set to native
File size: 7.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.applet.AppletContext;
7import java.applet.AppletStub;
8import java.awt.GridBagLayout;
9import java.awt.event.ActionEvent;
10import java.awt.event.KeyEvent;
11import java.io.File;
12import java.net.URL;
13import java.util.Arrays;
14import java.util.Collection;
15import java.util.HashMap;
16import java.util.LinkedList;
17import java.util.Map;
18
19import javax.swing.JApplet;
20import javax.swing.JFrame;
21import javax.swing.JLabel;
22import javax.swing.JOptionPane;
23import javax.swing.JPanel;
24
25import org.openstreetmap.josm.Main;
26import org.openstreetmap.josm.actions.JosmAction;
27import org.openstreetmap.josm.data.ServerSidePreferences;
28import org.openstreetmap.josm.gui.MainApplication.Option;
29import org.openstreetmap.josm.gui.widgets.JosmPasswordField;
30import org.openstreetmap.josm.gui.widgets.JosmTextField;
31import org.openstreetmap.josm.tools.GBC;
32import org.openstreetmap.josm.tools.I18n;
33import org.openstreetmap.josm.tools.Shortcut;
34import org.openstreetmap.josm.tools.Utils;
35
36public class MainApplet extends JApplet {
37
38 static final JFrame frame = new JFrame("Java OpenStreetMap Editor");
39
40 public static final class UploadPreferencesAction extends JosmAction {
41 /**
42 * Constructs a new {@code UploadPreferencesAction}.
43 */
44 public UploadPreferencesAction() {
45 super(tr("Upload Preferences"), "upload-preferences", tr("Upload the current preferences to the server"),
46 Shortcut.registerShortcut("applet:uploadprefs", tr("Upload Preferences"), KeyEvent.CHAR_UNDEFINED, Shortcut.NONE), true);
47 }
48 @Override
49 public void actionPerformed(ActionEvent e) {
50 ((ServerSidePreferences)Main.pref).upload();
51 }
52 }
53
54 private final class MainCaller extends Main {
55 private MainCaller(Map<Option, Collection<String>> mapargs) {
56 addListener();
57 setContentPane(contentPanePrivate);
58 setJMenuBar(menu);
59 postConstructorProcessCmdLine(mapargs);
60 }
61 }
62
63 private static final String[][] paramInfo = {
64 {"username", tr("string"), tr("Name of the user.")},
65 {"password", tr("string"), tr("OSM Password.")},
66 {"geometry", tr("string"), tr("Resize the applet to the given geometry (format: WIDTHxHEIGHT)")},
67 {"download", tr("string;string;..."), tr("Download each. Can be x1,y1,x2,y2 an URL containing lat=y&lon=x&zoom=z or a filename")},
68 {"downloadgps", tr("string;string;..."), tr("Download each as raw gps. Can be x1,y1,x2,y2 an URL containing lat=y&lon=x&zoom=z or a filename")},
69 {"selection", tr("string;string;..."), tr("Add each to the initial selection. Can be a google-like search string or an URL which returns osm-xml")},
70 {"reset-preferences", tr("any"),tr("If specified, reset the configuration instead of reading it.")}
71 };
72
73 private Map<String, Collection<String>> args = new HashMap<String, Collection<String>>();
74
75 @Override public String[][] getParameterInfo() {
76 return paramInfo;
77 }
78
79 @Override public void init() {
80 for (String[] s : paramInfo) {
81 Collection<String> p = readParameter(s[0], args.get(s[0]));
82 if (p != null) {
83 args.put(s[0], p);
84 }
85 }
86 if (!args.containsKey("geometry") && getParameter("width") != null && getParameter("height") != null) {
87 args.put("geometry", Arrays.asList(new String[]{getParameter("width")+"x"+getParameter("height")}));
88 }
89 }
90
91 @Override public void start() {
92 I18n.init();
93 Main.checkJava6();
94
95 String url = getParameter("load_url");
96 if(url != null)
97 args.put("download", Arrays.asList(new String[]{url}));
98
99 // initialize the platform hook, and
100 Main.determinePlatformHook();
101 // call the really early hook before we do anything else
102 Main.platform.preStartupHook();
103
104 Main.initAppletPreferences(getCodeBase());
105
106 String lang = getParameter("language");
107 I18n.set(lang != null ? lang : Main.pref.get("language", null));
108 Main.pref.updateSystemProperties();
109
110 try
111 {
112 ((ServerSidePreferences)Main.pref).download();
113 } catch (ServerSidePreferences.MissingPassword e) {
114 String username = args.containsKey("username") ? args.get("username").iterator().next() : null;
115 String password = args.containsKey("password") ? args.get("password").iterator().next() : null;
116 if (username == null || password == null) {
117 JPanel p = new JPanel(new GridBagLayout());
118 p.add(new JLabel(tr(e.realm)), GBC.eol().fill(GBC.HORIZONTAL));
119 p.add(new JLabel(tr("Username")), GBC.std().insets(0,0,20,0));
120 JosmTextField user = new JosmTextField(username == null ? "" : username);
121 p.add(user, GBC.eol().fill(GBC.HORIZONTAL));
122 p.add(new JLabel(tr("Password")), GBC.std().insets(0,0,20,0));
123 JosmPasswordField pass = new JosmPasswordField(password == null ? "" : password);
124 p.add(pass, GBC.eol().fill(GBC.HORIZONTAL));
125 JOptionPane.showMessageDialog(null, p);
126 username = user.getText();
127 if("".equals(username))
128 username = null;
129 password = new String(pass.getPassword());
130 if("".equals(password))
131 password = null;
132 }
133 if (username != null && password != null) {
134 ((ServerSidePreferences)Main.pref).download(username, password);
135 }
136 }
137
138 Main.preConstructorInit(Option.fromStringMap(args));
139 Main.pref.updateSystemProperties();
140 Main.parent = frame;
141
142 new MainCaller(Option.fromStringMap(args));
143
144 MainMenu m = Main.main.menu; // shortcut
145
146 // remove offending stuff from JOSM (that would break the SecurityManager)
147 m.editMenu.add(new UploadPreferencesAction());
148 m.openFile.setEnabled(false);
149 m.exit.setEnabled(false);
150 m.save.setEnabled(false);
151 m.saveAs.setEnabled(false);
152 m.gpxExport.setEnabled(false);
153 }
154
155 private Collection<String> readParameter(String s, Collection<String> v) {
156 String param = getParameter(s);
157 if (param != null) {
158 if (v == null) {
159 v = new LinkedList<String>();
160 }
161 v.addAll(Arrays.asList(param.split(";")));
162 }
163 return v;
164 }
165
166 public static void main(String[] args) {
167 Main.applet = true;
168 MainApplet applet = new MainApplet();
169 Main.initAppletPreferences(applet.getCodeBase());
170 applet.setStub(new AppletStub() {
171 @Override
172 public void appletResize(int w, int h) {
173 frame.setSize(w, h);
174 }
175
176 @Override
177 public AppletContext getAppletContext() {
178 return null;
179 }
180
181 @Override
182 public URL getCodeBase() {
183 return Utils.fileToURL(new File("."));
184 }
185
186 @Override
187 public URL getDocumentBase() {
188 return getCodeBase();
189 }
190
191 @Override
192 public String getParameter(String k) {
193 return null;
194 }
195
196 @Override
197 public boolean isActive() {
198 return true;
199 }
200 });
201 applet.init();
202 applet.start();
203 frame.setContentPane(applet);
204 frame.setVisible(true);
205 }
206}
Note: See TracBrowser for help on using the repository browser.