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

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