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

Last change on this file since 5436 was 5279, checked in by bastiK, 13 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
RevLine 
[298]1// License: GPL. Copyright 2007 by Immanuel Scholz and others
[283]2package org.openstreetmap.josm.gui;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
[2017]6import java.applet.AppletContext;
[431]7import java.applet.AppletStub;
[283]8import java.awt.GridBagLayout;
9import java.awt.event.ActionEvent;
10import java.awt.event.KeyEvent;
[431]11import java.io.File;
12import java.net.URL;
[283]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;
[431]20import javax.swing.JFrame;
[283]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;
[5279]30import org.openstreetmap.josm.gui.MainApplication.Option;
[283]31import org.openstreetmap.josm.tools.GBC;
[3815]32import org.openstreetmap.josm.tools.I18n;
[1084]33import org.openstreetmap.josm.tools.Shortcut;
[283]34
35public class MainApplet extends JApplet {
36
[3239]37 final static JFrame frame = new JFrame("Java OpenStreetMap Editor");
38
[1169]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"),
[4982]42 Shortcut.registerShortcut("applet:uploadprefs", tr("Upload Preferences"), KeyEvent.VK_U, Shortcut.ALT_CTRL_SHIFT), true);
[283]43 }
[1169]44 public void actionPerformed(ActionEvent e) {
45 ((ServerSidePreferences)Main.pref).upload();
46 }
[283]47 }
48
49 private final class MainCaller extends Main {
[1169]50 private MainCaller() {
[3252]51 setContentPane(contentPanePrivate);
[1169]52 setJMenuBar(menu);
53 }
54 }
[283]55
[1169]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)")},
[1195]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")},
[1169]63 {"reset-preferences", tr("any"),tr("If specified, reset the configuration instead of reading it.")}
64 };
[283]65
[1169]66 private Map<String, Collection<String>> args = new HashMap<String, Collection<String>>();
[283]67
[1169]68 @Override public String[][] getParameterInfo() {
69 return paramInfo;
70 }
[283]71
[1169]72 @Override public void init() {
73 for (String[] s : paramInfo) {
74 Collection<String> p = readParameter(s[0], args.get(s[0]));
[2053]75 if (p != null) {
[1169]76 args.put(s[0], p);
[2053]77 }
[1169]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 }
[283]83
[1169]84 @Override public void start() {
[3815]85 I18n.init();
86 Main.checkJava6();
87
[3819]88 String url = getParameter("load_url");
89 if(url != null)
90 args.put("download", Arrays.asList(new String[]{url}));
91
[3815]92 // initialize the platform hook, and
[3239]93 Main.determinePlatformHook();
[3528]94 // call the really early hook before we do anything else
[3239]95 Main.platform.preStartupHook();
96
[1169]97 Main.pref = new ServerSidePreferences(getCodeBase());
[4220]98
99 String lang = getParameter("language");
100 I18n.set(lang != null ? lang : Main.pref.get("language", null));
101
[3815]102 try
103 {
104 ((ServerSidePreferences)Main.pref).download();
105 } catch (ServerSidePreferences.MissingPassword e) {
[3528]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());
[3815]110 p.add(new JLabel(tr(e.realm)), GBC.eol().fill(GBC.HORIZONTAL));
[3528]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();
[3815]119 if("".equals(username))
120 username = null;
[3528]121 password = new String(pass.getPassword());
[3815]122 if("".equals(password))
123 password = null;
[3528]124 }
[3815]125 if (username != null && password != null) {
126 ((ServerSidePreferences)Main.pref).download(username, password);
127 }
[3528]128 }
129
[5279]130 Main.preConstructorInit(Option.fromStringMap(args));
[3239]131 Main.parent = frame;
[2876]132 Main.addListener();
[1849]133
[5279]134 new MainCaller().postConstructorProcessCmdLine(Option.fromStringMap(args));
[1023]135
[1169]136 MainMenu m = Main.main.menu; // shortcut
[283]137
[1169]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 }
[283]146
[1169]147 private Collection<String> readParameter(String s, Collection<String> v) {
148 String param = getParameter(s);
149 if (param != null) {
[2053]150 if (v == null) {
[1169]151 v = new LinkedList<String>();
[2053]152 }
[1169]153 v.addAll(Arrays.asList(param.split(";")));
154 }
155 return v;
156 }
[431]157
[1169]158 public static void main(String[] args) {
[3248]159 Main.applet = true;
[1169]160 MainApplet applet = new MainApplet();
[3542]161 Main.pref = new ServerSidePreferences(applet.getCodeBase());
[1169]162 applet.setStub(new AppletStub() {
163 public void appletResize(int w, int h) {
164 frame.setSize(w, h);
165 }
[431]166
[1169]167 public AppletContext getAppletContext() {
168 return null;
169 }
[431]170
[1169]171 public URL getCodeBase() {
172 try {
173 return new File(".").toURI().toURL();
174 } catch (Exception e) {
175 e.printStackTrace();
176 return null;
177 }
178 }
[431]179
[1169]180 public URL getDocumentBase() {
181 return getCodeBase();
182 }
[431]183
[1169]184 public String getParameter(String k) {
185 return null;
186 }
[431]187
[1169]188 public boolean isActive() {
189 return true;
190 }
191 });
192 applet.init();
193 applet.start();
194 frame.setContentPane(applet);
195 frame.setVisible(true);
196 }
[283]197}
Note: See TracBrowser for help on using the repository browser.