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

Last change on this file since 2612 was 2053, checked in by Gubaer, 15 years ago

fixed #2322: Regression in r2038 (patches "JOSM destroys its preferences, when disk is full")

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