KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > util > ElementValidator


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 package org.eclipse.jdt.internal.ui.util;
12
13 import java.util.HashSet JavaDoc;
14 import java.util.Set JavaDoc;
15
16 import org.eclipse.core.runtime.IAdaptable;
17 import org.eclipse.core.runtime.IStatus;
18
19 import org.eclipse.core.resources.IResource;
20
21 import org.eclipse.swt.widgets.Shell;
22
23 import org.eclipse.jface.dialogs.ErrorDialog;
24
25 import org.eclipse.jdt.core.ICompilationUnit;
26 import org.eclipse.jdt.core.IJavaElement;
27
28 import org.eclipse.jdt.internal.corext.util.Resources;
29
30 import org.eclipse.jdt.internal.ui.JavaUIMessages;
31
32 /**
33  * Helper class to check if a set of <tt>IJavaElement</tt> objects can be
34  * modified by an operation.
35  *
36  * @since 2.1
37  */

38 public class ElementValidator {
39
40     private ElementValidator() {
41         // no instance
42
}
43
44     /**
45      * Checks if the given element is in sync with the underlying file system.
46      *
47      * @param element the element to be checked
48      * @param parent a parent shell used to present a dialog to the user if the
49      * element is not in sync
50      * @param title a dialog's title used to present a dialog to the user if the
51      * element is not in sync
52      * @return boolean <code>true</code> if the element is in sync with the file
53      * system. Otherwise <code>false</code> is returned
54      */

55     public static boolean checkInSync(IAdaptable element, Shell parent,String JavaDoc title) {
56         return checkInSync(new IAdaptable[] {element}, parent, title);
57     }
58     
59     /**
60      * Checks if the given array of elements is in sync with the underlying file
61      * system.
62      *
63      * @param elements the array of elements to be checked
64      * @param parent a parent shell used to present a dialog to the user if
65      * one of the elements is not in sync
66      * @param title a dialog's title used to present a dialog to the user if
67      * one of the elements is not in sync
68      * @return boolean <code>true</code> if the all elements are in sync with
69      * the file system. Otherwise <code>false</code> is returned
70      */

71     public static boolean checkInSync(IAdaptable[] elements, Shell parent, String JavaDoc title) {
72         return checkInSync(getResources(elements), parent, title);
73     }
74     
75     /**
76      * Checks if the given element is read-only and if so the methods tries
77      * to make the element writable by calling validate edit. If
78      * <code>validateEdit</code> was able to make the file writable the method
79      * additionally checks if the file has been changed by calling
80      * <code>validateEdit</code>.
81      *
82      * @param element the element to be checked
83      * @param parent a parent shell used to present a dialog to the user if the
84      * check fails
85      * @param title a dialog's title used to present a dialog to the user if the
86      * check fails
87      * @return boolean <code>true</code> if the element is writable and its
88      * content didn't change by calling <code>validateEdit</code>. Otherwise
89      * <code>false</code> is returned
90      *
91      * @see org.eclipse.core.resources.IWorkspace#validateEdit(org.eclipse.core.resources.IFile[], java.lang.Object)
92      */

93     public static boolean checkValidateEdit(IJavaElement element, Shell parent, String JavaDoc title) {
94         return checkValidateEdit(new IJavaElement[] {element}, parent, title);
95     }
96     
97     /**
98      * Checks if the given elements are read-only and if so the methods tries to
99      * make the element writable by calling <code>validateEdit</code>. If
100      * <code>validateEdit</code> was able to make the file writable the method
101      * additionally checks if the file has been changed by calling
102      * <code>validateEdit</code>.
103      *
104      * @param elements the elements to be checked
105      * @param parent a parent shell used to present a dialog to the user if the
106      * check fails
107      * @param title a dialog's title used to present a dialog to the user if the
108      * check fails
109      * @return boolean <code>true</code> if all elements are writable and their
110      * content didn't change by calling <code>validateEdit</code>. Otherwise
111      * <code>false</code> is returned
112      *
113      * @see org.eclipse.core.resources.IWorkspace#validateEdit(org.eclipse.core.resources.IFile[], java.lang.Object)
114      */

115     public static boolean checkValidateEdit(IJavaElement[] elements, Shell parent, String JavaDoc title) {
116         return checkValidateEdit(getResources(elements), parent, title);
117     }
118     
119     /**
120      * Checks a combination of <code>checkInSync</code> and
121      * <code>checkValidateEdit</code> depending of the value of
122      * <code>editor</code>. If <code>editor</code> is <code>true</code> only
123      * <code>checkValidateEdit</code> is performed since the editor does a in
124      * sync check on focus change. If <code>editor</code> is <code>false</code>
125      * both checks are performed.
126      *
127      * @param element the element to be checked
128      * @param parent a parent shell used to present a dialog to the user if the
129      * check fails
130      * @param title a dialog's title used to present a dialog to the user if the
131      * check fails
132      * @return boolean <code>true</code> if the element passed the checks.
133      * Otherwise <code>false</code> is returned
134      *
135      * @see #checkInSync(IAdaptable, Shell, String)
136      * @see #checkValidateEdit(IJavaElement, Shell, String)
137      */

138     public static boolean check(IJavaElement element, Shell parent, String JavaDoc title, boolean editor) {
139         return check(new IJavaElement[] {element}, parent, title, editor);
140     }
141     
142     /**
143      * Checks a combination of <code>checkInSync</code> and
144      * <code>checkValidateEdit</code> depending of the value of
145      * <code>editor</code>. If <code>editor</code> is <code>true</code> only
146      * <code>checkValidateEdit</code> is performed since the editor does a in
147      * sync check on focus change. If <code>editor</code> is <code>false</code>
148      * both checks are performed.
149      *
150      * @param elements the elements to be checked
151      * @param parent a parent shell used to present a dialog to the user if the
152      * check fails
153      * @param title a dialog's title used to present a dialog to the user if the
154      * check fails
155      * @return boolean <code>true</code> if all elements pass the checks.
156      * Otherwise <code>false</code> is returned
157      *
158      * @see #checkInSync(IAdaptable[], Shell, String)
159      * @see #checkValidateEdit(IJavaElement[], Shell, String)
160      */

161     public static boolean check(IJavaElement[] elements, Shell parent,String JavaDoc title, boolean editor) {
162         IResource[] resources= getResources(elements);
163         if (!editor && !checkInSync(resources, parent, title))
164             return false;
165         return checkValidateEdit(resources, parent, title);
166     }
167
168     private static boolean checkInSync(IResource[] resources, Shell parent, String JavaDoc title) {
169         IStatus status= Resources.checkInSync(resources);
170         if (status.isOK())
171             return true;
172         ErrorDialog.openError(parent, title,
173             JavaUIMessages.ElementValidator_cannotPerform,
174             status);
175         return false;
176     }
177
178     private static boolean checkValidateEdit(IResource[] resources, Shell parent, String JavaDoc title) {
179         IStatus status= Resources.makeCommittable(resources, parent);
180         if (!status.isOK()) {
181             ErrorDialog.openError(parent, title,
182                 JavaUIMessages.ElementValidator_cannotPerform,
183                 status);
184             return false;
185         }
186         return true;
187     }
188     
189     private static IResource[] getResources(IAdaptable[] elements) {
190         Set JavaDoc result= new HashSet JavaDoc();
191         for (int i= 0; i < elements.length; i++) {
192             IAdaptable element= elements[i];
193             IResource resource= null;
194             if (element instanceof IJavaElement) {
195                 IJavaElement je= (IJavaElement)element;
196                 ICompilationUnit cu= (ICompilationUnit)je.getAncestor(IJavaElement.COMPILATION_UNIT);
197                 if (cu != null) {
198                     je= cu.getPrimary();
199                 }
200                 resource= je.getResource();
201             } else {
202                 resource= (IResource)element.getAdapter(IResource.class);
203             }
204             if (resource != null)
205                 result.add(resource);
206         }
207         return (IResource[]) result.toArray(new IResource[result.size()]);
208     }
209 }
210
Popular Tags