01: package net.refractions.udig.project.ui.internal.actions;
02:
03: import net.refractions.udig.project.internal.Layer;
04: import net.refractions.udig.project.ui.internal.LayersView;
05: import net.refractions.udig.ui.IDropAction;
06: import net.refractions.udig.ui.ViewerDropLocation;
07:
08: import org.eclipse.core.runtime.IProgressMonitor;
09:
10: /**
11: * Action that moves layers when a layer within a map is dropped on a layer within the same map.
12: *
13: * @author Jesse
14: * @since 1.1.0
15: */
16: public class LayerDropAction extends IDropAction {
17:
18: @Override
19: public boolean accept() {
20: if (!(getDestination() instanceof Layer)
21: || !(getData() instanceof Layer))
22: return false;
23: Layer destination2 = (Layer) getDestination();
24: Layer data2 = (Layer) getData();
25: if (data2 == destination2
26: && getViewerLocation() != ViewerDropLocation.NONE)
27: return false;
28:
29: if ((destination2).getMap() == (data2).getMap())
30: return true;
31: return false;
32:
33: }
34:
35: @Override
36: public void perform(IProgressMonitor monitor) {
37: Layer layer = (Layer) getData();
38: Layer target = (Layer) getDestination();
39:
40: ViewerDropLocation location = getViewerLocation();
41:
42: if (location == ViewerDropLocation.NONE) {
43: layer.setZorder(0);
44: return;
45: }
46: if (Math.abs(layer.getZorder() - target.getZorder()) == 1) {
47: int tmp = layer.getZorder();
48: layer.setZorder(target.getZorder());
49: target.setZorder(tmp);
50: return;
51: }
52:
53: // Moving something AFTER a layer is the same as moving something BEFORE a layer.
54: // So we will use BEFORE as much as possible to prevent duplication here.
55: // This code will retrieve the layer before. Or the first one, if we are at the
56: // beginning of the list.
57: if (location == ViewerDropLocation.BEFORE) {
58: int i = target.getZorder();
59:
60: if (layer.getZorder() > target.getZorder()) {
61: i++;
62: }
63:
64: if (i > layer.getMap().getMapLayers().size()) {
65: layer.setZorder(layer.getMap().getMapLayers().size());
66: return;
67: }
68: if (layer.equals(target)) {
69: return;
70: }
71: layer.setZorder(i);
72:
73: } else if (location == ViewerDropLocation.ON) {
74: int i = target.getZorder();
75:
76: // if( layer.getZorder()>target.getZorder()){
77: // i--;
78: // }
79:
80: if (layer.equals(target)) {
81: return;
82: }
83: layer.setZorder(i);
84:
85: } else {
86: int i = target.getZorder();
87:
88: if (layer.getZorder() < target.getZorder()) {
89: i--;
90: }
91: if (i < 0)
92: i = 0;
93: layer.setZorder(i);
94: }
95: }
96: }
|