source: josm/trunk/src/org/openstreetmap/josm/gui/dialogs/LayerListPopup.java@ 13691

Last change on this file since 13691 was 13130, checked in by Don-vip, 6 years ago

fix #15572 - use ImageProvider attach API for all JOSM actions to ensure proper icon size everywhere

  • Property svn:eol-style set to native
File size: 4.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.dialogs;
3
4import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
5import static org.openstreetmap.josm.tools.I18n.tr;
6
7import java.awt.Component;
8import java.awt.Dimension;
9import java.awt.event.ActionEvent;
10import java.util.ArrayList;
11import java.util.Arrays;
12import java.util.Collections;
13import java.util.List;
14
15import javax.swing.AbstractAction;
16import javax.swing.Action;
17import javax.swing.JMenuItem;
18import javax.swing.JOptionPane;
19import javax.swing.JPopupMenu;
20
21import org.openstreetmap.josm.Main;
22import org.openstreetmap.josm.gui.ExtendedDialog;
23import org.openstreetmap.josm.gui.layer.Layer;
24import org.openstreetmap.josm.gui.layer.Layer.LayerAction;
25import org.openstreetmap.josm.gui.layer.Layer.MultiLayerAction;
26import org.openstreetmap.josm.gui.layer.Layer.SeparatorLayerAction;
27import org.openstreetmap.josm.tools.ImageProvider;
28
29/**
30 * Popup menu handler for the layer list.
31 */
32public 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}
Note: See TracBrowser for help on using the repository browser.