Changeset 11885 in josm


Ignore:
Timestamp:
2017-04-10T23:49:54+02:00 (4 months ago)
Author:
Don-vip
Message:

add basic unit tests for MergeLayerAction + improve javadoc, code cleanup

Location:
trunk
Files:
1 added
5 edited

Legend:

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

    r11848 r11885  
    1010
    1111import javax.swing.DefaultListCellRenderer;
    12 import javax.swing.Icon;
    1312import javax.swing.JLabel;
    1413import javax.swing.JList;
     
    2423import org.openstreetmap.josm.tools.Utils;
    2524
     25/**
     26 * Abstract superclass of different "Merge" actions.
     27 * @since 1890
     28 */
    2629public abstract class AbstractMergeAction extends JosmAction {
    2730
    2831    /**
    2932     * the list cell renderer used to render layer list entries
    30      *
    3133     */
    3234    public static class LayerListCellRenderer extends DefaultListCellRenderer {
     
    3638            Layer layer = (Layer) value;
    3739            JLabel label = (JLabel) super.getListCellRendererComponent(list, layer.getName(), index, isSelected, cellHasFocus);
    38             Icon icon = layer.getIcon();
    39             label.setIcon(icon);
     40            label.setIcon(layer.getIcon());
    4041            label.setToolTipText(layer.getToolTipText());
    4142            return label;
     
    4546    /**
    4647     * Constructs a new {@code AbstractMergeAction}.
     48     * @param name the action's text as displayed on the menu (if it is added to a menu)
     49     * @param iconName the filename of the icon to use
     50     * @param tooltip  a longer description of the action that will be displayed in the tooltip. Please note
     51     *           that html is not supported for menu actions on some platforms.
     52     * @param shortcut a ready-created shortcut object or null if you don't want a shortcut. But you always
     53     *            do want a shortcut, remember you can always register it with group=none, so you
     54     *            won't be assigned a shortcut unless the user configures one. If you pass null here,
     55     *            the user CANNOT configure a shortcut for your action.
     56     * @param register register this action for the toolbar preferences?
    4757     */
    48     public AbstractMergeAction() {
    49         super();
    50     }
    51 
    5258    public AbstractMergeAction(String name, String iconName, String tooltip, Shortcut shortcut, boolean register) {
    5359        super(name, iconName, tooltip, shortcut, register);
    5460    }
    5561
     62    /**
     63     * Constructs a new {@code AbstractMergeAction}.
     64     * @param name the action's text as displayed on the menu (if it is added to a menu)
     65     * @param iconName the filename of the icon to use
     66     * @param tooltip  a longer description of the action that will be displayed in the tooltip. Please note
     67     *           that html is not supported for menu actions on some platforms.
     68     * @param shortcut a ready-created shortcut object or null if you don't want a shortcut. But you always
     69     *            do want a shortcut, remember you can always register it with group=none, so you
     70     *            won't be assigned a shortcut unless the user configures one. If you pass null here,
     71     *            the user CANNOT configure a shortcut for your action.
     72     * @param register register this action for the toolbar preferences?
     73     * @param toolbar identifier for the toolbar preferences. The iconName is used, if this parameter is null
     74     * @param installAdapters false, if you don't want to install layer changed and selection changed adapters
     75     */
    5676    public AbstractMergeAction(String name, String iconName, String tooltip, Shortcut shortcut,
    5777    boolean register, String toolbar, boolean installAdapters) {
     
    5979    }
    6080
     81    /**
     82     * Ask user to choose the target layer.
     83     * @param targetLayers list of candidate target layers.
     84     * @return the chosen layer
     85     */
    6186    protected static Layer askTargetLayer(List<Layer> targetLayers) {
    6287        return askTargetLayer(targetLayers.toArray(new Layer[targetLayers.size()]),
     
    99124    }
    100125
     126    /**
     127     * Warns user when there no layers the source layer could be merged to.
     128     * @param sourceLayer source layer
     129     */
    101130    protected void warnNoTargetLayersForSourceLayer(Layer sourceLayer) {
    102         JOptionPane.showMessageDialog(Main.parent,
    103                 tr("<html>There are no layers the source layer<br>''{0}''<br>could be merged to.</html>",
    104                         Utils.escapeReservedCharactersHTML(sourceLayer.getName())),
    105                 tr("No target layers"), JOptionPane.WARNING_MESSAGE);
     131        String message = tr("<html>There are no layers the source layer<br>''{0}''<br>could be merged to.</html>",
     132                Utils.escapeReservedCharactersHTML(sourceLayer.getName()));
     133        if (!GraphicsEnvironment.isHeadless()) {
     134            JOptionPane.showMessageDialog(Main.parent, message, tr("No target layers"), JOptionPane.WARNING_MESSAGE);
     135        }
    106136    }
    107137}
  • trunk/src/org/openstreetmap/josm/actions/MergeLayerAction.java

    r11848 r11885  
    1010import java.util.Collections;
    1111import java.util.List;
     12import java.util.concurrent.Future;
    1213
    1314import org.openstreetmap.josm.Main;
     
    3839    }
    3940
    40     protected void doMerge(List<Layer> targetLayers, final Collection<Layer> sourceLayers) {
     41    /**
     42     * Submits merge of layers.
     43     * @param targetLayers possible target layers
     44     * @param sourceLayers source layers
     45     * @return a Future representing pending completion of the merge task, or {@code null}
     46     * @since 11885 (return type)
     47     */
     48    protected Future<?> doMerge(List<Layer> targetLayers, final Collection<Layer> sourceLayers) {
    4149        final Layer targetLayer = askTargetLayer(targetLayers);
    4250        if (targetLayer == null)
    43             return;
     51            return null;
    4452        final Object actionName = getValue(NAME);
    45         Main.worker.submit(() -> {
     53        return Main.worker.submit(() -> {
    4654                final long start = System.currentTimeMillis();
    4755                boolean layerMerged = false;
     
    6977     * Merges a list of layers together.
    7078     * @param sourceLayers The layers to merge
     79     * @return a Future representing pending completion of the merge task, or {@code null}
     80     * @since 11885 (return type)
    7181     */
    72     public void merge(List<Layer> sourceLayers) {
    73         doMerge(sourceLayers, sourceLayers);
     82    public Future<?> merge(List<Layer> sourceLayers) {
     83        return doMerge(sourceLayers, sourceLayers);
    7484    }
    7585
     
    7787     * Merges the given source layer with another one, determined at runtime.
    7888     * @param sourceLayer The source layer to merge
     89     * @return a Future representing pending completion of the merge task, or {@code null}
     90     * @since 11885 (return type)
    7991     */
    80     public void merge(Layer sourceLayer) {
     92    public Future<?> merge(Layer sourceLayer) {
    8193        if (sourceLayer == null)
    82             return;
     94            return null;
    8395        List<Layer> targetLayers = LayerListDialog.getInstance().getModel().getPossibleMergeTargets(sourceLayer);
    8496        if (targetLayers.isEmpty()) {
    8597            warnNoTargetLayersForSourceLayer(sourceLayer);
    86             return;
     98            return null;
    8799        }
    88         doMerge(targetLayers, Collections.singleton(sourceLayer));
     100        return doMerge(targetLayers, Collections.singleton(sourceLayer));
    89101    }
    90102
     
    112124    }
    113125
     126    /**
     127     * Returns the source layer.
     128     * @return the source layer
     129     */
    114130    protected Layer getSourceLayer() {
    115         return Main.map != null ? Main.getLayerManager().getActiveLayer() : null;
     131        return Main.getLayerManager().getActiveLayer();
    116132    }
    117133
  • trunk/src/org/openstreetmap/josm/gui/MapFrame.java

    r11713 r11885  
    243243
    244244        // toolBarToggles, toggle dialog buttons
    245         LayerListDialog.createInstance(this);
     245        LayerListDialog.createInstance(mapView.getLayerManager());
    246246        propertiesDialog = new PropertiesDialog();
    247247        selectionListDialog = new SelectionListDialog();
  • trunk/src/org/openstreetmap/josm/gui/dialogs/LayerListDialog.java

    r11881 r11885  
    4444import org.openstreetmap.josm.actions.MergeLayerAction;
    4545import org.openstreetmap.josm.data.preferences.AbstractProperty;
    46 import org.openstreetmap.josm.gui.MapFrame;
    4746import org.openstreetmap.josm.gui.MapView;
    4847import org.openstreetmap.josm.gui.SideButton;
     
    9089
    9190    /**
    92      * Creates the instance of the dialog. It's connected to the map frame <code>mapFrame</code>
     91     * Creates the instance of the dialog. It's connected to the layer manager
    9392     *
    94      * @param mapFrame the map frame
    95      */
    96     public static void createInstance(MapFrame mapFrame) {
     93     * @param layerManager the layer manager
     94     * @since 11885 (signature)
     95     */
     96    public static void createInstance(MainLayerManager layerManager) {
    9797        if (instance != null)
    9898            throw new IllegalStateException("Dialog was already created");
    99         instance = new LayerListDialog(mapFrame);
     99        instance = new LayerListDialog(layerManager);
    100100    }
    101101
     
    105105     * @return the instance of the dialog
    106106     * @throws IllegalStateException if the dialog is not created yet
    107      * @see #createInstance(MapFrame)
     107     * @see #createInstance(MainLayerManager)
    108108     */
    109109    public static LayerListDialog getInstance() {
     
    164164
    165165    /**
    166      * Creates a layer list and attach it to the given mapView.
    167      * @param mapFrame map frame
    168      */
    169     protected LayerListDialog(MapFrame mapFrame) {
    170         this(mapFrame.mapView.getLayerManager());
    171     }
    172 
    173     /**
    174      * Creates a layer list and attach it to the given mapView.
     166     * Creates a layer list and attach it to the given layer manager.
    175167     * @param layerManager The layer manager this list is for
    176168     * @since 10467
  • trunk/src/org/openstreetmap/josm/gui/dialogs/ToggleDialog.java

    r11538 r11885  
    265265     */
    266266    protected void registerInWindowMenu() {
    267         windowMenuItem = MainMenu.addWithCheckbox(Main.main.menu.windowMenu,
    268                 (JosmAction) getToggleAction(),
    269                 MainMenu.WINDOW_MENU_GROUP.TOGGLE_DIALOG);
     267        if (Main.main != null) {
     268            windowMenuItem = MainMenu.addWithCheckbox(Main.main.menu.windowMenu,
     269                    (JosmAction) getToggleAction(),
     270                    MainMenu.WINDOW_MENU_GROUP.TOGGLE_DIALOG);
     271        }
    270272    }
    271273
     
    329331        // toggling the selected value in order to enforce PropertyChangeEvents
    330332        setIsShowing(true);
    331         windowMenuItem.setState(true);
     333        if (windowMenuItem != null) {
     334            windowMenuItem.setState(true);
     335        }
    332336        toggleAction.putValue("selected", Boolean.FALSE);
    333337        toggleAction.putValue("selected", Boolean.TRUE);
     
    374378        closeDetachedDialog();
    375379        this.setVisible(false);
    376         windowMenuItem.setState(false);
     380        if (windowMenuItem != null) {
     381            windowMenuItem.setState(false);
     382        }
    377383        setIsShowing(false);
    378384        toggleAction.putValue("selected", Boolean.FALSE);
     
    456462            hideNotify();
    457463        }
    458         Main.main.menu.windowMenu.remove(windowMenuItem);
     464        if (Main.main != null) {
     465            Main.main.menu.windowMenu.remove(windowMenuItem);
     466        }
    459467        Toolkit.getDefaultToolkit().removeAWTEventListener(this);
    460468        Main.pref.removePreferenceChangeListener(this);
Note: See TracChangeset for help on using the changeset viewer.