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.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 029public class CustomizeDrawingAction extends AbstractAction implements LayerAction, MultiLayerAction { 030 private transient List<Layer> layers; 031 032 public CustomizeDrawingAction(List<Layer> l) { 033 this(); 034 layers = l; 035 } 036 037 public CustomizeDrawingAction(Layer l) { 038 this(); 039 layers = new LinkedList<>(); 040 layers.add(l); 041 } 042 043 private CustomizeDrawingAction() { 044 super(tr("Customize track drawing"), ImageProvider.get("mapmode/addsegment")); 045 putValue("help", ht("/Action/GPXLayerCustomizeLineDrawing")); 046 } 047 048 @Override 049 public boolean supportLayers(List<Layer> layers) { 050 for (Layer layer : layers) { 051 if (!(layer instanceof GpxLayer)) { 052 return false; 053 } 054 } 055 return true; 056 } 057 058 @Override 059 public Component createMenuComponent() { 060 return new JMenuItem(this); 061 } 062 063 @Override 064 public Action getMultiLayerAction(List<Layer> layers) { 065 return new CustomizeDrawingAction(layers); 066 } 067 068 @Override 069 public void actionPerformed(ActionEvent e) { 070 boolean hasLocal = false; 071 boolean hasNonlocal = false; 072 for (Layer layer : layers) { 073 if (layer instanceof GpxLayer) { 074 if (((GpxLayer) layer).isLocalFile()) { 075 hasLocal = true; 076 } else { 077 hasNonlocal = true; 078 } 079 } 080 } 081 GPXSettingsPanel panel = new GPXSettingsPanel(layers.get(0).getName(), hasLocal, hasNonlocal); 082 JScrollPane scrollpane = GuiHelper.embedInVerticalScrollPane(panel); 083 scrollpane.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0)); 084 int screenHeight = GuiHelper.getScreenSize().height; 085 if (screenHeight < 700) { 086 // to fit on screen 800x600 087 scrollpane.setPreferredSize(new Dimension(panel.getPreferredSize().width, Math.min(panel.getPreferredSize().height, 450))); 088 } 089 int answer = JOptionPane.showConfirmDialog(Main.parent, scrollpane, tr("Customize track drawing"), 090 JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE); 091 if (answer == JOptionPane.CANCEL_OPTION || answer == JOptionPane.CLOSED_OPTION) { 092 return; 093 } 094 for (Layer layer : layers) { 095 // save preferences for all layers 096 boolean f = false; 097 if (layer instanceof GpxLayer) { 098 f = ((GpxLayer) layer).isLocalFile(); 099 } 100 panel.savePreferences(layer.getName(), f); 101 } 102 Main.map.repaint(); 103 } 104 105}