001: /**
002: *
003: */package core;
004:
005: import gui.section.IEnvEntryViewer;
006:
007: import java.util.Iterator;
008: import java.util.Set;
009:
010: import org.apache.commons.logging.Log;
011: import org.apache.commons.logging.LogFactory;
012: import org.eclipse.core.runtime.CoreException;
013: import org.eclipse.core.runtime.IProgressMonitor;
014: import org.eclipse.core.runtime.OperationCanceledException;
015: import org.eclipse.jdt.core.JavaModelException;
016: import org.eclipse.jdt.core.dom.FieldDeclaration;
017: import org.eclipse.ltk.core.refactoring.Change;
018: import org.eclipse.ltk.core.refactoring.CompositeChange;
019: import org.eclipse.ltk.core.refactoring.RefactoringStatus;
020: import org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext;
021:
022: import diagram.section.EnvEntry;
023:
024: /**
025: * The Delegate Class which does the refactoring modifications
026: * in the source and onto the model. It also contains the undo
027: * modifications.
028: *
029: * @author sh
030: *
031: */
032: public class EnvEntryDelegate {
033:
034: // the Info Object containing all nesessary refactor info
035: private final RefactorInfo info;
036:
037: // FIXME
038: // the viewer set and the EnvEntry is needed to execute the table model update
039: // after an environment entry has changed. It will be
040: // transmitted from the EnvEntryList, to RefactorEnvEntryAction,
041: // to EnvEntryProcessor and finally to EnvEntryDelegate where the
042: // update will be performed.
043: private Set<IEnvEntryViewer> changeListeners = null;
044: private EnvEntry envEntry = null;
045:
046: private Log log = LogFactory.getLog(getClass());
047:
048: /**
049: * Constructor
050: *
051: * @param info the refactoring info object
052: * @param envEntry the selected environment entry to change
053: * @param changeListeners the Listeners which listen to envEntry changes
054: */
055: public EnvEntryDelegate(RefactorInfo info, EnvEntry envEntry,
056: Set<IEnvEntryViewer> changeListeners) {
057: super ();
058: this .info = info;
059: this .envEntry = envEntry;
060: this .changeListeners = changeListeners;
061: }
062:
063: /**
064: * Check if the info object contains valid information.
065: *
066: * @return result a Refactoring Status object
067: */
068: public RefactoringStatus checkInitialConditions() {
069: RefactoringStatus result = new RefactoringStatus();
070:
071: if (info.getNewName() == null || info.getNewName().equals("")) {
072: result
073: .addFatalError("No new type name could be determined");
074: } else if (info.getOldName() == null
075: || info.getOldName().equals("")) {
076: result.addFatalError("The old name could't be determined.");
077: }
078:
079: if (info.getElement() instanceof FieldDeclaration == false) {
080: result
081: .addWarning("Environment Entry not yet available, will be created");
082: }
083:
084: boolean isConsitent = false;
085: try {
086: isConsitent = info.getUnit().isConsistent();
087: } catch (JavaModelException e) {
088: result.addFatalError("No source file could be determined");
089: }
090:
091: if (!isConsitent)
092: result.addFatalError("No source file could be determined.");
093: else if (info.getUnit() == null || info.getUnit().isReadOnly()) {
094: result.addFatalError("No source file could be determined.");
095: }
096: return result;
097: }
098:
099: /**
100: * Checks the final conditions before the refactoring will be executed.
101: * If an error will be returned no refactoring operation will be invoked.
102: *
103: * @param pm a progress monitor
104: * @param ctxt
105: * @return result a Refactoring Status object
106: */
107: public RefactoringStatus checkFinalConditions(
108: final IProgressMonitor pm, final CheckConditionsContext ctxt) {
109: RefactoringStatus result = new RefactoringStatus();
110:
111: // TODO use the CheckConditionContext and check the model
112:
113: return result;
114: }
115:
116: /**
117: * The code refactoring, environment entry creation and the model
118: * refactoring are added as one task to the Composite Changed object
119: * used by the processor. Each refactoring change object contains
120: * its undo handling. If an undo operation was executed, all tasks
121: * of the Composite Change object are considered.
122: *
123: * @param pm a progress monitor
124: * @param rootChange the Composite Change object
125: */
126: void createChange(final IProgressMonitor pm,
127: final CompositeChange rootChange) {
128: // if no environment entry in the source is available,
129: // generate it.
130: if (info.getElement() instanceof FieldDeclaration == false) {
131: try {
132: pm.beginTask("Collecting changes", 100);
133: rootChange.add(new EnvEntryCreation());
134: pm.worked(30);
135: rootChange.add(new ModelChange());
136: } finally {
137: pm.done();
138: }
139: }
140:
141: // refactor the source and the model
142: else {
143: try {
144: pm.beginTask("Collecting changes", 100);
145: rootChange.add(new TypeChange());
146: pm.worked(30);
147: rootChange.add(new ModelChange());
148: } finally {
149: pm.done();
150: }
151: }
152: }
153:
154: /**
155: * The change class responsible for environment entry ereation
156: * and undo handling.
157: *
158: * @author sh
159: *
160: */
161: private class EnvEntryCreation extends Change {
162: public Object getModifiedElement() {
163: return null;
164: }
165:
166: public String getName() {
167: return "Environment Entry source code creation";
168: }
169:
170: public void initializeValidationData(IProgressMonitor pm) {
171: }
172:
173: public RefactoringStatus isValid(IProgressMonitor pm)
174: throws CoreException, OperationCanceledException {
175: return new RefactoringStatus();
176: }
177:
178: /**
179: * performs the type refactoring
180: */
181: @Override
182: public Change perform(IProgressMonitor pm) throws CoreException {
183: CreateFieldDeclaration createField = new CreateFieldDeclaration();
184: createField.createFieldDeclarations(info);
185: log
186: .info("Source Code: Environment Entry creation perfomed");
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 Environment Entry source code creation";
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: new RemoveFieldDeclaration()
214: .removeFieldDeclarations(info);
215: log
216: .info("Source Code: Environment Entry creation undone");
217: return null;
218: }
219: };
220: }
221: }
222:
223: /**
224: * The change class responsible for source type refacturing
225: * and undo handling.
226: *
227: * @author sh
228: *
229: */
230: private class TypeChange extends Change {
231: public Object getModifiedElement() {
232: return null;
233: }
234:
235: public String getName() {
236: return "Environment Entry source code change";
237: }
238:
239: public void initializeValidationData(IProgressMonitor pm) {
240: }
241:
242: public RefactoringStatus isValid(IProgressMonitor pm)
243: throws CoreException, OperationCanceledException {
244: return new RefactoringStatus();
245: }
246:
247: /**
248: * performs the type refactoring
249: */
250: @Override
251: public Change perform(IProgressMonitor pm) throws CoreException {
252: RenameFieldDeclaration renField = new RenameFieldDeclaration();
253: renField.process(info);
254: log.info("Source Code: Environment Entry change perfomed");
255:
256: // returns the undo change object
257: return new Change() {
258: public Object getModifiedElement() {
259: return null;
260: }
261:
262: public String getName() {
263: return "Undo Environment Entry source code change";
264: }
265:
266: public void initializeValidationData(IProgressMonitor pm) {
267: }
268:
269: public RefactoringStatus isValid(IProgressMonitor pm)
270: throws CoreException,
271: OperationCanceledException {
272: return new RefactoringStatus();
273: }
274:
275: /**
276: * performs the undo operation
277: */
278: @Override
279: public Change perform(IProgressMonitor pm)
280: throws CoreException {
281: RenameFieldDeclaration renField = new RenameFieldDeclaration();
282: RefactorInfo undoInfo = new RefactorInfo();
283:
284: // invert values
285: undoInfo.setElement(info.getElement());
286: undoInfo.setUnit(info.getUnit());
287: undoInfo.setNewName(info.getOldName());
288: undoInfo.setOldName(info.getNewName());
289: undoInfo.setNewVarName(info.getOldVarName());
290: undoInfo.setOldVarName(info.getNewVarName());
291:
292: // start the undo operation
293: renField.process(undoInfo);
294: log
295: .info("Source Code: Environment Entry change undone");
296: return null;
297: }
298: };
299: }
300: }
301:
302: /**
303: * The change class responsible for model type refacturing
304: * and undo handling.
305: *
306: * @author sh
307: *
308: */
309: private class ModelChange extends Change {
310: public Object getModifiedElement() {
311: return null;
312: }
313:
314: public String getName() {
315: return "Environment Entry model change";
316: }
317:
318: public void initializeValidationData(IProgressMonitor pm) {
319: }
320:
321: public RefactoringStatus isValid(IProgressMonitor pm)
322: throws CoreException, OperationCanceledException {
323: return new RefactoringStatus();
324: }
325:
326: /**
327: * performs the type refactoring
328: */
329: @Override
330: public Change perform(IProgressMonitor pm) throws CoreException {
331: // perform the model modification
332: RenameModelField renField = new RenameModelField();
333: renField.process(info);
334: log.info("Model: Environment Entry change performed");
335:
336: // update the model
337: envEntry.setName(info.getNewVarName());
338: envEntry.setType(info.getNewName());
339:
340: // perform the table refresh
341: Iterator<IEnvEntryViewer> iterator = changeListeners
342: .iterator();
343: while (iterator.hasNext())
344: ((IEnvEntryViewer) iterator.next())
345: .updateEnvEntry(envEntry);
346:
347: // returns the undo change object
348: return new Change() {
349: public Object getModifiedElement() {
350: return null;
351: }
352:
353: public String getName() {
354: return "Undo Environment Entry model change";
355: }
356:
357: public void initializeValidationData(IProgressMonitor pm) {
358: }
359:
360: public RefactoringStatus isValid(IProgressMonitor pm)
361: throws CoreException,
362: OperationCanceledException {
363: return new RefactoringStatus();
364: }
365:
366: /**
367: * performs the undo operation
368: */
369: @Override
370: public Change perform(IProgressMonitor pm)
371: throws CoreException {
372: RenameModelField renField = new RenameModelField();
373: RefactorInfo undoInfo = new RefactorInfo();
374:
375: // invert values
376: undoInfo.setElement(info.getElement());
377: undoInfo.setUnit(info.getUnit());
378: undoInfo.setNewName(info.getOldName());
379: undoInfo.setOldName(info.getNewName());
380: undoInfo.setNewVarName(info.getOldVarName());
381: undoInfo.setOldVarName(info.getNewVarName());
382:
383: // start the undo operation
384: renField.process(undoInfo);
385: log.info("Model: Environment Entry change undone");
386:
387: // update the model
388: envEntry.setName(undoInfo.getNewVarName());
389: envEntry.setType(undoInfo.getNewName());
390:
391: // perform the table refresh
392: Iterator<IEnvEntryViewer> iterator = changeListeners
393: .iterator();
394: while (iterator.hasNext())
395: ((IEnvEntryViewer) iterator.next())
396: .updateEnvEntry(envEntry);
397:
398: return null;
399: }
400: };
401: }
402: }
403: }
|