Changeset 7693 in josm


Ignore:
Timestamp:
2014-11-02T01:19:59+01:00 (9 years ago)
Author:
stoecker
Message:

see #10684 - no double loading of images, cleanup action icons - menu icon size defaults now to 16x16 (previously most time 24x24) with some errors - maybe 24x24 should be default?

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

Legend:

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

    r7692 r7693  
    1212
    1313import javax.swing.Action;
    14 import javax.swing.ImageIcon;
    1514import javax.swing.JComboBox;
    1615import javax.swing.JOptionPane;
     
    2928import org.openstreetmap.josm.tools.GBC;
    3029import org.openstreetmap.josm.tools.ImageProvider;
    31 import org.openstreetmap.josm.tools.ImageProvider.ImageCallback;
     30import org.openstreetmap.josm.tools.ImageProvider.ImageResourceCallback;
     31import org.openstreetmap.josm.tools.ImageResource;
    3232
    3333/**
     
    5252        // change toolbar icon from if specified
    5353        try {
    54             if (info.getIcon() != null) {
    55                 new ImageProvider(info.getIcon()).setOptional(true).
    56                         setMaxSize(ImageProvider.ImageSizes.SMALLICON).getInBackground(new ImageCallback() {
     54            String icon = info.getIcon();
     55            if (icon != null) {
     56                new ImageProvider(icon).setOptional(true).getInBackground(new ImageResourceCallback() {
    5757                            @Override
    58                             public void finished(final ImageIcon result) {
     58                            public void finished(final ImageResource result) {
    5959                                if (result != null) {
    6060                                    GuiHelper.runInEDT(new Runnable() {
    6161                                        @Override
    6262                                        public void run() {
    63                                             putValue(Action.SMALL_ICON, result);
    64                                         }
    65                                     });
    66                                 }
    67                             }
    68                         });
    69                 new ImageProvider(info.getIcon()).setOptional(true).
    70                         setMaxSize(ImageProvider.ImageSizes.LARGEICON).getInBackground(new ImageCallback() {
    71                             @Override
    72                             public void finished(final ImageIcon result) {
    73                                 if (result != null) {
    74                                     GuiHelper.runInEDT(new Runnable() {
    75                                         @Override
    76                                         public void run() {
    77                                             putValue(Action.LARGE_ICON_KEY, result);
     63                                            result.getImageIcon(AddImageryLayerAction.this);
    7864                                        }
    7965                                    });
  • trunk/src/org/openstreetmap/josm/actions/JosmAction.java

    r7534 r7693  
    7272     * @param toolbarId identifier for the toolbar preferences. The iconName is used, if this parameter is null
    7373     * @param installAdapters false, if you don't want to install layer changed and selection changed adapters
    74      */
     74     * @deprecated do not pass Icon, pass ImageProvider instead
     75     */
     76    @Deprecated
    7577    public JosmAction(String name, Icon icon, String tooltip, Shortcut shortcut, boolean registerInToolbar, String toolbarId, boolean installAdapters) {
    7678        super(name, icon);
     
    9395
    9496    /**
     97     * Constructs a {@code JosmAction}.
     98     *
     99     * @param name the action's text as displayed on the menu (if it is added to a menu)
     100     * @param icon the icon to use
     101     * @param tooltip  a longer description of the action that will be displayed in the tooltip. Please note
     102     *           that html is not supported for menu actions on some platforms.
     103     * @param shortcut a ready-created shortcut object or null if you don't want a shortcut. But you always
     104     *            do want a shortcut, remember you can always register it with group=none, so you
     105     *            won't be assigned a shortcut unless the user configures one. If you pass null here,
     106     *            the user CANNOT configure a shortcut for your action.
     107     * @param registerInToolbar register this action for the toolbar preferences?
     108     * @param toolbarId identifier for the toolbar preferences. The iconName is used, if this parameter is null
     109     * @param installAdapters false, if you don't want to install layer changed and selection changed adapters
     110     * TODO: do not pass Icon, pass ImageProvider instead
     111     */
     112    public JosmAction(String name, ImageProvider icon, String tooltip, Shortcut shortcut, boolean registerInToolbar, String toolbarId, boolean installAdapters) {
     113        super(name);
     114        if(icon != null)
     115            icon.getResource().getImageIcon(this);
     116        setHelpId();
     117        sc = shortcut;
     118        if (sc != null) {
     119            Main.registerActionShortcut(this, sc);
     120        }
     121        setTooltip(tooltip);
     122        if (getValue("toolbar") == null) {
     123            putValue("toolbar", toolbarId);
     124        }
     125        if (registerInToolbar && Main.toolbar != null) {
     126            Main.toolbar.register(this);
     127        }
     128        if (installAdapters) {
     129            installAdapters();
     130        }
     131    }
     132
     133    /**
    95134     * The new super for all actions.
    96135     *
     
    110149     */
    111150    public JosmAction(String name, String iconName, String tooltip, Shortcut shortcut, boolean registerInToolbar, String toolbarId, boolean installAdapters) {
    112         this(name, iconName == null ? null : ImageProvider.get(iconName), tooltip, shortcut, registerInToolbar,
     151        this(name, iconName == null ? null : new ImageProvider(iconName), tooltip, shortcut, registerInToolbar,
    113152                toolbarId == null ? iconName : toolbarId, installAdapters);
    114153    }
  • trunk/src/org/openstreetmap/josm/actions/JumpToAction.java

    r7081 r7693  
    99import java.awt.event.KeyEvent;
    1010
    11 import javax.swing.Icon;
    1211import javax.swing.JLabel;
    1312import javax.swing.JOptionPane;
     
    2423import org.openstreetmap.josm.gui.widgets.JosmTextField;
    2524import org.openstreetmap.josm.tools.GBC;
     25import org.openstreetmap.josm.tools.ImageProvider;
    2626import org.openstreetmap.josm.tools.OsmUrlToBounds;
    2727import org.openstreetmap.josm.tools.Shortcut;
     
    3737     */
    3838    public JumpToAction() {
    39         super(tr("Jump To Position"), (Icon) null, tr("Opens a dialog that allows to jump to a specific location"),
     39        super(tr("Jump To Position"), (ImageProvider) null, tr("Opens a dialog that allows to jump to a specific location"),
    4040                Shortcut.registerShortcut("tools:jumpto", tr("Tool: {0}", tr("Jump To Position")),
    4141                        KeyEvent.VK_J, Shortcut.CTRL), true, "action/jumpto", true);
  • trunk/src/org/openstreetmap/josm/gui/tagging/TaggingPreset.java

    r7687 r7693  
    5252import org.openstreetmap.josm.tools.GBC;
    5353import org.openstreetmap.josm.tools.ImageProvider;
     54import org.openstreetmap.josm.tools.ImageResource;
    5455import org.openstreetmap.josm.tools.Predicate;
    5556import org.openstreetmap.josm.tools.Utils;
     
    153154        imgProv.setArchive(arch);
    154155        imgProv.setOptional(true);
    155         imgProv.setSize(ImageProvider.ImageSizes.SMALLICON);
    156         imgProv.getInBackground(new ImageProvider.ImageCallback() {
     156        imgProv.getInBackground(new ImageProvider.ImageResourceCallback() {
    157157            @Override
    158             public void finished(final ImageIcon result) {
     158            public void finished(final ImageResource result) {
    159159                if (result != null) {
    160160                    GuiHelper.runInEDT(new Runnable() {
    161161                        @Override
    162162                        public void run() {
    163                             putValue(Action.SMALL_ICON, result);
     163                            result.getImageIcon(TaggingPreset.this);
    164164                        }
    165165                    });
    166166                } else {
    167167                    Main.warn("Could not get presets icon " + iconName);
    168                 }
    169             }
    170         });
    171         imgProv = new ImageProvider(iconName);
    172         imgProv.setDirs(s);
    173         imgProv.setId("presets");
    174         imgProv.setArchive(arch);
    175         imgProv.setOptional(true);
    176         imgProv.setSize(ImageProvider.ImageSizes.LARGEICON);
    177         imgProv.getInBackground(new ImageProvider.ImageCallback() {
    178             @Override
    179             public void finished(final ImageIcon result) {
    180                 if (result != null) {
    181                     GuiHelper.runInEDT(new Runnable() {
    182                         @Override
    183                         public void run() {
    184                             putValue(Action.LARGE_ICON_KEY, result);
    185                         }
    186                     });
    187168                }
    188169            }
  • trunk/src/org/openstreetmap/josm/tools/ImageProvider.java

    r7689 r7693  
    163163    private static final ExecutorService IMAGE_FETCHER = Executors.newSingleThreadExecutor();
    164164
     165    /**
     166     * Callback interface for asynchronous image loading.
     167     */
    165168    public interface ImageCallback {
    166169        void finished(ImageIcon result);
     170    }
     171
     172    /**
     173     * Callback interface for asynchronous image loading (with delayed scaling possibility).
     174     * @since 7693
     175     */
     176    public interface ImageResourceCallback {
     177        void finished(ImageResource result);
    167178    }
    168179
     
    237248     * @since 7687
    238249     */
    239     public Dimension getImageSizes(ImageSizes size) {
     250    static public Dimension getImageSizes(ImageSizes size) {
    240251        int sizeval;
    241252        switch(size) {
     
    382393
    383394    /**
     395     * Execute the image request and scale result.
     396     * @return the requested image or null if the request failed
     397     */
     398    public ImageIcon get() {
     399        ImageResource ir = getResource();
     400        if (maxWidth != -1 || maxHeight != -1)
     401            return ir.getImageIconBounded(new Dimension(maxWidth, maxHeight));
     402        else
     403            return ir.getImageIcon(new Dimension(width, height));
     404    }
     405
     406    /**
    384407     * Execute the image request.
    385408     * @return the requested image or null if the request failed
    386      */
    387     public ImageIcon get() {
     409     * @since 7693
     410     */
     411    public ImageResource getResource() {
    388412        ImageResource ir = getIfAvailableImpl(additionalClassLoaders);
    389413        if (ir == null) {
     
    398422            }
    399423        }
    400         if (maxWidth != -1 || maxHeight != -1)
    401             return ir.getImageIconBounded(new Dimension(maxWidth, maxHeight));
    402         else
    403             return ir.getImageIcon(new Dimension(width, height));
     424        return ir;
    404425    }
    405426
     
    428449            ImageIcon result = get();
    429450            callback.finished(result);
     451        }
     452    }
     453
     454    /**
     455     * Load the image in a background thread.
     456     *
     457     * This method returns immediately and runs the image request
     458     * asynchronously.
     459     *
     460     * @param callback a callback. It is called, when the image is ready.
     461     * This can happen before the call to this method returns or it may be
     462     * invoked some time (seconds) later. If no image is available, a null
     463     * value is returned to callback (just like {@link #get}).
     464     * @since 7693
     465     */
     466    public void getInBackground(final ImageResourceCallback callback) {
     467        if (name.startsWith("http://") || name.startsWith("wiki://")) {
     468            Runnable fetch = new Runnable() {
     469                @Override
     470                public void run() {
     471                    callback.finished(getResource());
     472                }
     473            };
     474            IMAGE_FETCHER.submit(fetch);
     475        } else {
     476            callback.finished(getResource());
    430477        }
    431478    }
  • trunk/src/org/openstreetmap/josm/tools/ImageResource.java

    r7005 r7693  
    88import java.util.Map;
    99
     10import javax.swing.Action;
     11import javax.swing.AbstractAction;
    1012import javax.swing.ImageIcon;
    1113
     
    2022 * @since 4271
    2123 */
    22 class ImageResource {
     24public class ImageResource {
    2325
    2426    /**
     
    4345    }
    4446
     47    /**
     48     * Set both icons of an Action
     49     * @param action The action for the icons
     50     * @since 7693
     51     */
     52    public void getImageIcon(AbstractAction a) {
     53        ImageIcon icon = getImageIcon(ImageProvider.getImageSizes(ImageProvider.ImageSizes.SMALLICON));
     54        a.putValue(Action.SMALL_ICON, icon);
     55        icon = getImageIcon(ImageProvider.getImageSizes(ImageProvider.ImageSizes.LARGEICON));
     56        a.putValue(Action.LARGE_ICON_KEY, icon);
     57    }
     58   
    4559    /**
    4660     * Get an ImageIcon object for the image of this resource
Note: See TracChangeset for help on using the changeset viewer.