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; 012import java.util.stream.Collectors; 013 014import javax.swing.AbstractAction; 015import javax.swing.Action; 016import javax.swing.BorderFactory; 017import javax.swing.JMenuItem; 018import javax.swing.JOptionPane; 019import javax.swing.JScrollPane; 020 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 return layers.stream().allMatch(l -> l instanceof GpxLayer); 064 } 065 066 @Override 067 public Component createMenuComponent() { 068 return new JMenuItem(this); 069 } 070 071 @Override 072 public Action getMultiLayerAction(List<Layer> layers) { 073 return new CustomizeDrawingAction(layers); 074 } 075 076 @Override 077 public void actionPerformed(ActionEvent e) { 078 GPXSettingsPanel panel = new GPXSettingsPanel( 079 layers.stream().filter(l -> l instanceof GpxLayer).map(l -> (GpxLayer) l).collect(Collectors.toList())); 080 JScrollPane scrollpane = GuiHelper.embedInVerticalScrollPane(panel); 081 scrollpane.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0)); 082 int screenHeight = GuiHelper.getScreenSize().height; 083 if (screenHeight < 700) { 084 // to fit on screen 800x600 085 scrollpane.setPreferredSize(new Dimension(panel.getPreferredSize().width, Math.min(panel.getPreferredSize().height, 450))); 086 } 087 int answer = JOptionPane.showConfirmDialog(MainApplication.getMainFrame(), scrollpane, tr("Customize track drawing"), 088 JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE); 089 if (answer == JOptionPane.CANCEL_OPTION || answer == JOptionPane.CLOSED_OPTION) { 090 return; 091 } 092 panel.savePreferences(); 093 MainApplication.getMainPanel().repaint(); 094 layers.stream().forEach(Layer::invalidate); 095 } 096 097}