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

Last change on this file since 3872 was 3819, checked in by stoecker, 13 years ago

add applet url handling

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