~ubuntu-branches/debian/sid/eclipse-cdt/sid

« back to all changes in this revision

Viewing changes to results/plugins/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/dnd/ResourceTransferDragAdapter.java

  • Committer: Package Import Robot
  • Author(s): Jakub Adam
  • Date: 2011-10-06 21:15:04 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20111006211504-8dutmljjih0zikfv
Tags: 8.0.1-1
* New upstream release.
* Split the JNI packages into a separate architecture dependent
  package and made eclipse-cdt architecture independent.
* Install JNI libraries into multiarch aware location
* Bumped Standards-Version to 3.9.2.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*******************************************************************************
2
 
 * Copyright (c) 2000, 2008 IBM Corporation and others.
3
 
 * All rights reserved. This program and the accompanying materials
4
 
 * are made available under the terms of the Eclipse Public License v1.0
5
 
 * which accompanies this distribution, and is available at
6
 
 * http://www.eclipse.org/legal/epl-v10.html
7
 
 *
8
 
 * Contributors:
9
 
 *     IBM Corporation - initial API and implementation
10
 
 *******************************************************************************/
11
 
package org.eclipse.cdt.internal.ui.dnd;
12
 
 
13
 
import java.util.ArrayList;
14
 
import java.util.Collections;
15
 
import java.util.Iterator;
16
 
import java.util.List;
17
 
 
18
 
import org.eclipse.core.resources.IResource;
19
 
import org.eclipse.core.runtime.Assert;
20
 
import org.eclipse.core.runtime.CoreException;
21
 
import org.eclipse.core.runtime.IAdaptable;
22
 
import org.eclipse.jface.viewers.ISelection;
23
 
import org.eclipse.jface.viewers.ISelectionProvider;
24
 
import org.eclipse.jface.viewers.IStructuredSelection;
25
 
import org.eclipse.swt.dnd.DND;
26
 
import org.eclipse.swt.dnd.DragSource;
27
 
import org.eclipse.swt.dnd.DragSourceEvent;
28
 
import org.eclipse.swt.dnd.Transfer;
29
 
import org.eclipse.swt.widgets.Control;
30
 
import org.eclipse.swt.widgets.Shell;
31
 
import org.eclipse.ui.actions.ReadOnlyStateChecker;
32
 
import org.eclipse.ui.part.ResourceTransfer;
33
 
 
34
 
import org.eclipse.cdt.internal.ui.CUIMessages;
35
 
 
36
 
/**
37
 
 * A drag adapter that transfers the current selection as </code>
38
 
 * IResource</code>. Only those elements in the selection are part 
39
 
 * of the transfer which can be converted into an <code>IResource
40
 
 * </code>.
41
 
 */
42
 
public class ResourceTransferDragAdapter implements TransferDragSourceListener {
43
 
        private final ISelectionProvider provider;
44
 
 
45
 
        /**
46
 
         * Creates a new ResourceTransferDragAdapter for the given selection provider.
47
 
         *
48
 
         * @param provider the selection provider to access the viewer's selection
49
 
         */
50
 
        public ResourceTransferDragAdapter(ISelectionProvider provider) {
51
 
                super();
52
 
                this.provider = provider;
53
 
                Assert.isNotNull(provider);
54
 
        }
55
 
 
56
 
        public Transfer getTransfer() {
57
 
                return ResourceTransfer.getInstance();
58
 
        }
59
 
 
60
 
        public void dragStart(DragSourceEvent event) {
61
 
                IResource[] resources = getSelectedResources();
62
 
                event.doit = resources.length > 0;
63
 
        }
64
 
 
65
 
        public void dragSetData(DragSourceEvent event) {
66
 
                event.data = getSelectedResources();
67
 
        }
68
 
 
69
 
        public void dragFinished(DragSourceEvent event) {
70
 
                if (event.doit && event.detail == DND.DROP_MOVE) {
71
 
                        IResource[] resources = getSelectedResources();
72
 
 
73
 
                        if (resources.length == 0)
74
 
                                return;
75
 
 
76
 
                        DragSource dragSource = (DragSource) event.widget;
77
 
                        Control control = dragSource.getControl();
78
 
                        Shell shell = control.getShell();
79
 
                        String title = CUIMessages.Drag_move_problem_title; 
80
 
                        String message = CUIMessages.Drag_move_problem_message; 
81
 
 
82
 
                        ReadOnlyStateChecker checker = new ReadOnlyStateChecker(shell, title, message);
83
 
 
84
 
                        resources = checker.checkReadOnlyResources(resources);
85
 
 
86
 
                        // delete the old elements
87
 
                        for (int i = 0; i < resources.length; ++i) {
88
 
                                try {
89
 
                                        resources[i].delete(IResource.KEEP_HISTORY | IResource.FORCE, null);
90
 
                                } catch (CoreException e) {
91
 
                                        e.printStackTrace();
92
 
                                }
93
 
                        }
94
 
                }
95
 
        }
96
 
 
97
 
        private IResource[] getSelectedResources() {
98
 
                List<IResource> resources = Collections.emptyList();
99
 
                ISelection selection = provider.getSelection();
100
 
 
101
 
                if (selection instanceof IStructuredSelection) {
102
 
                        IStructuredSelection structured = (IStructuredSelection) selection;
103
 
 
104
 
                        resources = new ArrayList<IResource>(structured.size());
105
 
 
106
 
                        for (Iterator<?> iterator = structured.iterator(); iterator.hasNext();) {
107
 
                                Object element = iterator.next();
108
 
                                IResource resource = null;
109
 
                                if (element instanceof IResource) {
110
 
                                        resource = (IResource)element;
111
 
                                } else if (element instanceof IAdaptable) {
112
 
                                        IAdaptable adaptable = (IAdaptable) element;
113
 
                                        resource = (IResource) adaptable.getAdapter(IResource.class);
114
 
                                }
115
 
                                if (resource != null) {
116
 
                                        resources.add(resource);
117
 
                                }
118
 
                        }
119
 
                }
120
 
 
121
 
                IResource[] result = new IResource[resources.size()];
122
 
                resources.toArray(result);
123
 
 
124
 
                return result;
125
 
        }
126
 
 
127
 
}