]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-before/ui/org/eclipse/jdt/internal/ui/util/ElementValidator.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-before / ui / org / eclipse / jdt / internal / ui / util / ElementValidator.java
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
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;
14 import java.util.Set;
15
16 import org.eclipse.swt.widgets.Shell;
17
18 import org.eclipse.core.runtime.IAdaptable;
19 import org.eclipse.core.runtime.IStatus;
20
21 import org.eclipse.core.resources.IResource;
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 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 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 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 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          * @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
135          *
136          * @see #checkInSync(IAdaptable, Shell, String)
137          * @see #checkValidateEdit(IJavaElement, Shell, String)
138          */
139         public static boolean check(IJavaElement element, Shell parent, String title, boolean editor) {
140                 return check(new IJavaElement[] {element}, parent, title, editor);
141         }
142
143         /**
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.
150          *
151          * @param elements the elements to be checked
152          * @param parent a parent shell used to present a dialog to the user if the
153          * check fails
154          * @param title a dialog's title used to present a dialog to the user if the
155          * check fails
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
159          *
160          * @see #checkInSync(IAdaptable[], Shell, String)
161          * @see #checkValidateEdit(IJavaElement[], Shell, String)
162          */
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))
166                         return false;
167                 return checkValidateEdit(resources, parent, title);
168         }
169
170         private static boolean checkInSync(IResource[] resources, Shell parent, String title) {
171                 IStatus status= Resources.checkInSync(resources);
172                 if (status.isOK())
173                         return true;
174                 ErrorDialog.openError(parent, title,
175                         JavaUIMessages.ElementValidator_cannotPerform,
176                         status);
177                 return false;
178         }
179
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,
185                                 status);
186                         return false;
187                 }
188                 return true;
189         }
190
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);
199                                 if (cu != null) {
200                                         je= cu.getPrimary();
201                                 }
202                                 resource= je.getResource();
203                         } else {
204                                 resource= (IResource)element.getAdapter(IResource.class);
205                         }
206                         if (resource != null)
207                                 result.add(resource);
208                 }
209                 return result.toArray(new IResource[result.size()]);
210         }
211 }