KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > junit > refactoring > JUnitRenameParticipant


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.junit.refactoring;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IProgressMonitor;
19 import org.eclipse.core.runtime.OperationCanceledException;
20
21 import org.eclipse.debug.core.DebugPlugin;
22 import org.eclipse.debug.core.ILaunchConfiguration;
23 import org.eclipse.debug.core.ILaunchConfigurationType;
24 import org.eclipse.debug.core.ILaunchManager;
25
26 import org.eclipse.ltk.core.refactoring.Change;
27 import org.eclipse.ltk.core.refactoring.CompositeChange;
28 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
29 import org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext;
30 import org.eclipse.ltk.core.refactoring.participants.RenameArguments;
31 import org.eclipse.ltk.core.refactoring.participants.RenameParticipant;
32
33 import org.eclipse.jdt.internal.junit.ui.JUnitMessages;
34 import org.eclipse.jdt.internal.junit.ui.JUnitPlugin;
35
36 public abstract class JUnitRenameParticipant extends RenameParticipant implements IChangeAdder {
37
38     static public class ChangeList {
39
40         private final RenameArguments fArguments;
41
42         private List JavaDoc fChanges;
43
44         private final ILaunchManager fLaunchManager;
45
46         private boolean fShouldFlagWarnings= true;
47
48         public ChangeList(RenameArguments arguments, ILaunchManager manager, List JavaDoc changes) {
49             fArguments= arguments;
50             fLaunchManager= manager;
51             fChanges= changes;
52         }
53
54         public void addChange(Change change) {
55             if (change != null) {
56                 fChanges.add(change);
57                 fShouldFlagWarnings= false;
58             }
59         }
60
61         public void createChangeForConfigs(ILaunchConfiguration[] configs, IChangeAdder changeCreator) throws CoreException {
62             for (int i= 0; i < configs.length; i++) {
63                 fShouldFlagWarnings= true;
64                 changeCreator.createChangeForConfig(this, new LaunchConfigurationContainer(configs[i]));
65             }
66         }
67
68         public boolean shouldFlagWarnings() {
69             return fShouldFlagWarnings;
70         }
71
72         public void addRenameChangeIfNeeded(LaunchConfigurationContainer config, String JavaDoc oldName) {
73             if (config.getName().equals(oldName)) {
74                 LaunchConfigRenameChange renameChange= new LaunchConfigRenameChange(config, fArguments.getNewName(), fLaunchManager, shouldFlagWarnings());
75                 addChange(renameChange);
76             }
77         }
78
79         public void addAttributeChangeIfNeeded(LaunchConfigurationContainer config, String JavaDoc attributeName, String JavaDoc oldValue, String JavaDoc newValue) throws CoreException {
80             String JavaDoc currentValue= config.getAttribute(attributeName, (String JavaDoc) null);
81             if (currentValue != null && oldValue.equals(currentValue)) {
82                 addAttributeChange(config, attributeName, newValue);
83             }
84         }
85
86         public void addAttributeChange(LaunchConfigurationContainer config, String JavaDoc attributeName, String JavaDoc newValue) {
87             addChange(new LaunchConfigSetAttributeChange(config, attributeName, newValue, shouldFlagWarnings()));
88         }
89     }
90
91     public RefactoringStatus checkConditions(IProgressMonitor pm, CheckConditionsContext context) {
92         return new RefactoringStatus();
93     }
94
95     public Change createChange(IProgressMonitor pm) throws CoreException {
96         if (!getArguments().getUpdateReferences())
97             return null;
98
99         ILaunchManager manager= getLaunchManager();
100         List JavaDoc launchConfigTypes= getLaunchConfigTypes();
101         List JavaDoc changes= new ArrayList JavaDoc();
102         for (Iterator JavaDoc types= launchConfigTypes.iterator(); types.hasNext();) {
103             String JavaDoc typeId= (String JavaDoc) types.next();
104             ILaunchConfigurationType type= manager.getLaunchConfigurationType(typeId);
105             ILaunchConfiguration configs[]= manager.getLaunchConfigurations(type);
106             new ChangeList(getArguments(), getLaunchManager(), changes).createChangeForConfigs(configs, this);
107             if (pm.isCanceled())
108                 throw new OperationCanceledException();
109         }
110         if (changes.size() > 0)
111             return new CompositeChange(getChangeName(), (Change[]) changes.toArray(new Change[changes.size()]));
112         return null;
113     }
114
115     // @see
116
// org.eclipse.jdt.internal.junit.refactoring.IChangeAdder#createChangeForConfig(org.eclipse.jdt.internal.junit.refactoring.JUnitRenameParticipant.ChangeList,
117
// org.eclipse.debug.core.ILaunchConfiguration)
118
public abstract void createChangeForConfig(ChangeList list, LaunchConfigurationContainer config) throws CoreException;
119
120     protected String JavaDoc getChangeName() {
121         return JUnitMessages.TypeRenameParticipant_change_name;
122     }
123
124     protected List JavaDoc getLaunchConfigTypes() {
125         return JUnitPlugin.getDefault().getJUnitLaunchConfigTypeIDs();
126     }
127
128     protected ILaunchManager getLaunchManager() {
129         return DebugPlugin.getDefault().getLaunchManager();
130     }
131
132     protected String JavaDoc getNewName() {
133         return getArguments().getNewName();
134     }
135
136     /**
137      * {@inheritDoc}
138      */

139     public String JavaDoc getName() {
140         return JUnitMessages.TypeRenameParticipant_name;
141     }
142 }
143
Popular Tags