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