001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.ui.util;
011:
012: import java.util.HashSet;
013: import java.util.Set;
014:
015: import org.eclipse.core.runtime.IAdaptable;
016: import org.eclipse.core.runtime.IStatus;
017:
018: import org.eclipse.core.resources.IResource;
019:
020: import org.eclipse.swt.widgets.Shell;
021:
022: import org.eclipse.jface.dialogs.ErrorDialog;
023:
024: import org.eclipse.jdt.core.ICompilationUnit;
025: import org.eclipse.jdt.core.IJavaElement;
026:
027: import org.eclipse.jdt.internal.corext.util.Resources;
028:
029: import org.eclipse.jdt.internal.ui.JavaUIMessages;
030:
031: /**
032: * Helper class to check if a set of <tt>IJavaElement</tt> objects can be
033: * modified by an operation.
034: *
035: * @since 2.1
036: */
037: public class ElementValidator {
038:
039: private ElementValidator() {
040: // no instance
041: }
042:
043: /**
044: * Checks if the given element is in sync with the underlying file system.
045: *
046: * @param element the element to be checked
047: * @param parent a parent shell used to present a dialog to the user if the
048: * element is not in sync
049: * @param title a dialog's title used to present a dialog to the user if the
050: * element is not in sync
051: * @return boolean <code>true</code> if the element is in sync with the file
052: * system. Otherwise <code>false</code> is returned
053: */
054: public static boolean checkInSync(IAdaptable element, Shell parent,
055: String title) {
056: return checkInSync(new IAdaptable[] { element }, parent, title);
057: }
058:
059: /**
060: * Checks if the given array of elements is in sync with the underlying file
061: * system.
062: *
063: * @param elements the array of elements to be checked
064: * @param parent a parent shell used to present a dialog to the user if
065: * one of the elements is not in sync
066: * @param title a dialog's title used to present a dialog to the user if
067: * one of the elements is not in sync
068: * @return boolean <code>true</code> if the all elements are in sync with
069: * the file system. Otherwise <code>false</code> is returned
070: */
071: public static boolean checkInSync(IAdaptable[] elements,
072: Shell parent, String title) {
073: return checkInSync(getResources(elements), parent, title);
074: }
075:
076: /**
077: * Checks if the given element is read-only and if so the methods tries
078: * to make the element writable by calling validate edit. If
079: * <code>validateEdit</code> was able to make the file writable the method
080: * additionally checks if the file has been changed by calling
081: * <code>validateEdit</code>.
082: *
083: * @param element the element to be checked
084: * @param parent a parent shell used to present a dialog to the user if the
085: * check fails
086: * @param title a dialog's title used to present a dialog to the user if the
087: * check fails
088: * @return boolean <code>true</code> if the element is writable and its
089: * content didn't change by calling <code>validateEdit</code>. Otherwise
090: * <code>false</code> is returned
091: *
092: * @see org.eclipse.core.resources.IWorkspace#validateEdit(org.eclipse.core.resources.IFile[], java.lang.Object)
093: */
094: public static boolean checkValidateEdit(IJavaElement element,
095: Shell parent, String title) {
096: return checkValidateEdit(new IJavaElement[] { element },
097: parent, title);
098: }
099:
100: /**
101: * Checks if the given elements are read-only and if so the methods tries to
102: * make the element writable by calling <code>validateEdit</code>. If
103: * <code>validateEdit</code> was able to make the file writable the method
104: * additionally checks if the file has been changed by calling
105: * <code>validateEdit</code>.
106: *
107: * @param elements the elements to be checked
108: * @param parent a parent shell used to present a dialog to the user if the
109: * check fails
110: * @param title a dialog's title used to present a dialog to the user if the
111: * check fails
112: * @return boolean <code>true</code> if all elements are writable and their
113: * content didn't change by calling <code>validateEdit</code>. Otherwise
114: * <code>false</code> is returned
115: *
116: * @see org.eclipse.core.resources.IWorkspace#validateEdit(org.eclipse.core.resources.IFile[], java.lang.Object)
117: */
118: public static boolean checkValidateEdit(IJavaElement[] elements,
119: Shell parent, String title) {
120: return checkValidateEdit(getResources(elements), parent, title);
121: }
122:
123: /**
124: * Checks a combination of <code>checkInSync</code> and
125: * <code>checkValidateEdit</code> depending of the value of
126: * <code>editor</code>. If <code>editor</code> is <code>true</code> only
127: * <code>checkValidateEdit</code> is performed since the editor does a in
128: * sync check on focus change. If <code>editor</code> is <code>false</code>
129: * both checks are performed.
130: *
131: * @param element the element to be checked
132: * @param parent a parent shell used to present a dialog to the user if the
133: * check fails
134: * @param title a dialog's title used to present a dialog to the user if the
135: * check fails
136: * @return boolean <code>true</code> if the element passed the checks.
137: * Otherwise <code>false</code> is returned
138: *
139: * @see #checkInSync(IAdaptable, Shell, String)
140: * @see #checkValidateEdit(IJavaElement, Shell, String)
141: */
142: public static boolean check(IJavaElement element, Shell parent,
143: String title, boolean editor) {
144: return check(new IJavaElement[] { element }, parent, title,
145: editor);
146: }
147:
148: /**
149: * Checks a combination of <code>checkInSync</code> and
150: * <code>checkValidateEdit</code> depending of the value of
151: * <code>editor</code>. If <code>editor</code> is <code>true</code> only
152: * <code>checkValidateEdit</code> is performed since the editor does a in
153: * sync check on focus change. If <code>editor</code> is <code>false</code>
154: * both checks are performed.
155: *
156: * @param elements the elements to be checked
157: * @param parent a parent shell used to present a dialog to the user if the
158: * check fails
159: * @param title a dialog's title used to present a dialog to the user if the
160: * check fails
161: * @return boolean <code>true</code> if all elements pass the checks.
162: * Otherwise <code>false</code> is returned
163: *
164: * @see #checkInSync(IAdaptable[], Shell, String)
165: * @see #checkValidateEdit(IJavaElement[], Shell, String)
166: */
167: public static boolean check(IJavaElement[] elements, Shell parent,
168: String title, boolean editor) {
169: IResource[] resources = getResources(elements);
170: if (!editor && !checkInSync(resources, parent, title))
171: return false;
172: return checkValidateEdit(resources, parent, title);
173: }
174:
175: private static boolean checkInSync(IResource[] resources,
176: Shell parent, String title) {
177: IStatus status = Resources.checkInSync(resources);
178: if (status.isOK())
179: return true;
180: ErrorDialog.openError(parent, title,
181: JavaUIMessages.ElementValidator_cannotPerform, status);
182: return false;
183: }
184:
185: private static boolean checkValidateEdit(IResource[] resources,
186: Shell parent, String title) {
187: IStatus status = Resources.makeCommittable(resources, parent);
188: if (!status.isOK()) {
189: ErrorDialog.openError(parent, title,
190: JavaUIMessages.ElementValidator_cannotPerform,
191: status);
192: return false;
193: }
194: return true;
195: }
196:
197: private static IResource[] getResources(IAdaptable[] elements) {
198: Set result = new HashSet();
199: for (int i = 0; i < elements.length; i++) {
200: IAdaptable element = elements[i];
201: IResource resource = null;
202: if (element instanceof IJavaElement) {
203: IJavaElement je = (IJavaElement) element;
204: ICompilationUnit cu = (ICompilationUnit) je
205: .getAncestor(IJavaElement.COMPILATION_UNIT);
206: if (cu != null) {
207: je = cu.getPrimary();
208: }
209: resource = je.getResource();
210: } else {
211: resource = (IResource) element
212: .getAdapter(IResource.class);
213: }
214: if (resource != null)
215: result.add(resource);
216: }
217: return (IResource[]) result
218: .toArray(new IResource[result.size()]);
219: }
220: }
|