KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.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 /**
38  * Adds an internal jar to the runtime class path.
39  */

40 public class AddJarAction extends RuntimeClasspathAction {
41
42     private ISelectionStatusValidator validator= new ISelectionStatusValidator() {
43         public IStatus validate(Object JavaDoc[] selection) {
44             if (selection.length == 0) {
45                 return new Status(IStatus.ERROR, JDIDebugPlugin.getUniqueIdentifier(), 0, "", null); //$NON-NLS-1$
46
}
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); //$NON-NLS-1$
50
}
51             }
52             return new Status(IStatus.OK, JDIDebugPlugin.getUniqueIdentifier(), 0, "", null); //$NON-NLS-1$
53
}
54     };
55
56     public AddJarAction(IClasspathViewer viewer) {
57         super(ActionMessages.AddJarAction_Add__JARs_1, viewer);
58     }
59
60     /**
61      * Prompts for a jar to add.
62      *
63      * @see IAction#run()
64      */

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 JavaDoc[] 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     /**
92      * Returns a list of resources of currently selected jars
93      */

94     protected List JavaDoc getSelectedJars() {
95         List JavaDoc list = getEntriesAsList();
96         List JavaDoc jars = new ArrayList JavaDoc();
97         Iterator JavaDoc 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