1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.history;
|
---|
3 |
|
---|
4 | import java.awt.datatransfer.Clipboard;
|
---|
5 |
|
---|
6 | import javax.swing.JComponent;
|
---|
7 | import javax.swing.JTable;
|
---|
8 | import javax.swing.TransferHandler;
|
---|
9 | import javax.swing.table.TableModel;
|
---|
10 |
|
---|
11 | import org.openstreetmap.josm.data.osm.TagMap;
|
---|
12 | import org.openstreetmap.josm.gui.datatransfer.ClipboardUtils;
|
---|
13 | import org.openstreetmap.josm.gui.datatransfer.TagTransferable;
|
---|
14 | import org.openstreetmap.josm.gui.datatransfer.data.TagTransferData;
|
---|
15 | import org.openstreetmap.josm.gui.history.HistoryBrowserModel.TagTableModel;
|
---|
16 |
|
---|
17 | /**
|
---|
18 | * This transfer handler allows to select and copy tags from a table with the {@link TagTableColumnModel}.
|
---|
19 | * @author Michael Zangl
|
---|
20 | * @since 10637
|
---|
21 | */
|
---|
22 | public class TagInfoTransferHandler extends TransferHandler {
|
---|
23 |
|
---|
24 | @Override
|
---|
25 | public void exportToClipboard(JComponent comp, Clipboard clip, int action) {
|
---|
26 | if (comp instanceof JTable) {
|
---|
27 | TableModel model = ((JTable) comp).getModel();
|
---|
28 | if (model instanceof TagTableModel) {
|
---|
29 | exportFromModel((JTable) comp, (TagTableModel) model);
|
---|
30 | }
|
---|
31 | }
|
---|
32 | }
|
---|
33 |
|
---|
34 | private static void exportFromModel(JTable comp, TagTableModel model) {
|
---|
35 | int[] selected = comp.getSelectedRows();
|
---|
36 | TagMap tags = new TagMap();
|
---|
37 | for (int row : selected) {
|
---|
38 | String key = model.getKeyAt(row);
|
---|
39 | String value = model.getValue(key);
|
---|
40 | if (value != null) {
|
---|
41 | tags.put(key, value);
|
---|
42 | }
|
---|
43 | }
|
---|
44 | TagTransferData data = new TagTransferData(tags);
|
---|
45 | ClipboardUtils.copy(new TagTransferable(data));
|
---|
46 | }
|
---|
47 | }
|
---|