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

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

fix #8404 - workaround for JDK bug 6322854 (crash when inserting password from clipboard corrupted by KeePass)

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