KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > refactoring > ui > j > api > AbstractRefactoring


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.xml.refactoring.ui.j.api;
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import org.openide.ErrorManager;
27 import org.openide.filesystems.FileObject;
28 import org.openide.filesystems.FileUtil;
29 import org.openide.loaders.DataObject;
30 import org.openide.loaders.DataObjectNotFoundException;
31 import org.openide.util.Lookup;
32
33
34 /**
35  * Abstract superclass for particular refactorings.
36  * Methods should be typically called in following order:
37  * <ul>
38  * <li>preCheck
39  * <li>setParameter1(..), setParameter2(..) (this methods will typically be added by the subclass
40  * to allow parametrization of the implemented refactoring)
41  * <li>fastCheckParameters() (performs only fast check - useful for online error checking)
42  * <li>checkParameters() (full check of parameters)
43  * <li>prepare() (collects usages)
44  * </ul>
45  * @author Martin Matula, Jan Becicka
46  */

47 public abstract class AbstractRefactoring {
48
49 // static {
50
// APIAccessor.DEFAULT = new AccessorImpl();
51
// }
52

53     /**
54      * Initial state
55      */

56     public static final int INIT = 0;
57     /** Pre-check state. */
58     public static final int PRE_CHECK = 1;
59     /** Parameters check state. */
60     public static final int PARAMETERS_CHECK = 2;
61     /** Prepare state. */
62     public static final int PREPARE = 3;
63     
64 // private int currentState = INIT;
65

66     private static final int PLUGIN_STEPS = 30;
67     
68     private ArrayList JavaDoc plugins;
69     
70     private ArrayList JavaDoc pluginsWithProgress;
71     
72 // private ArrayList gbHandlers;
73

74 // private ProgressListener progressListener = new ProgressL();
75

76 // private ProgressSupport progressSupport;
77
// AbstractRefactoring caller;
78

79     private Collection JavaDoc getPlugins() {
80 // if (plugins == null) {
81
// plugins = new ArrayList();
82
// // get plugins from the lookup
83
// Lookup.Result result = Lookup.getDefault().lookup(new Lookup.Template(RefactoringPluginFactory.class));
84
// for (Iterator it = result.allInstances().iterator(); it.hasNext();) {
85
// RefactoringPluginFactory factory = (RefactoringPluginFactory) it.next();
86
// RefactoringPlugin plugin = factory.createInstance(this);
87
// if (plugin != null) {
88
// if (caller == null || factory.getClass().getClassLoader().equals(getClass().getClassLoader()) || factory.createInstance(caller)==null) {
89
// //caller is internal non-api field. Plugin is always added for API calls.
90
// //For non-api internal calls:
91
// // Plugins from different modules are ignored,
92
// // if factory for the caller return non-null plugin.
93
// // This quite hacky method is used for SafeDeleteRefactoring:
94
// // SafeDeleteRefactorinPlugin uses WhereUsedQuery internally.
95
// // If some module implements both plugins (SafeDelete and WhereUsed),
96
// // WhereUsedRefactoringPlugin should be ignored, because whole process will be handled by
97
// // SafeDeleteRefactoringPlugin.
98
// // #65980
99
// plugins.add(plugin);
100
// }
101
// }
102
// }
103
// }
104
return plugins;
105     }
106     
107 // Collection getGBHandlers() {
108
// if (gbHandlers == null) {
109
// gbHandlers = new ArrayList();
110
// // get plugins from the lookup
111
// Lookup.Result result = Lookup.getDefault().lookup(new Lookup.Template(GuardedBlockHandlerFactory.class));
112
// for (Iterator it = result.allInstances().iterator(); it.hasNext();) {
113
// GuardedBlockHandler handler = ((GuardedBlockHandlerFactory) it.next()).createInstance(this);
114
// if (handler != null) gbHandlers.add(handler);
115
// }
116
// }
117
// return gbHandlers;
118
// }
119

120     /** Perform checks to ensure that the preconditions are met for the implemented
121      * refactoring.
122      * @return Chain of problems encountered or <code>null</code> if no problems
123      * were found.
124      */

125     public final Problem preCheck() {
126 // JavaModel.getJavaRepository().beginTrans(true);
127
// try {
128
// //workaround for #68803
129
// if (!(this instanceof WhereUsedQuery)) {
130
// if (progressSupport != null)
131
// progressSupport.fireProgressListenerStart(this, ProgressEvent.START, -1);
132
// setCP();
133
// if (progressSupport != null)
134
// progressSupport.fireProgressListenerStop(this);
135
// }
136
// currentState = PRE_CHECK;
137
// return pluginsPreCheck(null);
138
// } finally {
139
// JavaModel.getJavaRepository().endTrans();
140
// }
141
return null;
142     }
143     
144     /** Collects and returns a set of refactoring elements - objects that
145      * will be affected by the refactoring.
146      * @param session RefactoringSession that the operation will use to return
147      * instances of {@link org.netbeans.modules.refactoring.api.RefactoringElement} class representing objects that
148      * will be affected by the refactoring.
149      * @return Chain of problems encountered or <code>null</code> in no problems
150      * were found.
151      */

152     public final Problem prepare(RefactoringSession session) {
153 // JavaModel.getJavaRepository().beginTrans(true);
154
// try {
155
// setCP();
156
// Problem p = null;
157
// if (currentState < PARAMETERS_CHECK) {
158
// p = checkParameters();
159
// }
160
// if (p != null && p.isFatal())
161
// return p;
162
//
163
// p = pluginsPrepare(p, session);
164
//
165
// return p;
166
// } finally {
167
// JavaModel.getJavaRepository().endTrans();
168
// }
169
return null;
170     }
171     
172     /**
173      * Checks if this refactoring has correctly set all parameters.
174      * @return Returns instancef Problem or null
175      */

176     public final Problem checkParameters() {
177 // JavaModel.getJavaRepository().beginTrans(true);
178
// try {
179
// //workaround for #68803
180
// if (this instanceof WhereUsedQuery) {
181
// if (progressSupport != null)
182
// progressSupport.fireProgressListenerStart(this, ProgressEvent.START, -1);
183
// setCP();
184
// if (progressSupport != null)
185
// progressSupport.fireProgressListenerStop(this);
186
// } else {
187
// setCP();
188
// }
189
// Problem p = fastCheckParameters();
190
// if (p != null && p.isFatal())
191
// return p;
192
// currentState = PARAMETERS_CHECK;
193
// return pluginsCheckParams(p);
194
// } finally {
195
// JavaModel.getJavaRepository().endTrans();
196
// }
197
return null;
198     }
199     
200     /**
201      * This method checks parameters. Its implementation is fast and allows on-line checking of errors.
202      * If you want complete check of parameters, use #checkParameters()
203      * @return Returns instance of Problem or null
204      */

205     public final Problem fastCheckParameters() {
206 // JavaModel.getJavaRepository().beginTrans(true);
207
// try {
208
// // Do not set classpath - use default merged class path
209
// // #57558
210
// // setCP();
211
// Problem p = null;
212
// if (currentState < PRE_CHECK) {
213
// p = preCheck();
214
// }
215
// if (p != null && p.isFatal())
216
// return p;
217
// return pluginsFastCheckParams(p);
218
// } finally {
219
// JavaModel.getJavaRepository().endTrans();
220
// }
221
return null;
222     }
223     
224     private void setCP() {
225 // if (JMManager.getTransactionMutex().getClassPath() == null) {
226
// setClassPath();
227
// }
228
}
229     /** Registers ProgressListener to receive events.
230      * @param listener The listener to register.
231      *
232      */

233     public final synchronized void addProgressListener(ProgressListener listener) {
234 // if (progressSupport == null ) {
235
// progressSupport = new ProgressSupport();
236
// }
237
// progressSupport.addProgressListener(listener);
238
//
239
// if (pluginsWithProgress == null) {
240
// pluginsWithProgress = new ArrayList();
241
// Iterator pIt=getPlugins().iterator();
242
// while(pIt.hasNext()) {
243
// RefactoringPlugin plugin=(RefactoringPlugin)pIt.next();
244
// if (plugin instanceof ProgressProvider) {
245
// ((ProgressProvider) plugin).addProgressListener(progressListener);
246
// pluginsWithProgress.add(plugin);
247
// }
248
// }
249
// }
250
}
251     
252     /** Removes ProgressListener from the list of listeners.
253      * @param listener The listener to remove.
254      *
255      */

256     public final synchronized void removeProgressListener(ProgressListener listener) {
257 // if (progressSupport != null ) {
258
// progressSupport.removeProgressListener(listener);
259
// }
260
//
261
// if (pluginsWithProgress != null) {
262
// Iterator pIt=pluginsWithProgress.iterator();
263
//
264
// while(pIt.hasNext()) {
265
// ProgressProvider plugin=(ProgressProvider)pIt.next();
266
// plugin.removeProgressListener(progressListener);
267
// }
268
// pluginsWithProgress.clear();
269
// pluginsWithProgress = null;
270
// }
271
}
272     
273     /**
274      * Sets scope of this refactoring
275      */

276     protected abstract void setClassPath();
277     
278     /**
279      * Asynchronous request to cancel ongoing long-term request (such as preCheck(), checkParameters() or prepare())
280      */

281     public final void cancelRequest() {
282 // Iterator pIt=getPlugins().iterator();
283
//
284
// while(pIt.hasNext()) {
285
// RefactoringPlugin plugin=(RefactoringPlugin)pIt.next();
286
// plugin.cancelRequest();
287
// }
288
}
289     
290     private Problem pluginsPreCheck(Problem problem) {
291 // Iterator pIt=getPlugins().iterator();
292
//
293
// while(pIt.hasNext()) {
294
// RefactoringPlugin plugin=(RefactoringPlugin)pIt.next();
295
//
296
// problem=chainProblems(plugin.preCheck(),problem);
297
// if (problem!=null && problem.isFatal())
298
// return problem;
299
// }
300
return problem;
301     }
302     
303     private Problem pluginsPrepare(Problem problem, RefactoringSession session) {
304 // RefactoringElementsBag elements = session.getElementsBag();
305
// Iterator pIt=getPlugins().iterator();
306
//
307
// while(pIt.hasNext()) {
308
// RefactoringPlugin plugin=(RefactoringPlugin)pIt.next();
309
//
310
// problem=chainProblems(plugin.prepare(elements),problem);
311
// if (problem!=null && problem.isFatal())
312
// return problem;
313
// }
314
//
315
// //following condition "!(this instanceof WhereUsedQuery)" is hotfix of #65785
316
// //correct solution would probably be this condition: "!isQuery()"
317
// //unfortunately isQuery() is not in AbstractRefactoring class, but in RefactoringIU
318
// //we should consider moving this method to AbstractRefactoring class in future release
319
// if (!(this instanceof WhereUsedQuery)) {
320
// ReadOnlyFilesHandler handler = getROHandler();
321
// if (handler!=null) {
322
// Collection files = SPIAccessor.DEFAULT.getReadOnlyFiles(elements);
323
// Collection allFiles = new HashSet();
324
// for (Iterator i = files.iterator(); i.hasNext();) {
325
// FileObject f = (FileObject) i.next();
326
// DataObject dob;
327
// try {
328
// dob = DataObject.find(f);
329
// for (Iterator j = dob.files().iterator(); j.hasNext();) {
330
// FileObject file = (FileObject) j.next();
331
// if (SharabilityQuery.getSharability(FileUtil.toFile(file)) == SharabilityQuery.SHARABLE) {
332
// allFiles.add(file);
333
// }
334
// }
335
// } catch (DataObjectNotFoundException e) {
336
// allFiles.add(f);
337
// }
338
// }
339
// problem = chainProblems(handler.createProblem(session, allFiles), problem);
340
// }
341
// }
342
//
343
// return problem;
344
// }
345
//
346
// private ReadOnlyFilesHandler getROHandler() {
347
// Lookup.Result result = Lookup.getDefault().lookup(new Lookup.Template(ReadOnlyFilesHandler.class));
348
// List handlers = (List) result.allInstances();
349
// if (handlers.size() == 0) {
350
// return null;
351
// }
352
// if (handlers.size() > 1) {
353
// ErrorManager.getDefault().log(ErrorManager.WARNING, "Multiple instances of ReadOnlyFilesHandler found in Lookup; only using first one: " + handlers); //NOI18N
354
// }
355
// return (ReadOnlyFilesHandler) handlers.get(0);
356
// }
357
//
358
// private Problem pluginsCheckParams(Problem problem) {
359
// Iterator pIt=getPlugins().iterator();
360
//
361
// while(pIt.hasNext()) {
362
// RefactoringPlugin plugin=(RefactoringPlugin)pIt.next();
363
//
364
// problem=chainProblems(plugin.checkParameters(),problem);
365
// if (problem!=null && problem.isFatal())
366
// return problem;
367
// }
368
return problem;
369     }
370     
371     private Problem pluginsFastCheckParams(Problem problem) {
372 // Iterator pIt=getPlugins().iterator();
373
//
374
// while(pIt.hasNext()) {
375
// RefactoringPlugin plugin=(RefactoringPlugin)pIt.next();
376
//
377
// problem=chainProblems(plugin.fastCheckParameters(),problem);
378
// if (problem!=null && problem.isFatal())
379
// return problem;
380
// }
381
return problem;
382     }
383     
384     static Problem chainProblems(Problem p,Problem p1) {
385         Problem problem;
386         
387         if (p==null) return p1;
388         if (p1==null) return p;
389         problem=p;
390         while(problem.getNext()!=null) {
391             problem=problem.getNext();
392         }
393         problem.setNext(p1);
394         return p;
395     }
396     
397     
398     private class ProgressL implements ProgressListener {
399
400         private float progressStep;
401         private float current;
402         public void start(ProgressEvent event) {
403             progressStep = (float) PLUGIN_STEPS / event.getCount();
404             
405             if (pluginsWithProgress.indexOf(event.getSource()) == 0) {
406                 //first plugin
407
//let's start
408
current = 0;
409                 if (event.getCount()==-1) {
410                     fireProgressListenerStart(event.getOperationType(), -1);
411                 } else {
412                     fireProgressListenerStart(event.getOperationType(), PLUGIN_STEPS*pluginsWithProgress.size());
413                 }
414             } else {
415                 current = pluginsWithProgress.indexOf(event.getSource())*PLUGIN_STEPS;
416                 fireProgressListenerStep((int) current);
417             }
418         }
419         
420         public void step(ProgressEvent event) {
421             current = current + progressStep;
422             fireProgressListenerStep((int) current) ;
423         }
424         
425         public void stop(ProgressEvent event) {
426             if (pluginsWithProgress.indexOf(event.getSource()) == pluginsWithProgress.size()-1) {
427                 fireProgressListenerStop();
428             }
429         }
430         /** Notifies all registered listeners about the event.
431          *
432          * @param type Type of operation that is starting.
433          * @param count Number of steps the operation consists of.
434          *
435          */

436         private void fireProgressListenerStart(int type, int count) {
437 // if (progressSupport != null)
438
// progressSupport.fireProgressListenerStart(this, type, count);
439
}
440         
441         /** Notifies all registered listeners about the event.
442          */

443 // private void fireProgressListenerStep() {
444
//// if (progressSupport != null)
445
//// progressSupport.fireProgressListenerStep(this);
446
// }
447

448         /**
449          * Notifies all registered listeners about the event.
450          * @param count
451          */

452         private void fireProgressListenerStep(int count) {
453 // if (progressSupport != null)
454
// progressSupport.fireProgressListenerStep(this, count);
455
}
456         
457         /** Notifies all registered listeners about the event.
458          */

459         private void fireProgressListenerStop() {
460 // if (progressSupport != null)
461
// progressSupport.fireProgressListenerStop(this);
462
}
463     }
464 }
465
Popular Tags