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.IFile; 19 import org.eclipse.core.resources.IResource; 20 import org.eclipse.core.resources.ResourcesPlugin; 21 import org.eclipse.core.runtime.IStatus; 22 import org.eclipse.core.runtime.Status; 23 import org.eclipse.jdt.internal.debug.core.JDIDebugPlugin; 24 import org.eclipse.jdt.internal.debug.ui.launcher.IClasspathViewer; 25 import org.eclipse.jdt.launching.IRuntimeClasspathEntry; 26 import org.eclipse.jdt.launching.JavaRuntime; 27 import org.eclipse.jface.viewers.ILabelProvider; 28 import org.eclipse.jface.viewers.ITreeContentProvider; 29 import org.eclipse.jface.viewers.ViewerFilter; 30 import org.eclipse.jface.window.Window; 31 import org.eclipse.ui.dialogs.ElementTreeSelectionDialog; 32 import org.eclipse.ui.dialogs.ISelectionStatusValidator; 33 import org.eclipse.ui.model.WorkbenchContentProvider; 34 import org.eclipse.ui.model.WorkbenchLabelProvider; 35 import org.eclipse.ui.views.navigator.ResourceComparator; 36 37 40 public class AddJarAction extends RuntimeClasspathAction { 41 42 private ISelectionStatusValidator validator= new ISelectionStatusValidator() { 43 public IStatus validate(Object [] selection) { 44 if (selection.length == 0) { 45 return new Status(IStatus.ERROR, JDIDebugPlugin.getUniqueIdentifier(), 0, "", null); } 47 for (int i= 0; i < selection.length; i++) { 48 if (!(selection[i] instanceof IFile)) { 49 return new Status(IStatus.ERROR, JDIDebugPlugin.getUniqueIdentifier(), 0, "", null); } 51 } 52 return new Status(IStatus.OK, JDIDebugPlugin.getUniqueIdentifier(), 0, "", null); } 54 }; 55 56 public AddJarAction(IClasspathViewer viewer) { 57 super(ActionMessages.AddJarAction_Add__JARs_1, viewer); 58 } 59 60 65 public void run() { 66 67 ViewerFilter filter= new ArchiveFilter(getSelectedJars()); 68 69 ILabelProvider lp= new WorkbenchLabelProvider(); 70 ITreeContentProvider cp= new WorkbenchContentProvider(); 71 72 ElementTreeSelectionDialog dialog= new ElementTreeSelectionDialog(getShell(), lp, cp); 73 dialog.setValidator(validator); 74 dialog.setTitle(ActionMessages.AddJarAction_JAR_Selection_7); 75 dialog.setMessage(ActionMessages.AddJarAction_Choose_jars_to_add__8); 76 dialog.addFilter(filter); 77 dialog.setInput(ResourcesPlugin.getWorkspace().getRoot()); 78 dialog.setComparator(new ResourceComparator(ResourceComparator.NAME)); 79 80 if (dialog.open() == Window.OK) { 81 Object [] elements= dialog.getResult(); 82 IRuntimeClasspathEntry[] res= new IRuntimeClasspathEntry[elements.length]; 83 for (int i= 0; i < res.length; i++) { 84 IResource elem= (IResource)elements[i]; 85 res[i]= JavaRuntime.newArchiveRuntimeClasspathEntry(elem); 86 } 87 getViewer().addEntries(res); 88 } 89 } 90 91 94 protected List getSelectedJars() { 95 List list = getEntriesAsList(); 96 List jars = new ArrayList (); 97 Iterator iter = list.iterator(); 98 while (iter.hasNext()) { 99 IRuntimeClasspathEntry entry = (IRuntimeClasspathEntry)iter.next(); 100 if (entry.getType() == IRuntimeClasspathEntry.ARCHIVE) { 101 IResource res = entry.getResource(); 102 if (res != null && res instanceof IFile) { 103 jars.add(res); 104 } 105 } 106 } 107 return jars; 108 } 109 110 protected int getActionType() { 111 return ADD; 112 } 113 } 114 | Popular Tags |