001 // License: GPL. Copyright 2007 by Immanuel Scholz and others
002 package org.openstreetmap.josm.gui.dialogs;
003
004 import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
005 import static org.openstreetmap.josm.tools.I18n.tr;
006
007 import java.awt.event.ActionEvent;
008 import java.util.ArrayList;
009 import java.util.Arrays;
010 import java.util.List;
011
012 import javax.swing.AbstractAction;
013 import javax.swing.Action;
014 import javax.swing.JMenuItem;
015 import javax.swing.JOptionPane;
016 import javax.swing.JPopupMenu;
017
018 import org.openstreetmap.josm.Main;
019 import org.openstreetmap.josm.gui.layer.Layer;
020 import org.openstreetmap.josm.gui.layer.Layer.LayerAction;
021 import org.openstreetmap.josm.gui.layer.Layer.MultiLayerAction;
022 import org.openstreetmap.josm.gui.layer.Layer.SeparatorLayerAction;
023 import org.openstreetmap.josm.tools.ImageProvider;
024
025 /**
026 * Popup menu handler for the layer list.
027 */
028 public class LayerListPopup extends JPopupMenu {
029
030 public final static class InfoAction extends AbstractAction {
031 private final Layer layer;
032 public InfoAction(Layer layer) {
033 super(tr("Info"), ImageProvider.get("info"));
034 putValue("help", ht("/Action/LayerInfo"));
035 this.layer = layer;
036 }
037 public void actionPerformed(ActionEvent e) {
038 JOptionPane.showMessageDialog(
039 Main.parent,
040 layer.getInfoComponent(),
041 tr("Information about layer"),
042 JOptionPane.INFORMATION_MESSAGE
043 );
044 }
045 }
046
047 public LayerListPopup(List<Layer> selectedLayers, final Layer layer) {
048
049 List<Action> actions;
050 if (selectedLayers.size() == 1) {
051 actions = Arrays.asList(selectedLayers.get(0).getMenuEntries());
052 } else {
053 // Very simple algorithm - first selected layer has actions order as in getMenuEntries, actions from other layers go to the end
054 actions = new ArrayList<Action>();
055 boolean separatorAdded = true;
056 for (Action a: selectedLayers.get(0).getMenuEntries()) {
057 if (!separatorAdded && a instanceof SeparatorLayerAction) {
058 separatorAdded = true;
059 actions.add(a);
060 } else if (a instanceof LayerAction && ((LayerAction)a).supportLayers(selectedLayers)) {
061 separatorAdded = false;
062 if(a instanceof MultiLayerAction)
063 a = ((MultiLayerAction)a).getMultiLayerAction(selectedLayers);
064 actions.add(a);
065 }
066 }
067 // This will usually add no action, because if some action support all selected layers then it was probably used also in first layer
068 for (int i=1; i<selectedLayers.size(); i++) {
069 separatorAdded = false;
070 for (Action a: selectedLayers.get(i).getMenuEntries()) {
071 if (a instanceof LayerAction && !(a instanceof MultiLayerAction)
072 && ((LayerAction)a).supportLayers(selectedLayers) && !actions.contains(a)) {
073 if (!separatorAdded) {
074 separatorAdded = true;
075 actions.add(SeparatorLayerAction.INSTANCE);
076 }
077 actions.add(a);
078 }
079 }
080 }
081 }
082 if (!actions.isEmpty() && actions.get(actions.size() - 1) instanceof SeparatorLayerAction) {
083 actions.remove(actions.size() - 1);
084 }
085 for (Action a : actions) {
086 if (a instanceof LayerAction) {
087 add (((LayerAction) a).createMenuComponent());
088 } else {
089 add(new JMenuItem(a));
090 }
091 }
092 }
093 }