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