1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.applet.AppletContext;
|
---|
7 | import java.applet.AppletStub;
|
---|
8 | import java.awt.GridBagLayout;
|
---|
9 | import java.awt.event.ActionEvent;
|
---|
10 | import java.awt.event.KeyEvent;
|
---|
11 | import java.io.File;
|
---|
12 | import java.net.URL;
|
---|
13 | import java.util.Arrays;
|
---|
14 | import java.util.Collection;
|
---|
15 | import java.util.HashMap;
|
---|
16 | import java.util.LinkedList;
|
---|
17 | import java.util.Map;
|
---|
18 |
|
---|
19 | import javax.swing.JApplet;
|
---|
20 | import javax.swing.JFrame;
|
---|
21 | import javax.swing.JLabel;
|
---|
22 | import javax.swing.JOptionPane;
|
---|
23 | import javax.swing.JPanel;
|
---|
24 |
|
---|
25 | import org.openstreetmap.josm.Main;
|
---|
26 | import org.openstreetmap.josm.actions.JosmAction;
|
---|
27 | import org.openstreetmap.josm.data.ServerSidePreferences;
|
---|
28 | import org.openstreetmap.josm.gui.MainApplication.Option;
|
---|
29 | import org.openstreetmap.josm.gui.widgets.JosmPasswordField;
|
---|
30 | import org.openstreetmap.josm.gui.widgets.JosmTextField;
|
---|
31 | import org.openstreetmap.josm.tools.GBC;
|
---|
32 | import org.openstreetmap.josm.tools.I18n;
|
---|
33 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
34 | import org.openstreetmap.josm.tools.Utils;
|
---|
35 |
|
---|
36 | public 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 | }
|
---|