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

Last change on this file since 195 was 195, checked in by imi, 18 years ago
  • changed ServerSidePreferences to actually upload / download something to server
File size: 4.4 KB
Line 
1package org.openstreetmap.josm.gui;
2
3import static org.openstreetmap.josm.tools.I18n.tr;
4
5import java.awt.GridBagLayout;
6import java.awt.event.ActionEvent;
7import java.awt.event.KeyEvent;
8import java.util.Arrays;
9import java.util.Collection;
10import java.util.HashMap;
11import java.util.LinkedList;
12import java.util.Map;
13
14import javax.swing.JApplet;
15import javax.swing.JLabel;
16import javax.swing.JOptionPane;
17import javax.swing.JPanel;
18import javax.swing.JPasswordField;
19import javax.swing.JTextField;
20
21import org.openstreetmap.josm.Main;
22import org.openstreetmap.josm.actions.JosmAction;
23import org.openstreetmap.josm.data.ServerSidePreferences;
24import org.openstreetmap.josm.tools.GBC;
25
26public class MainApplet extends JApplet {
27
28 public static final class UploadPreferencesAction extends JosmAction {
29 public UploadPreferencesAction() {
30 super(tr("Upload Preferences"), "upload-preferences", tr("Upload the current preferences to the server"), KeyEvent.VK_U, KeyEvent.CTRL_DOWN_MASK | KeyEvent.SHIFT_DOWN_MASK, true);
31 }
32 public void actionPerformed(ActionEvent e) {
33 ((ServerSidePreferences)Main.pref).upload();
34 }
35 }
36
37 private final class MainCaller extends Main {
38 private MainCaller() {
39 setContentPane(contentPane);
40 setJMenuBar(menu);
41 setBounds(bounds);
42 }
43 }
44
45 private final static String[][] paramInfo = {
46 {"username", tr("string"), tr("Name of the user.")},
47 {"password", tr("string"), tr("OSM Password.")},
48 {"geometry", tr("string"), tr("Size the applet to the given geometry (format: WIDTHxHEIGHT)")},
49 {"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")},
50 {"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")},
51 {"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")},
52 {"reset-preferences", tr("any"),tr("If specified, reset the configuration instead of reading it.")}
53 };
54
55 private Map<String, Collection<String>> args = new HashMap<String, Collection<String>>();
56
57 @Override public String[][] getParameterInfo() {
58 return paramInfo;
59 }
60
61 @Override public void init() {
62 for (String[] s : paramInfo) {
63 Collection<String> p = readParameter(s[0], args.get(s[0]));
64 if (p != null)
65 args.put(s[0], p);
66 }
67 if (!args.containsKey("geometry") && getParameter("width") != null && getParameter("height") != null) {
68 args.put("geometry", Arrays.asList(new String[]{getParameter("width")+"x"+getParameter("height")}));
69 }
70 }
71
72 @Override public void start() {
73 String username = args.containsKey("username") ? args.get("username").iterator().next() : null;
74 String password = args.containsKey("password") ? args.get("password").iterator().next() : null;
75 if (username == null || password == null) {
76 JPanel p = new JPanel(new GridBagLayout());
77 p.add(new JLabel(tr("Username")), GBC.std().insets(0,0,20,0));
78 JTextField user = new JTextField(username == null ? "" : username);
79 p.add(user, GBC.eol().fill(GBC.HORIZONTAL));
80 p.add(new JLabel(tr("Password")), GBC.std().insets(0,0,20,0));
81 JPasswordField pass = new JPasswordField(password == null ? "" : password);
82 p.add(pass, GBC.eol().fill(GBC.HORIZONTAL));
83 JOptionPane.showMessageDialog(null, p);
84 username = user.getText();
85 password = new String(pass.getPassword());
86 args.put("password", Arrays.asList(new String[]{password}));
87 }
88
89 Main.applet = true;
90 Main.pref = new ServerSidePreferences(getCodeBase());
91 ((ServerSidePreferences)Main.pref).download(username, password);
92
93 Main.preConstructorInit(args);
94 Main.parent = this;
95 new MainCaller().postConstructorProcessCmdLine(args);
96
97 MainMenu m = Main.main.menu; // shortcut
98
99 // remove offending stuff from JOSM (that would break the SecurityManager)
100 m.remove(m.fileMenu);
101 m.editMenu.add(new UploadPreferencesAction());
102 m.open.setEnabled(false);
103 m.exit.setEnabled(false);
104 m.save.setEnabled(false);
105 m.saveAs.setEnabled(false);
106 m.gpxExport.setEnabled(false);
107 }
108
109 private Collection<String> readParameter(String s, Collection<String> v) {
110 String param = getParameter(s);
111 if (param != null) {
112 if (v == null)
113 v = new LinkedList<String>();
114 v.addAll(Arrays.asList(param.split(";")));
115 }
116 return v;
117 }
118}
Note: See TracBrowser for help on using the repository browser.