001 // License: GPL. Copyright 2007 by Immanuel Scholz and others 002 package org.openstreetmap.josm.gui; 003 004 import static org.openstreetmap.josm.tools.I18n.tr; 005 006 import java.applet.AppletContext; 007 import java.applet.AppletStub; 008 import java.awt.GridBagLayout; 009 import java.awt.event.ActionEvent; 010 import java.awt.event.KeyEvent; 011 import java.io.File; 012 import java.net.URL; 013 import java.util.Arrays; 014 import java.util.Collection; 015 import java.util.HashMap; 016 import java.util.LinkedList; 017 import java.util.Map; 018 019 import javax.swing.JApplet; 020 import javax.swing.JFrame; 021 import javax.swing.JLabel; 022 import javax.swing.JOptionPane; 023 import javax.swing.JPanel; 024 import javax.swing.JPasswordField; 025 import javax.swing.JTextField; 026 027 import org.openstreetmap.josm.Main; 028 import org.openstreetmap.josm.actions.JosmAction; 029 import org.openstreetmap.josm.data.ServerSidePreferences; 030 import org.openstreetmap.josm.gui.MainApplication.Option; 031 import org.openstreetmap.josm.tools.GBC; 032 import org.openstreetmap.josm.tools.I18n; 033 import org.openstreetmap.josm.tools.Shortcut; 034 035 public class MainApplet extends JApplet { 036 037 final static JFrame frame = new JFrame("Java OpenStreetMap Editor"); 038 039 public static final class UploadPreferencesAction extends JosmAction { 040 public UploadPreferencesAction() { 041 super(tr("Upload Preferences"), "upload-preferences", tr("Upload the current preferences to the server"), 042 Shortcut.registerShortcut("applet:uploadprefs", tr("Upload Preferences"), KeyEvent.VK_U, Shortcut.ALT_CTRL_SHIFT), true); 043 } 044 public void actionPerformed(ActionEvent e) { 045 ((ServerSidePreferences)Main.pref).upload(); 046 } 047 } 048 049 private final class MainCaller extends Main { 050 private MainCaller() { 051 setContentPane(contentPanePrivate); 052 setJMenuBar(menu); 053 } 054 } 055 056 private final static String[][] paramInfo = { 057 {"username", tr("string"), tr("Name of the user.")}, 058 {"password", tr("string"), tr("OSM Password.")}, 059 {"geometry", tr("string"), tr("Resize the applet to the given geometry (format: WIDTHxHEIGHT)")}, 060 {"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")}, 061 {"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")}, 062 {"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")}, 063 {"reset-preferences", tr("any"),tr("If specified, reset the configuration instead of reading it.")} 064 }; 065 066 private Map<String, Collection<String>> args = new HashMap<String, Collection<String>>(); 067 068 @Override public String[][] getParameterInfo() { 069 return paramInfo; 070 } 071 072 @Override public void init() { 073 for (String[] s : paramInfo) { 074 Collection<String> p = readParameter(s[0], args.get(s[0])); 075 if (p != null) { 076 args.put(s[0], p); 077 } 078 } 079 if (!args.containsKey("geometry") && getParameter("width") != null && getParameter("height") != null) { 080 args.put("geometry", Arrays.asList(new String[]{getParameter("width")+"x"+getParameter("height")})); 081 } 082 } 083 084 @Override public void start() { 085 I18n.init(); 086 Main.checkJava6(); 087 088 String url = getParameter("load_url"); 089 if(url != null) 090 args.put("download", Arrays.asList(new String[]{url})); 091 092 // initialize the platform hook, and 093 Main.determinePlatformHook(); 094 // call the really early hook before we do anything else 095 Main.platform.preStartupHook(); 096 097 Main.pref = new ServerSidePreferences(getCodeBase()); 098 099 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 }