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

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

remove deprecated stuff, code cleanup, javadoc, unit tests

  • Property svn:eol-style set to native
File size: 4.6 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 /**
70 * Constructs a new {@code LayerListPopup}.
71 * @param selectedLayers list of selected layers
72 */
73 public LayerListPopup(List<Layer> selectedLayers) {
74
75 List<Action> actions;
76 if (selectedLayers.size() == 1) {
77 actions = Arrays.asList(selectedLayers.get(0).getMenuEntries());
78 } else {
79 // Very simple algorithm - first selected layer has actions order as in getMenuEntries, actions from other layers go to the end
80 actions = new ArrayList<>();
81 boolean separatorAdded = true;
82 for (Action a: selectedLayers.get(0).getMenuEntries()) {
83 if (!separatorAdded && a instanceof SeparatorLayerAction) {
84 separatorAdded = true;
85 actions.add(a);
86 } else if (a instanceof LayerAction && ((LayerAction) a).supportLayers(selectedLayers)) {
87 separatorAdded = false;
88 if (a instanceof MultiLayerAction)
89 a = ((MultiLayerAction) a).getMultiLayerAction(selectedLayers);
90 actions.add(a);
91 }
92 }
93 // This will usually add no action, because if some action support all selected layers then it was probably used also in first layer
94 for (int i = 1; i < selectedLayers.size(); i++) {
95 separatorAdded = false;
96 for (Action a: selectedLayers.get(i).getMenuEntries()) {
97 if (a instanceof LayerAction && !(a instanceof MultiLayerAction)
98 && ((LayerAction) a).supportLayers(selectedLayers) && !actions.contains(a)) {
99 if (!separatorAdded) {
100 separatorAdded = true;
101 actions.add(SeparatorLayerAction.INSTANCE);
102 }
103 actions.add(a);
104 }
105 }
106 }
107 }
108 if (!actions.isEmpty() && actions.get(actions.size() - 1) instanceof SeparatorLayerAction) {
109 actions.remove(actions.size() - 1);
110 }
111 for (Action a : actions) {
112 if (a instanceof LayerAction) {
113 add(((LayerAction) a).createMenuComponent());
114 } else {
115 add(new JMenuItem(a));
116 }
117 }
118 }
119}
Note: See TracBrowser for help on using the repository browser.