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