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

Last change on this file since 12301 was 12301, checked in by michael2402, 7 years ago

Add missing javadoc in the org.openstreetmap.josm.gui.dialogs package.

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