001: /**
002: *
003: */package core;
004:
005: import java.util.Iterator;
006: import java.util.List;
007:
008: import model.ModelChangeCommand;
009: import model.ModelUtil;
010:
011: import org.apache.commons.logging.Log;
012: import org.apache.commons.logging.LogFactory;
013: import org.eclipse.core.runtime.CoreException;
014: import org.eclipse.core.runtime.IProgressMonitor;
015: import org.eclipse.core.runtime.OperationCanceledException;
016: import org.eclipse.gef.EditPart;
017: import org.eclipse.jdt.core.ICompilationUnit;
018: import org.eclipse.ltk.core.refactoring.Change;
019: import org.eclipse.ltk.core.refactoring.CompositeChange;
020: import org.eclipse.ltk.core.refactoring.RefactoringStatus;
021: import org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext;
022:
023: /**
024: * The Delegate Class which does the refactoring modifications
025: * in the model and onto the source. It also contains the undo
026: * modifications.
027: *
028: * @author sh
029: *
030: */
031: public class RenameImplementationDelegate {
032:
033: // the Info Object containing all nesessary refactor info
034: private final ModelInfo info;
035:
036: private Log log = LogFactory.getLog(getClass());
037:
038: /**
039: * Constructor
040: *
041: * @param info the refactoring info object
042: */
043: public RenameImplementationDelegate(ModelInfo info) {
044: super ();
045: this .info = info;
046: }
047:
048: /**
049: * Check if the info object contains valid information.
050: *
051: * @return result a Refactoring Status object
052: */
053: public RefactoringStatus checkInitialConditions() {
054: RefactoringStatus result = new RefactoringStatus();
055:
056: if (info.getNewImplementation() == null
057: || info.getNewImplementation().equals("")) {
058: result
059: .addFatalError("No new implementation could be determined.");
060: } else if (info.getOldImplementation() == null
061: || info.getOldImplementation().equals("")) {
062: result
063: .addFatalError("The old implementation could't be determined.");
064: } else if (info.getSelectedEditPart() == null) {
065: result
066: .addFatalError("The current selection could't be determined.");
067: }
068: return result;
069: }
070:
071: /**
072: * Checks the final conditions before the refactoring will be executed.
073: * If an error will be returned no refactoring operation will be invoked.
074: *
075: * @param pm a progress monitor
076: * @param ctxt
077: * @return result a Refactoring Status object
078: */
079: public RefactoringStatus checkFinalConditions(
080: final IProgressMonitor pm, final CheckConditionsContext ctxt) {
081: RefactoringStatus result = new RefactoringStatus();
082:
083: // check if the model could be written
084: if (!canExecute())
085: result
086: .addFatalError("implementation change couldn't be performed.");
087:
088: // check if corresponding source exist
089: if (!sourceFound())
090: result
091: .addFatalError("no corresponding source code has been found.");
092:
093: return result;
094: }
095:
096: /**
097: * The code refactoring and the model refactoring are added
098: * as one task to the Composite Changed object used by the processor.
099: * Each refactoring change object contains its undo handling.
100: * If an undo operation was executed, all tasks of the Composite
101: * Change object are considered.
102: *
103: * @param pm a progress monitor
104: * @param rootChange the Composite Change object
105: */
106: void createChange(final IProgressMonitor pm,
107: final CompositeChange rootChange) {
108: try {
109: pm.beginTask("Collecting changes", 100);
110: rootChange.add(new ModelChange());
111: pm.worked(50);
112: rootChange.add(new TypeChange());
113: } finally {
114: pm.done();
115: }
116: }
117:
118: /**
119: * Helper method which checks if
120: * the model could be edited.
121: *
122: * @return true if the model could be edited
123: */
124: private boolean canExecute() {
125: ModelChangeCommand changeCom = new ModelChangeCommand();
126: return changeCom.canExecuteImplChange(info
127: .getSelectedEditPart(), info.getNewImplementation());
128: }
129:
130: /**
131: * Helper method which checks if
132: * the source refactoring could be
133: * applied and if corresponding
134: * class files exist.
135: *
136: * @return true if corresponding source exist
137: */
138: private boolean sourceFound() {
139: EditPart editPart = info.getSelectedEditPart();
140: String modelImplementation = ModelUtil
141: .getModelImplProperty(editPart);
142:
143: boolean sourceFound = false;
144: List<ICompilationUnit> units = Util.getICompilationUnits();
145: for (Iterator<ICompilationUnit> iterator = units.iterator(); iterator
146: .hasNext();) {
147: ICompilationUnit u = (ICompilationUnit) iterator.next();
148: String sourceImplementation = Util.getImplementation(u);
149: if (sourceImplementation.equals(modelImplementation))
150: sourceFound = true;
151: }
152: return sourceFound;
153: }
154:
155: /**
156: * The change class responsible for source type refacturing
157: * and undo handling.
158: *
159: * @author sh
160: *
161: */
162: private class TypeChange extends Change {
163: public Object getModifiedElement() {
164: return null;
165: }
166:
167: public String getName() {
168: return "Class name change";
169: }
170:
171: public void initializeValidationData(IProgressMonitor pm) {
172: }
173:
174: public RefactoringStatus isValid(IProgressMonitor pm)
175: throws CoreException, OperationCanceledException {
176: return new RefactoringStatus();
177: }
178:
179: /**
180: * performs the type refactoring
181: */
182: @Override
183: public Change perform(IProgressMonitor pm) throws CoreException {
184: RenameImplementation renImpl = new RenameImplementation();
185: renImpl.process(info);
186: log.info("Source: class name change performed");
187:
188: // returns the undo change object
189: return new Change() {
190: public Object getModifiedElement() {
191: return null;
192: }
193:
194: public String getName() {
195: return "Undo class name change";
196: }
197:
198: public void initializeValidationData(IProgressMonitor pm) {
199: }
200:
201: public RefactoringStatus isValid(IProgressMonitor pm)
202: throws CoreException,
203: OperationCanceledException {
204: return new RefactoringStatus();
205: }
206:
207: /**
208: * performs the undo operation
209: */
210: @Override
211: public Change perform(IProgressMonitor pm)
212: throws CoreException {
213: RenameImplementation renImpl = new RenameImplementation();
214: ModelInfo undoInfo = new ModelInfo();
215:
216: // invert values
217: undoInfo.setSelectedEditPart(info
218: .getSelectedEditPart());
219: undoInfo.setNewImplementation(info
220: .getOldImplementation());
221: undoInfo.setOldImplementation(info
222: .getNewImplementation());
223:
224: // start the undo operation
225: renImpl.process(undoInfo);
226: log.info("Source: class name change undone");
227: return null;
228: }
229: };
230: }
231: }
232:
233: /**
234: * The change class responsible for model type refacturing
235: * and undo handling.
236: *
237: * @author sh
238: *
239: */
240: private class ModelChange extends Change {
241: public Object getModifiedElement() {
242: return null;
243: }
244:
245: public String getName() {
246: return "Implementation change";
247: }
248:
249: public void initializeValidationData(IProgressMonitor pm) {
250: }
251:
252: public RefactoringStatus isValid(IProgressMonitor pm)
253: throws CoreException, OperationCanceledException {
254: return new RefactoringStatus();
255: }
256:
257: /**
258: * performs the type refactoring
259: */
260: @Override
261: public Change perform(IProgressMonitor pm) throws CoreException {
262: ModelChangeCommand changeCommand = new ModelChangeCommand();
263: changeCommand.executeImplChange(info.getSelectedEditPart(),
264: info.getNewImplementation());
265: log.info("Model: Implementation change perfomed");
266:
267: // returns the undo change object
268: return new Change() {
269: public Object getModifiedElement() {
270: return null;
271: }
272:
273: public String getName() {
274: return "Undo implementation change";
275: }
276:
277: public void initializeValidationData(IProgressMonitor pm) {
278: }
279:
280: public RefactoringStatus isValid(IProgressMonitor pm)
281: throws CoreException,
282: OperationCanceledException {
283: return new RefactoringStatus();
284: }
285:
286: /**
287: * performs the undo operation
288: */
289: @Override
290: public Change perform(IProgressMonitor pm)
291: throws CoreException {
292: // invert values
293: ModelInfo undoInfo = new ModelInfo();
294: undoInfo.setSelectedEditPart(info
295: .getSelectedEditPart());
296: undoInfo.setNewImplementation(info
297: .getOldImplementation());
298:
299: // start the undo operation
300: ModelChangeCommand changeCommand = new ModelChangeCommand();
301: changeCommand.executeImplChange(undoInfo
302: .getSelectedEditPart(), undoInfo
303: .getNewImplementation());
304: log.info("Model: Implementation change undone");
305: return null;
306: }
307: };
308: }
309: }
310: }
|