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

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

sonar - squid:UselessParenthesesCheck

File size: 6.2 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.Main;
18import org.openstreetmap.josm.data.osm.DataSet;
19import org.openstreetmap.josm.gui.datatransfer.LayerTransferable;
20import org.openstreetmap.josm.gui.dialogs.LayerListDialog.LayerListModel;
21import org.openstreetmap.josm.gui.layer.Layer;
22import 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 */
32public class LayerListTransferHandler extends TransferHandler {
33 @Override
34 public int getSourceActions(JComponent c) {
35 // we know that the source is a layer list, so don't check c.
36 LayerListModel tableModel = (LayerListModel) ((JTable) c).getModel();
37 if (tableModel.getSelectedLayers().isEmpty()) {
38 return 0;
39 }
40 int actions = MOVE;
41 if (onlyDataLayersSelected(tableModel)) {
42 actions |= COPY;
43 }
44 return actions /* soon: | LINK*/;
45 }
46
47 private static boolean onlyDataLayersSelected(LayerListModel tableModel) {
48 for (Layer l : tableModel.getSelectedLayers()) {
49 if (!(l instanceof OsmDataLayer)) {
50 return false;
51 }
52 }
53 return true;
54 }
55
56 @Override
57 protected Transferable createTransferable(JComponent c) {
58 LayerListModel tableModel = (LayerListModel) ((JTable) c).getModel();
59 return new LayerTransferable(tableModel.getLayerManager(), tableModel.getSelectedLayers());
60 }
61
62 @Override
63 public boolean canImport(TransferSupport support) {
64 if (support.isDrop()) {
65 support.setShowDropLocation(true);
66 }
67
68 if (!support.isDataFlavorSupported(LayerTransferable.LAYER_DATA)) {
69 return false;
70 }
71
72 if (support.getDropAction() == LINK) {
73 // cannot link yet.
74 return false;
75 }
76
77 return true;
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 JTable.DropLocation dl = (JTable.DropLocation) support.getDropLocation();
91 dropLocation = dl.getRow();
92 } else {
93 dropLocation = layers.getLayers().get(0).getDefaultLayerPosition().getPosition(layers.getManager());
94 }
95
96 boolean isSameLayerManager = tableModel.getLayerManager() == layers.getManager();
97
98 if (support.getDropAction() == MOVE && isSameLayerManager) {
99 for (Layer layer : layers.getLayers()) {
100 boolean wasBeforeInsert = layers.getManager().getLayers().indexOf(layer) <= dropLocation;
101 if (wasBeforeInsert) {
102 // need to move insertion point one down to preserve order
103 dropLocation--;
104 }
105 layers.getManager().moveLayer(layer, dropLocation);
106 dropLocation++;
107 }
108 } else {
109 List<Layer> layersToUse = layers.getLayers();
110 if (support.getDropAction() == COPY) {
111 layersToUse = createCopy(layersToUse, layers.getManager().getLayers());
112 }
113 for (Layer layer : layersToUse) {
114 layers.getManager().addLayer(layer);
115 layers.getManager().moveLayer(layer, dropLocation);
116 dropLocation++;
117 }
118 }
119
120 return true;
121 } catch (UnsupportedFlavorException e) {
122 Main.warn("Flavor not supported", e);
123 return false;
124 } catch (IOException e) {
125 Main.warn("Error while pasting layer", e);
126 return false;
127 }
128 }
129
130 private static List<Layer> createCopy(List<Layer> layersToUse, List<Layer> namesToAvoid) {
131 Collection<String> layerNames = getNames(namesToAvoid);
132 ArrayList<Layer> layers = new ArrayList<>();
133 for (Layer layer : layersToUse) {
134 if (layer instanceof OsmDataLayer) {
135 String newName = suggestNewLayerName(layer.getName(), layerNames);
136 OsmDataLayer newLayer = new OsmDataLayer(new DataSet(((OsmDataLayer) layer).data), newName, null);
137 layers.add(newLayer);
138 layerNames.add(newName);
139 }
140 }
141 return layers;
142 }
143
144 /**
145 * Suggests a new name in the form "copy of name"
146 * @param name The base name
147 * @param namesToAvoid The list of layers to use to avoid duplicate names.
148 * @return The new name
149 */
150 public static String suggestNewLayerName(String name, List<Layer> namesToAvoid) {
151 Collection<String> layerNames = getNames(namesToAvoid);
152
153 return suggestNewLayerName(name, layerNames);
154 }
155
156 private static List<String> getNames(List<Layer> namesToAvoid) {
157 List<String> layerNames = new ArrayList<>();
158 for (Layer l: namesToAvoid) {
159 layerNames.add(l.getName());
160 }
161 return layerNames;
162 }
163
164 private static String suggestNewLayerName(String name, Collection<String> layerNames) {
165 // Translators: "Copy of {layer name}"
166 String newName = tr("Copy of {0}", name);
167 int i = 2;
168 while (layerNames.contains(newName)) {
169 // Translators: "Copy {number} of {layer name}"
170 newName = tr("Copy {1} of {0}", name, i);
171 i++;
172 }
173 return newName;
174 }
175}
Note: See TracBrowser for help on using the repository browser.