1 | package org.openstreetmap.josm.gui.dialogs;
|
---|
2 |
|
---|
3 | import java.awt.BorderLayout;
|
---|
4 | import java.awt.Component;
|
---|
5 | import java.awt.Dimension;
|
---|
6 | import java.awt.GridLayout;
|
---|
7 | import java.awt.event.ActionEvent;
|
---|
8 | import java.awt.event.ActionListener;
|
---|
9 | import java.awt.event.KeyEvent;
|
---|
10 | import java.awt.event.MouseAdapter;
|
---|
11 | import java.awt.event.MouseEvent;
|
---|
12 | import java.util.Collection;
|
---|
13 |
|
---|
14 | import javax.swing.DefaultListCellRenderer;
|
---|
15 | import javax.swing.DefaultListModel;
|
---|
16 | import javax.swing.Icon;
|
---|
17 | import javax.swing.JButton;
|
---|
18 | import javax.swing.JLabel;
|
---|
19 | import javax.swing.JList;
|
---|
20 | import javax.swing.JPanel;
|
---|
21 | import javax.swing.JScrollPane;
|
---|
22 | import javax.swing.ListSelectionModel;
|
---|
23 | import javax.swing.UIManager;
|
---|
24 | import javax.swing.event.ListSelectionEvent;
|
---|
25 | import javax.swing.event.ListSelectionListener;
|
---|
26 |
|
---|
27 | import org.openstreetmap.josm.Main;
|
---|
28 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
29 | import org.openstreetmap.josm.gui.MapFrame;
|
---|
30 | import org.openstreetmap.josm.gui.MapView;
|
---|
31 | import org.openstreetmap.josm.gui.MapView.LayerChangeListener;
|
---|
32 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
33 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
34 | import org.openstreetmap.josm.tools.ImageProvider.OverlayPosition;
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * A component that manages the list of all layers and react to selection changes
|
---|
38 | * by setting the active layer in the mapview.
|
---|
39 | *
|
---|
40 | * @author imi
|
---|
41 | */
|
---|
42 | public class LayerList extends ToggleDialog implements LayerChangeListener {
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * The data model for the list component.
|
---|
46 | */
|
---|
47 | DefaultListModel model = new DefaultListModel();
|
---|
48 | /**
|
---|
49 | * The list component holding all layers.
|
---|
50 | */
|
---|
51 | JList layers = new JList(model);
|
---|
52 | /**
|
---|
53 | * The invisible icon blended over invisible layers.
|
---|
54 | */
|
---|
55 | static final Icon invisible = ImageProvider.get("layer", "invisible");
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * The merge action. This is only called, if the current selection and its
|
---|
59 | * item below are editable datasets and the merge button is clicked.
|
---|
60 | */
|
---|
61 | private final JButton mergeButton = new JButton(ImageProvider.get("dialogs", "mergedown"));
|
---|
62 | /**
|
---|
63 | * Button for moving layer up.
|
---|
64 | */
|
---|
65 | private JButton upButton = new JButton(ImageProvider.get("dialogs", "up"));
|
---|
66 | /**
|
---|
67 | * Button for moving layer down.
|
---|
68 | */
|
---|
69 | private JButton downButton = new JButton(ImageProvider.get("dialogs", "down"));
|
---|
70 | /**
|
---|
71 | * Button for delete layer.
|
---|
72 | */
|
---|
73 | private JButton deleteButton = new JButton(ImageProvider.get("dialogs", "delete"));
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * Create an layerlist and attach it to the given mapView.
|
---|
77 | */
|
---|
78 | public LayerList(MapFrame mapFrame) {
|
---|
79 | super("Layers", "List of all layers", "layerlist", "Open a list of all loaded layers.", "L", KeyEvent.VK_L);
|
---|
80 | setPreferredSize(new Dimension(320,100));
|
---|
81 | add(new JScrollPane(layers), BorderLayout.CENTER);
|
---|
82 | layers.setBackground(UIManager.getColor("Button.background"));
|
---|
83 | layers.setCellRenderer(new DefaultListCellRenderer(){
|
---|
84 | @Override
|
---|
85 | public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
|
---|
86 | Layer layer = (Layer)value;
|
---|
87 | JLabel label = (JLabel)super.getListCellRendererComponent(list,
|
---|
88 | layer.name, index, isSelected, cellHasFocus);
|
---|
89 | Icon icon = layer.getIcon();
|
---|
90 | if (!layer.visible)
|
---|
91 | icon = ImageProvider.overlay(icon, invisible, OverlayPosition.SOUTHEAST);
|
---|
92 | label.setIcon(icon);
|
---|
93 | label.setToolTipText(layer.getToolTipText());
|
---|
94 | return label;
|
---|
95 | }
|
---|
96 | });
|
---|
97 |
|
---|
98 | final MapView mapView = mapFrame.mapView;
|
---|
99 |
|
---|
100 | Collection<Layer> data = mapView.getAllLayers();
|
---|
101 | for (Layer l : data)
|
---|
102 | model.addElement(l);
|
---|
103 |
|
---|
104 | layers.setSelectedValue(mapView.getActiveLayer(), true);
|
---|
105 | layers.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
---|
106 | layers.addListSelectionListener(new ListSelectionListener(){
|
---|
107 | public void valueChanged(ListSelectionEvent e) {
|
---|
108 | if (layers.getSelectedIndex() == -1)
|
---|
109 | layers.setSelectedIndex(e.getFirstIndex());
|
---|
110 | mapView.setActiveLayer((Layer)layers.getSelectedValue());
|
---|
111 | }
|
---|
112 | });
|
---|
113 | mapView.addLayerChangeListener(this);
|
---|
114 |
|
---|
115 | layers.addMouseListener(new MouseAdapter(){
|
---|
116 | private void openPopup(MouseEvent e) {
|
---|
117 | int index = layers.locationToIndex(e.getPoint());
|
---|
118 | Layer layer = (Layer)layers.getModel().getElementAt(index);
|
---|
119 | LayerListPopup menu = new LayerListPopup(layer);
|
---|
120 | menu.show(LayerList.this, e.getX(), e.getY());
|
---|
121 | }
|
---|
122 | @Override public void mousePressed(MouseEvent e) {
|
---|
123 | if (e.isPopupTrigger())
|
---|
124 | openPopup(e);
|
---|
125 | }
|
---|
126 | @Override public void mouseReleased(MouseEvent e) {
|
---|
127 | if (e.isPopupTrigger())
|
---|
128 | openPopup(e);
|
---|
129 | }
|
---|
130 | });
|
---|
131 |
|
---|
132 |
|
---|
133 | // Buttons
|
---|
134 | JPanel buttonPanel = new JPanel(new GridLayout(1, 5));
|
---|
135 |
|
---|
136 | ActionListener upDown = new ActionListener(){
|
---|
137 | public void actionPerformed(ActionEvent e) {
|
---|
138 | Layer l = (Layer)layers.getSelectedValue();
|
---|
139 | int sel = layers.getSelectedIndex();
|
---|
140 | int selDest = e.getActionCommand().equals("up") ? sel-1 : sel+1;
|
---|
141 | mapView.moveLayer(l, selDest);
|
---|
142 | model.set(sel, model.get(selDest));
|
---|
143 | model.set(selDest, l);
|
---|
144 | layers.setSelectedIndex(selDest);
|
---|
145 | updateButtonEnabled();
|
---|
146 | mapView.repaint();
|
---|
147 | }
|
---|
148 | };
|
---|
149 |
|
---|
150 | upButton.setToolTipText("Move the selected layer one row up.");
|
---|
151 | upButton.addActionListener(upDown);
|
---|
152 | upButton.setActionCommand("up");
|
---|
153 | buttonPanel.add(upButton);
|
---|
154 |
|
---|
155 | downButton.setToolTipText("Move the selected layer one row down.");
|
---|
156 | downButton.addActionListener(upDown);
|
---|
157 | downButton.setActionCommand("down");
|
---|
158 | buttonPanel.add(downButton);
|
---|
159 |
|
---|
160 | JButton visible = new JButton(ImageProvider.get("dialogs", "showhide"));
|
---|
161 | visible.setToolTipText("Toggle visible state of the selected layer.");
|
---|
162 | visible.addActionListener(new ActionListener(){
|
---|
163 | public void actionPerformed(ActionEvent e) {
|
---|
164 | Layer l = (Layer)layers.getSelectedValue();
|
---|
165 | l.visible = !l.visible;
|
---|
166 | mapView.repaint();
|
---|
167 | layers.repaint();
|
---|
168 | }
|
---|
169 | });
|
---|
170 | buttonPanel.add(visible);
|
---|
171 |
|
---|
172 | deleteButton.setToolTipText("Delete the selected layer.");
|
---|
173 | deleteButton.addActionListener(new ActionListener(){
|
---|
174 | public void actionPerformed(ActionEvent e) {
|
---|
175 | if (model.size() == 1) {
|
---|
176 | Main.main.setMapFrame(null);
|
---|
177 | Main.main.ds = new DataSet();
|
---|
178 | } else {
|
---|
179 | int sel = layers.getSelectedIndex();
|
---|
180 | mapView.removeLayer((Layer)layers.getSelectedValue());
|
---|
181 | if (sel >= model.getSize())
|
---|
182 | sel = model.getSize()-1;
|
---|
183 | layers.setSelectedIndex(sel);
|
---|
184 | }
|
---|
185 | }
|
---|
186 | });
|
---|
187 | buttonPanel.add(deleteButton);
|
---|
188 |
|
---|
189 | mergeButton.setToolTipText("Merge the selected layer into the layer directly below.");
|
---|
190 | mergeButton.addActionListener(new ActionListener(){
|
---|
191 | public void actionPerformed(ActionEvent e) {
|
---|
192 | Layer lFrom = (Layer)layers.getSelectedValue();
|
---|
193 | Layer lTo = (Layer)model.get(layers.getSelectedIndex()+1);
|
---|
194 | lTo.mergeFrom(lFrom);
|
---|
195 | layers.setSelectedValue(lTo, true);
|
---|
196 | mapView.removeLayer(lFrom);
|
---|
197 | }
|
---|
198 | });
|
---|
199 | buttonPanel.add(mergeButton);
|
---|
200 |
|
---|
201 | add(buttonPanel, BorderLayout.SOUTH);
|
---|
202 |
|
---|
203 | updateButtonEnabled();
|
---|
204 | }
|
---|
205 |
|
---|
206 | /**
|
---|
207 | * Updates the state of the Buttons.
|
---|
208 | */
|
---|
209 | void updateButtonEnabled() {
|
---|
210 | int sel = layers.getSelectedIndex();
|
---|
211 | Layer l = (Layer)layers.getSelectedValue();
|
---|
212 | boolean enable = model.getSize() > 1;
|
---|
213 | enable = enable && sel < model.getSize()-1;
|
---|
214 | enable = enable && l.isMergable((Layer)model.get(sel+1));
|
---|
215 | mergeButton.setEnabled(enable);
|
---|
216 | upButton.setEnabled(sel > 0);
|
---|
217 | downButton.setEnabled(sel < model.getSize()-1);
|
---|
218 | deleteButton.setEnabled(!model.isEmpty());
|
---|
219 | }
|
---|
220 |
|
---|
221 | /**
|
---|
222 | * Add the new layer to the list.
|
---|
223 | */
|
---|
224 | public void layerAdded(Layer newLayer) {
|
---|
225 | model.add(0, newLayer);
|
---|
226 | updateButtonEnabled();
|
---|
227 | }
|
---|
228 |
|
---|
229 | public void layerRemoved(Layer oldLayer) {
|
---|
230 | model.removeElement(oldLayer);
|
---|
231 | if (layers.getSelectedIndex() == -1)
|
---|
232 | layers.setSelectedIndex(0);
|
---|
233 | updateButtonEnabled();
|
---|
234 | }
|
---|
235 |
|
---|
236 | /**
|
---|
237 | * If the newLayer is not the actual selection, select it.
|
---|
238 | */
|
---|
239 | public void activeLayerChange(Layer oldLayer, Layer newLayer) {
|
---|
240 | if (newLayer != layers.getSelectedValue())
|
---|
241 | layers.setSelectedValue(newLayer, true);
|
---|
242 | updateButtonEnabled();
|
---|
243 | }
|
---|
244 |
|
---|
245 | public void layerMoved(Layer layer, int newPosition) {}
|
---|
246 | }
|
---|