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

Last change on this file since 6380 was 6380, checked in by Don-vip, 11 years ago

update license/copyright information

  • Property svn:eol-style set to native
File size: 7.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
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;
24
25import org.openstreetmap.josm.Main;
26import org.openstreetmap.josm.actions.JosmAction;
27import org.openstreetmap.josm.data.ServerSidePreferences;
28import org.openstreetmap.josm.gui.MainApplication.Option;
29import org.openstreetmap.josm.gui.widgets.JosmPasswordField;
30import org.openstreetmap.josm.gui.widgets.JosmTextField;
31import org.openstreetmap.josm.tools.GBC;
32import org.openstreetmap.josm.tools.I18n;
33import org.openstreetmap.josm.tools.Shortcut;
34
35public 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}
Note: See TracBrowser for help on using the repository browser.