1 | package org.openstreetmap.josm.gui.dialogs;
|
---|
2 |
|
---|
3 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
4 |
|
---|
5 | import java.awt.BorderLayout;
|
---|
6 | import java.awt.Component;
|
---|
7 | import java.awt.GridLayout;
|
---|
8 | import java.awt.Point;
|
---|
9 | import java.awt.event.ActionEvent;
|
---|
10 | import java.awt.event.ActionListener;
|
---|
11 | import java.awt.event.KeyEvent;
|
---|
12 | import java.awt.event.MouseAdapter;
|
---|
13 | import java.awt.event.MouseEvent;
|
---|
14 | import java.util.Collection;
|
---|
15 |
|
---|
16 | import javax.swing.AbstractAction;
|
---|
17 | import javax.swing.Action;
|
---|
18 | import javax.swing.DefaultListCellRenderer;
|
---|
19 | import javax.swing.DefaultListModel;
|
---|
20 | import javax.swing.Icon;
|
---|
21 | import javax.swing.JButton;
|
---|
22 | import javax.swing.JLabel;
|
---|
23 | import javax.swing.JList;
|
---|
24 | import javax.swing.JPanel;
|
---|
25 | import javax.swing.JScrollPane;
|
---|
26 | import javax.swing.ListSelectionModel;
|
---|
27 | import javax.swing.UIManager;
|
---|
28 | import javax.swing.event.ListSelectionEvent;
|
---|
29 | import javax.swing.event.ListSelectionListener;
|
---|
30 |
|
---|
31 | import org.openstreetmap.josm.Main;
|
---|
32 | import org.openstreetmap.josm.gui.MapFrame;
|
---|
33 | import org.openstreetmap.josm.gui.MapView;
|
---|
34 | import org.openstreetmap.josm.gui.MapView.LayerChangeListener;
|
---|
35 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
36 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
37 | import org.openstreetmap.josm.tools.DontShowAgainInfo;
|
---|
38 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
39 | import org.openstreetmap.josm.tools.ImageProvider.OverlayPosition;
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * A component that manages the list of all layers and react to selection changes
|
---|
43 | * by setting the active layer in the mapview.
|
---|
44 | *
|
---|
45 | * @author imi
|
---|
46 | */
|
---|
47 | public class LayerListDialog extends ToggleDialog implements LayerChangeListener {
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * The last layerlist created. Used to update the list in the Show/Hide and Delete actions.
|
---|
51 | * TODO: Replace with Listener-Pattern.
|
---|
52 | */
|
---|
53 | static JList instance;
|
---|
54 | private JScrollPane listScrollPane;
|
---|
55 |
|
---|
56 | public final static class DeleteLayerAction extends AbstractAction {
|
---|
57 |
|
---|
58 | private final Layer layer;
|
---|
59 |
|
---|
60 | public DeleteLayerAction(Layer layer) {
|
---|
61 | super(tr("Delete"), ImageProvider.get("dialogs", "delete"));
|
---|
62 | putValue(SHORT_DESCRIPTION, tr("Delete the selected layer."));
|
---|
63 | putValue("help", "Dialog/LayerList/Delete");
|
---|
64 | this.layer = layer;
|
---|
65 | }
|
---|
66 |
|
---|
67 | public void actionPerformed(ActionEvent e) {
|
---|
68 | int sel = instance.getSelectedIndex();
|
---|
69 | Layer l = layer != null ? layer : (Layer)instance.getSelectedValue();
|
---|
70 | if (l instanceof OsmDataLayer && !DontShowAgainInfo.show("delete_layer", tr("Do you really want to delete the whole layer?")))
|
---|
71 | return;
|
---|
72 | Main.main.removeLayer(l);
|
---|
73 | if (sel >= instance.getModel().getSize())
|
---|
74 | sel = instance.getModel().getSize()-1;
|
---|
75 | if (instance.getSelectedValue() == null)
|
---|
76 | instance.setSelectedIndex(sel);
|
---|
77 | }
|
---|
78 | }
|
---|
79 |
|
---|
80 | public final static class ShowHideLayerAction extends AbstractAction {
|
---|
81 | private final Layer layer;
|
---|
82 |
|
---|
83 | public ShowHideLayerAction(Layer layer) {
|
---|
84 | super(tr("Show/Hide"), ImageProvider.get("dialogs", "showhide"));
|
---|
85 | putValue(SHORT_DESCRIPTION, tr("Toggle visible state of the selected layer."));
|
---|
86 | putValue("help", "Dialog/LayerList/ShowHide");
|
---|
87 | this.layer = layer;
|
---|
88 | }
|
---|
89 |
|
---|
90 | public void actionPerformed(ActionEvent e) {
|
---|
91 | Layer l = layer == null ? (Layer)instance.getSelectedValue() : layer;
|
---|
92 | l.visible = !l.visible;
|
---|
93 | Main.map.mapView.repaint();
|
---|
94 | instance.repaint();
|
---|
95 | }
|
---|
96 | }
|
---|
97 |
|
---|
98 | public final static class ShowHideMarkerText extends AbstractAction {
|
---|
99 | private final Layer layer;
|
---|
100 |
|
---|
101 | public ShowHideMarkerText(Layer layer) {
|
---|
102 | super(tr("Show/Hide Text"), ImageProvider.get("dialogs", "showhide"));
|
---|
103 | putValue(SHORT_DESCRIPTION, tr("Toggle visible state of the marker text."));
|
---|
104 | putValue("help", "Dialog/LayerList/ShowHideMarkerText");
|
---|
105 | this.layer = layer;
|
---|
106 | }
|
---|
107 |
|
---|
108 | public void actionPerformed(ActionEvent e) {
|
---|
109 | Layer l = layer == null ? (Layer)instance.getSelectedValue() : layer;
|
---|
110 | String current = Main.pref.get("marker.show "+l.name,"show");
|
---|
111 | Main.pref.put("marker.show "+l.name, current.equalsIgnoreCase("show") ? "hide" : "show");
|
---|
112 | Main.map.mapView.repaint();
|
---|
113 | instance.repaint();
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|
117 | /**
|
---|
118 | * The data model for the list component.
|
---|
119 | */
|
---|
120 | DefaultListModel model = new DefaultListModel();
|
---|
121 | /**
|
---|
122 | * The merge action. This is only called, if the current selection and its
|
---|
123 | * item below are editable datasets and the merge button is clicked.
|
---|
124 | */
|
---|
125 | private final JButton mergeButton = new JButton(ImageProvider.get("dialogs", "mergedown"));
|
---|
126 | /**
|
---|
127 | * Button for moving layer up.
|
---|
128 | */
|
---|
129 | private JButton upButton = new JButton(ImageProvider.get("dialogs", "up"));
|
---|
130 | /**
|
---|
131 | * Button for moving layer down.
|
---|
132 | */
|
---|
133 | private JButton downButton = new JButton(ImageProvider.get("dialogs", "down"));
|
---|
134 | /**
|
---|
135 | * Button for delete layer.
|
---|
136 | */
|
---|
137 | private Action deleteAction = new DeleteLayerAction(null);
|
---|
138 |
|
---|
139 | /**
|
---|
140 | * Create an layerlist and attach it to the given mapView.
|
---|
141 | */
|
---|
142 | public LayerListDialog(MapFrame mapFrame) {
|
---|
143 | super(tr("Layers"), "layerlist", tr("Open a list of all loaded layers."), KeyEvent.VK_L, 100);
|
---|
144 | instance = new JList(model);
|
---|
145 | listScrollPane = new JScrollPane(instance);
|
---|
146 | add(listScrollPane, BorderLayout.CENTER);
|
---|
147 | instance.setBackground(UIManager.getColor("Button.background"));
|
---|
148 | instance.setCellRenderer(new DefaultListCellRenderer(){
|
---|
149 | @Override public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
|
---|
150 | Layer layer = (Layer)value;
|
---|
151 | JLabel label = (JLabel)super.getListCellRendererComponent(list,
|
---|
152 | layer.name, index, isSelected, cellHasFocus);
|
---|
153 | Icon icon = layer.getIcon();
|
---|
154 | if (!layer.visible)
|
---|
155 | icon = ImageProvider.overlay(icon, "overlay/invisible", OverlayPosition.SOUTHEAST);
|
---|
156 | label.setIcon(icon);
|
---|
157 | label.setToolTipText(layer.getToolTipText());
|
---|
158 | return label;
|
---|
159 | }
|
---|
160 | });
|
---|
161 |
|
---|
162 | final MapView mapView = mapFrame.mapView;
|
---|
163 |
|
---|
164 | Collection<Layer> data = mapView.getAllLayers();
|
---|
165 | for (Layer l : data)
|
---|
166 | model.addElement(l);
|
---|
167 |
|
---|
168 | instance.setSelectedValue(mapView.getActiveLayer(), true);
|
---|
169 | instance.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
---|
170 | instance.addListSelectionListener(new ListSelectionListener(){
|
---|
171 | public void valueChanged(ListSelectionEvent e) {
|
---|
172 | if (instance.getModel().getSize() == 0)
|
---|
173 | return;
|
---|
174 | if (instance.getSelectedIndex() == -1)
|
---|
175 | instance.setSelectedIndex(e.getFirstIndex());
|
---|
176 | mapView.setActiveLayer((Layer)instance.getSelectedValue());
|
---|
177 | }
|
---|
178 | });
|
---|
179 | mapView.addLayerChangeListener(this);
|
---|
180 |
|
---|
181 | instance.addMouseListener(new MouseAdapter(){
|
---|
182 | private void openPopup(MouseEvent e) {
|
---|
183 | Point p = listScrollPane.getMousePosition();
|
---|
184 | if (p == null)
|
---|
185 | return; // user is faster than swing with mouse movement
|
---|
186 | int index = instance.locationToIndex(e.getPoint());
|
---|
187 | Layer layer = (Layer)instance.getModel().getElementAt(index);
|
---|
188 | LayerListPopup menu = new LayerListPopup(instance, layer);
|
---|
189 | menu.show(listScrollPane, p.x, p.y-3);
|
---|
190 | }
|
---|
191 | @Override public void mousePressed(MouseEvent e) {
|
---|
192 | if (e.isPopupTrigger())
|
---|
193 | openPopup(e);
|
---|
194 | }
|
---|
195 | @Override public void mouseReleased(MouseEvent e) {
|
---|
196 | if (e.isPopupTrigger())
|
---|
197 | openPopup(e);
|
---|
198 | }
|
---|
199 | });
|
---|
200 |
|
---|
201 |
|
---|
202 | // Buttons
|
---|
203 | JPanel buttonPanel = new JPanel(new GridLayout(1, 5));
|
---|
204 |
|
---|
205 | ActionListener upDown = new ActionListener(){
|
---|
206 | public void actionPerformed(ActionEvent e) {
|
---|
207 | Layer l = (Layer)instance.getSelectedValue();
|
---|
208 | int sel = instance.getSelectedIndex();
|
---|
209 | int selDest = e.getActionCommand().equals("up") ? sel-1 : sel+1;
|
---|
210 | mapView.moveLayer(l, selDest);
|
---|
211 | model.set(sel, model.get(selDest));
|
---|
212 | model.set(selDest, l);
|
---|
213 | instance.setSelectedIndex(selDest);
|
---|
214 | updateButtonEnabled();
|
---|
215 | mapView.repaint();
|
---|
216 | }
|
---|
217 | };
|
---|
218 |
|
---|
219 | upButton.setToolTipText(tr("Move the selected layer one row up."));
|
---|
220 | upButton.addActionListener(upDown);
|
---|
221 | upButton.setActionCommand("up");
|
---|
222 | upButton.putClientProperty("help", "Dialog/LayerList/Up");
|
---|
223 | buttonPanel.add(upButton);
|
---|
224 |
|
---|
225 | downButton.setToolTipText(tr("Move the selected layer one row down."));
|
---|
226 | downButton.addActionListener(upDown);
|
---|
227 | downButton.setActionCommand("down");
|
---|
228 | downButton.putClientProperty("help", "Dialog/LayerList/Down");
|
---|
229 | buttonPanel.add(downButton);
|
---|
230 |
|
---|
231 | JButton showHideButton = new JButton(new ShowHideLayerAction(null));
|
---|
232 | showHideButton.setText("");
|
---|
233 | buttonPanel.add(showHideButton);
|
---|
234 |
|
---|
235 | JButton deleteButton = new JButton(deleteAction);
|
---|
236 | deleteButton.setText("");
|
---|
237 | buttonPanel.add(deleteButton);
|
---|
238 |
|
---|
239 | mergeButton.setToolTipText(tr("Merge the selected layer into the layer directly below."));
|
---|
240 | mergeButton.addActionListener(new ActionListener(){
|
---|
241 | public void actionPerformed(ActionEvent e) {
|
---|
242 | Layer lFrom = (Layer)instance.getSelectedValue();
|
---|
243 | Layer lTo = (Layer)model.get(instance.getSelectedIndex()+1);
|
---|
244 | lTo.mergeFrom(lFrom);
|
---|
245 | instance.setSelectedValue(lTo, true);
|
---|
246 | mapView.removeLayer(lFrom);
|
---|
247 | }
|
---|
248 | });
|
---|
249 | mergeButton.putClientProperty("help", "Dialog/LayerList/Merge");
|
---|
250 | buttonPanel.add(mergeButton);
|
---|
251 |
|
---|
252 | add(buttonPanel, BorderLayout.SOUTH);
|
---|
253 |
|
---|
254 | updateButtonEnabled();
|
---|
255 | }
|
---|
256 |
|
---|
257 | /**
|
---|
258 | * Updates the state of the Buttons.
|
---|
259 | */
|
---|
260 | void updateButtonEnabled() {
|
---|
261 | int sel = instance.getSelectedIndex();
|
---|
262 | Layer l = (Layer)instance.getSelectedValue();
|
---|
263 | boolean enable = model.getSize() > 1;
|
---|
264 | enable = enable && sel < model.getSize()-1;
|
---|
265 | enable = enable && l.isMergable((Layer)model.get(sel+1));
|
---|
266 | mergeButton.setEnabled(enable);
|
---|
267 | upButton.setEnabled(sel > 0);
|
---|
268 | downButton.setEnabled(sel < model.getSize()-1);
|
---|
269 | deleteAction.setEnabled(!model.isEmpty());
|
---|
270 | }
|
---|
271 |
|
---|
272 | /**
|
---|
273 | * Add the new layer to the list.
|
---|
274 | */
|
---|
275 | public void layerAdded(Layer newLayer) {
|
---|
276 | model.add(0, newLayer);
|
---|
277 | updateButtonEnabled();
|
---|
278 | }
|
---|
279 |
|
---|
280 | public void layerRemoved(Layer oldLayer) {
|
---|
281 | model.removeElement(oldLayer);
|
---|
282 | if (instance.getSelectedIndex() == -1)
|
---|
283 | instance.setSelectedIndex(0);
|
---|
284 | updateButtonEnabled();
|
---|
285 | }
|
---|
286 |
|
---|
287 | /**
|
---|
288 | * If the newLayer is not the actual selection, select it.
|
---|
289 | */
|
---|
290 | public void activeLayerChange(Layer oldLayer, Layer newLayer) {
|
---|
291 | if (newLayer != instance.getSelectedValue())
|
---|
292 | instance.setSelectedValue(newLayer, true);
|
---|
293 | updateButtonEnabled();
|
---|
294 | }
|
---|
295 |
|
---|
296 | public void layerMoved(Layer layer, int newPosition) {}
|
---|
297 | }
|
---|