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.AppletStub;
|
---|
7 | import java.applet.AppletContext;
|
---|
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 | import javax.swing.JPasswordField;
|
---|
25 | import javax.swing.JTextField;
|
---|
26 |
|
---|
27 | import org.openstreetmap.josm.Main;
|
---|
28 | import org.openstreetmap.josm.actions.JosmAction;
|
---|
29 | import org.openstreetmap.josm.data.ServerSidePreferences;
|
---|
30 | import org.openstreetmap.josm.tools.GBC;
|
---|
31 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
32 |
|
---|
33 | public class MainApplet extends JApplet {
|
---|
34 |
|
---|
35 | public static final class UploadPreferencesAction extends JosmAction {
|
---|
36 | public UploadPreferencesAction() {
|
---|
37 | super(tr("Upload Preferences"), "upload-preferences", tr("Upload the current preferences to the server"),
|
---|
38 | Shortcut.registerShortcut("applet:uploadprefs", tr("Upload Preferences"), KeyEvent.VK_U, Shortcut.GROUP_HOTKEY), true);
|
---|
39 | }
|
---|
40 | public void actionPerformed(ActionEvent e) {
|
---|
41 | ((ServerSidePreferences)Main.pref).upload();
|
---|
42 | }
|
---|
43 | }
|
---|
44 |
|
---|
45 | private final class MainCaller extends Main {
|
---|
46 | private MainCaller() {
|
---|
47 | setContentPane(contentPane);
|
---|
48 | setJMenuBar(menu);
|
---|
49 | setBounds(bounds);
|
---|
50 | }
|
---|
51 | }
|
---|
52 |
|
---|
53 | private final static String[][] paramInfo = {
|
---|
54 | {"username", tr("string"), tr("Name of the user.")},
|
---|
55 | {"password", tr("string"), tr("OSM Password.")},
|
---|
56 | {"geometry", tr("string"), tr("Resize the applet to the given geometry (format: WIDTHxHEIGHT)")},
|
---|
57 | {"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")},
|
---|
58 | {"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")},
|
---|
59 | {"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")},
|
---|
60 | {"reset-preferences", tr("any"),tr("If specified, reset the configuration instead of reading it.")}
|
---|
61 | };
|
---|
62 |
|
---|
63 | private Map<String, Collection<String>> args = new HashMap<String, Collection<String>>();
|
---|
64 |
|
---|
65 | @Override public String[][] getParameterInfo() {
|
---|
66 | return paramInfo;
|
---|
67 | }
|
---|
68 |
|
---|
69 | @Override public void init() {
|
---|
70 | for (String[] s : paramInfo) {
|
---|
71 | Collection<String> p = readParameter(s[0], args.get(s[0]));
|
---|
72 | if (p != null)
|
---|
73 | args.put(s[0], p);
|
---|
74 | }
|
---|
75 | if (!args.containsKey("geometry") && getParameter("width") != null && getParameter("height") != null) {
|
---|
76 | args.put("geometry", Arrays.asList(new String[]{getParameter("width")+"x"+getParameter("height")}));
|
---|
77 | }
|
---|
78 | }
|
---|
79 |
|
---|
80 | @Override public void start() {
|
---|
81 | String username = args.containsKey("username") ? args.get("username").iterator().next() : null;
|
---|
82 | String password = args.containsKey("password") ? args.get("password").iterator().next() : null;
|
---|
83 | if (username == null || password == null) {
|
---|
84 | JPanel p = new JPanel(new GridBagLayout());
|
---|
85 | p.add(new JLabel(tr("Username")), GBC.std().insets(0,0,20,0));
|
---|
86 | JTextField user = new JTextField(username == null ? "" : username);
|
---|
87 | p.add(user, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
88 | p.add(new JLabel(tr("Password")), GBC.std().insets(0,0,20,0));
|
---|
89 | JPasswordField pass = new JPasswordField(password == null ? "" : password);
|
---|
90 | p.add(pass, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
91 | JOptionPane.showMessageDialog(null, p);
|
---|
92 | username = user.getText();
|
---|
93 | password = new String(pass.getPassword());
|
---|
94 | args.put("password", Arrays.asList(new String[]{password}));
|
---|
95 | }
|
---|
96 |
|
---|
97 | Main.applet = true;
|
---|
98 | Main.pref = new ServerSidePreferences(getCodeBase());
|
---|
99 | ((ServerSidePreferences)Main.pref).download(username, password);
|
---|
100 |
|
---|
101 | Main.preConstructorInit(args);
|
---|
102 | Main.parent = this;
|
---|
103 | new MainCaller().postConstructorProcessCmdLine(args);
|
---|
104 |
|
---|
105 | MainMenu m = Main.main.menu; // shortcut
|
---|
106 |
|
---|
107 | // remove offending stuff from JOSM (that would break the SecurityManager)
|
---|
108 | m.remove(m.fileMenu);
|
---|
109 | m.editMenu.add(new UploadPreferencesAction());
|
---|
110 | m.openFile.setEnabled(false);
|
---|
111 | m.exit.setEnabled(false);
|
---|
112 | m.save.setEnabled(false);
|
---|
113 | m.saveAs.setEnabled(false);
|
---|
114 | m.gpxExport.setEnabled(false);
|
---|
115 | }
|
---|
116 |
|
---|
117 | private Collection<String> readParameter(String s, Collection<String> v) {
|
---|
118 | String param = getParameter(s);
|
---|
119 | if (param != null) {
|
---|
120 | if (v == null)
|
---|
121 | v = new LinkedList<String>();
|
---|
122 | v.addAll(Arrays.asList(param.split(";")));
|
---|
123 | }
|
---|
124 | return v;
|
---|
125 | }
|
---|
126 |
|
---|
127 | public static void main(String[] args) {
|
---|
128 | final JFrame frame = new JFrame("Java OpenStreetMap Applet");
|
---|
129 | MainApplet applet = new MainApplet();
|
---|
130 | applet.setStub(new AppletStub() {
|
---|
131 | public void appletResize(int w, int h) {
|
---|
132 | frame.setSize(w, h);
|
---|
133 | }
|
---|
134 |
|
---|
135 | public AppletContext getAppletContext() {
|
---|
136 | return null;
|
---|
137 | }
|
---|
138 |
|
---|
139 | public URL getCodeBase() {
|
---|
140 | try {
|
---|
141 | return new File(".").toURI().toURL();
|
---|
142 | } catch (Exception e) {
|
---|
143 | e.printStackTrace();
|
---|
144 | return null;
|
---|
145 | }
|
---|
146 | }
|
---|
147 |
|
---|
148 | public URL getDocumentBase() {
|
---|
149 | return getCodeBase();
|
---|
150 | }
|
---|
151 |
|
---|
152 | public String getParameter(String k) {
|
---|
153 | return null;
|
---|
154 | }
|
---|
155 |
|
---|
156 | public boolean isActive() {
|
---|
157 | return true;
|
---|
158 | }
|
---|
159 | });
|
---|
160 | applet.init();
|
---|
161 | applet.start();
|
---|
162 | frame.setContentPane(applet);
|
---|
163 | frame.setVisible(true);
|
---|
164 | }
|
---|
165 | }
|
---|