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

Last change on this file since 2017 was 2017, checked in by Gubaer, 16 years ago

removed OptionPaneUtil
cleanup of deprecated Layer API
cleanup of deprecated APIs in OsmPrimitive and Way
cleanup of imports

  • Property svn:eol-style set to native
File size: 6.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.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.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
104 // initialize the plaform hook, and
105 Main.determinePlatformHook();
106 // call the really early hook before we anything else
107 Main.platform.preStartupHook();
108
109 new MainCaller().postConstructorProcessCmdLine(args);
110
111 MainMenu m = Main.main.menu; // shortcut
112
113 // remove offending stuff from JOSM (that would break the SecurityManager)
114 m.remove(m.fileMenu);
115 m.editMenu.add(new UploadPreferencesAction());
116 m.openFile.setEnabled(false);
117 m.exit.setEnabled(false);
118 m.save.setEnabled(false);
119 m.saveAs.setEnabled(false);
120 m.gpxExport.setEnabled(false);
121 }
122
123 private Collection<String> readParameter(String s, Collection<String> v) {
124 String param = getParameter(s);
125 if (param != null) {
126 if (v == null)
127 v = new LinkedList<String>();
128 v.addAll(Arrays.asList(param.split(";")));
129 }
130 return v;
131 }
132
133 public static void main(String[] args) {
134 final JFrame frame = new JFrame("Java OpenStreetMap Editor");
135 MainApplet applet = new MainApplet();
136 applet.setStub(new AppletStub() {
137 public void appletResize(int w, int h) {
138 frame.setSize(w, h);
139 }
140
141 public AppletContext getAppletContext() {
142 return null;
143 }
144
145 public URL getCodeBase() {
146 try {
147 return new File(".").toURI().toURL();
148 } catch (Exception e) {
149 e.printStackTrace();
150 return null;
151 }
152 }
153
154 public URL getDocumentBase() {
155 return getCodeBase();
156 }
157
158 public String getParameter(String k) {
159 return null;
160 }
161
162 public boolean isActive() {
163 return true;
164 }
165 });
166 applet.init();
167 applet.start();
168 frame.setContentPane(applet);
169 frame.setVisible(true);
170 }
171}
Note: See TracBrowser for help on using the repository browser.