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 |
|
---|
35 | public class MainApplet extends JApplet {
|
---|
36 |
|
---|
37 | final static JFrame frame = new JFrame("Java OpenStreetMap Editor");
|
---|
38 |
|
---|
39 | public static final class UploadPreferencesAction extends JosmAction {
|
---|
40 | /**
|
---|
41 | * Constructs a new {@code UploadPreferencesAction}.
|
---|
42 | */
|
---|
43 | public UploadPreferencesAction() {
|
---|
44 | super(tr("Upload Preferences"), "upload-preferences", tr("Upload the current preferences to the server"),
|
---|
45 | Shortcut.registerShortcut("applet:uploadprefs", tr("Upload Preferences"), KeyEvent.CHAR_UNDEFINED, Shortcut.NONE), true);
|
---|
46 | }
|
---|
47 | @Override
|
---|
48 | public void actionPerformed(ActionEvent e) {
|
---|
49 | ((ServerSidePreferences)Main.pref).upload();
|
---|
50 | }
|
---|
51 | }
|
---|
52 |
|
---|
53 | private final class MainCaller extends Main {
|
---|
54 | private MainCaller(Map<Option, Collection<String>> mapargs) {
|
---|
55 | addListener();
|
---|
56 | setContentPane(contentPanePrivate);
|
---|
57 | setJMenuBar(menu);
|
---|
58 | postConstructorProcessCmdLine(mapargs);
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | private final static String[][] paramInfo = {
|
---|
63 | {"username", tr("string"), tr("Name of the user.")},
|
---|
64 | {"password", tr("string"), tr("OSM Password.")},
|
---|
65 | {"geometry", tr("string"), tr("Resize the applet to the given geometry (format: WIDTHxHEIGHT)")},
|
---|
66 | {"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")},
|
---|
67 | {"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")},
|
---|
68 | {"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")},
|
---|
69 | {"reset-preferences", tr("any"),tr("If specified, reset the configuration instead of reading it.")}
|
---|
70 | };
|
---|
71 |
|
---|
72 | private Map<String, Collection<String>> args = new HashMap<String, Collection<String>>();
|
---|
73 |
|
---|
74 | @Override public String[][] getParameterInfo() {
|
---|
75 | return paramInfo;
|
---|
76 | }
|
---|
77 |
|
---|
78 | @Override public void init() {
|
---|
79 | for (String[] s : paramInfo) {
|
---|
80 | Collection<String> p = readParameter(s[0], args.get(s[0]));
|
---|
81 | if (p != null) {
|
---|
82 | args.put(s[0], p);
|
---|
83 | }
|
---|
84 | }
|
---|
85 | if (!args.containsKey("geometry") && getParameter("width") != null && getParameter("height") != null) {
|
---|
86 | args.put("geometry", Arrays.asList(new String[]{getParameter("width")+"x"+getParameter("height")}));
|
---|
87 | }
|
---|
88 | }
|
---|
89 |
|
---|
90 | @Override public void start() {
|
---|
91 | I18n.init();
|
---|
92 | Main.checkJava6();
|
---|
93 |
|
---|
94 | String url = getParameter("load_url");
|
---|
95 | if(url != null)
|
---|
96 | args.put("download", Arrays.asList(new String[]{url}));
|
---|
97 |
|
---|
98 | // initialize the platform hook, and
|
---|
99 | Main.determinePlatformHook();
|
---|
100 | // call the really early hook before we do anything else
|
---|
101 | Main.platform.preStartupHook();
|
---|
102 |
|
---|
103 | Main.pref = new ServerSidePreferences(getCodeBase());
|
---|
104 |
|
---|
105 | String lang = getParameter("language");
|
---|
106 | I18n.set(lang != null ? lang : Main.pref.get("language", null));
|
---|
107 | Main.pref.updateSystemProperties();
|
---|
108 |
|
---|
109 | try
|
---|
110 | {
|
---|
111 | ((ServerSidePreferences)Main.pref).download();
|
---|
112 | } catch (ServerSidePreferences.MissingPassword e) {
|
---|
113 | String username = args.containsKey("username") ? args.get("username").iterator().next() : null;
|
---|
114 | String password = args.containsKey("password") ? args.get("password").iterator().next() : null;
|
---|
115 | if (username == null || password == null) {
|
---|
116 | JPanel p = new JPanel(new GridBagLayout());
|
---|
117 | p.add(new JLabel(tr(e.realm)), GBC.eol().fill(GBC.HORIZONTAL));
|
---|
118 | p.add(new JLabel(tr("Username")), GBC.std().insets(0,0,20,0));
|
---|
119 | JosmTextField user = new JosmTextField(username == null ? "" : username);
|
---|
120 | p.add(user, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
121 | p.add(new JLabel(tr("Password")), GBC.std().insets(0,0,20,0));
|
---|
122 | JosmPasswordField pass = new JosmPasswordField(password == null ? "" : password);
|
---|
123 | p.add(pass, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
124 | JOptionPane.showMessageDialog(null, p);
|
---|
125 | username = user.getText();
|
---|
126 | if("".equals(username))
|
---|
127 | username = null;
|
---|
128 | password = new String(pass.getPassword());
|
---|
129 | if("".equals(password))
|
---|
130 | password = null;
|
---|
131 | }
|
---|
132 | if (username != null && password != null) {
|
---|
133 | ((ServerSidePreferences)Main.pref).download(username, password);
|
---|
134 | }
|
---|
135 | }
|
---|
136 |
|
---|
137 | Main.preConstructorInit(Option.fromStringMap(args));
|
---|
138 | Main.pref.updateSystemProperties();
|
---|
139 | Main.parent = frame;
|
---|
140 |
|
---|
141 | new MainCaller(Option.fromStringMap(args));
|
---|
142 |
|
---|
143 | MainMenu m = Main.main.menu; // shortcut
|
---|
144 |
|
---|
145 | // remove offending stuff from JOSM (that would break the SecurityManager)
|
---|
146 | m.editMenu.add(new UploadPreferencesAction());
|
---|
147 | m.openFile.setEnabled(false);
|
---|
148 | m.exit.setEnabled(false);
|
---|
149 | m.save.setEnabled(false);
|
---|
150 | m.saveAs.setEnabled(false);
|
---|
151 | m.gpxExport.setEnabled(false);
|
---|
152 | }
|
---|
153 |
|
---|
154 | private Collection<String> readParameter(String s, Collection<String> v) {
|
---|
155 | String param = getParameter(s);
|
---|
156 | if (param != null) {
|
---|
157 | if (v == null) {
|
---|
158 | v = new LinkedList<String>();
|
---|
159 | }
|
---|
160 | v.addAll(Arrays.asList(param.split(";")));
|
---|
161 | }
|
---|
162 | return v;
|
---|
163 | }
|
---|
164 |
|
---|
165 | public static void main(String[] args) {
|
---|
166 | Main.applet = true;
|
---|
167 | MainApplet applet = new MainApplet();
|
---|
168 | Main.pref = new ServerSidePreferences(applet.getCodeBase());
|
---|
169 | applet.setStub(new AppletStub() {
|
---|
170 | @Override
|
---|
171 | public void appletResize(int w, int h) {
|
---|
172 | frame.setSize(w, h);
|
---|
173 | }
|
---|
174 |
|
---|
175 | @Override
|
---|
176 | public AppletContext getAppletContext() {
|
---|
177 | return null;
|
---|
178 | }
|
---|
179 |
|
---|
180 | @Override
|
---|
181 | public URL getCodeBase() {
|
---|
182 | try {
|
---|
183 | return new File(".").toURI().toURL();
|
---|
184 | } catch (Exception e) {
|
---|
185 | e.printStackTrace();
|
---|
186 | return null;
|
---|
187 | }
|
---|
188 | }
|
---|
189 |
|
---|
190 | @Override
|
---|
191 | public URL getDocumentBase() {
|
---|
192 | return getCodeBase();
|
---|
193 | }
|
---|
194 |
|
---|
195 | @Override
|
---|
196 | public String getParameter(String k) {
|
---|
197 | return null;
|
---|
198 | }
|
---|
199 |
|
---|
200 | @Override
|
---|
201 | public boolean isActive() {
|
---|
202 | return true;
|
---|
203 | }
|
---|
204 | });
|
---|
205 | applet.init();
|
---|
206 | applet.start();
|
---|
207 | frame.setContentPane(applet);
|
---|
208 | frame.setVisible(true);
|
---|
209 | }
|
---|
210 | }
|
---|