1 | // License: GPL. For details, see LICENSE file.
|
---|
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.Component;
|
---|
8 | import java.awt.Dimension;
|
---|
9 | import java.awt.event.ActionEvent;
|
---|
10 | import java.util.ArrayList;
|
---|
11 | import java.util.Arrays;
|
---|
12 | import java.util.List;
|
---|
13 |
|
---|
14 | import javax.swing.AbstractAction;
|
---|
15 | import javax.swing.Action;
|
---|
16 | import javax.swing.JMenuItem;
|
---|
17 | import javax.swing.JOptionPane;
|
---|
18 | import javax.swing.JPopupMenu;
|
---|
19 |
|
---|
20 | import org.openstreetmap.josm.Main;
|
---|
21 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
22 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
23 | import org.openstreetmap.josm.gui.layer.Layer.LayerAction;
|
---|
24 | import org.openstreetmap.josm.gui.layer.Layer.MultiLayerAction;
|
---|
25 | import org.openstreetmap.josm.gui.layer.Layer.SeparatorLayerAction;
|
---|
26 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Popup menu handler for the layer list.
|
---|
30 | */
|
---|
31 | public class LayerListPopup extends JPopupMenu {
|
---|
32 |
|
---|
33 | public static final class InfoAction extends AbstractAction {
|
---|
34 | private final Layer layer;
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * Constructs a new {@code InfoAction} for the given layer.
|
---|
38 | * @param layer The layer
|
---|
39 | */
|
---|
40 | public InfoAction(Layer layer) {
|
---|
41 | super(tr("Info"), ImageProvider.get("info"));
|
---|
42 | putValue("help", ht("/Action/LayerInfo"));
|
---|
43 | this.layer = layer;
|
---|
44 | }
|
---|
45 |
|
---|
46 | @Override
|
---|
47 | public void actionPerformed(ActionEvent e) {
|
---|
48 | Object object = layer.getInfoComponent();
|
---|
49 | if (object instanceof Component) {
|
---|
50 | ExtendedDialog ed = new ExtendedDialog(
|
---|
51 | Main.parent, tr("Information about layer"),
|
---|
52 | new String[] {tr("OK")});
|
---|
53 | ed.setButtonIcons(new String[] {"ok"});
|
---|
54 | ed.setIcon(JOptionPane.INFORMATION_MESSAGE);
|
---|
55 | ed.setContent((Component) object);
|
---|
56 | ed.setResizable(layer.isInfoResizable());
|
---|
57 | ed.setMinimumSize(new Dimension(270, 170));
|
---|
58 | ed.showDialog();
|
---|
59 | } else {
|
---|
60 | JOptionPane.showMessageDialog(
|
---|
61 | Main.parent, object,
|
---|
62 | tr("Information about layer"),
|
---|
63 | JOptionPane.INFORMATION_MESSAGE
|
---|
64 | );
|
---|
65 | }
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | public LayerListPopup(List<Layer> selectedLayers, final Layer layer) {
|
---|
70 |
|
---|
71 | List<Action> actions;
|
---|
72 | if (selectedLayers.size() == 1) {
|
---|
73 | actions = Arrays.asList(selectedLayers.get(0).getMenuEntries());
|
---|
74 | } else {
|
---|
75 | // Very simple algorithm - first selected layer has actions order as in getMenuEntries, actions from other layers go to the end
|
---|
76 | actions = new ArrayList<Action>();
|
---|
77 | boolean separatorAdded = true;
|
---|
78 | for (Action a: selectedLayers.get(0).getMenuEntries()) {
|
---|
79 | if (!separatorAdded && a instanceof SeparatorLayerAction) {
|
---|
80 | separatorAdded = true;
|
---|
81 | actions.add(a);
|
---|
82 | } else if (a instanceof LayerAction && ((LayerAction)a).supportLayers(selectedLayers)) {
|
---|
83 | separatorAdded = false;
|
---|
84 | if(a instanceof MultiLayerAction)
|
---|
85 | a = ((MultiLayerAction)a).getMultiLayerAction(selectedLayers);
|
---|
86 | actions.add(a);
|
---|
87 | }
|
---|
88 | }
|
---|
89 | // This will usually add no action, because if some action support all selected layers then it was probably used also in first layer
|
---|
90 | for (int i=1; i<selectedLayers.size(); i++) {
|
---|
91 | separatorAdded = false;
|
---|
92 | for (Action a: selectedLayers.get(i).getMenuEntries()) {
|
---|
93 | if (a instanceof LayerAction && !(a instanceof MultiLayerAction)
|
---|
94 | && ((LayerAction)a).supportLayers(selectedLayers) && !actions.contains(a)) {
|
---|
95 | if (!separatorAdded) {
|
---|
96 | separatorAdded = true;
|
---|
97 | actions.add(SeparatorLayerAction.INSTANCE);
|
---|
98 | }
|
---|
99 | actions.add(a);
|
---|
100 | }
|
---|
101 | }
|
---|
102 | }
|
---|
103 | }
|
---|
104 | if (!actions.isEmpty() && actions.get(actions.size() - 1) instanceof SeparatorLayerAction) {
|
---|
105 | actions.remove(actions.size() - 1);
|
---|
106 | }
|
---|
107 | for (Action a : actions) {
|
---|
108 | if (a instanceof LayerAction) {
|
---|
109 | add (((LayerAction) a).createMenuComponent());
|
---|
110 | } else {
|
---|
111 | add(new JMenuItem(a));
|
---|
112 | }
|
---|
113 | }
|
---|
114 | }
|
---|
115 | }
|
---|