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.corext.refactoring.changes;
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.core.runtime.IProgressMonitor;
15 import org.eclipse.core.runtime.ISafeRunnable;
16 import org.eclipse.core.runtime.SafeRunner;
17 import org.eclipse.core.runtime.jobs.ISchedulingRule;
19 import org.eclipse.core.resources.IWorkspaceRunnable;
20 import org.eclipse.core.resources.ResourcesPlugin;
22 import org.eclipse.ltk.core.refactoring.Change;
23 import org.eclipse.ltk.core.refactoring.CompositeChange;
24 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
26 import org.eclipse.jdt.core.JavaCore;
28 import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages;
30 import org.eclipse.jdt.internal.ui.JavaPlugin;
32 //import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages;
34 public class DynamicValidationStateChange extends CompositeChange implements WorkspaceTracker.Listener {
36 private boolean fListenerRegistered= false;
37 private RefactoringStatus fValidationState= null;
38 private long fTimeStamp;
39 private ISchedulingRule fSchedulingRule;
42 private static final long LIFE_TIME= 30 * 60 * 1000;
44 public DynamicValidationStateChange(Change change) {
45 super(change.getName());
48 fSchedulingRule= ResourcesPlugin.getWorkspace().getRoot();
51 public DynamicValidationStateChange(String name) {
54 fSchedulingRule= ResourcesPlugin.getWorkspace().getRoot();
57 public DynamicValidationStateChange(String name, Change[] changes) {
60 fSchedulingRule= ResourcesPlugin.getWorkspace().getRoot();
67 public void initializeValidationData(IProgressMonitor pm) {
68 super.initializeValidationData(pm);
69 WorkspaceTracker.INSTANCE.addListener(this);
70 fListenerRegistered= true;
71 fTimeStamp= System.currentTimeMillis();
75 public void dispose() {
76 if (fListenerRegistered) {
77 WorkspaceTracker.INSTANCE.removeListener(this);
78 fListenerRegistered= false;
87 public RefactoringStatus isValid(IProgressMonitor pm) throws CoreException {
88 if (fValidationState == null) {
89 return super.isValid(pm);
91 return fValidationState;
98 public Change perform(IProgressMonitor pm) throws CoreException {
99 final Change[] result= new Change[1];
100 IWorkspaceRunnable runnable= new IWorkspaceRunnable() {
101 public void run(IProgressMonitor monitor) throws CoreException {
102 result[0]= DynamicValidationStateChange.super.perform(monitor);
105 JavaCore.run(runnable, fSchedulingRule, pm);
113 protected Change createUndoChange(Change[] childUndos) {
114 DynamicValidationStateChange result= new DynamicValidationStateChange(getName());
115 for (int i= 0; i < childUndos.length; i++) {
116 result.add(childUndos[i]);
121 public void workspaceChanged() {
122 long currentTime= System.currentTimeMillis();
123 if (currentTime - fTimeStamp < LIFE_TIME)
125 fValidationState= RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.DynamicValidationStateChange_workspace_changed);
126 // remove listener from workspace tracker
127 WorkspaceTracker.INSTANCE.removeListener(this);
128 fListenerRegistered= false;
129 // clear up the children to not hang onto too much memory
130 Change[] children= clear();
131 for (int i= 0; i < children.length; i++) {
132 final Change change= children[i];
133 SafeRunner.run(new ISafeRunnable() {
134 public void run() throws Exception {
137 public void handleException(Throwable exception) {
138 JavaPlugin.log(exception);
144 public void setSchedulingRule(ISchedulingRule schedulingRule) {
145 fSchedulingRule= schedulingRule;
148 public ISchedulingRule getSchedulingRule() {
149 return fSchedulingRule;