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.corext.refactoring.changes;
011:
012: import org.eclipse.core.runtime.CoreException;
013: import org.eclipse.core.runtime.IProgressMonitor;
014: import org.eclipse.core.runtime.ISafeRunnable;
015: import org.eclipse.core.runtime.SafeRunner;
016: import org.eclipse.core.runtime.jobs.ISchedulingRule;
017:
018: import org.eclipse.core.resources.IWorkspaceRunnable;
019: import org.eclipse.core.resources.ResourcesPlugin;
020:
021: import org.eclipse.ltk.core.refactoring.Change;
022: import org.eclipse.ltk.core.refactoring.CompositeChange;
023: import org.eclipse.ltk.core.refactoring.RefactoringStatus;
024:
025: import org.eclipse.jdt.core.JavaCore;
026:
027: import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages;
028:
029: import org.eclipse.jdt.internal.ui.JavaPlugin;
030:
031: //import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages;
032:
033: public class DynamicValidationStateChange extends CompositeChange
034: implements WorkspaceTracker.Listener {
035:
036: private boolean fListenerRegistered = false;
037: private RefactoringStatus fValidationState = null;
038: private long fTimeStamp;
039: private ISchedulingRule fSchedulingRule;
040:
041: // 30 minutes
042: private static final long LIFE_TIME = 30 * 60 * 1000;
043:
044: public DynamicValidationStateChange(Change change) {
045: super (change.getName());
046: add(change);
047: markAsSynthetic();
048: fSchedulingRule = ResourcesPlugin.getWorkspace().getRoot();
049: }
050:
051: public DynamicValidationStateChange(String name) {
052: super (name);
053: markAsSynthetic();
054: fSchedulingRule = ResourcesPlugin.getWorkspace().getRoot();
055: }
056:
057: public DynamicValidationStateChange(String name, Change[] changes) {
058: super (name, changes);
059: markAsSynthetic();
060: fSchedulingRule = ResourcesPlugin.getWorkspace().getRoot();
061: }
062:
063: /**
064: * {@inheritDoc}
065: */
066: public void initializeValidationData(IProgressMonitor pm) {
067: super .initializeValidationData(pm);
068: WorkspaceTracker.INSTANCE.addListener(this );
069: fListenerRegistered = true;
070: fTimeStamp = System.currentTimeMillis();
071: }
072:
073: public void dispose() {
074: if (fListenerRegistered) {
075: WorkspaceTracker.INSTANCE.removeListener(this );
076: fListenerRegistered = false;
077: }
078: super .dispose();
079: }
080:
081: /**
082: * {@inheritDoc}
083: */
084: public RefactoringStatus isValid(IProgressMonitor pm)
085: throws CoreException {
086: if (fValidationState == null) {
087: return super .isValid(pm);
088: }
089: return fValidationState;
090: }
091:
092: /**
093: * {@inheritDoc}
094: */
095: public Change perform(IProgressMonitor pm) throws CoreException {
096: final Change[] result = new Change[1];
097: IWorkspaceRunnable runnable = new IWorkspaceRunnable() {
098: public void run(IProgressMonitor monitor)
099: throws CoreException {
100: result[0] = DynamicValidationStateChange.super
101: .perform(monitor);
102: }
103: };
104: JavaCore.run(runnable, fSchedulingRule, pm);
105: return result[0];
106: }
107:
108: /**
109: * {@inheritDoc}
110: */
111: protected Change createUndoChange(Change[] childUndos) {
112: DynamicValidationStateChange result = new DynamicValidationStateChange(
113: getName());
114: for (int i = 0; i < childUndos.length; i++) {
115: result.add(childUndos[i]);
116: }
117: return result;
118: }
119:
120: public void workspaceChanged() {
121: long currentTime = System.currentTimeMillis();
122: if (currentTime - fTimeStamp < LIFE_TIME)
123: return;
124: fValidationState = RefactoringStatus
125: .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 {
135: change.dispose();
136: }
137:
138: public void handleException(Throwable exception) {
139: JavaPlugin.log(exception);
140: }
141: });
142: }
143: }
144:
145: public void setSchedulingRule(ISchedulingRule schedulingRule) {
146: fSchedulingRule = schedulingRule;
147: }
148:
149: public ISchedulingRule getSchedulingRule() {
150: return fSchedulingRule;
151: }
152: }
|