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

Last change on this file since 8855 was 8855, checked in by Don-vip, 9 years ago

sonar - Unused private method should be removed
sonar - Unused protected methods should be removed
sonar - Sections of code should not be "commented out"
sonar - Empty statements should be removed
sonar - squid:S1172 - Unused method parameters should be removed
sonar - squid:S1481 - Unused local variables should be removed

  • Property svn:eol-style set to native
File size: 4.5 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.List;
13
14import javax.swing.AbstractAction;
15import javax.swing.Action;
16import javax.swing.JMenuItem;
17import javax.swing.JOptionPane;
18import javax.swing.JPopupMenu;
19
20import org.openstreetmap.josm.Main;
21import org.openstreetmap.josm.gui.ExtendedDialog;
22import org.openstreetmap.josm.gui.layer.Layer;
23import org.openstreetmap.josm.gui.layer.Layer.LayerAction;
24import org.openstreetmap.josm.gui.layer.Layer.MultiLayerAction;
25import org.openstreetmap.josm.gui.layer.Layer.SeparatorLayerAction;
26import org.openstreetmap.josm.tools.ImageProvider;
27
28/**
29 * Popup menu handler for the layer list.
30 */
31public class LayerListPopup extends JPopupMenu {
32
33 public static final class InfoAction extends AbstractAction {
34 private final transient 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) {
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<>();
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}
Note: See TracBrowser for help on using the repository browser.