source: josm/trunk/src/org/openstreetmap/josm/gui/dialogs/layer/LayerListTransferHandler.java@ 12620

Last change on this file since 12620 was 12620, checked in by Don-vip, 7 years ago

see #15182 - deprecate all Main logging methods and introduce suitable replacements in Logging for most of them

File size: 6.4 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.dialogs.layer;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.datatransfer.Transferable;
7import java.awt.datatransfer.UnsupportedFlavorException;
8import java.io.IOException;
9import java.util.ArrayList;
10import java.util.Collection;
11import java.util.List;
12
13import javax.swing.JComponent;
14import javax.swing.JTable;
15import javax.swing.TransferHandler;
16
17import org.openstreetmap.josm.data.osm.DataSet;
18import org.openstreetmap.josm.gui.datatransfer.LayerTransferable;
19import org.openstreetmap.josm.gui.dialogs.LayerListDialog.LayerListModel;
20import org.openstreetmap.josm.gui.layer.Layer;
21import org.openstreetmap.josm.gui.layer.OsmDataLayer;
22import org.openstreetmap.josm.tools.Logging;
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 */
32public 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 // cannot link yet.
77 return support.getDropAction() != LINK;
78 }
79
80 @Override
81 public boolean importData(TransferSupport support) {
82 try {
83 LayerListModel tableModel = (LayerListModel) ((JTable) support.getComponent()).getModel();
84
85 LayerTransferable.Data layers = (LayerTransferable.Data) support.getTransferable()
86 .getTransferData(LayerTransferable.LAYER_DATA);
87
88 int dropLocation;
89 if (support.isDrop()) {
90 DropLocation dl = support.getDropLocation();
91 if (dl instanceof JTable.DropLocation) {
92 dropLocation = ((JTable.DropLocation) dl).getRow();
93 } else {
94 dropLocation = 0;
95 }
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 Logging.warn("Flavor not supported", e);
127 return false;
128 } catch (IOException e) {
129 Logging.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}
Note: See TracBrowser for help on using the repository browser.