KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > actions > AddFolderAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.jdt.internal.debug.ui.actions;
12
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.core.resources.IContainer;
19 import org.eclipse.core.resources.IFolder;
20 import org.eclipse.core.resources.IProject;
21 import org.eclipse.core.resources.IResource;
22 import org.eclipse.core.resources.ResourcesPlugin;
23 import org.eclipse.core.runtime.IStatus;
24 import org.eclipse.core.runtime.Status;
25 import org.eclipse.jdt.debug.ui.IJavaDebugUIConstants;
26 import org.eclipse.jdt.internal.debug.core.JDIDebugPlugin;
27 import org.eclipse.jdt.internal.debug.ui.launcher.IClasspathViewer;
28 import org.eclipse.jdt.launching.IRuntimeClasspathEntry;
29 import org.eclipse.jdt.launching.JavaRuntime;
30 import org.eclipse.jface.viewers.ILabelProvider;
31 import org.eclipse.jface.viewers.ITreeContentProvider;
32 import org.eclipse.jface.viewers.Viewer;
33 import org.eclipse.jface.viewers.ViewerFilter;
34 import org.eclipse.jface.window.Window;
35 import org.eclipse.ui.dialogs.ElementTreeSelectionDialog;
36 import org.eclipse.ui.dialogs.ISelectionStatusValidator;
37 import org.eclipse.ui.model.WorkbenchContentProvider;
38 import org.eclipse.ui.model.WorkbenchLabelProvider;
39 import org.eclipse.ui.views.navigator.ResourceComparator;
40
41 /**
42  * Adds an internal folder to the runtime class path.
43  */

44 public class AddFolderAction extends RuntimeClasspathAction {
45
46     /**
47      * provides a filter to remove the files from the ElementSelectionDialog
48      *
49      * @since 3.2
50      *
51      */

52     class FileFilter extends ViewerFilter {
53         public boolean select(Viewer viewer, Object JavaDoc parentElement, Object JavaDoc element) {
54             if(element instanceof IProject) {
55                 return true;
56             }
57             if(element instanceof IFolder) {
58                 return true;
59             }
60             return false;
61         }
62         
63     }
64     
65     public AddFolderAction(IClasspathViewer viewer) {
66         super(ActionMessages.AddFolderAction_Add__Folders_1, viewer);
67     }
68
69     /**
70      * Prompts for folder(s) to add.
71      *
72      * @see IAction#run()
73      */

74     public void run() {
75         
76         ISelectionStatusValidator validator= new ISelectionStatusValidator() {
77             List JavaDoc fAlreadySelected = getSelectedFolders();
78             public IStatus validate(Object JavaDoc[] selection) {
79                 for (int i= 0; i < selection.length; i++) {
80                     if (!(selection[i] instanceof IContainer)) {
81                         return new Status(IStatus.ERROR, JDIDebugPlugin.getUniqueIdentifier(), IJavaDebugUIConstants.INTERNAL_ERROR, ActionMessages.AddFolderAction_Selection_must_be_a_folder_2, null);
82                     } else if (fAlreadySelected.contains(selection[i])) {
83                         return new Status(IStatus.ERROR, JDIDebugPlugin.getUniqueIdentifier(), IJavaDebugUIConstants.INTERNAL_ERROR, ActionMessages.AddFolderAction_Classpath_already_includes_selected_folder_s___1, null);
84                     }
85                     
86                 }
87                 return new Status(IStatus.OK, JDIDebugPlugin.getUniqueIdentifier(), 0, "", null); //$NON-NLS-1$
88
}
89         };
90         
91         ILabelProvider lp= new WorkbenchLabelProvider();
92         ITreeContentProvider cp= new WorkbenchContentProvider();
93
94         ElementTreeSelectionDialog dialog= new ElementTreeSelectionDialog(getShell(), lp, cp);
95         dialog.addFilter(new FileFilter());
96         dialog.setComparator(new ResourceComparator(ResourceComparator.NAME));
97         dialog.setValidator(validator);
98         dialog.setTitle(ActionMessages.AddFolderAction_Folder_Selection_4);
99         dialog.setMessage(ActionMessages.AddFolderAction_Choose_folders_to_add__5);
100         dialog.setInput(ResourcesPlugin.getWorkspace().getRoot());
101
102         if (dialog.open() == Window.OK) {
103             Object JavaDoc[] elements= dialog.getResult();
104             IRuntimeClasspathEntry[] res= new IRuntimeClasspathEntry[elements.length];
105             for (int i= 0; i < res.length; i++) {
106                 IResource elem= (IResource)elements[i];
107                 res[i]= JavaRuntime.newArchiveRuntimeClasspathEntry(elem);
108             }
109             getViewer().addEntries(res);
110         }
111                     
112     }
113     
114     /**
115      * Returns a list of resources of currently selected folders
116      */

117     protected List JavaDoc getSelectedFolders() {
118         List JavaDoc list = getEntriesAsList();
119         List JavaDoc folders = new ArrayList JavaDoc();
120         Iterator JavaDoc iter = list.iterator();
121         while (iter.hasNext()) {
122             IRuntimeClasspathEntry entry = (IRuntimeClasspathEntry)iter.next();
123             if (entry.getType() == IRuntimeClasspathEntry.ARCHIVE) {
124                 IResource res = entry.getResource();
125                 if (res != null && res instanceof IContainer) {
126                     folders.add(res);
127                 }
128             }
129         }
130         return folders;
131     }
132     
133     protected int getActionType() {
134         return ADD;
135     }
136 }
137
Popular Tags