KickJava   Java API By Example, From Geeks To Geeks.

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


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
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.jface.operation.IRunnableWithProgress;
21
22 import org.eclipse.jdt.core.JavaModelException;
23
24 import org.eclipse.jdt.internal.ui.wizards.buildpaths.newsourcepage.DialogPackageExplorerActionGroup;
25
26
27 /**
28  * Abstract class which represents classpath modifier operation, this is,
29  * Operation that call methods on <code>ClasspathModifier</code>.
30  */

31 public abstract class ClasspathModifierOperation extends ClasspathModifier implements IRunnableWithProgress {
32     protected IClasspathInformationProvider fInformationProvider;
33     protected CoreException fException;
34     private int fType;
35     /**
36      * A human readable name for this operation
37      */

38     private String JavaDoc fName;
39     
40     /**
41      * Constructor
42      *
43      * @param listener a <code>IClasspathModifierListener</code> that is notified about
44      * changes on classpath entries or <code>null</code> if no such notification is
45      * necessary.
46      * @param informationProvider a provider to offer information to the operation
47      * @param name a human readable name for this operation
48      * @param type the type of the operation, that is a constant of <code>
49      * IClasspathInformationProvider</code>
50      *
51      * @see IClasspathInformationProvider
52      * @see ClasspathModifier
53      */

54     public ClasspathModifierOperation(IClasspathModifierListener listener, IClasspathInformationProvider informationProvider, String JavaDoc name, int type) {
55         super(listener);
56         fInformationProvider= informationProvider;
57         fException= null;
58         fName= name;
59         fType= type;
60     }
61     
62     protected void handleResult(List JavaDoc result, IProgressMonitor monitor) throws InvocationTargetException JavaDoc{
63         /*
64          * if (fMonitor != null && fException != null) then
65          * the action was called with the run method of
66          * the IRunnableWithProgress which will throw an
67          * InvocationTargetException in the case that an
68          * exception ocurred. Then error handling is
69          * done by the client which called run(monitor).
70          *
71          * Otherwise we pass the information back to the
72          * information provider.
73          */

74         if (monitor == null || fException == null)
75             fInformationProvider.handleResult(result, fException, fType);
76         else
77             throw new InvocationTargetException JavaDoc(fException);
78         fException= null;
79     }
80     
81     /**
82      * Method which runs the actions with a progress monitor.<br>
83      *
84      * @param monitor a progress monitor, can be <code>null</code>
85      */

86     public abstract void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc;
87     
88     /**
89      * Get the type converted into a string.
90      *
91      * @return the ID (that is the type) of this operation as string.
92      */

93     public String JavaDoc getId() {
94         return Integer.toString(fType);
95     }
96     
97     /**
98      * Find out whether this operation can be executed on
99      * the provided list of elements.
100      *
101      * @param elements a list of elements
102      * @param types an array of types for each element, that is,
103      * the type at position 'i' belongs to the selected element
104      * at position 'i'
105      *
106      * @return <code>true</code> if the operation can be
107      * executed on the provided list of elements, <code>
108      * false</code> otherwise.
109      *
110      * @throws JavaModelException
111      */

112     public abstract boolean isValid(List JavaDoc elements, int[] types) throws JavaModelException;
113     
114     /**
115      * Get a description for this operation. The description depends on
116      * the provided type parameter, which must be a constant of
117      * <code>DialogPackageExplorerActionGroup</code>. If the type is
118      * <code>DialogPackageExplorerActionGroup.MULTI</code>, then the
119      * description will be very general to describe the situation of
120      * all the different selected objects as good as possible.
121      *
122      * @param type the type of the selected object, must be a constant of
123      * <code>DialogPackageExplorerActionGroup</code>.
124      * @return a string describing the operation.
125      */

126     public abstract String JavaDoc getDescription(int type);
127     
128     public String JavaDoc getName() {
129         return fName;
130     }
131     
132     public List JavaDoc getSelectedElements() {
133         return fInformationProvider.getSelection().toList();
134     }
135     
136     public int getTypeId() {
137         return fType;
138     }
139
140     public boolean isValid() throws JavaModelException {
141         List JavaDoc selectedElements= getSelectedElements();
142         int[] types= new int[selectedElements.size()];
143         for(int i= 0; i < types.length; i++) {
144             types[i]= DialogPackageExplorerActionGroup.getType(selectedElements.get(i), fInformationProvider.getJavaProject());
145         }
146         return isValid(selectedElements, types);
147     }
148 }
149
Popular Tags