1 11 package org.eclipse.jdt.internal.debug.ui.actions; 12 13 14 import java.util.ArrayList ; 15 import java.util.Iterator ; 16 import java.util.List ; 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 44 public class AddFolderAction extends RuntimeClasspathAction { 45 46 52 class FileFilter extends ViewerFilter { 53 public boolean select(Viewer viewer, Object parentElement, Object 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 74 public void run() { 75 76 ISelectionStatusValidator validator= new ISelectionStatusValidator() { 77 List fAlreadySelected = getSelectedFolders(); 78 public IStatus validate(Object [] 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); } 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 [] 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 117 protected List getSelectedFolders() { 118 List list = getEntriesAsList(); 119 List folders = new ArrayList (); 120 Iterator 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 |