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

Last change on this file since 4230 was 4230, checked in by stoecker, 13 years ago

allow to color the entries in layer list dialog according to assigned layer drawing color

  • Property svn:eol-style set to native
File size: 3.7 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
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.event.ActionEvent;
8import java.util.ArrayList;
9import java.util.Arrays;
10import java.util.List;
11
12import javax.swing.AbstractAction;
13import javax.swing.Action;
14import javax.swing.JMenuItem;
15import javax.swing.JOptionPane;
16import javax.swing.JPopupMenu;
17
18import org.openstreetmap.josm.Main;
19import org.openstreetmap.josm.gui.layer.Layer;
20import org.openstreetmap.josm.gui.layer.Layer.LayerAction;
21import org.openstreetmap.josm.gui.layer.Layer.MultiLayerAction;
22import org.openstreetmap.josm.gui.layer.Layer.SeparatorLayerAction;
23import org.openstreetmap.josm.tools.ImageProvider;
24
25/**
26 * Popup menu handler for the layer list.
27 */
28public class LayerListPopup extends JPopupMenu {
29
30 public final static class InfoAction extends AbstractAction {
31 private final Layer layer;
32 public InfoAction(Layer layer) {
33 super(tr("Info"), ImageProvider.get("info"));
34 putValue("help", ht("/Action/LayerInfo"));
35 this.layer = layer;
36 }
37 public void actionPerformed(ActionEvent e) {
38 JOptionPane.showMessageDialog(
39 Main.parent,
40 layer.getInfoComponent(),
41 tr("Information about layer"),
42 JOptionPane.INFORMATION_MESSAGE
43 );
44 }
45 }
46
47 public LayerListPopup(List<Layer> selectedLayers, final Layer layer) {
48
49 List<Action> actions;
50 if (selectedLayers.size() == 1) {
51 actions = Arrays.asList(selectedLayers.get(0).getMenuEntries());
52 } else {
53 // Very simple algorithm - first selected layer has actions order as in getMenuEntries, actions from other layers go to the end
54 actions = new ArrayList<Action>();
55 boolean separatorAdded = true;
56 for (Action a: selectedLayers.get(0).getMenuEntries()) {
57 if (!separatorAdded && a instanceof SeparatorLayerAction) {
58 separatorAdded = true;
59 actions.add(a);
60 } else if (a instanceof LayerAction && ((LayerAction)a).supportLayers(selectedLayers)) {
61 separatorAdded = false;
62 if(a instanceof MultiLayerAction)
63 a = ((MultiLayerAction)a).getMultiLayerAction(selectedLayers);
64 actions.add(a);
65 }
66 }
67 // This will usually add no action, because if some action support all selected layers then it was probably used also in first layer
68 for (int i=1; i<selectedLayers.size(); i++) {
69 separatorAdded = false;
70 for (Action a: selectedLayers.get(i).getMenuEntries()) {
71 if (a instanceof LayerAction && !(a instanceof MultiLayerAction)
72 && ((LayerAction)a).supportLayers(selectedLayers) && !actions.contains(a)) {
73 if (!separatorAdded) {
74 separatorAdded = true;
75 actions.add(SeparatorLayerAction.INSTANCE);
76 }
77 actions.add(a);
78 }
79 }
80 }
81 }
82 if (!actions.isEmpty() && actions.get(actions.size() - 1) instanceof SeparatorLayerAction) {
83 actions.remove(actions.size() - 1);
84 }
85 for (Action a : actions) {
86 if (a instanceof LayerAction) {
87 add (((LayerAction) a).createMenuComponent());
88 } else {
89 add(new JMenuItem(a));
90 }
91 }
92 }
93}
Note: See TracBrowser for help on using the repository browser.