1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.gui.dialogs;
|
---|
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.event.ActionEvent;
|
---|
8 | import java.util.ArrayList;
|
---|
9 | import java.util.Arrays;
|
---|
10 | import java.util.List;
|
---|
11 |
|
---|
12 | import javax.swing.AbstractAction;
|
---|
13 | import javax.swing.Action;
|
---|
14 | import javax.swing.JMenuItem;
|
---|
15 | import javax.swing.JOptionPane;
|
---|
16 | import javax.swing.JPopupMenu;
|
---|
17 |
|
---|
18 | import org.openstreetmap.josm.Main;
|
---|
19 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
20 | import org.openstreetmap.josm.gui.layer.Layer.LayerAction;
|
---|
21 | import org.openstreetmap.josm.gui.layer.Layer.MultiLayerAction;
|
---|
22 | import org.openstreetmap.josm.gui.layer.Layer.SeparatorLayerAction;
|
---|
23 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * Popup menu handler for the layer list.
|
---|
27 | */
|
---|
28 | public class LayerListPopup extends JPopupMenu {
|
---|
29 |
|
---|
30 | public final static class InfoAction extends AbstractAction {
|
---|
31 | private final Layer layer;
|
---|
32 | public InfoAction(Layer layer) {
|
---|
33 | super(tr("Info"), ImageProvider.get("info"));
|
---|
34 | putValue("help", ht("/Action/LayerInfo"));
|
---|
35 | this.layer = layer;
|
---|
36 | }
|
---|
37 | public void actionPerformed(ActionEvent e) {
|
---|
38 | JOptionPane.showMessageDialog(
|
---|
39 | Main.parent,
|
---|
40 | layer.getInfoComponent(),
|
---|
41 | tr("Information about layer"),
|
---|
42 | JOptionPane.INFORMATION_MESSAGE
|
---|
43 | );
|
---|
44 | }
|
---|
45 | }
|
---|
46 |
|
---|
47 | public LayerListPopup(List<Layer> selectedLayers, final Layer layer) {
|
---|
48 |
|
---|
49 | List<Action> actions;
|
---|
50 | if (selectedLayers.size() == 1) {
|
---|
51 | actions = Arrays.asList(selectedLayers.get(0).getMenuEntries());
|
---|
52 | } else {
|
---|
53 | // Very simple algorithm - first selected layer has actions order as in getMenuEntries, actions from other layers go to the end
|
---|
54 | actions = new ArrayList<Action>();
|
---|
55 | boolean separatorAdded = true;
|
---|
56 | for (Action a: selectedLayers.get(0).getMenuEntries()) {
|
---|
57 | if (!separatorAdded && a instanceof SeparatorLayerAction) {
|
---|
58 | separatorAdded = true;
|
---|
59 | actions.add(a);
|
---|
60 | } else if (a instanceof LayerAction && ((LayerAction)a).supportLayers(selectedLayers)) {
|
---|
61 | separatorAdded = false;
|
---|
62 | if(a instanceof MultiLayerAction)
|
---|
63 | a = ((MultiLayerAction)a).getMultiLayerAction(selectedLayers);
|
---|
64 | actions.add(a);
|
---|
65 | }
|
---|
66 | }
|
---|
67 | // This will usually add no action, because if some action support all selected layers then it was probably used also in first layer
|
---|
68 | for (int i=1; i<selectedLayers.size(); i++) {
|
---|
69 | separatorAdded = false;
|
---|
70 | for (Action a: selectedLayers.get(i).getMenuEntries()) {
|
---|
71 | if (a instanceof LayerAction && !(a instanceof MultiLayerAction)
|
---|
72 | && ((LayerAction)a).supportLayers(selectedLayers) && !actions.contains(a)) {
|
---|
73 | if (!separatorAdded) {
|
---|
74 | separatorAdded = true;
|
---|
75 | actions.add(SeparatorLayerAction.INSTANCE);
|
---|
76 | }
|
---|
77 | actions.add(a);
|
---|
78 | }
|
---|
79 | }
|
---|
80 | }
|
---|
81 | }
|
---|
82 | if (!actions.isEmpty() && actions.get(actions.size() - 1) instanceof SeparatorLayerAction) {
|
---|
83 | actions.remove(actions.size() - 1);
|
---|
84 | }
|
---|
85 | for (Action a : actions) {
|
---|
86 | if (a instanceof LayerAction) {
|
---|
87 | add (((LayerAction) a).createMenuComponent());
|
---|
88 | } else {
|
---|
89 | add(new JMenuItem(a));
|
---|
90 | }
|
---|
91 | }
|
---|
92 | }
|
---|
93 | }
|
---|