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