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

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

fix #13337 - NPE

  • Property svn:eol-style set to native
File size: 4.7 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 public static final class InfoAction extends AbstractAction {
35 private final transient Layer layer;
36
37 /**
38 * Constructs a new {@code InfoAction} for the given layer.
39 * @param layer The layer
40 */
41 public InfoAction(Layer layer) {
42 super(tr("Info"), ImageProvider.get("info"));
43 putValue("help", ht("/Action/LayerInfo"));
44 this.layer = layer;
45 }
46
47 @Override
48 public void actionPerformed(ActionEvent e) {
49 Object object = layer.getInfoComponent();
50 if (object instanceof Component) {
51 ExtendedDialog ed = new ExtendedDialog(
52 Main.parent, tr("Information about layer"),
53 new String[] {tr("OK")});
54 ed.setButtonIcons(new String[] {"ok"});
55 ed.setIcon(JOptionPane.INFORMATION_MESSAGE);
56 ed.setContent((Component) object);
57 ed.setResizable(layer.isInfoResizable());
58 ed.setMinimumSize(new Dimension(270, 170));
59 ed.showDialog();
60 } else {
61 JOptionPane.showMessageDialog(
62 Main.parent, object,
63 tr("Information about layer"),
64 JOptionPane.INFORMATION_MESSAGE
65 );
66 }
67 }
68 }
69
70 /**
71 * Constructs a new {@code LayerListPopup}.
72 * @param selectedLayers list of selected layers
73 */
74 public LayerListPopup(List<Layer> selectedLayers) {
75
76 List<Action> actions;
77 if (selectedLayers.size() == 1) {
78 Action[] entries = selectedLayers.get(0).getMenuEntries();
79 actions = entries != null ? Arrays.asList(entries) : Collections.emptyList();
80 } else {
81 // Very simple algorithm - first selected layer has actions order as in getMenuEntries, actions from other layers go to the end
82 actions = new ArrayList<>();
83 boolean separatorAdded = true;
84 for (Action a: selectedLayers.get(0).getMenuEntries()) {
85 if (!separatorAdded && a instanceof SeparatorLayerAction) {
86 separatorAdded = true;
87 actions.add(a);
88 } else if (a instanceof LayerAction && ((LayerAction) a).supportLayers(selectedLayers)) {
89 separatorAdded = false;
90 if (a instanceof MultiLayerAction)
91 a = ((MultiLayerAction) a).getMultiLayerAction(selectedLayers);
92 actions.add(a);
93 }
94 }
95 // This will usually add no action, because if some action support all selected layers then it was probably used also in first layer
96 for (int i = 1; i < selectedLayers.size(); i++) {
97 separatorAdded = false;
98 for (Action a: selectedLayers.get(i).getMenuEntries()) {
99 if (a instanceof LayerAction && !(a instanceof MultiLayerAction)
100 && ((LayerAction) a).supportLayers(selectedLayers) && !actions.contains(a)) {
101 if (!separatorAdded) {
102 separatorAdded = true;
103 actions.add(SeparatorLayerAction.INSTANCE);
104 }
105 actions.add(a);
106 }
107 }
108 }
109 }
110 if (!actions.isEmpty() && actions.get(actions.size() - 1) instanceof SeparatorLayerAction) {
111 actions.remove(actions.size() - 1);
112 }
113 for (Action a : actions) {
114 if (a instanceof LayerAction) {
115 add(((LayerAction) a).createMenuComponent());
116 } else {
117 add(new JMenuItem(a));
118 }
119 }
120 }
121}
Note: See TracBrowser for help on using the repository browser.