| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm.gui.layer.gpx;
|
|---|
| 3 |
|
|---|
| 4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
|---|
| 5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
|---|
| 6 |
|
|---|
| 7 | import java.awt.Component;
|
|---|
| 8 | import java.awt.Dimension;
|
|---|
| 9 | import java.awt.event.ActionEvent;
|
|---|
| 10 | import java.util.LinkedList;
|
|---|
| 11 | import java.util.List;
|
|---|
| 12 | import java.util.stream.Collectors;
|
|---|
| 13 |
|
|---|
| 14 | import javax.swing.AbstractAction;
|
|---|
| 15 | import javax.swing.Action;
|
|---|
| 16 | import javax.swing.BorderFactory;
|
|---|
| 17 | import javax.swing.JMenuItem;
|
|---|
| 18 | import javax.swing.JOptionPane;
|
|---|
| 19 | import javax.swing.JScrollPane;
|
|---|
| 20 |
|
|---|
| 21 | import org.openstreetmap.josm.gui.MainApplication;
|
|---|
| 22 | import org.openstreetmap.josm.gui.layer.GpxLayer;
|
|---|
| 23 | import org.openstreetmap.josm.gui.layer.Layer;
|
|---|
| 24 | import org.openstreetmap.josm.gui.layer.Layer.LayerAction;
|
|---|
| 25 | import org.openstreetmap.josm.gui.layer.Layer.MultiLayerAction;
|
|---|
| 26 | import org.openstreetmap.josm.gui.preferences.display.GPXSettingsPanel;
|
|---|
| 27 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
|---|
| 28 | import org.openstreetmap.josm.tools.ImageProvider;
|
|---|
| 29 |
|
|---|
| 30 | /**
|
|---|
| 31 | * An action that is displayed in the popup menu for the layer to change the drawing of the GPX layer
|
|---|
| 32 | */
|
|---|
| 33 | public class CustomizeDrawingAction extends AbstractAction implements LayerAction, MultiLayerAction {
|
|---|
| 34 | private transient List<Layer> layers;
|
|---|
| 35 |
|
|---|
| 36 | /**
|
|---|
| 37 | * Create a new {@link CustomizeDrawingAction}
|
|---|
| 38 | * @param l The layers that should be customized
|
|---|
| 39 | */
|
|---|
| 40 | public CustomizeDrawingAction(List<Layer> l) {
|
|---|
| 41 | this();
|
|---|
| 42 | layers = l;
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
| 45 | /**
|
|---|
| 46 | * Create a new {@link CustomizeDrawingAction}
|
|---|
| 47 | * @param l The layer that should be customized
|
|---|
| 48 | */
|
|---|
| 49 | public CustomizeDrawingAction(Layer l) {
|
|---|
| 50 | this();
|
|---|
| 51 | layers = new LinkedList<>();
|
|---|
| 52 | layers.add(l);
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | private CustomizeDrawingAction() {
|
|---|
| 56 | super(tr("Customize track drawing"));
|
|---|
| 57 | new ImageProvider("preference").getResource().attachImageIcon(this, true);
|
|---|
| 58 | putValue("help", ht("/Action/GPXLayerCustomizeLineDrawing"));
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | @Override
|
|---|
| 62 | public boolean supportLayers(List<Layer> layers) {
|
|---|
| 63 | return layers.stream().allMatch(l -> l instanceof GpxLayer);
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | @Override
|
|---|
| 67 | public Component createMenuComponent() {
|
|---|
| 68 | return new JMenuItem(this);
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | @Override
|
|---|
| 72 | public Action getMultiLayerAction(List<Layer> layers) {
|
|---|
| 73 | return new CustomizeDrawingAction(layers);
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | @Override
|
|---|
| 77 | public void actionPerformed(ActionEvent e) {
|
|---|
| 78 | GPXSettingsPanel panel = new GPXSettingsPanel(
|
|---|
| 79 | layers.stream().filter(l -> l instanceof GpxLayer).map(l -> (GpxLayer) l).collect(Collectors.toList()));
|
|---|
| 80 | JScrollPane scrollpane = GuiHelper.embedInVerticalScrollPane(panel);
|
|---|
| 81 | scrollpane.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
|
|---|
| 82 | int screenHeight = GuiHelper.getScreenSize().height;
|
|---|
| 83 | if (screenHeight < 700) {
|
|---|
| 84 | // to fit on screen 800x600
|
|---|
| 85 | scrollpane.setPreferredSize(new Dimension(panel.getPreferredSize().width, Math.min(panel.getPreferredSize().height, 450)));
|
|---|
| 86 | }
|
|---|
| 87 | int answer = JOptionPane.showConfirmDialog(MainApplication.getMainFrame(), scrollpane, tr("Customize track drawing"),
|
|---|
| 88 | JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
|
|---|
| 89 | if (answer == JOptionPane.CANCEL_OPTION || answer == JOptionPane.CLOSED_OPTION) {
|
|---|
| 90 | return;
|
|---|
| 91 | }
|
|---|
| 92 | panel.savePreferences();
|
|---|
| 93 | MainApplication.getMainPanel().repaint();
|
|---|
| 94 | layers.forEach(Layer::invalidate);
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | }
|
|---|