KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > browser > WebBrowserViewDropAdapter


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 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.ui.internal.browser;
12
13 import java.io.File JavaDoc;
14
15 import org.eclipse.swt.dnd.DND;
16 import org.eclipse.swt.dnd.DropTargetAdapter;
17 import org.eclipse.swt.dnd.DropTargetEvent;
18 import org.eclipse.swt.dnd.FileTransfer;
19 import org.eclipse.swt.dnd.TransferData;
20 /**
21  *
22  */

23 public class WebBrowserViewDropAdapter extends DropTargetAdapter {
24    /**
25     * The view to which this drop support has been added.
26     */

27    private BrowserViewer view;
28
29    /**
30     * The current operation.
31     */

32    private int currentOperation = DND.DROP_NONE;
33
34    /**
35     * The last valid operation.
36     */

37    private int lastValidOperation = DND.DROP_NONE;
38    
39    protected WebBrowserViewDropAdapter(BrowserViewer view) {
40         this.view = view;
41     }
42
43    /* (non-Javadoc)
44     * Method declared on DropTargetAdapter.
45     * The mouse has moved over the drop target. If the
46     * target item has changed, notify the action and check
47     * that it is still enabled.
48     */

49    private void doDropValidation(DropTargetEvent event) {
50        //update last valid operation
51
if (event.detail != DND.DROP_NONE)
52            lastValidOperation = event.detail;
53        
54        //valid drop and set event detail accordingly
55
if (validateDrop(event.detail, event.currentDataType))
56            currentOperation = lastValidOperation;
57        else
58            currentOperation = DND.DROP_NONE;
59        
60        event.detail = currentOperation;
61    }
62
63    /* (non-Javadoc)
64     * Method declared on DropTargetAdapter.
65     * The drop operation has changed, see if the action
66     * should still be enabled.
67     */

68    public void dragOperationChanged(DropTargetEvent event) {
69        doDropValidation(event);
70    }
71
72    /* (non-Javadoc)
73     * Method declared on DropTargetAdapter.
74     * The mouse has moved over the drop target. If the
75     * target item has changed, notify the action and check
76     * that it is still enabled.
77     */

78    public void dragOver(DropTargetEvent event) {
79        //set the location feedback
80
event.feedback = DND.FEEDBACK_SELECT;
81
82        //see if anything has really changed before doing validation.
83
doDropValidation(event);
84    }
85
86    /* (non-Javadoc)
87     * Method declared on DropTargetAdapter.
88     * The user has dropped something on the desktop viewer.
89     */

90    public void drop(DropTargetEvent event) {
91        //perform the drop behaviour
92
if (!performDrop(event.data))
93            event.detail = DND.DROP_NONE;
94        
95        currentOperation = event.detail;
96    }
97
98    /* (non-Javadoc)
99     * Method declared on DropTargetAdapter.
100     * Last chance for the action to disable itself
101     */

102    public void dropAccept(DropTargetEvent event) {
103        if (!validateDrop(event.detail, event.currentDataType))
104            event.detail = DND.DROP_NONE;
105    }
106
107     public void dragEnter(DropTargetEvent event) {
108         if (event.detail == DND.DROP_DEFAULT)
109             event.detail = DND.DROP_COPY;
110
111        doDropValidation(event);
112     }
113
114     /**
115     * Performs any work associated with the drop.
116     * <p>
117     * Subclasses must implement this method to provide drop behavior.
118     * </p>
119     *
120     * @param data the drop data
121     * @return <code>true</code> if the drop was successful, and
122     * <code>false</code> otherwise
123     */

124     protected boolean performDrop(Object JavaDoc data) {
125         if (data instanceof String JavaDoc[]) {
126             String JavaDoc[] s = (String JavaDoc[]) data;
127             if (s == null || s.length == 0)
128                 return true;
129             File JavaDoc f = new File JavaDoc(s[0]);
130             try {
131                 view.setURL(f.toURL().toExternalForm());
132             } catch (Exception JavaDoc e) {
133                 // TODO
134
}
135         }
136         
137         return true;
138     }
139
140     /**
141     * Validates dropping on the given object. This method is called whenever some
142     * aspect of the drop operation changes.
143     * <p>
144     * Subclasses must implement this method to define which drops make sense.
145     * </p>
146     *
147     * @param target the object that the mouse is currently hovering over, or
148     * <code>null</code> if the mouse is hovering over empty space
149     * @param operation the current drag operation (copy, move, etc.)
150     * @param transferType the current transfer type
151     * @return <code>true</code> if the drop is valid, and <code>false</code>
152     * otherwise
153     */

154     protected boolean validateDrop(int operation, TransferData transferType) {
155         if (FileTransfer.getInstance().isSupportedType(transferType))
156             return true;
157         /*if (ResourceTransfer.getInstance().isSupportedType(transferType))
158             return true;
159         if (LocalSelectionTransfer.getInstance().isSupportedType(transferType))
160             return true;*/

161         
162         return false;
163     }
164 }
165
Popular Tags