KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ltk > core > refactoring > participants > ValidateEditChecker


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.ltk.core.refactoring.participants;
12
13 import java.util.Arrays JavaDoc;
14 import java.util.HashSet JavaDoc;
15 import java.util.Set JavaDoc;
16
17 import org.eclipse.core.runtime.Assert;
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IProgressMonitor;
20 import org.eclipse.core.runtime.IStatus;
21
22 import org.eclipse.core.resources.IFile;
23 import org.eclipse.core.resources.IResource;
24
25 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
26
27 import org.eclipse.ltk.internal.core.refactoring.RefactoringCoreMessages;
28 import org.eclipse.ltk.internal.core.refactoring.Resources;
29
30 /**
31  * A validate edit checker is a shared checker to collect files
32  * to be validated all at once. A validate edit checker checks
33  * if the files are in sync with the underlying files system.
34  * Additionally <code>IWorkspace#validateEdit</code> is called for
35  * all read-only resources.
36  * <p>
37  * Since 3.2 a {@link ResourceChangeChecker} exists. If clients
38  * add their changed files to the {@link ResourceChangeChecker}
39  * their is no need to add them to a validate edit checker as
40  * well. Files marked as changed in the resource operation checker
41  * will be automatically added to a validate edit checker (if one
42  * exists).
43  * </p>
44  * <p>
45  * Note: this class is not intended to be extended by clients.
46  * </p>
47  *
48  * @see org.eclipse.core.resources.IWorkspace#validateEdit(org.eclipse.core.resources.IFile[], java.lang.Object)
49  *
50  * @since 3.0
51  */

52 public class ValidateEditChecker implements IConditionChecker {
53
54     private Set JavaDoc fFiles= new HashSet JavaDoc();
55     private Object JavaDoc fContext;
56     
57     /**
58      * The context passed to the validate edit call.
59      *
60      * @param context the <code>org.eclipse.swt.widgets.Shell</code> that is
61      * to be used to parent any dialogs with the user, or <code>null</code> if
62      * there is no UI context (declared as an <code>Object</code> to avoid any
63      * direct references on the SWT component)
64      *
65      * @see org.eclipse.core.resources.IWorkspace#validateEdit(org.eclipse.core.resources.IFile[], java.lang.Object)
66      */

67     public ValidateEditChecker(Object JavaDoc context) {
68         fContext= context;
69     }
70     
71     /**
72      * Adds the given file to this checker.
73      *
74      * @param file the file to add
75      */

76     public void addFile(IFile file) {
77         Assert.isNotNull(file);
78         fFiles.add(file);
79     }
80     
81     /**
82      * Adds the given array of files.
83      *
84      * @param files the array of files to add
85      */

86     public void addFiles(IFile[] files) {
87         Assert.isNotNull(files);
88         fFiles.addAll(Arrays.asList(files));
89     }
90     
91     /**
92      * {@inheritDoc}
93      */

94     public RefactoringStatus check(IProgressMonitor monitor) throws CoreException {
95         IResource[] resources= (IResource[])fFiles.toArray(new IResource[fFiles.size()]);
96         RefactoringStatus result= new RefactoringStatus();
97         IStatus status= Resources.checkInSync(resources);
98         if (!status.isOK())
99             result.merge(RefactoringStatus.create(status));
100         status= Resources.makeCommittable(resources, fContext);
101         if (!status.isOK()) {
102             result.merge(RefactoringStatus.create(status));
103             if (!result.hasFatalError()) {
104                 result.addFatalError(RefactoringCoreMessages.ValidateEditChecker_failed);
105             }
106         }
107         return result;
108     }
109 }
110
Popular Tags