KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > buildpath > AddSelectedLibraryOperation


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
12 package org.eclipse.jdt.internal.corext.buildpath;
13
14 import java.lang.reflect.InvocationTargetException JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IProgressMonitor;
19
20 import org.eclipse.core.resources.IFile;
21
22 import org.eclipse.jdt.core.IJavaProject;
23 import org.eclipse.jdt.core.JavaModelException;
24
25 import org.eclipse.jdt.internal.corext.util.Messages;
26
27 import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
28 import org.eclipse.jdt.internal.ui.wizards.buildpaths.newsourcepage.DialogPackageExplorerActionGroup;
29 import org.eclipse.jdt.internal.ui.wizards.buildpaths.newsourcepage.ClasspathModifierQueries.OutputFolderQuery;
30
31
32 /**
33  * Operation to add objects (of type <code>IFile</code> or <code>
34  * IFolder</code> as library entries to the classpath.
35  *
36  * @see org.eclipse.jdt.internal.corext.buildpath.ClasspathModifier#addToClasspath(List, IJavaProject, OutputFolderQuery, IProgressMonitor)
37  * @see org.eclipse.jdt.internal.corext.buildpath.RemoveFromClasspathOperation
38  */

39 public class AddSelectedLibraryOperation extends ClasspathModifierOperation {
40     
41     /**
42      * Constructor
43      *
44      * @param listener a <code>IClasspathModifierListener</code> that is notified about
45      * changes on classpath entries or <code>null</code> if no such notification is
46      * necessary.
47      * @param informationProvider a provider to offer information to the action
48      *
49      * @see IClasspathInformationProvider
50      * @see ClasspathModifier
51      */

52     public AddSelectedLibraryOperation(IClasspathModifierListener listener, IClasspathInformationProvider informationProvider) {
53         super(listener, informationProvider, NewWizardMessages.NewSourceContainerWorkbookPage_ToolBar_AddSelLibToCP_tooltip, IClasspathInformationProvider.ADD_SEL_LIB_TO_BP);
54     }
55     
56     /**
57      * Method which runs the actions with a progress monitor.<br>
58      *
59      * This operation requires the following query from the provider:
60      * <li>IOutputFolderQuery</li>
61      *
62      * @param monitor a progress monitor, can be <code>null</code>
63      */

64     public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc {
65         List JavaDoc result= null;
66         fException= null;
67         try {
68             List JavaDoc elements= getSelectedElements();
69             IJavaProject project= fInformationProvider.getJavaProject();
70             result= addLibraryEntries(elements, project, monitor);
71         } catch (CoreException e) {
72             fException= e;
73             result= null;
74         }
75        super.handleResult(result, monitor);
76     }
77     
78     /**
79      * Find out whether this operation can be executed on
80      * the provided list of elements.
81      *
82      * @param elements a list of elements
83      * @param types an array of types for each element, that is,
84      * the type at position 'i' belongs to the selected element
85      * at position 'i'
86      *
87      * @return <code>true</code> if the operation can be
88      * executed on the provided list of elements, <code>
89      * false</code> otherwise.
90      * @throws JavaModelException
91      */

92     public boolean isValid(List JavaDoc elements, int[] types) throws JavaModelException {
93         if (elements.size() == 0)
94             return false;
95         for (int i= 0; i < elements.size(); i++) {
96             if (types[i] != DialogPackageExplorerActionGroup.ARCHIVE) {
97                 return false;
98             }
99         }
100         return true;
101     }
102         
103     /**
104      * Get a description for this operation. The description depends on
105      * the provided type parameter, which must be a constant of
106      * <code>DialogPackageExplorerActionGroup</code>. If the type is
107      * <code>DialogPackageExplorerActionGroup.MULTI</code>, then the
108      * description will be very general to describe the situation of
109      * all the different selected objects as good as possible.
110      *
111      * @param type the type of the selected object, must be a constant of
112      * <code>DialogPackageExplorerActionGroup</code>.
113      * @return a string describing the operation
114      */

115     public String JavaDoc getDescription(int type) {
116         Object JavaDoc obj= getSelectedElements().get(0);
117         String JavaDoc name= escapeSpecialChars(((IFile) obj).getName());
118         
119         if (type == DialogPackageExplorerActionGroup.ARCHIVE)
120             return Messages.format(NewWizardMessages.PackageExplorerActionGroup_FormText_ArchiveToBuildpath, name);
121         return NewWizardMessages.PackageExplorerActionGroup_FormText_Default_toBuildpath;
122     }
123 }
124
Popular Tags