kopia lustrzana https://github.com/JOSM/MapWithAI
Enable/disable download based off of user interactions in the layer panel
Signed-off-by: Taylor Smock <taylor.smock@kaart.com>pull/1/head
rodzic
7fab30e4d0
commit
db7089e406
|
@ -28,6 +28,7 @@ import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAIInfo.Map
|
|||
import org.openstreetmap.josm.plugins.mapwithai.io.mapwithai.ESRISourceReader;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.io.mapwithai.MapWithAISourceReader;
|
||||
import org.openstreetmap.josm.spi.preferences.Config;
|
||||
import org.openstreetmap.josm.tools.ListenerList;
|
||||
import org.openstreetmap.josm.tools.Logging;
|
||||
import org.openstreetmap.josm.tools.Utils;
|
||||
|
||||
|
@ -40,6 +41,7 @@ public class MapWithAILayerInfo {
|
|||
private final List<MapWithAIInfo> layers = Collections.synchronizedList(new ArrayList<>());
|
||||
/** List of layer ids of all usable layers */
|
||||
private final Map<String, MapWithAIInfo> layerIds = new HashMap<>();
|
||||
private ListenerList<LayerChangeListener> listeners = ListenerList.create();
|
||||
/** List of all available default layers */
|
||||
static final List<MapWithAIInfo> defaultLayers = new ArrayList<>();
|
||||
/** List of all available default layers (including mirrors) */
|
||||
|
@ -381,6 +383,7 @@ public class MapWithAILayerInfo {
|
|||
*/
|
||||
public void add(MapWithAIInfo info) {
|
||||
layers.add(info);
|
||||
this.listeners.fireEvent(l -> l.changeEvent(info));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -390,6 +393,7 @@ public class MapWithAILayerInfo {
|
|||
*/
|
||||
public void remove(MapWithAIInfo info) {
|
||||
layers.remove(info);
|
||||
this.listeners.fireEvent(l -> l.changeEvent(info));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -480,4 +484,30 @@ public class MapWithAILayerInfo {
|
|||
*/
|
||||
void onFinish();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a listener that is called on layer change events. Only fires on single
|
||||
* add/remove events.
|
||||
*
|
||||
* @param listener The listener to be called.
|
||||
*/
|
||||
public void addListener(LayerChangeListener listener) {
|
||||
this.listeners.addListener(listener);
|
||||
}
|
||||
|
||||
/**
|
||||
* An interface to tell listeners what info object has changed
|
||||
*
|
||||
* @author Taylor Smock
|
||||
*
|
||||
*/
|
||||
public interface LayerChangeListener {
|
||||
/**
|
||||
* Fired when an info object has been added/removed to the layer list
|
||||
*
|
||||
* @param modified A MapWithAIInfo object that has been removed or added to the
|
||||
* layers
|
||||
*/
|
||||
void changeEvent(MapWithAIInfo modified);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,6 +3,8 @@ package org.openstreetmap.josm.plugins.mapwithai.gui.download;
|
|||
|
||||
import static org.openstreetmap.josm.tools.I18n.tr;
|
||||
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import javax.swing.JCheckBox;
|
||||
import javax.swing.event.ChangeListener;
|
||||
|
||||
|
@ -13,10 +15,14 @@ import org.openstreetmap.josm.data.preferences.BooleanProperty;
|
|||
import org.openstreetmap.josm.gui.download.IDownloadSourceType;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.backend.DownloadMapWithAITask;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.backend.MapWithAIDataUtils;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAIInfo;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAILayerInfo;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAILayerInfo.LayerChangeListener;
|
||||
|
||||
public class MapWithAIDownloadSourceType implements IDownloadSourceType {
|
||||
public class MapWithAIDownloadSourceType implements IDownloadSourceType, LayerChangeListener {
|
||||
static final BooleanProperty IS_ENABLED = new BooleanProperty("download.mapwithai.data", false);
|
||||
JCheckBox cbDownloadMapWithAIData;
|
||||
private static MapWithAIDownloadSourceType INSTANCE;
|
||||
|
||||
@Override
|
||||
public JCheckBox getCheckBox(ChangeListener checkboxChangeListener) {
|
||||
|
@ -25,6 +31,7 @@ public class MapWithAIDownloadSourceType implements IDownloadSourceType {
|
|||
cbDownloadMapWithAIData
|
||||
.setToolTipText(tr("Select to download MapWithAI data in the selected download area."));
|
||||
cbDownloadMapWithAIData.getModel().addChangeListener(checkboxChangeListener);
|
||||
MapWithAILayerInfo.getInstance().addListener(this);
|
||||
}
|
||||
if (checkboxChangeListener != null) {
|
||||
cbDownloadMapWithAIData.getModel().addChangeListener(checkboxChangeListener);
|
||||
|
@ -62,4 +69,16 @@ public class MapWithAIDownloadSourceType implements IDownloadSourceType {
|
|||
|| width > MapWithAIDataUtils.MAXIMUM_SIDE_DIMENSIONS;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void changeEvent(MapWithAIInfo modified) {
|
||||
if (Stream.of(Thread.currentThread().getStackTrace()).map(p -> p.getClassName())
|
||||
.noneMatch(p -> p.contains("PreferenceDialog"))) {
|
||||
if (MapWithAILayerInfo.getInstance().getLayers().contains(modified)) {
|
||||
this.cbDownloadMapWithAIData.setSelected(true);
|
||||
} else if (MapWithAILayerInfo.getInstance().getLayers().isEmpty()) {
|
||||
this.cbDownloadMapWithAIData.setSelected(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -62,6 +62,7 @@ import org.openstreetmap.josm.gui.widgets.HtmlPanel;
|
|||
import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAICategory;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAIInfo;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAILayerInfo;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAILayerInfo.LayerChangeListener;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAIType;
|
||||
import org.openstreetmap.josm.plugins.mapwithai.io.mapwithai.ESRISourceReader;
|
||||
import org.openstreetmap.josm.tools.GBC;
|
||||
|
@ -670,7 +671,7 @@ public class MapWithAIProvidersPanel extends JPanel {
|
|||
}
|
||||
}
|
||||
|
||||
private class ActivateAction extends AbstractAction implements ListSelectionListener {
|
||||
private class ActivateAction extends AbstractAction implements ListSelectionListener, LayerChangeListener {
|
||||
private static final long serialVersionUID = -452335751201424801L;
|
||||
private final transient ImageResource activate;
|
||||
private final transient ImageResource deactivate;
|
||||
|
@ -684,6 +685,7 @@ public class MapWithAIProvidersPanel extends JPanel {
|
|||
activate = new ImageProvider("svpDown").setMaxSize(ImageProvider.ImageSizes.MENU).getResource();
|
||||
activate.attachImageIcon(this, true);
|
||||
deactivate = new ImageProvider("svpUp").setMaxSize(ImageProvider.ImageSizes.MENU).getResource();
|
||||
MapWithAILayerInfo.getInstance().addListener(this);
|
||||
}
|
||||
|
||||
protected void updateEnabledState() {
|
||||
|
@ -744,6 +746,11 @@ public class MapWithAIProvidersPanel extends JPanel {
|
|||
MapWithAILayerInfo.getInstance().save();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void changeEvent(MapWithAIInfo modified) {
|
||||
updateEnabledState();
|
||||
}
|
||||
}
|
||||
|
||||
private class ReloadAction extends AbstractAction {
|
||||
|
|
Ładowanie…
Reference in New Issue