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

Last change on this file since 1023 was 1023, checked in by stoecker, 16 years ago

close bug #1622. Keyboard shortcuts and specific OS handling

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