]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-before/core refactoring/org/eclipse/jdt/internal/corext/refactoring/changes/DynamicValidationStateChange.java
a73419c0f90759515ee29a5bde03f030abc2a6c0
[ifi-stolz-refaktor.git] / case-study / jdt-before / core refactoring / org / eclipse / jdt / internal / corext / refactoring / changes / DynamicValidationStateChange.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.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 name) {
52                 super(name);
53                 markAsSynthetic();
54                 fSchedulingRule= ResourcesPlugin.getWorkspace().getRoot();
55         }
56
57         public DynamicValidationStateChange(String name, Change[] changes) {
58                 super(name, changes);
59                 markAsSynthetic();
60                 fSchedulingRule= ResourcesPlugin.getWorkspace().getRoot();
61         }
62
63         /**
64          * {@inheritDoc}
65          */
66         @Override
67         public void initializeValidationData(IProgressMonitor pm) {
68                 super.initializeValidationData(pm);
69                 WorkspaceTracker.INSTANCE.addListener(this);
70                 fListenerRegistered= true;
71                 fTimeStamp= System.currentTimeMillis();
72         }
73
74         @Override
75         public void dispose() {
76                 if (fListenerRegistered) {
77                         WorkspaceTracker.INSTANCE.removeListener(this);
78                         fListenerRegistered= false;
79                 }
80                 super.dispose();
81         }
82
83         /**
84          * {@inheritDoc}
85          */
86         @Override
87         public RefactoringStatus isValid(IProgressMonitor pm) throws CoreException {
88                 if (fValidationState == null) {
89                         return super.isValid(pm);
90                 }
91                 return fValidationState;
92         }
93
94         /**
95          * {@inheritDoc}
96          */
97         @Override
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);
103                         }
104                 };
105                 JavaCore.run(runnable, fSchedulingRule, pm);
106                 return result[0];
107         }
108
109         /**
110          * {@inheritDoc}
111          */
112         @Override
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]);
117                 }
118                 return result;
119         }
120
121         public void workspaceChanged() {
122                 long currentTime= System.currentTimeMillis();
123                 if (currentTime - fTimeStamp < LIFE_TIME)
124                         return;
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 {
135                                         change.dispose();
136                                 }
137                                 public void handleException(Throwable exception) {
138                                         JavaPlugin.log(exception);
139                                 }
140                         });
141                 }
142         }
143
144         public void setSchedulingRule(ISchedulingRule schedulingRule) {
145                 fSchedulingRule= schedulingRule;
146         }
147
148         public ISchedulingRule getSchedulingRule() {
149                 return fSchedulingRule;
150         }
151 }