KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > views > AntViewDropAdapter


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.ant.internal.ui.views;
12
13 import org.eclipse.ant.internal.ui.AntUtil;
14 import org.eclipse.ant.internal.ui.model.AntProjectNode;
15 import org.eclipse.ant.internal.ui.model.AntProjectNodeProxy;
16 import org.eclipse.core.resources.IFile;
17 import org.eclipse.swt.custom.BusyIndicator;
18 import org.eclipse.swt.dnd.DND;
19 import org.eclipse.swt.dnd.DropTargetAdapter;
20 import org.eclipse.swt.dnd.DropTargetEvent;
21
22 /**
23  * A drop adapter which adds files to the Ant view.
24  */

25 public class AntViewDropAdapter extends DropTargetAdapter {
26     
27     private AntView view;
28     
29     /**
30      * Creates a new drop adapter for the given Ant view.
31      * @param view the view which dropped files will be added to
32      */

33     public AntViewDropAdapter(AntView view) {
34         this.view= view;
35     }
36
37     /**
38      * @see
39      * org.eclipse.swt.dnd.DropTargetListener#drop(org.eclipse.swt.dnd.DropTargetEvent)
40      */

41     public void drop(DropTargetEvent event) {
42         Object JavaDoc data = event.data;
43         if (data instanceof String JavaDoc[]) {
44             final String JavaDoc[] strings = (String JavaDoc[]) data;
45             BusyIndicator.showWhile(null, new Runnable JavaDoc() {
46                 public void run() {
47                     for (int i = 0; i < strings.length; i++) {
48                         processString(strings[i]);
49                     }
50                 }
51             });
52         }
53     }
54     
55     /**
56      * Attempts to process the given string as a path to an
57      * XML file. If the string is determined to be a path to an
58      * XML file in the workspace, that file is added to the Ant
59      * view.
60      * @param buildFileName the string to process
61      */

62     private void processString(String JavaDoc buildFileName) {
63         IFile buildFile = AntUtil.getFileForLocation(buildFileName, null);
64         if (buildFile == null || !buildFileName.toLowerCase().endsWith(".xml")) { //$NON-NLS-1$
65
return;
66         }
67         buildFileName = buildFile.getFullPath().toString();
68         AntProjectNode[] existingProjects = view.getProjects();
69         for (int j = 0; j < existingProjects.length; j++) {
70             AntProjectNodeProxy existingProject = (AntProjectNodeProxy)existingProjects[j];
71             if (existingProject.getBuildFileName().equals(buildFileName)) {
72                 // Don't parse projects that have already been added.
73
return;
74             }
75         }
76         AntProjectNode project = new AntProjectNodeProxy(buildFileName);
77         view.addProject(project);
78     }
79     
80     /* (non-Javadoc)
81      * @see org.eclipse.swt.dnd.DropTargetListener#dragEnter(org.eclipse.swt.dnd.DropTargetEvent)
82      */

83     public void dragEnter(DropTargetEvent event) {
84         event.detail= DND.DROP_COPY;
85         super.dragEnter(event);
86     }
87     
88     /* (non-Javadoc)
89      * @see org.eclipse.swt.dnd.DropTargetListener#dragOperationChanged(org.eclipse.swt.dnd.DropTargetEvent)
90      */

91     public void dragOperationChanged(DropTargetEvent event) {
92         event.detail= DND.DROP_COPY;
93         super.dragOperationChanged(event);
94     }
95 }
96
Popular Tags