Changeset 292 in josm for src


Ignore:
Timestamp:
2007-07-19T23:19:21+02:00 (17 years ago)
Author:
imi
Message:
  • fixed Bug Report module to work better with plugins
  • fixed Plugin.getPreferenceDir and Plugin.copy
  • fixed Ctrl-Q not asking for changed data
  • fixed several typos in Shortcuts
  • removed check for latest josm version
Location:
src/org/openstreetmap/josm
Files:
12 edited

Legend:

Unmodified
Added
Removed
  • src/org/openstreetmap/josm/actions/AboutAction.java

    r288 r292  
    2323import javax.swing.JTabbedPane;
    2424import javax.swing.JTextArea;
    25 import javax.swing.SwingUtilities;
    2625
    2726import org.openstreetmap.josm.Main;
     
    7069
    7170                JPanel info = new JPanel(new GridBagLayout());
    72                 info.add(new JLabel(tr("Java OpenStreetMap Editor Version {0}",version)), GBC.eol());
    73                 info.add(new JLabel(tr("last change at {0}",time)), GBC.eol());
    74                 info.add(new JLabel(tr("Java Version {0}",System.getProperty("java.version"))), GBC.eol());
    75                 info.add(new JLabel(tr("Latest Version on JOSM homepage is")), GBC.std().insets(0,0,5,0));
    76                 final JLabel checkVersionLabel = new JLabel("<html><em>"+tr("checking...")+"</em></html>");
    77                 info.add(checkVersionLabel, GBC.eol());
    78                 new Thread(){
    79                         @Override public void run() {
    80                                 final String version = checkLatestVersion();
    81                                 try {
    82                                         if (version == null)
    83                                                 throw new NullPointerException();
    84                         SwingUtilities.invokeAndWait(new Runnable(){
    85                                 public void run() {
    86                                         checkVersionLabel.setText(version);
    87                             }
    88                         });
    89                 } catch (Exception e) {
    90                         checkVersionLabel.setText("failed.");
    91                 }
    92             }
    93                 }.start();
    94                
     71                info.add(new JLabel(tr("Java OpenStreetMap Editor Version {0}",version)), GBC.eol().fill(GBC.HORIZONTAL));
     72                info.add(new JLabel(tr("last change at {0}",time)), GBC.eol().fill(GBC.HORIZONTAL));
     73                info.add(new JLabel(tr("Java Version {0}",System.getProperty("java.version"))), GBC.eol().fill(GBC.HORIZONTAL));
    9574                info.add(GBC.glue(0,10), GBC.eol());
    96                
    9775                info.add(new JLabel(tr("Homepage")), GBC.std().insets(0,0,10,0));
    98                 info.add(new UrlLabel("http://josm.openstreetmap.de"), GBC.eol());
     76                info.add(new UrlLabel("http://josm.openstreetmap.de"), GBC.eol().fill(GBC.HORIZONTAL));
    9977                info.add(new JLabel(tr("Bug Reports")), GBC.std().insets(0,0,10,0));
    100                 info.add(new UrlLabel("http://josm.openstreetmap.de/newticket"), GBC.eol());
     78                info.add(new UrlLabel("http://josm.openstreetmap.de/newticket"), GBC.eol().fill(GBC.HORIZONTAL));
    10179                info.add(new JLabel(tr("News about JOSM")), GBC.std().insets(0,0,10,0));
    102                 info.add(new UrlLabel("http://www.opengeodata.org/?cat=17"), GBC.eol());
     80                info.add(new UrlLabel("http://www.opengeodata.org/?cat=17"), GBC.eol().fill(GBC.HORIZONTAL));
    10381
    10482                StringBuilder pluginsStr = new StringBuilder();
  • src/org/openstreetmap/josm/actions/AutoScaleAction.java

    r214 r292  
    2929
    3030        public AutoScaleAction(String mode) {
    31                 super(tr(mode), "dialogs/autoscale/"+mode, tr("Zoom the view to {0}."), 0, 0, true);
     31                super(tr("Zoom to {0}", mode), "dialogs/autoscale/"+mode, tr("Zoom the view to {0}.", tr(mode)), 0, 0, true);
    3232                String modeHelp = Character.toUpperCase(mode.charAt(0))+mode.substring(1);
    3333                putValue("help", "Action/AutoScale/"+modeHelp);
  • src/org/openstreetmap/josm/actions/ExitAction.java

    r178 r292  
    55import java.awt.event.ActionEvent;
    66import java.awt.event.KeyEvent;
     7
     8import org.openstreetmap.josm.Main;
    79
    810/**
     
    1820                super(tr("Exit"), "exit", tr("Exit the application."), KeyEvent.VK_Q, KeyEvent.CTRL_DOWN_MASK, true);
    1921        }
    20        
     22
    2123        public void actionPerformed(ActionEvent e) {
    22                 System.exit(0);
     24                if (!Main.breakBecauseUnsavedChanges())
     25                        System.exit(0);
    2326        }
    2427}
  • src/org/openstreetmap/josm/actions/JosmAction.java

    r208 r292  
    2727                super(name, ImageProvider.get(iconName));
    2828                setHelpId();
    29                 putValue(SHORT_DESCRIPTION, "<html>"+tooltip+" <font size='-2'>"+ShortCutLabel.name(shortCut, modifier)+"</font>&nbsp;</html>");
    30                 this.shortCut = KeyStroke.getKeyStroke(shortCut, modifier);
    31         Main.contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(this.shortCut, name);
    32         Main.contentPane.getActionMap().put(name, this);
     29                String scl = ShortCutLabel.name(shortCut, modifier);
     30                putValue(SHORT_DESCRIPTION, "<html>"+tooltip+" <font size='-2'>"+scl+"</font>"+(scl.equals("")?"":"&nbsp;")+"</html>");
     31                if (shortCut != 0) {
     32                        this.shortCut = KeyStroke.getKeyStroke(shortCut, modifier);
     33                        Main.contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(this.shortCut, name);
     34                        Main.contentPane.getActionMap().put(name, this);
     35                }
    3336        putValue("toolbar", iconName);
    3437        if (register)
     
    3740
    3841        public void destroy() {
    39                 Main.contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).remove(shortCut);
    40                 Main.contentPane.getActionMap().remove(shortCut);
     42                if (shortCut != null) {
     43                        Main.contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).remove(shortCut);
     44                        Main.contentPane.getActionMap().remove(shortCut);
     45                }
    4146        }
    4247       
  • src/org/openstreetmap/josm/actions/mapmode/ZoomAction.java

    r113 r292  
    4444         */
    4545        public ZoomAction(MapFrame mapFrame) {
    46                 super(tr("Zoom"), "zoom", tr("Zoom in by dragging. (Ctrl+up,left,down,right,+,-)"), KeyEvent.VK_Z, mapFrame, ImageProvider.getCursor("normal", "zoom"));
     46                super(tr("Zoom"), "zoom", tr("Zoom in by dragging. (Ctrl+up,left,down,right,',','.')"), KeyEvent.VK_Z, mapFrame, ImageProvider.getCursor("normal", "zoom"));
    4747                mv = mapFrame.mapView;
    4848                selectionManager = new SelectionManager(this, true, mv);
  • src/org/openstreetmap/josm/gui/download/BoundingBoxSelection.java

    r275 r292  
    196196                                double size = 180.0 / Math.pow(2, Integer.parseInt(map.get("zoom")));
    197197                                b = new Bounds(
    198                         new LatLon(Double.parseDouble(map.get("mlat")) - size/2, Double.parseDouble(map.get("mlon")) - size),
    199                         new LatLon(Double.parseDouble(map.get("mlat")) + size/2, Double.parseDouble(map.get("mlon")) + size));
     198                        new LatLon(parseDouble(map, "lat") - size/2, parseDouble(map, "lon") - size),
     199                        new LatLon(parseDouble(map, "lat") + size/2, parseDouble(map, "lon") + size));
    200200                        }
    201201                } catch (NumberFormatException x) {
     
    204204                return b;
    205205        }
     206
     207        private static double parseDouble(HashMap<String, String> map, String key) {
     208                if (map.containsKey(key))
     209                        return Double.parseDouble(map.get(key));
     210                return Double.parseDouble(map.get("m"+key));
     211    }
    206212}
  • src/org/openstreetmap/josm/plugins/Plugin.java

    r291 r292  
    66import java.io.IOException;
    77import java.io.InputStream;
    8 import java.net.URL;
    9 import java.net.URLClassLoader;
    108import java.util.List;
    119
     
    1715/**
    1816 * All plugins *must* have an standard constructor taking no arguments.
     17 *
    1918 * This constructor is called at JOSM startup, after all Main-objects have been initialized.
    2019 * For all purposes of loading dynamic resources, the Plugin's class loader should be used
    2120 * (or else, the plugin jar will not be within the class path).
    2221 *
    23  * All plugins should have at least one class subclassing this abstract base class.
     22 * A plugin may subclass this abstract base class (but it is optional).
    2423 *
    25  * The actual implementation of this interface is optional, as all functions will be called
    26  * via reflection. This is to be able to change this interface without the need of recompiling
    27  * or even breaking the plugins. If your class does not provide a function here (or does
    28  * provide a function with a mismatching signature), it will not be called. That simple.
     24 * The actual implementation of this class is optional, as all functions will be called
     25 * via reflection. This is to be able to change this interface without the need of
     26 * recompiling or even breaking the plugins. If your class does not provide a
     27 * function here (or does provide a function with a mismatching signature), it will not
     28 * be called. That simple.
    2929 *
    30  * Or in other words: See this base class as an documentation of what functions are provided.
    31  * Subclassing it and overriding some functions makes it easy for you to keep sync with the
    32  * correct actual plugin architecture of JOSM.
    33  *
    34  *
    35  * The pluginname provided to the constructor is also the name of the directory to
    36  * store the plugin's own stuff (located under the josm preferences directory)
     30 * Or in other words: See this base class as an documentation of what automatic callbacks
     31 * are provided (you can register yourself to more callbacks in your plugin class
     32 * constructor).
     33 *
     34 * Subclassing Plugin and overriding some functions makes it easy for you to keep sync
     35 * with the correct actual plugin architecture of JOSM.
    3736 *
    3837 * @author Immanuel.Scholz
     
    4039public abstract class Plugin {
    4140
    42         String name;
    43 
    44         public Plugin() {
    45                 try {
    46                 URL[] urls = ((URLClassLoader)getClass().getClassLoader()).getURLs();
    47                 name = urls[urls.length-1].toString();
    48                 if (name.toLowerCase().endsWith(".jar")) {
    49                         int lastSlash = name.lastIndexOf('/');
    50                         name = name.substring(lastSlash+1, name.length()-4);
    51                 }
    52         } catch (RuntimeException e) {
    53                 name = "unknown";
    54         }
    55     }
     41        /**
     42         * This is the info available for this plugin. You can access this from your
     43         * constructor.
     44         *
     45         * (The actual implementation to request the info from a static variable
     46         * is a bit hacky, but it works).
     47         */
     48        public final PluginInformation info = PluginInformation.currentPluginInitialization;
    5649
    5750        /**
    58          * @return The name of this plugin. This is the name of the .jar file.
    59          * @deprecated Plugins have to know their name by themself.
     51         * @return The directory for the plugin to store all kind of stuff.
    6052         */
    61         @Deprecated public final String getName() {
    62                 return name;
     53        public final String getPluginDir() {
     54                return Main.pref.getPreferencesDir()+"plugins/"+info.name+"/";
    6355        }
    64         /**
    65          * @return The directory for the plugin to store all kind of stuff.
    66          * @deprecated Use <code>Main.pref.getPreferencesDir()+"plugins/"+name+"/";</code> instead.
    67          */
    68         @Deprecated public final String getPluginDir() {
    69                 return Main.pref.getPreferencesDir()+"plugins/"+name+"/";
    70         }
    71 
    72 
    7356
    7457        /**
     
    9275       
    9376        /**
    94          * @deprecated Use copy(String pluginName, String from, String to) instead
    95          */
    96         @Deprecated public void copy(String from, String to) throws FileNotFoundException, IOException {
    97                 copy(name, from, to);
    98     }
    99 
    100         /**
    10177         * Copies the ressource 'from' to the file in the plugin directory named 'to'.
    10278         */
    103         public void copy(String pluginName, String from, String to) throws FileNotFoundException, IOException {
    104             String pluginDirName = Main.pref.getPreferencesDir()+"plugins/"+pluginName+"/";
    105                 File pluginDir = new File(pluginDirName);
    106                 if (!pluginDir.exists())
    107                         pluginDir.mkdirs();
    108         FileOutputStream out = new FileOutputStream(pluginDirName+to);
    109         InputStream in = getClass().getResourceAsStream(from);
    110         byte[] buffer = new byte[8192];
    111         for(int len = in.read(buffer); len > 0; len = in.read(buffer))
    112                 out.write(buffer, 0, len);
    113         in.close();
    114         out.close();
     79        public void copy(String from, String to) throws FileNotFoundException, IOException {
     80                String pluginDirName = Main.pref.getPreferencesDir()+"plugins/"+info.name+"/";
     81        File pluginDir = new File(pluginDirName);
     82        if (!pluginDir.exists())
     83                pluginDir.mkdirs();
     84        FileOutputStream out = new FileOutputStream(pluginDirName+to);
     85        InputStream in = getClass().getResourceAsStream(from);
     86        byte[] buffer = new byte[8192];
     87        for(int len = in.read(buffer); len > 0; len = in.read(buffer))
     88                out.write(buffer, 0, len);
     89        in.close();
     90        out.close();
    11591    }
    11692}
  • src/org/openstreetmap/josm/plugins/PluginInformation.java

    r291 r292  
    3636
    3737        public final Map<String, String> attr = new TreeMap<String, String>();
     38
     39        /**
     40         * Used in the Plugin constructor to make the information of the plugin
     41         * that is currently initializing available.
     42         *
     43         * If you think this is hacky, you are probably right. But it is
     44         * convinient anyway ;-)
     45         */
     46        static PluginInformation currentPluginInitialization = null;
    3847
    3948        /**
     
    102111        public PluginProxy load(Class<?> klass) {
    103112                try {
     113                        currentPluginInitialization = this;
    104114                        return new PluginProxy(klass.newInstance(), this);
    105115                } catch (Exception e) {
  • src/org/openstreetmap/josm/plugins/PluginProxy.java

    r267 r292  
    1919        public final Object plugin;
    2020        public final PluginInformation info;
    21         public boolean misbehaving = false;
    2221
    2322        public PluginProxy(Object plugin, PluginInformation info) {
    2423                this.plugin = plugin;
    2524                this.info = info;
    26 
    27                 // setting name of the plugin by reflection
    28                 if (plugin instanceof Plugin) {
    29                         try {
    30                         Plugin.class.getDeclaredField("name").set(plugin, info.name);
    31                 } catch (Exception e) {
    32                         if (e instanceof RuntimeException)
    33                                 throw (RuntimeException)e;
    34                         throw new RuntimeException(e);
    35                 }
    36                 }
    3725    }
    3826
  • src/org/openstreetmap/josm/tools/BugReportExceptionHandler.java

    r283 r292  
    44
    55import java.awt.GridBagLayout;
     6import java.awt.Toolkit;
     7import java.awt.datatransfer.Clipboard;
     8import java.awt.datatransfer.ClipboardOwner;
     9import java.awt.datatransfer.StringSelection;
     10import java.awt.datatransfer.Transferable;
    611import java.io.BufferedReader;
    712import java.io.File;
     
    3742                if (Main.parent != null) {
    3843                        if (e instanceof OutOfMemoryError) {
     44                                // do not translate the string, as translation may raise an exception
    3945                                JOptionPane.showMessageDialog(Main.parent, "You are out of memory. Strange things may happen.\nPlease restart JOSM and load smaller data sets.");
    4046                                return;
    4147                        }
    4248
     49                        PluginProxy plugin = null;
     50
    4351                        // Check for an explicit problem when calling a plugin function
    44                         if (e instanceof PluginException) {
    45                                 PluginProxy plugin = ((PluginException)e).plugin;
    46                                 if (plugin != null && !plugin.misbehaving) {
    47                                         JOptionPane.showMessageDialog(Main.parent, tr("The plugin {0} threw an exception: {1}\nIt may be outdated. Please contact the plugin's author.\nThis message will not shown again until JOSM is restarted.", plugin.info.name, e.getMessage()));
    48                                         plugin.misbehaving = true;
     52                        if (e instanceof PluginException)
     53                                plugin = ((PluginException)e).plugin;
     54                       
     55                        if (plugin == null)
     56                                plugin = guessPlugin(e);
     57
     58                        if (plugin != null) {
     59                                int answer = JOptionPane.showConfirmDialog(
     60                                                Main.parent,
     61                                                tr("An unexpected exception occurred, that may come from in the ''{0}'' plugin.", plugin.info.name)+"\n"+
     62                                                (plugin.info.author != null ? tr("According to the information within the plugin, the author is {0}.", plugin.info.author) : "")+"\n"+
     63                                                tr("Should the plugin be disabled?"),
     64                                                tr("Disable plugin"),
     65                                                JOptionPane.YES_NO_OPTION);
     66                                if (answer == JOptionPane.OK_OPTION) {
     67                                        LinkedList<String> plugins = new LinkedList<String>(Arrays.asList(Main.pref.get("plugins").split(",")));
     68                                        if (plugins.contains(plugin.info.name)) {
     69                                                while (plugins.remove(plugin.info.name)) {}
     70                                                String p = "";
     71                                                for (String s : plugins)
     72                                                        p += ","+s;
     73                                                if (p.length() > 0)
     74                                                        p = p.substring(1);
     75                                                Main.pref.put("plugins", p);
     76                                                JOptionPane.showMessageDialog(Main.parent, tr("The plugin has been removed from the configuration. Please restart JOSM to unload the plugin."));
     77                                        } else {
     78                                                JOptionPane.showMessageDialog(Main.parent, tr("The plugin could not be removed. Please tell the people you got JOSM from about the problem."));
     79                                        }
    4980                                        return;
    50                                 }
    51                         }
    52 
    53                         // Try a heuristic to guess whether the problem may be within a plugin
    54                         String pluginName = guessPlugin(e);
    55                         if (pluginName != null) {
    56                                 boolean loaded = false;
    57                                 for (PluginProxy p : Main.plugins)
    58                                         if (p.info.name.equals(pluginName))
    59                                                 loaded = true;
    60                                 if (loaded) {
    61                                         String author = findPluginAuthor(pluginName);
    62                                         int answer = JOptionPane.showConfirmDialog(
    63                                                         Main.parent,
    64                                                         tr("An unexpected exception occurred, that may come from in the ''{0}'' plugin.", pluginName)+"\n"+
    65                                                                 (author != null ? tr("According to the information within the plugin, the author is {0}.", author) : "")+"\n"+
    66                                                                 tr("Should the plugin be disabled?"),
    67                                                         tr("Disable plugin"),
    68                                                         JOptionPane.YES_NO_OPTION);
    69                                         if (answer == JOptionPane.OK_OPTION) {
    70                                                 LinkedList<String> plugins = new LinkedList<String>(Arrays.asList(Main.pref.get("plugins").split(",")));
    71                                                 if (plugins.contains(pluginName)) {
    72                                                         while (plugins.remove(pluginName)) {}
    73                                                         String p = "";
    74                                                         for (String s : plugins)
    75                                                                 p += ","+s;
    76                                                         if (p.length() > 0)
    77                                                                 p = p.substring(1);
    78                                                         Main.pref.put("plugins", p);
    79                                                         JOptionPane.showMessageDialog(Main.parent, tr("The plugin has been removed from the configuration. Please restart JOSM to unload the plugin."));
    80                                                 } else {
    81                                                         JOptionPane.showMessageDialog(Main.parent, tr("The plugin could not be removed. Please tell the people you got JOSM from about the problem."));
    82                                                 }
    83                                                 return;
    84                                         }
    8581                                }
    8682                        }
     
    9894
    9995                                        URL revUrl = Main.class.getResource("/REVISION");
    100                                         StringBuilder sb = new StringBuilder(tr("Please send this to josm@eigenheimstrasse.de\n\n"));
     96                                        StringBuilder sb = new StringBuilder();
    10197                                        if (revUrl == null) {
    10298                                                sb.append("Development version. Unknown revision.");
     
    121117
    122118                                        JPanel p = new JPanel(new GridBagLayout());
    123                                         p.add(new JLabel(tr("Please send an email with the following information to josm@eigenheimstrasse.de")), GBC.eop());
     119                                        p.add(new JLabel(tr("Please send an email with the following information to josm@eigenheimstrasse.de")), GBC.eol());
     120                                        try {
     121                            Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(sb.toString()), new ClipboardOwner(){
     122                                public void lostOwnership(Clipboard clipboard, Transferable contents) {}
     123                            });
     124                            p.add(new JLabel(tr("The text has already been copied to your clipboard.")), GBC.eop());
     125                    } catch (RuntimeException x) {
     126                    }
    124127
    125128                                        JTextArea info = new JTextArea(sb.toString(), 20, 60);
     
    136139        }
    137140
    138         /**
    139          * Try to find the author of the given plugin. Return <code>null</code>
    140          * if no author specified or the plugin is not found.
    141          */
    142         private String findPluginAuthor(String pluginName) {
    143                 for (PluginProxy proxy : Main.plugins)
    144                         if (pluginName.equals(proxy.info.name))
    145                                 return proxy.info.author;
     141        private PluginProxy guessPlugin(Throwable e) {
     142                String name = guessPluginName(e);
     143                for (PluginProxy p : Main.plugins)
     144                        if (p.info.name.equals(name))
     145                                return p;
    146146                return null;
    147147        }
     
    158158         * bug reports ;-).
    159159         */
    160         private String guessPlugin(Throwable e) {
     160        private String guessPluginName(Throwable e) {
    161161                for (StackTraceElement element : e.getStackTrace()) {
    162162                        String c = element.getClassName();
     163                       
    163164                        if (c.contains("wmsplugin.") || c.contains(".WMSLayer"))
    164165                                return "wmsplugin";
     
    171172                        if (c.contains("annotationtester."))
    172173                                return "annotation-tester";
     174                        if (c.startsWith("UtilsPlugin."))
     175                                return "UtilsPlugin";
     176
     177                        if (c.startsWith("org.openstreetmap.josm.plugins.")) {
     178                                String p = c.substring("org.openstreetmap.josm.plugins.".length());
     179                                if (p.indexOf('.') != -1 && p.matches("[a-z].*")) {
     180                                        return p.substring(0,p.indexOf('.'));
     181                                }
     182                        }
    173183                }
    174184                return null;
  • src/org/openstreetmap/josm/tools/OpenBrowser.java

    r200 r292  
    5252
    5353        private static void linux(String url) throws IOException {
    54                 try {
    55                         Runtime.getRuntime().exec("gnome-open " + url);
    56                 } catch (IOException e) {
    57                         Runtime.getRuntime().exec("kfmclient openURL " + url);
     54                String[] programs = {"gnome-open", "kfmclient openURL", "firefox"};
     55                for (String program : programs) {
     56                        try {
     57                                Runtime.getRuntime().exec(program+" "+url);
     58                                return;
     59                        } catch (IOException e) {
     60            }
    5861                }
    5962        }
  • src/org/openstreetmap/josm/tools/ShortCutLabel.java

    r178 r292  
    88public class ShortCutLabel {
    99        public static String name(int shortCut, int modifiers) {
     10                if (shortCut == 0 && modifiers == 0)
     11                        return "";
    1012                String s = "";
    1113                if ((modifiers & KeyEvent.CTRL_MASK) != 0 || (modifiers & KeyEvent.CTRL_DOWN_MASK) != 0)
Note: See TracChangeset for help on using the changeset viewer.