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