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

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

don't call init twice

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