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

Last change on this file since 5704 was 5279, checked in by bastiK, 12 years ago

use gnu getopt for command line processing

improvements:

  • long options can be separated by space from the argument (currently

only "=" works as seperator)

  • short options can be grouped (there aren't many short options at the moment, this is for later)
  • Property svn:eol-style set to native
File size: 7.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.gui.MainApplication.Option;
31import org.openstreetmap.josm.tools.GBC;
32import org.openstreetmap.josm.tools.I18n;
33import org.openstreetmap.josm.tools.Shortcut;
34
35public class MainApplet extends JApplet {
36
37 final static JFrame frame = new JFrame("Java OpenStreetMap Editor");
38
39 public static final class UploadPreferencesAction extends JosmAction {
40 public UploadPreferencesAction() {
41 super(tr("Upload Preferences"), "upload-preferences", tr("Upload the current preferences to the server"),
42 Shortcut.registerShortcut("applet:uploadprefs", tr("Upload Preferences"), KeyEvent.VK_U, Shortcut.ALT_CTRL_SHIFT), true);
43 }
44 public void actionPerformed(ActionEvent e) {
45 ((ServerSidePreferences)Main.pref).upload();
46 }
47 }
48
49 private final class MainCaller extends Main {
50 private MainCaller() {
51 setContentPane(contentPanePrivate);
52 setJMenuBar(menu);
53 }
54 }
55
56 private final static String[][] paramInfo = {
57 {"username", tr("string"), tr("Name of the user.")},
58 {"password", tr("string"), tr("OSM Password.")},
59 {"geometry", tr("string"), tr("Resize the applet to the given geometry (format: WIDTHxHEIGHT)")},
60 {"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")},
61 {"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")},
62 {"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")},
63 {"reset-preferences", tr("any"),tr("If specified, reset the configuration instead of reading it.")}
64 };
65
66 private Map<String, Collection<String>> args = new HashMap<String, Collection<String>>();
67
68 @Override public String[][] getParameterInfo() {
69 return paramInfo;
70 }
71
72 @Override public void init() {
73 for (String[] s : paramInfo) {
74 Collection<String> p = readParameter(s[0], args.get(s[0]));
75 if (p != null) {
76 args.put(s[0], p);
77 }
78 }
79 if (!args.containsKey("geometry") && getParameter("width") != null && getParameter("height") != null) {
80 args.put("geometry", Arrays.asList(new String[]{getParameter("width")+"x"+getParameter("height")}));
81 }
82 }
83
84 @Override public void start() {
85 I18n.init();
86 Main.checkJava6();
87
88 String url = getParameter("load_url");
89 if(url != null)
90 args.put("download", Arrays.asList(new String[]{url}));
91
92 // initialize the platform hook, and
93 Main.determinePlatformHook();
94 // call the really early hook before we do anything else
95 Main.platform.preStartupHook();
96
97 Main.pref = new ServerSidePreferences(getCodeBase());
98
99 String lang = getParameter("language");
100 I18n.set(lang != null ? lang : Main.pref.get("language", null));
101
102 try
103 {
104 ((ServerSidePreferences)Main.pref).download();
105 } catch (ServerSidePreferences.MissingPassword e) {
106 String username = args.containsKey("username") ? args.get("username").iterator().next() : null;
107 String password = args.containsKey("password") ? args.get("password").iterator().next() : null;
108 if (username == null || password == null) {
109 JPanel p = new JPanel(new GridBagLayout());
110 p.add(new JLabel(tr(e.realm)), GBC.eol().fill(GBC.HORIZONTAL));
111 p.add(new JLabel(tr("Username")), GBC.std().insets(0,0,20,0));
112 JTextField user = new JTextField(username == null ? "" : username);
113 p.add(user, GBC.eol().fill(GBC.HORIZONTAL));
114 p.add(new JLabel(tr("Password")), GBC.std().insets(0,0,20,0));
115 JPasswordField pass = new JPasswordField(password == null ? "" : password);
116 p.add(pass, GBC.eol().fill(GBC.HORIZONTAL));
117 JOptionPane.showMessageDialog(null, p);
118 username = user.getText();
119 if("".equals(username))
120 username = null;
121 password = new String(pass.getPassword());
122 if("".equals(password))
123 password = null;
124 }
125 if (username != null && password != null) {
126 ((ServerSidePreferences)Main.pref).download(username, password);
127 }
128 }
129
130 Main.preConstructorInit(Option.fromStringMap(args));
131 Main.parent = frame;
132 Main.addListener();
133
134 new MainCaller().postConstructorProcessCmdLine(Option.fromStringMap(args));
135
136 MainMenu m = Main.main.menu; // shortcut
137
138 // remove offending stuff from JOSM (that would break the SecurityManager)
139 m.editMenu.add(new UploadPreferencesAction());
140 m.openFile.setEnabled(false);
141 m.exit.setEnabled(false);
142 m.save.setEnabled(false);
143 m.saveAs.setEnabled(false);
144 m.gpxExport.setEnabled(false);
145 }
146
147 private Collection<String> readParameter(String s, Collection<String> v) {
148 String param = getParameter(s);
149 if (param != null) {
150 if (v == null) {
151 v = new LinkedList<String>();
152 }
153 v.addAll(Arrays.asList(param.split(";")));
154 }
155 return v;
156 }
157
158 public static void main(String[] args) {
159 Main.applet = true;
160 MainApplet applet = new MainApplet();
161 Main.pref = new ServerSidePreferences(applet.getCodeBase());
162 applet.setStub(new AppletStub() {
163 public void appletResize(int w, int h) {
164 frame.setSize(w, h);
165 }
166
167 public AppletContext getAppletContext() {
168 return null;
169 }
170
171 public URL getCodeBase() {
172 try {
173 return new File(".").toURI().toURL();
174 } catch (Exception e) {
175 e.printStackTrace();
176 return null;
177 }
178 }
179
180 public URL getDocumentBase() {
181 return getCodeBase();
182 }
183
184 public String getParameter(String k) {
185 return null;
186 }
187
188 public boolean isActive() {
189 return true;
190 }
191 });
192 applet.init();
193 applet.start();
194 frame.setContentPane(applet);
195 frame.setVisible(true);
196 }
197}
Note: See TracBrowser for help on using the repository browser.