001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.layer.gpx;
003
004import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
005import static org.openstreetmap.josm.tools.I18n.tr;
006
007import java.awt.Component;
008import java.awt.Dimension;
009import java.awt.event.ActionEvent;
010import java.util.LinkedList;
011import java.util.List;
012
013import javax.swing.AbstractAction;
014import javax.swing.Action;
015import javax.swing.BorderFactory;
016import javax.swing.JMenuItem;
017import javax.swing.JOptionPane;
018import javax.swing.JScrollPane;
019
020import org.openstreetmap.josm.Main;
021import org.openstreetmap.josm.gui.MainApplication;
022import org.openstreetmap.josm.gui.layer.GpxLayer;
023import org.openstreetmap.josm.gui.layer.Layer;
024import org.openstreetmap.josm.gui.layer.Layer.LayerAction;
025import org.openstreetmap.josm.gui.layer.Layer.MultiLayerAction;
026import org.openstreetmap.josm.gui.preferences.display.GPXSettingsPanel;
027import org.openstreetmap.josm.gui.util.GuiHelper;
028import org.openstreetmap.josm.tools.ImageProvider;
029
030/**
031 * An action that is displayed in the popup menu for the layer to change the drawing of the GPX layer
032 */
033public class CustomizeDrawingAction extends AbstractAction implements LayerAction, MultiLayerAction {
034    private transient List<Layer> layers;
035
036    /**
037     * Create a new {@link CustomizeDrawingAction}
038     * @param l The layers that should be customized
039     */
040    public CustomizeDrawingAction(List<Layer> l) {
041        this();
042        layers = l;
043    }
044
045    /**
046     * Create a new {@link CustomizeDrawingAction}
047     * @param l The layer that should be customized
048     */
049    public CustomizeDrawingAction(Layer l) {
050        this();
051        layers = new LinkedList<>();
052        layers.add(l);
053    }
054
055    private CustomizeDrawingAction() {
056        super(tr("Customize track drawing"));
057        new ImageProvider("mapmode/addsegment").getResource().attachImageIcon(this, true);
058        putValue("help", ht("/Action/GPXLayerCustomizeLineDrawing"));
059    }
060
061    @Override
062    public boolean supportLayers(List<Layer> layers) {
063        for (Layer layer : layers) {
064            if (!(layer instanceof GpxLayer)) {
065                return false;
066            }
067        }
068        return true;
069    }
070
071    @Override
072    public Component createMenuComponent() {
073        return new JMenuItem(this);
074    }
075
076    @Override
077    public Action getMultiLayerAction(List<Layer> layers) {
078        return new CustomizeDrawingAction(layers);
079    }
080
081    @Override
082    public void actionPerformed(ActionEvent e) {
083        boolean hasLocal = false;
084        boolean hasNonlocal = false;
085        for (Layer layer : layers) {
086            if (layer instanceof GpxLayer) {
087                if (((GpxLayer) layer).isLocalFile()) {
088                    hasLocal = true;
089                } else {
090                    hasNonlocal = true;
091                }
092            }
093        }
094        GPXSettingsPanel panel = new GPXSettingsPanel(layers.get(0).getName(), hasLocal, hasNonlocal);
095        JScrollPane scrollpane = GuiHelper.embedInVerticalScrollPane(panel);
096        scrollpane.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
097        int screenHeight = GuiHelper.getScreenSize().height;
098        if (screenHeight < 700) {
099            // to fit on screen 800x600
100            scrollpane.setPreferredSize(new Dimension(panel.getPreferredSize().width, Math.min(panel.getPreferredSize().height, 450)));
101        }
102        int answer = JOptionPane.showConfirmDialog(Main.parent, scrollpane, tr("Customize track drawing"),
103                JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
104        if (answer == JOptionPane.CANCEL_OPTION || answer == JOptionPane.CLOSED_OPTION) {
105            return;
106        }
107        for (Layer layer : layers) {
108            // save preferences for all layers
109            boolean f = false;
110            if (layer instanceof GpxLayer) {
111                f = ((GpxLayer) layer).isLocalFile();
112            }
113            panel.savePreferences(layer.getName(), f);
114        }
115        MainApplication.getMap().repaint();
116    }
117
118}