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

Last change on this file since 3815 was 3815, checked in by stoecker, 14 years ago

fixes for applet

  • Property svn:eol-style set to native
File size: 6.9 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 // initialize the platform hook, and
89 Main.determinePlatformHook();
90 // call the really early hook before we do anything else
91 Main.platform.preStartupHook();
92
93 Main.pref = new ServerSidePreferences(getCodeBase());
94 try
95 {
96 ((ServerSidePreferences)Main.pref).download();
97 } catch (ServerSidePreferences.MissingPassword e) {
98 String username = args.containsKey("username") ? args.get("username").iterator().next() : null;
99 String password = args.containsKey("password") ? args.get("password").iterator().next() : null;
100 if (username == null || password == null) {
101 JPanel p = new JPanel(new GridBagLayout());
102 p.add(new JLabel(tr(e.realm)), GBC.eol().fill(GBC.HORIZONTAL));
103 p.add(new JLabel(tr("Username")), GBC.std().insets(0,0,20,0));
104 JTextField user = new JTextField(username == null ? "" : username);
105 p.add(user, GBC.eol().fill(GBC.HORIZONTAL));
106 p.add(new JLabel(tr("Password")), GBC.std().insets(0,0,20,0));
107 JPasswordField pass = new JPasswordField(password == null ? "" : password);
108 p.add(pass, GBC.eol().fill(GBC.HORIZONTAL));
109 JOptionPane.showMessageDialog(null, p);
110 username = user.getText();
111 if("".equals(username))
112 username = null;
113 password = new String(pass.getPassword());
114 if("".equals(password))
115 password = null;
116 }
117 if (username != null && password != null) {
118 ((ServerSidePreferences)Main.pref).download(username, password);
119 }
120 }
121
122 Main.preConstructorInit(args);
123 Main.parent = frame;
124 Main.addListener();
125
126 new MainCaller().postConstructorProcessCmdLine(args);
127
128 MainMenu m = Main.main.menu; // shortcut
129
130 // remove offending stuff from JOSM (that would break the SecurityManager)
131 m.editMenu.add(new UploadPreferencesAction());
132 m.openFile.setEnabled(false);
133 m.exit.setEnabled(false);
134 m.save.setEnabled(false);
135 m.saveAs.setEnabled(false);
136 m.gpxExport.setEnabled(false);
137 }
138
139 private Collection<String> readParameter(String s, Collection<String> v) {
140 String param = getParameter(s);
141 if (param != null) {
142 if (v == null) {
143 v = new LinkedList<String>();
144 }
145 v.addAll(Arrays.asList(param.split(";")));
146 }
147 return v;
148 }
149
150 public static void main(String[] args) {
151 Main.applet = true;
152 MainApplet applet = new MainApplet();
153 Main.pref = new ServerSidePreferences(applet.getCodeBase());
154 applet.setStub(new AppletStub() {
155 public void appletResize(int w, int h) {
156 frame.setSize(w, h);
157 }
158
159 public AppletContext getAppletContext() {
160 return null;
161 }
162
163 public URL getCodeBase() {
164 try {
165 return new File(".").toURI().toURL();
166 } catch (Exception e) {
167 e.printStackTrace();
168 return null;
169 }
170 }
171
172 public URL getDocumentBase() {
173 return getCodeBase();
174 }
175
176 public String getParameter(String k) {
177 return null;
178 }
179
180 public boolean isActive() {
181 return true;
182 }
183 });
184 applet.init();
185 applet.start();
186 frame.setContentPane(applet);
187 frame.setVisible(true);
188 }
189}
Note: See TracBrowser for help on using the repository browser.