Changeset 991 in josm


Ignore:
Timestamp:
2008-09-19T08:33:02+02:00 (16 years ago)
Author:
stoecker
Message:

improved mappaint loader a lot

Location:
trunk/src/org/openstreetmap/josm
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/mappaint/ElemStyleHandler.java

    r990 r991  
    150150                                {
    151151                                        if (atts.getQName(count).equals("src"))
    152                                                 rule.icon.icon = MapPaintStyles.getIcon(atts.getValue(count));
     152                                                rule.icon.icon = MapPaintStyles.getIcon(atts.getValue(count), styleName);
    153153                                        else if (atts.getQName(count).equals("annotate"))
    154154                                                rule.icon.annotate = Boolean.parseBoolean (atts.getValue(count));
  • trunk/src/org/openstreetmap/josm/gui/mappaint/MapPaintStyles.java

    r987 r991  
    66import java.net.URL;
    77import java.util.HashMap;
     8import java.util.LinkedList;
     9import java.util.List;
    810import java.util.Iterator;
    911
     
    1315import org.openstreetmap.josm.data.osm.OsmPrimitive;
    1416import org.openstreetmap.josm.gui.mappaint.ElemStyles;
     17import org.openstreetmap.josm.io.MirroredInputStream;
     18import org.openstreetmap.josm.tools.ImageProvider;
    1519import org.xml.sax.InputSource;
    1620import org.xml.sax.XMLReader;
     
    1923public class MapPaintStyles {
    2024
    21         private static String styleDir;
    22         private static String imageDir;
    23         private static String internalImageDir;
    24         private static Boolean isInternal = false;
    2525        private static ElemStyles styles = new ElemStyles();
     26        private static String iconDirs;
    2627       
    2728        public static ElemStyles getStyles()
     
    3031        }
    3132
    32         public static ImageIcon getIcon(String name)
     33        public static ImageIcon getIcon(String name, String styleName)
    3334        {
    34                 try {
    35                         if(isInternal)
    36                         {
    37                                 String imageFile = imageDir+name;
    38                                 File f = new File(imageFile);
    39                                 if(f.exists())
    40                                 {
    41                                         //open icon from user directory
    42                                         return new ImageIcon(imageFile);
    43                                 }
    44                         }
    45                         URL path = Main.class.getResource(internalImageDir+name);
    46                         if(path == null)
    47                                 path = Main.class.getResource("/images/styles/"+name);
    48                         if(path == null)
    49                         {
    50                                 System.out.println("Mappaint: Icon " + name + " not found, using default icon");
    51                                 path = Main.class.getResource(internalImageDir+"misc/no_icon.png");
    52                         }
    53                         return new ImageIcon(Toolkit.getDefaultToolkit().createImage(path));
     35                List<String> dirs = new LinkedList<String>();
     36                for(String fileset : iconDirs.split(";"))
     37                {
     38                        String[] a;
     39                        if(fileset.indexOf("=") >= 0)
     40                                a = fileset.split("=", 2);
     41                        else
     42                                a = new String[] {"", fileset};
     43
     44                        /* non-prefixed path is generic path, always take it */
     45                        if(a[0].length() == 0 || styleName.equals(a[0]))
     46                                dirs.add(a[1]);
    5447                }
    55                 catch (Exception e)
     48                ImageIcon i = ImageProvider.getIfAvailable(dirs, "mappaint."+styleName, null, name);           
     49                if(i == null)
    5650                {
    57                         URL path = Main.class.getResource(internalImageDir+"incomming/amenity.png");
    58                         return new ImageIcon(Toolkit.getDefaultToolkit().createImage(path));
     51                        System.out.println("Mappaint-Style \""+styleName+"\" icon \"" + name + "\" not found.");
     52                        i = ImageProvider.getIfAvailable(dirs, "mappaint."+styleName, null, "misc/no_icon.png");
    5953                }
     54                return i;
    6055        }
    6156
    6257        public static void readFromPreferences() {
    63                 String styleName = Main.pref.get("mappaint.style", "standard");
    64                 // fallback to standard name for internal case, as we only have one internal style
    65                 String internalStyleName = "standard";
    66                 styleDir = Main.pref.get("mappaint.styledir", Main.pref.getPreferencesDir()+"plugins/mappaint/"+styleName+"/");
    67                 String elemStylesFile = styleDir+"elemstyles.xml";
    68                 imageDir = styleDir+"icons/";
    69                 internalImageDir = "/images/styles/"+internalStyleName+"/";
     58                /* don't prefix icon path, as it should be generic */
     59                String internalicon = "resource://images/styles/standard/;resource://images/styles/";
     60                String internalfile = "standard=resource://styles/standard/elemstyles.xml";
    7061
    71 //              System.out.println("mappaint: Using style: " + styleName);
    72 //              System.out.println("mappaint: Using style dir: " + styleDir);
    73 //              System.out.println("mappaint: Using style file: " + elemStylesFile);
     62                iconDirs = Main.pref.get("mappaint.iconpaths");
     63                iconDirs = iconDirs == null || iconDirs.length() == 0 ? internalicon : iconDirs + ";" + internalicon;
    7464
    75                 File f = new File(elemStylesFile);
    76                 if (f.exists())
     65                String file = Main.pref.get("mappaint.sources");
     66                file = file == null || file.length() == 0 ? internalfile : internalfile + ";" + file;
     67
     68                for(String fileset : file.split(";"))
    7769                {
    78                         try// reading file from file system
     70                        try
    7971                        {
    80 //                              System.out.println("mappaint: Using style file: \"" + f + "\"");
     72                                String[] a;
     73                                if(fileset.indexOf("=") >= 0)
     74                                        a = fileset.split("=", 2);
     75                                else
     76                                        a = new String[] {"standard", fileset};
    8177                                XMLReader xmlReader = XMLReaderFactory.createXMLReader();
    82                                 ElemStyleHandler handler = new ElemStyleHandler(styleName);
     78                                ElemStyleHandler handler = new ElemStyleHandler(a[0]);
    8379                                xmlReader.setContentHandler(handler);
    8480                                xmlReader.setErrorHandler(handler);
    85 //                              temporary only!
    86                                 xmlReader.parse(new InputSource(new FileReader(f)));
     81                                xmlReader.parse(new InputSource(new MirroredInputStream(a[1])));
    8782                        }
    8883                        catch (Exception e)
    8984                        {
    90                                 throw new RuntimeException(e);
     85                                System.out.println("Mappaint-Style problems: \"" + fileset + "\"");
    9186                        }
    9287                }
    93                 else {// reading the builtin file from the plugin jar file
    94                         URL elemStylesPath = Main.class.getResource("/styles/"+internalStyleName+"/elemstyles.xml");
    95 
    96 //                      System.out.println("mappaint: Using jar's elemstyles.xml: \"" + elemStylesPath + "\"");
    97                         if (elemStylesPath != null)
    98                         {
    99                                 isInternal = true;
    100                                 try
    101                                 {
    102                                         XMLReader xmlReader = XMLReaderFactory.createXMLReader();
    103                                         ElemStyleHandler handler = new ElemStyleHandler(internalStyleName);
    104                                         xmlReader.setContentHandler(handler);
    105                                         xmlReader.setErrorHandler(handler);
    106 //                                      temporary only!
    107                                         xmlReader.parse(new InputSource(elemStylesPath.openStream()));
    108                                 }
    109                                 catch (Exception e)
    110                                 {
    111                                         throw new RuntimeException(e);
    112                                 }
    113                         } else {
    114                                 System.out.println("mappaint: Couldn't find style: \"" + styleDir + "elemstyles.xml\"");
    115                         }
    116                 }
     88                iconDirs = null;
    11789        }
    11890
  • trunk/src/org/openstreetmap/josm/gui/tagging/TaggingPreset.java

    r949 r991  
    405405         */
    406406        public void setIcon(String iconName) {
    407                 ImageIcon icon = ImageProvider.getIfAvailable(null, iconName);
     407                String s = Main.pref.get("taggingpreset.iconpaths");
     408                ImageIcon icon = ImageProvider.getIfAvailable((s != null ? s.split(";") : null), "presets", null, iconName);
    408409                if (icon == null)
    409410                {
  • trunk/src/org/openstreetmap/josm/tools/ImageProvider.java

    r627 r991  
    1414import java.net.MalformedURLException;
    1515import java.net.URL;
     16import java.util.Arrays;
     17import java.util.Collection;
    1618import java.util.HashMap;
    1719import java.util.LinkedList;
     
    6365        }
    6466
     67        public static ImageIcon getIfAvailable(String subdir, String name)
     68        {
     69                return getIfAvailable((Collection<String>)null, null, subdir, name);
     70        }
     71        public static final ImageIcon getIfAvailable(String[] dirs, String id, String subdir, String name)
     72        {
     73                return getIfAvailable(Arrays.asList(dirs), id, subdir, name);
     74        }
     75
    6576        /**
    6677         * Like {@link #get(String)}, but does not throw and return <code>null</code>
    6778         * in case of nothing is found. Use this, if the image to retrieve is optional.
    6879         */
    69         public static ImageIcon getIfAvailable(String subdir, String name) {
     80        public static ImageIcon getIfAvailable(Collection<String> dirs, String id, String subdir, String name)
     81        {
    7082                if (name == null)
    7183                        return null;
     
    7688                String ext = name.indexOf('.') != -1 ? "" : ".png";
    7789                String full_name = subdir+name+ext;
    78 
    79                 Image img = cache.get(full_name);
     90                String cache_name = full_name;
     91                /* cache separately */
     92                if(dirs != null && dirs.size() > 0)
     93                        cache_name = "id:"+id+":"+full_name;
     94
     95                Image img = cache.get(cache_name);
    8096                if (img == null) {
    8197                        // getImageUrl() does a ton of "stat()" calls and gets expensive
     
    84100                        // and don't bother to create a URL unless we're actually
    85101                        // creating the image.
    86                         URL path = getImageUrl(full_name);
     102                        URL path = getImageUrl(full_name, dirs);
    87103                        if (path == null)
    88104                                return null;
    89105                        img = Toolkit.getDefaultToolkit().createImage(path);
    90                         cache.put(full_name, img);
     106                        cache.put(cache_name, img);
    91107                }
    92108       
     
    94110        }
    95111
    96         private static URL getImageUrl(String imageName) {
    97             URL path = null;
    98             // Try user-preference directory first
    99         try {
    100                 if (new File(Main.pref.getPreferencesDir()+"images/"+imageName).exists())
    101                         return new URL("file", "", Main.pref.getPreferencesDir()+"images/"+imageName);
    102         } catch (MalformedURLException e) {
    103         }
    104 
    105             // Try plugins and josm classloader
    106             for (ClassLoader source : sources)
    107                         if ((path = source.getResource("images/"+imageName)) != null)
    108                                 return path;
    109 
    110             // Try all other ressource directories
    111             for (String location : Main.pref.getAllPossiblePreferenceDirs()) {
    112                 try {
    113                         if (new File(location+"images/"+imageName).exists())
    114                                 return new URL("file", "", location+"images/"+imageName);
    115                 } catch (MalformedURLException e) {
    116                 }
    117             }
    118             return null;
    119     }
     112        private static URL getImageUrl(String path, String name)
     113        {
     114                if(path.startsWith("resource://"))
     115                {
     116                        String p = path.substring("resource://".length());
     117                        for (ClassLoader source : sources)
     118                        {
     119                                URL res;
     120                                if ((res = source.getResource(p+name)) != null)
     121                                        return res;
     122                        }
     123                }
     124                else
     125                {
     126                        try {
     127                                File f = new File(path, name);
     128                                if(f.exists())
     129                                        return f.toURI().toURL();
     130                        } catch (MalformedURLException e) {}
     131                }
     132                return null;
     133        }
     134
     135        private static URL getImageUrl(String imageName, Collection<String> dirs)
     136        {
     137                URL u;
     138                // Try passed directories first
     139                if(dirs != null)
     140                {
     141                        for (String name : dirs)
     142                        {
     143                                u = getImageUrl(name, imageName);
     144                                if(u != null) return u;
     145                        }
     146                }
     147                // Try user-preference directory
     148                u = getImageUrl(Main.pref.getPreferencesDir()+"images", imageName);
     149                if(u != null) return u;
     150
     151                // Try plugins and josm classloader
     152                u = getImageUrl("resource://images/", imageName);
     153                if(u != null) return u;
     154
     155                // Try all other ressource directories
     156                for (String location : Main.pref.getAllPossiblePreferenceDirs())
     157                {
     158                        u = getImageUrl(location+"images", imageName);
     159                        if(u != null) return u;
     160                        u = getImageUrl(location, imageName);
     161                        if(u != null) return u;
     162                }
     163                return null;
     164        }
    120165
    121166        /**
Note: See TracChangeset for help on using the changeset viewer.