Changeset 8097 in josm for trunk/src/org


Ignore:
Timestamp:
2015-02-22T14:19:20+01:00 (9 years ago)
Author:
stoecker
Message:

see #10684, see #10688 - fix image scaling for mappaint

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

Legend:

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

    r7937 r8097  
    3434
    3535        public MapPaintAction(StyleSource style) {
    36             super(style.getDisplayString(), style.getIcon(),
     36            super(style.getDisplayString(), style.getIconProvider(),
    3737                    tr("Select the map painting styles"), null, true, "mappaint/" + style.getDisplayString(), true);
    3838            this.button = new StayOpenCheckBoxMenuItem(this);
  • trunk/src/org/openstreetmap/josm/gui/mappaint/MapPaintStyles.java

    r8085 r8097  
    102102    }
    103103
    104     public static ImageIcon getIcon(IconReference ref, int width, int height) {
     104    /**
     105     * Image provider for icon. Note that this is a provider only. A @link{ImageProvider#get()} call may still
     106     * fail!
     107     *
     108     * @param ref reference to the requested icon
     109     * @param test if <code>true</code> than the icon is request is tested
     110     * @return image provider for icon (can be <code>null</code> when <code>test</code> is <code>true</code>).
     111     * @since 8097
     112     * @see #getIcon(IconReference, int,int)
     113     */
     114    public static ImageProvider getIconProvider(IconReference ref, boolean test) {
    105115        final String namespace = ref.source.getPrefName();
    106         ImageIcon i = new ImageProvider(ref.iconName)
     116        ImageProvider i = new ImageProvider(ref.iconName)
    107117                .setDirs(getIconSourceDirs(ref.source))
    108118                .setId("mappaint."+namespace)
    109119                .setArchive(ref.source.zipIcons)
    110120                .setInArchiveDir(ref.source.getZipEntryDirName())
    111                 .setWidth(width)
    112                 .setHeight(height)
    113                 .setOptional(true).get();
     121                .setOptional(true);
     122        if (test && i.get() == null) {
     123            Main.warn("Mappaint style \""+namespace+"\" ("+ref.source.getDisplayString()+") icon \"" + ref.iconName + "\" not found.");
     124            return null;
     125        }
     126        return i;
     127    }
     128
     129    /**
     130     * Return scaled icon.
     131     *
     132     * @param ref reference to the requested icon
     133     * @param width icon width or -1 for autoscale
     134     * @param height icon height or -1 for autoscale
     135     * @return image icon or <code>null</code>.
     136     * @see #getIconProvider(IconReference, boolean)
     137     */
     138    public static ImageIcon getIcon(IconReference ref, int width, int height) {
     139        final String namespace = ref.source.getPrefName();
     140        ImageIcon i = getIconProvider(ref, false).setWidth(width).setHeight(height).get();
    114141        if (i == null) {
    115142            Main.warn("Mappaint style \""+namespace+"\" ("+ref.source.getDisplayString()+") icon \"" + ref.iconName + "\" not found.");
  • trunk/src/org/openstreetmap/josm/gui/mappaint/StyleSource.java

    r7592 r8097  
    2121import org.openstreetmap.josm.gui.preferences.SourceEntry;
    2222import org.openstreetmap.josm.io.CachedFile;
     23import org.openstreetmap.josm.tools.ImageOverlay;
    2324import org.openstreetmap.josm.tools.ImageProvider;
    2425import org.openstreetmap.josm.tools.Utils;
     
    3536    public File zipIcons;
    3637
    37     private ImageIcon imageIcon;
     38    /** image provider returning the icon for this style */
     39    private ImageProvider imageIconProvider;
     40
     41    /** image provider returning the default icon */
     42    private static ImageProvider defaultIconProvider;
    3843
    3944    /******
     
    4146     * of the source file.
    4247     */
    43 
    4448    public String icon;
    4549
     
    115119    }
    116120
     121    /**
     122     * Initialize the class.
     123     */
    117124    protected void init() {
    118125        errors.clear();
    119         imageIcon = null;
     126        imageIconProvider = null;
    120127        icon = null;
    121128    }
    122129
    123     private static ImageIcon defaultIcon;
    124 
    125     private static ImageIcon getDefaultIcon() {
    126         if (defaultIcon == null) {
    127             defaultIcon = ImageProvider.get("dialogs/mappaint", "pencil");
     130    /**
     131     * Image provider for default icon.
     132     *
     133     * @return image provider for default styles icon
     134     * @since 8097
     135     * @see #getIconProvider()
     136     */
     137    private static ImageProvider getDefaultIconProvider() {
     138        if (defaultIconProvider == null) {
     139            defaultIconProvider = new ImageProvider("dialogs/mappaint", "pencil");
    128140        }
    129         return defaultIcon;
    130     }
    131 
    132     protected ImageIcon getSourceIcon() {
    133         if (imageIcon == null) {
     141        return defaultIconProvider;
     142    }
     143
     144    /**
     145     * Image provider for source icon. Uses default icon, when not else available.
     146     *
     147     * @return image provider for styles icon
     148     * @since 8097
     149     * @see #getIconProvider()
     150     */
     151    protected ImageProvider getSourceIconProvider() {
     152        if (imageIconProvider == null) {
    134153            if (icon != null) {
    135                 imageIcon = MapPaintStyles.getIcon(new IconReference(icon, this), -1, -1);
     154                imageIconProvider = MapPaintStyles.getIconProvider(new IconReference(icon, this), true);
    136155            }
    137             if (imageIcon == null) {
    138                 imageIcon = getDefaultIcon();
     156            if (imageIconProvider == null) {
     157                imageIconProvider = getDefaultIconProvider();
    139158            }
    140159        }
    141         return imageIcon;
    142     }
    143 
     160        return imageIconProvider;
     161    }
     162
     163    /**
     164     * Image provider for source icon.
     165     *
     166     * @return image provider for styles icon
     167     * @since 8097
     168     */
     169    public final ImageProvider getIconProvider() {
     170        ImageProvider i = getSourceIconProvider();
     171        if (!getErrors().isEmpty()) {
     172            i = new ImageProvider(i).addOverlay(new ImageOverlay(new ImageProvider("dialogs/mappaint/error_small")));
     173        }
     174        return i;
     175    }
     176
     177    /**
     178     * Image for source icon.
     179     *
     180     * @return styles icon for display
     181     */
    144182    public final ImageIcon getIcon() {
    145         if (getErrors().isEmpty())
    146             return getSourceIcon();
    147         else
    148             return ImageProvider.overlay(getSourceIcon(),
    149                     ImageProvider.get("dialogs/mappaint/error_small"),
    150                     ImageProvider.OverlayPosition.SOUTHEAST);
    151     }
    152 
     183        return getIconProvider().setMaxSize(ImageProvider.ImageSizes.MENU).get();
     184    }
     185
     186    /**
     187     * Return text to display as ToolTip.
     188     *
     189     * @return tooltip text containing error status
     190     */
    153191    public String getToolTipText() {
    154192        if (errors.isEmpty())
  • trunk/src/org/openstreetmap/josm/tools/ImageOverlay.java

    r8095 r8097  
    8181        ImageIcon overlay;
    8282        if(width != -1 || height != -1) {
    83             /* Don't modify ImageProvider, but apply maximum size, probably cloning the ImageProvider
    84                would be a better approach. */
    85             overlay = image.getResource().getImageIconBounded(new Dimension(width, height));
    86         } else {
    87             overlay = image.get();
     83            image = new ImageProvider(image).resetMaxSize(new Dimension(width, height));
    8884        }
     85        overlay = image.get();
    8986        int x, y;
    9087        if (width == -1 && offsetLeft < 0) {
  • trunk/src/org/openstreetmap/josm/tools/ImageProvider.java

    r8096 r8097  
    240240
    241241    /**
     242     * Constructs a new {@code ImageProvider} from an existing one.
     243     * @param image the existing image provider to be copied
     244     * @since 8095
     245     */
     246    public ImageProvider(ImageProvider image) {
     247        this.dirs = image.dirs;
     248        this.id = image.id;
     249        this.subdir = image.subdir;
     250        this.name = image.name;
     251        this.archive = image.archive;
     252        this.inArchiveDir = image.inArchiveDir;
     253        this.width = image.width;
     254        this.height = image.height;
     255        this.maxWidth = image.maxWidth;
     256        this.maxHeight = image.maxHeight;
     257        this.optional = image.optional;
     258        this.suppressWarnings = image.suppressWarnings;
     259        this.additionalClassLoaders = image.additionalClassLoaders;
     260        this.overlayInfo = image.overlayInfo;
     261    }
     262
     263    /**
    242264     * Directories to look for the image.
    243265     * @param dirs The directories to look for.
     
    384406        this.maxWidth = maxSize.width;
    385407        this.maxHeight = maxSize.height;
     408        return this;
     409    }
     410
     411    /**
     412     * Limit the maximum size of the image.
     413     *
     414     * It will shrink the image if necessary, but keep the aspect ratio.
     415     * The given width or height can be -1 which means this direction is not bounded.
     416     *
     417     * This function sets value using the most restrictive of the new or existing set of
     418     * values.
     419     *
     420     * @param maxSize maximum image size
     421     * @return the current object, for convenience
     422     * @see #setMaxSize(Dimension)
     423     */
     424    public ImageProvider resetMaxSize(Dimension maxSize) {
     425        if (this.maxWidth == -1 || maxSize.width < this.maxWidth) {
     426            this.maxWidth = maxSize.width;
     427        }
     428        if (this.maxHeight == -1 || maxSize.height < this.maxHeight) {
     429            this.maxHeight = maxSize.height;
     430        }
    386431        return this;
    387432    }
Note: See TracChangeset for help on using the changeset viewer.