Changeset 15404 in josm


Ignore:
Timestamp:
2019-10-02T23:11:24+02:00 (2 months ago)
Author:
Don-vip
Message:

fix #12669 - Disable upload and save buttons when not needed

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

Legend:

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

    r14690 r15404  
    219219        // make this action listen to layer change and selection change events
    220220        if (listenToLayerChange()) {
    221             layerChangeAdapter = new LayerChangeAdapter();
    222             activeLayerChangeAdapter = new ActiveLayerChangeAdapter();
     221            layerChangeAdapter = buildLayerChangeAdapter();
     222            activeLayerChangeAdapter = buildActiveLayerChangeAdapter();
    223223            getLayerManager().addLayerChangeListener(layerChangeAdapter);
    224224            getLayerManager().addActiveLayerChangeListener(activeLayerChangeAdapter);
     
    229229        }
    230230        initEnabledState();
     231    }
     232
     233    /**
     234     * Override this if calling {@link #updateEnabledState()} on layer change events is not enough.
     235     * @return the {@link LayerChangeAdapter} that will be called on layer change events
     236     * @since 15404
     237     */
     238    protected LayerChangeAdapter buildLayerChangeAdapter() {
     239        return new LayerChangeAdapter();
     240    }
     241
     242    /**
     243     * Override this if calling {@link #updateEnabledState()} on active layer change event is not enough.
     244     * @return the {@link LayerChangeAdapter} that will be called on active layer change event
     245     * @since 15404
     246     */
     247    protected ActiveLayerChangeAdapter buildActiveLayerChangeAdapter() {
     248        return new ActiveLayerChangeAdapter();
    231249    }
    232250
  • trunk/src/org/openstreetmap/josm/actions/SaveAction.java

    r14397 r15404  
    66
    77import java.awt.event.KeyEvent;
     8import java.beans.PropertyChangeListener;
    89import java.io.File;
    910
     
    1213import org.openstreetmap.josm.gui.layer.GpxLayer;
    1314import org.openstreetmap.josm.gui.layer.Layer;
     15import org.openstreetmap.josm.gui.layer.LayerManager.LayerAddEvent;
     16import org.openstreetmap.josm.gui.layer.LayerManager.LayerRemoveEvent;
     17import org.openstreetmap.josm.gui.layer.OsmDataLayer;
     18import org.openstreetmap.josm.gui.layer.SaveToFile;
    1419import org.openstreetmap.josm.tools.Shortcut;
    1520
     
    2126public final class SaveAction extends SaveActionBase {
    2227    private static SaveAction instance = new SaveAction();
     28
     29    private final PropertyChangeListener updateOnRequireSaveChange = evt -> {
     30        if (OsmDataLayer.REQUIRES_SAVE_TO_DISK_PROP.equals(evt.getPropertyName())) {
     31            updateEnabledState();
     32        }
     33    };
    2334
    2435    /**
     
    3950    }
    4051
    41     @Override public File getFile(Layer layer) {
     52    @Override
     53    protected LayerChangeAdapter buildLayerChangeAdapter() {
     54        return new LayerChangeAdapter() {
     55            @Override
     56            public void layerAdded(LayerAddEvent e) {
     57                if (e.getAddedLayer() instanceof OsmDataLayer) {
     58                    e.getAddedLayer().addPropertyChangeListener(updateOnRequireSaveChange);
     59                }
     60                super.layerAdded(e);
     61            }
     62
     63            @Override
     64            public void layerRemoving(LayerRemoveEvent e) {
     65                if (e.getRemovedLayer() instanceof OsmDataLayer) {
     66                    e.getRemovedLayer().removePropertyChangeListener(updateOnRequireSaveChange);
     67                }
     68                super.layerRemoving(e);
     69            }
     70        };
     71    }
     72
     73    @Override
     74    protected void updateEnabledState() {
     75        Layer activeLayer = getLayerManager().getActiveLayer();
     76        setEnabled(activeLayer != null && activeLayer.isSavable()
     77                && (!(activeLayer.getAssociatedFile() != null
     78                    && activeLayer instanceof SaveToFile && !((SaveToFile) activeLayer).requiresSaveToFile())));
     79    }
     80
     81    @Override
     82    public File getFile(Layer layer) {
    4283        File f = layer.getAssociatedFile();
    4384        if (f != null && !f.exists()) {
  • trunk/src/org/openstreetmap/josm/actions/SaveActionBase.java

    r14950 r15404  
    2222import org.openstreetmap.josm.gui.layer.Layer;
    2323import org.openstreetmap.josm.gui.layer.OsmDataLayer;
     24import org.openstreetmap.josm.gui.layer.SaveToFile;
    2425import org.openstreetmap.josm.gui.util.GuiHelper;
    2526import org.openstreetmap.josm.gui.widgets.AbstractFileChooser;
     
    7374        if (!layer.checkSaveConditions())
    7475            return false;
    75         return doInternalSave(layer, getFile(layer));
     76        final boolean requiresSave = layer instanceof SaveToFile && ((SaveToFile) layer).requiresSaveToFile();
     77        final boolean result = doInternalSave(layer, getFile(layer));
     78        if (!requiresSave) {
     79            updateEnabledState();
     80        }
     81        return result;
    7682    }
    7783
     
    122128                ((OsmDataLayer) layer).onPostSaveToFile();
    123129            }
    124             MainApplication.getMainFrame().repaint();
    125130        } catch (IOException | InvalidPathException e) {
    126131            showAndLogException(e);
     
    133138    protected abstract File getFile(Layer layer);
    134139
    135     /**
    136      * Refreshes the enabled state
    137      *
    138      */
    139140    @Override
    140141    protected void updateEnabledState() {
  • trunk/src/org/openstreetmap/josm/actions/UploadAction.java

    r14397 r15404  
    77import java.awt.event.ActionEvent;
    88import java.awt.event.KeyEvent;
     9import java.beans.PropertyChangeListener;
    910import java.util.LinkedList;
    1011import java.util.List;
     
    2526import org.openstreetmap.josm.gui.HelpAwareOptionPane;
    2627import org.openstreetmap.josm.gui.MainApplication;
     28import org.openstreetmap.josm.gui.Notification;
    2729import org.openstreetmap.josm.gui.io.AsynchronousUploadPrimitivesTask;
    2830import org.openstreetmap.josm.gui.io.UploadDialog;
    2931import org.openstreetmap.josm.gui.io.UploadPrimitivesTask;
    3032import org.openstreetmap.josm.gui.layer.AbstractModifiableLayer;
     33import org.openstreetmap.josm.gui.layer.LayerManager.LayerAddEvent;
     34import org.openstreetmap.josm.gui.layer.LayerManager.LayerRemoveEvent;
    3135import org.openstreetmap.josm.gui.layer.OsmDataLayer;
    3236import org.openstreetmap.josm.gui.util.GuiHelper;
     
    4145 * Action that opens a connection to the osm server and uploads all changes.
    4246 *
    43  * An dialog is displayed asking the user to specify a rectangle to grab.
     47 * A dialog is displayed asking the user to specify a rectangle to grab.
    4448 * The url and account settings from the preferences are used.
    4549 *
     
    9195    }
    9296
     97    private final PropertyChangeListener updateOnRequireUploadChange = evt -> {
     98        if (OsmDataLayer.REQUIRES_UPLOAD_TO_SERVER_PROP.equals(evt.getPropertyName())) {
     99            updateEnabledState();
     100        }
     101    };
     102
    93103    /**
    94104     * Registers an upload hook. Adds the hook at the first position of the upload hooks.
     
    146156
    147157    @Override
     158    protected LayerChangeAdapter buildLayerChangeAdapter() {
     159        return new LayerChangeAdapter() {
     160            @Override
     161            public void layerAdded(LayerAddEvent e) {
     162                if (e.getAddedLayer() instanceof OsmDataLayer) {
     163                    e.getAddedLayer().addPropertyChangeListener(updateOnRequireUploadChange);
     164                }
     165                super.layerAdded(e);
     166            }
     167
     168            @Override
     169            public void layerRemoving(LayerRemoveEvent e) {
     170                if (e.getRemovedLayer() instanceof OsmDataLayer) {
     171                    e.getRemovedLayer().removePropertyChangeListener(updateOnRequireUploadChange);
     172                }
     173                super.layerRemoving(e);
     174            }
     175        };
     176    }
     177
     178    @Override
    148179    protected void updateEnabledState() {
    149180        OsmDataLayer editLayer = getLayerManager().getEditLayer();
    150         setEnabled(editLayer != null && editLayer.isUploadable());
     181        setEnabled(editLayer != null && editLayer.requiresUploadToServer());
    151182    }
    152183
     
    232263    public void uploadData(final OsmDataLayer layer, APIDataSet apiData) {
    233264        if (apiData.isEmpty()) {
    234             JOptionPane.showMessageDialog(
    235                     MainApplication.getMainFrame(),
    236                     tr("No changes to upload."),
    237                     tr("Warning"),
    238                     JOptionPane.INFORMATION_MESSAGE
    239             );
     265            new Notification(tr("No changes to upload.")).show();
    240266            return;
    241267        }
     
    292318            return;
    293319        if (MainApplication.getMap() == null) {
    294             JOptionPane.showMessageDialog(
    295                     MainApplication.getMainFrame(),
    296                     tr("Nothing to upload. Get some data first."),
    297                     tr("Warning"),
    298                     JOptionPane.WARNING_MESSAGE
    299             );
     320            new Notification(tr("Nothing to upload. Get some data first.")).show();
    300321            return;
    301322        }
  • trunk/src/org/openstreetmap/josm/actions/UploadSelectionAction.java

    r15152 r15404  
    1414import java.util.stream.Collectors;
    1515
    16 import javax.swing.JOptionPane;
    1716import javax.swing.SwingUtilities;
    1817
     
    2625import org.openstreetmap.josm.data.osm.visitor.OsmPrimitiveVisitor;
    2726import org.openstreetmap.josm.gui.MainApplication;
     27import org.openstreetmap.josm.gui.Notification;
    2828import org.openstreetmap.josm.gui.PleaseWaitRunnable;
    2929import org.openstreetmap.josm.gui.io.UploadSelectionDialog;
     
    6565        updateEnabledStateOnModifiableSelection(selection);
    6666        OsmDataLayer editLayer = getLayerManager().getEditLayer();
    67         if (editLayer != null && !editLayer.isUploadable()) {
     67        if (isEnabled() && editLayer != null && !editLayer.isUploadable()) {
     68            setEnabled(false);
     69        }
     70        if (isEnabled() && selection.stream().noneMatch(OsmPrimitive::isModified)) {
    6871            setEnabled(false);
    6972        }
     
    9396        Collection<OsmPrimitive> deletedCandidates = getDeletedPrimitives(editLayer.getDataSet());
    9497        if (modifiedCandidates.isEmpty() && deletedCandidates.isEmpty()) {
    95             JOptionPane.showMessageDialog(
    96                     MainApplication.getMainFrame(),
    97                     tr("No changes to upload."),
    98                     tr("Warning"),
    99                     JOptionPane.INFORMATION_MESSAGE
    100             );
     98            new Notification(tr("No changes to upload.")).show();
    10199            return;
    102100        }
     
    111109        Collection<OsmPrimitive> toUpload = new UploadHullBuilder().build(dialog.getSelectedPrimitives());
    112110        if (toUpload.isEmpty()) {
    113             JOptionPane.showMessageDialog(
    114                     MainApplication.getMainFrame(),
    115                     tr("No changes to upload."),
    116                     tr("Warning"),
    117                     JOptionPane.INFORMATION_MESSAGE
    118             );
     111            new Notification(tr("No changes to upload.")).show();
    119112            return;
    120113        }
  • trunk/src/org/openstreetmap/josm/data/osm/DataSet.java

    r15378 r15404  
    825825    @Override
    826826    public boolean isModified() {
    827         for (OsmPrimitive p : allPrimitives) {
    828             if (p.isModified())
    829                 return true;
    830         }
    831         return false;
     827        return allPrimitives.parallelStream().anyMatch(OsmPrimitive::isModified);
    832828    }
    833829
     
    838834     */
    839835    public boolean requiresUploadToServer() {
    840         for (OsmPrimitive p : allPrimitives) {
    841             if (APIOperation.of(p) != null)
    842                 return true;
    843         }
    844         return false;
     836        return allPrimitives.parallelStream().anyMatch(p -> APIOperation.of(p) != null);
    845837    }
    846838
  • trunk/src/org/openstreetmap/josm/io/session/GenericSessionExporter.java

    r14741 r15404  
    2828import org.openstreetmap.josm.gui.layer.Layer;
    2929import org.openstreetmap.josm.gui.layer.OsmDataLayer;
     30import org.openstreetmap.josm.gui.layer.SaveToFile;
    3031import org.openstreetmap.josm.gui.util.GuiHelper;
    3132import org.openstreetmap.josm.gui.widgets.JosmTextField;
     
    7374        LayerSaveAction() {
    7475            new ImageProvider("save").getResource().attachImageIcon(this);
    75             putValue(SHORT_DESCRIPTION, ((AbstractModifiableLayer) layer).requiresSaveToFile() ?
     76            putValue(SHORT_DESCRIPTION, ((SaveToFile) layer).requiresSaveToFile() ?
    7677                    tr("Layer contains unsaved data - save to file.") :
    7778                    tr("Layer does not contain unsaved data."));
     
    8687
    8788        public final void updateEnabledState() {
    88             setEnabled(((AbstractModifiableLayer) layer).requiresSaveToFile());
     89            setEnabled(((SaveToFile) layer).requiresSaveToFile());
    8990        }
    9091    }
Note: See TracChangeset for help on using the changeset viewer.