KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > changes > DynamicValidationStateChange


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.jdt.internal.corext.refactoring.changes;
12
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;
18
19 import org.eclipse.core.resources.IWorkspaceRunnable;
20 import org.eclipse.core.resources.ResourcesPlugin;
21
22 import org.eclipse.ltk.core.refactoring.Change;
23 import org.eclipse.ltk.core.refactoring.CompositeChange;
24 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
25
26 import org.eclipse.jdt.core.JavaCore;
27
28 import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages;
29
30 import org.eclipse.jdt.internal.ui.JavaPlugin;
31
32 //import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages;
33

34 public class DynamicValidationStateChange extends CompositeChange implements WorkspaceTracker.Listener {
35     
36     private boolean fListenerRegistered= false;
37     private RefactoringStatus fValidationState= null;
38     private long fTimeStamp;
39     private ISchedulingRule fSchedulingRule;
40     
41     // 30 minutes
42
private static final long LIFE_TIME= 30 * 60 * 1000;
43     
44     public DynamicValidationStateChange(Change change) {
45         super(change.getName());
46         add(change);
47         markAsSynthetic();
48         fSchedulingRule= ResourcesPlugin.getWorkspace().getRoot();
49     }
50     
51     public DynamicValidationStateChange(String JavaDoc name) {
52         super(name);
53         markAsSynthetic();
54         fSchedulingRule= ResourcesPlugin.getWorkspace().getRoot();
55     }
56     
57     public DynamicValidationStateChange(String JavaDoc name, Change[] changes) {
58         super(name, changes);
59         markAsSynthetic();
60         fSchedulingRule= ResourcesPlugin.getWorkspace().getRoot();
61     }
62     
63     /**
64      * {@inheritDoc}
65      */

66     public void initializeValidationData(IProgressMonitor pm) {
67         super.initializeValidationData(pm);
68         WorkspaceTracker.INSTANCE.addListener(this);
69         fListenerRegistered= true;
70         fTimeStamp= System.currentTimeMillis();
71     }
72     
73     public void dispose() {
74         if (fListenerRegistered) {
75             WorkspaceTracker.INSTANCE.removeListener(this);
76             fListenerRegistered= false;
77         }
78         super.dispose();
79     }
80     
81     /**
82      * {@inheritDoc}
83      */

84     public RefactoringStatus isValid(IProgressMonitor pm) throws CoreException {
85         if (fValidationState == null) {
86             return super.isValid(pm);
87         }
88         return fValidationState;
89     }
90     
91     /**
92      * {@inheritDoc}
93      */

94     public Change perform(IProgressMonitor pm) throws CoreException {
95         final Change[] result= new Change[1];
96         IWorkspaceRunnable runnable= new IWorkspaceRunnable() {
97             public void run(IProgressMonitor monitor) throws CoreException {
98                 result[0]= DynamicValidationStateChange.super.perform(monitor);
99             }
100         };
101         JavaCore.run(runnable, fSchedulingRule, pm);
102         return result[0];
103     }
104
105     /**
106      * {@inheritDoc}
107      */

108     protected Change createUndoChange(Change[] childUndos) {
109         DynamicValidationStateChange result= new DynamicValidationStateChange(getName());
110         for (int i= 0; i < childUndos.length; i++) {
111             result.add(childUndos[i]);
112         }
113         return result;
114     }
115     
116     public void workspaceChanged() {
117         long currentTime= System.currentTimeMillis();
118         if (currentTime - fTimeStamp < LIFE_TIME)
119             return;
120         fValidationState= RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.DynamicValidationStateChange_workspace_changed);
121         // remove listener from workspace tracker
122
WorkspaceTracker.INSTANCE.removeListener(this);
123         fListenerRegistered= false;
124         // clear up the children to not hang onto too much memory
125
Change[] children= clear();
126         for (int i= 0; i < children.length; i++) {
127             final Change change= children[i];
128             SafeRunner.run(new ISafeRunnable() {
129                 public void run() throws Exception JavaDoc {
130                     change.dispose();
131                 }
132                 public void handleException(Throwable JavaDoc exception) {
133                     JavaPlugin.log(exception);
134                 }
135             });
136         }
137     }
138     
139     public void setSchedulingRule(ISchedulingRule schedulingRule) {
140         fSchedulingRule= schedulingRule;
141     }
142     
143     public ISchedulingRule getSchedulingRule() {
144         return fSchedulingRule;
145     }
146 }
147
Popular Tags