1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.dialogs.layer;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.datatransfer.Transferable;
|
---|
7 | import java.awt.datatransfer.UnsupportedFlavorException;
|
---|
8 | import java.io.IOException;
|
---|
9 | import java.util.ArrayList;
|
---|
10 | import java.util.Collection;
|
---|
11 | import java.util.List;
|
---|
12 |
|
---|
13 | import javax.swing.JComponent;
|
---|
14 | import javax.swing.JTable;
|
---|
15 | import javax.swing.TransferHandler;
|
---|
16 |
|
---|
17 | import org.openstreetmap.josm.Main;
|
---|
18 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
19 | import org.openstreetmap.josm.gui.datatransfer.LayerTransferable;
|
---|
20 | import org.openstreetmap.josm.gui.dialogs.LayerListDialog.LayerListModel;
|
---|
21 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
22 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * This class allows the user to transfer layers using drag+drop.
|
---|
26 | * <p>
|
---|
27 | * It supports copy (duplication) of layers, simple moves and linking layers to a new layer manager.
|
---|
28 | *
|
---|
29 | * @author Michael Zangl
|
---|
30 | * @since 10605
|
---|
31 | */
|
---|
32 | public class LayerListTransferHandler extends TransferHandler {
|
---|
33 | @Override
|
---|
34 | public int getSourceActions(JComponent c) {
|
---|
35 | if (c instanceof JTable) {
|
---|
36 | LayerListModel tableModel = (LayerListModel) ((JTable) c).getModel();
|
---|
37 | if (!tableModel.getSelectedLayers().isEmpty()) {
|
---|
38 | int actions = MOVE;
|
---|
39 | if (onlyDataLayersSelected(tableModel)) {
|
---|
40 | actions |= COPY;
|
---|
41 | }
|
---|
42 | return actions /* soon: | LINK*/;
|
---|
43 | }
|
---|
44 | }
|
---|
45 | return NONE;
|
---|
46 | }
|
---|
47 |
|
---|
48 | private static boolean onlyDataLayersSelected(LayerListModel tableModel) {
|
---|
49 | for (Layer l : tableModel.getSelectedLayers()) {
|
---|
50 | if (!(l instanceof OsmDataLayer)) {
|
---|
51 | return false;
|
---|
52 | }
|
---|
53 | }
|
---|
54 | return true;
|
---|
55 | }
|
---|
56 |
|
---|
57 | @Override
|
---|
58 | protected Transferable createTransferable(JComponent c) {
|
---|
59 | if (c instanceof JTable) {
|
---|
60 | LayerListModel tableModel = (LayerListModel) ((JTable) c).getModel();
|
---|
61 | return new LayerTransferable(tableModel.getLayerManager(), tableModel.getSelectedLayers());
|
---|
62 | }
|
---|
63 | return null;
|
---|
64 | }
|
---|
65 |
|
---|
66 | @Override
|
---|
67 | public boolean canImport(TransferSupport support) {
|
---|
68 | if (support.isDrop()) {
|
---|
69 | support.setShowDropLocation(true);
|
---|
70 | }
|
---|
71 |
|
---|
72 | if (!support.isDataFlavorSupported(LayerTransferable.LAYER_DATA)) {
|
---|
73 | return false;
|
---|
74 | }
|
---|
75 |
|
---|
76 | if (support.getDropAction() == LINK) {
|
---|
77 | // cannot link yet.
|
---|
78 | return false;
|
---|
79 | }
|
---|
80 |
|
---|
81 | return true;
|
---|
82 | }
|
---|
83 |
|
---|
84 | @Override
|
---|
85 | public boolean importData(TransferSupport support) {
|
---|
86 | try {
|
---|
87 | LayerListModel tableModel = (LayerListModel) ((JTable) support.getComponent()).getModel();
|
---|
88 |
|
---|
89 | LayerTransferable.Data layers = (LayerTransferable.Data) support.getTransferable()
|
---|
90 | .getTransferData(LayerTransferable.LAYER_DATA);
|
---|
91 |
|
---|
92 | int dropLocation;
|
---|
93 | if (support.isDrop()) {
|
---|
94 | JTable.DropLocation dl = (JTable.DropLocation) support.getDropLocation();
|
---|
95 | dropLocation = dl.getRow();
|
---|
96 | } else {
|
---|
97 | dropLocation = layers.getLayers().get(0).getDefaultLayerPosition().getPosition(layers.getManager());
|
---|
98 | }
|
---|
99 |
|
---|
100 | boolean isSameLayerManager = tableModel.getLayerManager() == layers.getManager();
|
---|
101 |
|
---|
102 | if (isSameLayerManager && support.getDropAction() == MOVE) {
|
---|
103 | for (Layer layer : layers.getLayers()) {
|
---|
104 | boolean wasBeforeInsert = layers.getManager().getLayers().indexOf(layer) <= dropLocation;
|
---|
105 | if (wasBeforeInsert) {
|
---|
106 | // need to move insertion point one down to preserve order
|
---|
107 | dropLocation--;
|
---|
108 | }
|
---|
109 | layers.getManager().moveLayer(layer, dropLocation);
|
---|
110 | dropLocation++;
|
---|
111 | }
|
---|
112 | } else {
|
---|
113 | List<Layer> layersToUse = layers.getLayers();
|
---|
114 | if (support.getDropAction() == COPY) {
|
---|
115 | layersToUse = createCopy(layersToUse, layers.getManager().getLayers());
|
---|
116 | }
|
---|
117 | for (Layer layer : layersToUse) {
|
---|
118 | layers.getManager().addLayer(layer);
|
---|
119 | layers.getManager().moveLayer(layer, dropLocation);
|
---|
120 | dropLocation++;
|
---|
121 | }
|
---|
122 | }
|
---|
123 |
|
---|
124 | return true;
|
---|
125 | } catch (UnsupportedFlavorException e) {
|
---|
126 | Main.warn("Flavor not supported", e);
|
---|
127 | return false;
|
---|
128 | } catch (IOException e) {
|
---|
129 | Main.warn("Error while pasting layer", e);
|
---|
130 | return false;
|
---|
131 | }
|
---|
132 | }
|
---|
133 |
|
---|
134 | private static List<Layer> createCopy(List<Layer> layersToUse, List<Layer> namesToAvoid) {
|
---|
135 | Collection<String> layerNames = getNames(namesToAvoid);
|
---|
136 | ArrayList<Layer> layers = new ArrayList<>();
|
---|
137 | for (Layer layer : layersToUse) {
|
---|
138 | if (layer instanceof OsmDataLayer) {
|
---|
139 | String newName = suggestNewLayerName(layer.getName(), layerNames);
|
---|
140 | OsmDataLayer newLayer = new OsmDataLayer(new DataSet(((OsmDataLayer) layer).data), newName, null);
|
---|
141 | layers.add(newLayer);
|
---|
142 | layerNames.add(newName);
|
---|
143 | }
|
---|
144 | }
|
---|
145 | return layers;
|
---|
146 | }
|
---|
147 |
|
---|
148 | /**
|
---|
149 | * Suggests a new name in the form "copy of name"
|
---|
150 | * @param name The base name
|
---|
151 | * @param namesToAvoid The list of layers to use to avoid duplicate names.
|
---|
152 | * @return The new name
|
---|
153 | */
|
---|
154 | public static String suggestNewLayerName(String name, List<Layer> namesToAvoid) {
|
---|
155 | Collection<String> layerNames = getNames(namesToAvoid);
|
---|
156 |
|
---|
157 | return suggestNewLayerName(name, layerNames);
|
---|
158 | }
|
---|
159 |
|
---|
160 | private static List<String> getNames(List<Layer> namesToAvoid) {
|
---|
161 | List<String> layerNames = new ArrayList<>();
|
---|
162 | for (Layer l: namesToAvoid) {
|
---|
163 | layerNames.add(l.getName());
|
---|
164 | }
|
---|
165 | return layerNames;
|
---|
166 | }
|
---|
167 |
|
---|
168 | private static String suggestNewLayerName(String name, Collection<String> layerNames) {
|
---|
169 | // Translators: "Copy of {layer name}"
|
---|
170 | String newName = tr("Copy of {0}", name);
|
---|
171 | int i = 2;
|
---|
172 | while (layerNames.contains(newName)) {
|
---|
173 | // Translators: "Copy {number} of {layer name}"
|
---|
174 | newName = tr("Copy {1} of {0}", name, i);
|
---|
175 | i++;
|
---|
176 | }
|
---|
177 | return newName;
|
---|
178 | }
|
---|
179 | }
|
---|