1 /*******************************************************************************
2 * Copyright (c) 2000, 2011 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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.ui.util;
13 import java.util.HashSet;
16 import org.eclipse.swt.widgets.Shell;
18 import org.eclipse.core.runtime.IAdaptable;
19 import org.eclipse.core.runtime.IStatus;
21 import org.eclipse.core.resources.IResource;
23 import org.eclipse.jface.dialogs.ErrorDialog;
25 import org.eclipse.jdt.core.ICompilationUnit;
26 import org.eclipse.jdt.core.IJavaElement;
28 import org.eclipse.jdt.internal.corext.util.Resources;
30 import org.eclipse.jdt.internal.ui.JavaUIMessages;
33 * Helper class to check if a set of <tt>IJavaElement</tt> objects can be
34 * modified by an operation.
38 public class ElementValidator {
40 private ElementValidator() {
45 * Checks if the given element is in sync with the underlying file system.
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
55 public static boolean checkInSync(IAdaptable element, Shell parent,String title) {
56 return checkInSync(new IAdaptable[] {element}, parent, title);
60 * Checks if the given array of elements is in sync with the underlying file
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
71 public static boolean checkInSync(IAdaptable[] elements, Shell parent, String title) {
72 return checkInSync(getResources(elements), parent, title);
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>.
82 * @param element the element to be checked
83 * @param parent a parent shell used to present a dialog to the user if the
85 * @param title a dialog's title used to present a dialog to the user if the
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
91 * @see org.eclipse.core.resources.IWorkspace#validateEdit(org.eclipse.core.resources.IFile[], java.lang.Object)
93 public static boolean checkValidateEdit(IJavaElement element, Shell parent, String title) {
94 return checkValidateEdit(new IJavaElement[] {element}, parent, title);
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>.
104 * @param elements the elements to be checked
105 * @param parent a parent shell used to present a dialog to the user if the
107 * @param title a dialog's title used to present a dialog to the user if the
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
113 * @see org.eclipse.core.resources.IWorkspace#validateEdit(org.eclipse.core.resources.IFile[], java.lang.Object)
115 public static boolean checkValidateEdit(IJavaElement[] elements, Shell parent, String title) {
116 return checkValidateEdit(getResources(elements), parent, title);
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.
127 * @param element the element to be checked
128 * @param parent a parent shell used to present a dialog to the user if the
130 * @param title a dialog's title used to present a dialog to the user if the
132 * @param editor specifies if we are in the editor
133 * @return boolean <code>true</code> if the element passed the checks.
134 * Otherwise <code>false</code> is returned
136 * @see #checkInSync(IAdaptable, Shell, String)
137 * @see #checkValidateEdit(IJavaElement, Shell, String)
139 public static boolean check(IJavaElement element, Shell parent, String title, boolean editor) {
140 return check(new IJavaElement[] {element}, parent, title, editor);
144 * Checks a combination of <code>checkInSync</code> and
145 * <code>checkValidateEdit</code> depending of the value of
146 * <code>editor</code>. If <code>editor</code> is <code>true</code> only
147 * <code>checkValidateEdit</code> is performed since the editor does a in
148 * sync check on focus change. If <code>editor</code> is <code>false</code>
149 * both checks are performed.
151 * @param elements the elements to be checked
152 * @param parent a parent shell used to present a dialog to the user if the
154 * @param title a dialog's title used to present a dialog to the user if the
156 * @param editor specifies if we are in the editor
157 * @return boolean <code>true</code> if all elements pass the checks.
158 * Otherwise <code>false</code> is returned
160 * @see #checkInSync(IAdaptable[], Shell, String)
161 * @see #checkValidateEdit(IJavaElement[], Shell, String)
163 public static boolean check(IJavaElement[] elements, Shell parent,String title, boolean editor) {
164 IResource[] resources= getResources(elements);
165 if (!editor && !checkInSync(resources, parent, title))
167 return checkValidateEdit(resources, parent, title);
170 private static boolean checkInSync(IResource[] resources, Shell parent, String title) {
171 IStatus status= Resources.checkInSync(resources);
174 ErrorDialog.openError(parent, title,
175 JavaUIMessages.ElementValidator_cannotPerform,
180 private static boolean checkValidateEdit(IResource[] resources, Shell parent, String title) {
181 IStatus status= Resources.makeCommittable(resources, parent);
182 if (!status.isOK()) {
183 ErrorDialog.openError(parent, title,
184 JavaUIMessages.ElementValidator_cannotPerform,
191 private static IResource[] getResources(IAdaptable[] elements) {
192 Set<IResource> result= new HashSet<IResource>();
193 for (int i= 0; i < elements.length; i++) {
194 IAdaptable element= elements[i];
195 IResource resource= null;
196 if (element instanceof IJavaElement) {
197 IJavaElement je= (IJavaElement)element;
198 ICompilationUnit cu= (ICompilationUnit)je.getAncestor(IJavaElement.COMPILATION_UNIT);
202 resource= je.getResource();
204 resource= (IResource)element.getAdapter(IResource.class);
206 if (resource != null)
207 result.add(resource);
209 return result.toArray(new IResource[result.size()]);