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