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

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