001: /**
002: *
003: */package gui.participants;
004:
005: import logging.LoggingPlugin;
006: import model.ModelModifier;
007:
008: import org.apache.commons.logging.Log;
009: import org.apache.commons.logging.LogFactory;
010: import org.eclipse.core.runtime.CoreException;
011: import org.eclipse.core.runtime.ILog;
012: import org.eclipse.core.runtime.IProgressMonitor;
013: import org.eclipse.core.runtime.IStatus;
014: import org.eclipse.core.runtime.OperationCanceledException;
015: import org.eclipse.core.runtime.Status;
016: import org.eclipse.jdt.core.ICompilationUnit;
017: import org.eclipse.jdt.internal.core.PackageFragment;
018: import org.eclipse.ltk.core.refactoring.Change;
019: import org.eclipse.ltk.core.refactoring.RefactoringStatus;
020: import org.eclipse.ltk.core.refactoring.participants.RenameArguments;
021:
022: import core.Util;
023:
024: /**
025: * This change class is responsible for updating the implementation
026: * property in the process models. It checks which model
027: * element has the same implementation statement. If so
028: * the new string will be set. It also returns the
029: * undo operation for this modification.
030: *
031: * @author sh
032: */
033: public class CUModelChange extends Change {
034: // the selected element to refacter
035: private Object selection = null;
036:
037: // the input of the refactoring dialog
038: private RenameArguments arguments = null;
039:
040: private Log log = LogFactory.getLog(getClass());
041:
042: /**
043: * Constructor
044: *
045: * @param selection the element to refacter
046: * @param arguments the input data of the refactoring dialog
047: */
048: public CUModelChange(Object selection, RenameArguments arguments) {
049: super ();
050: this .selection = selection;
051: this .arguments = arguments;
052: }
053:
054: /**
055: * Returns the element modified by this <code>Change</code>. The method may return
056: * <code>null</code> if the change isn't related to an element.
057: *
058: * @return the element modified by this change
059: */
060: @Override
061: public Object getModifiedElement() {
062: return null;
063: }
064:
065: /* (non-Javadoc)
066: * @see org.eclipse.ltk.core.refactoring.Change#getName()
067: */
068: @Override
069: public String getName() {
070: return new String("Implementation change");
071: }
072:
073: /* (non-Javadoc)
074: * @see org.eclipse.ltk.core.refactoring.Change#initializeValidationData(org.eclipse.core.runtime.IProgressMonitor)
075: */
076: @Override
077: public void initializeValidationData(IProgressMonitor pm) {
078: }
079:
080: /* (non-Javadoc)
081: * @see org.eclipse.ltk.core.refactoring.Change#isValid(org.eclipse.core.runtime.IProgressMonitor)
082: */
083: @Override
084: public RefactoringStatus isValid(IProgressMonitor pm)
085: throws CoreException, OperationCanceledException {
086:
087: // if an fatal error will return, the refactoring will stoped
088: RefactoringStatus result = new RefactoringStatus();
089:
090: // TODO: check if some model could be read
091:
092: return result;
093: }
094:
095: /**
096: * Performs this change after the method isValid has been called. If isValid
097: * returns an fatal error or the change is disabled this change will not be performed.
098: * Changes should in general not respond to {@link IProgressMonitor#isCanceled()}
099: * since canceling a change tree in the middle of its execution leaves the workspace
100: * in a half changed state.
101: *
102: * @param pm a progress monitor
103: * @return the undo change for this change object or <code>null</code> if no
104: * undo is provided
105: * @throws CoreException if an error occurred during change execution
106: */
107: @Override
108: public Change perform(IProgressMonitor pm) throws CoreException {
109: // performs the model modification
110:
111: // get new implementation
112: String newImplementation = getNewImplementation();
113:
114: // format old implementation
115: String oldImplementation = getOldImplementation();
116:
117: // get the new class name
118: //String newClassName = Util.packagename(this.arguments.getNewName());
119:
120: // check if values are valid
121: if (!valuesValid(newImplementation, oldImplementation)) {
122: String message = "Model Update could't be done";
123: ILog logger = LoggingPlugin.getDefault().getLog();
124: String symName = LoggingPlugin.getDefault().getBundle()
125: .getSymbolicName();
126: logger.log(new Status(IStatus.ERROR, symName, 0, message,
127: null));
128: throw new RuntimeException(message);
129: }
130:
131: // start the model update
132: new ModelModifier().doImplementationModification(
133: oldImplementation, newImplementation);
134: log.info("Model: Implementation change performed");
135:
136: // for the transaction handling the undo change element is returned
137: // which reverts the implementation property change to the original state
138: String undoOldImpl = newImplementation;
139: String undoNewImpl = oldImplementation;
140: return new CUUndoModelChange(undoOldImpl, undoNewImpl);
141: }
142:
143: /**
144: * check if the given values are valid, means they are initialized and
145: * the string is not empty.
146: *
147: * @param newImpl the full qualified name of a compilation unit
148: * @param oldImpl the full qualified name of a compilation unit
149: *
150: * @return true if all values pass the check
151: */
152: private boolean valuesValid(String newImpl, String oldImpl) {
153: boolean valid = false;
154:
155: if (newImpl != null)
156: if (!newImpl.equals(""))
157: valid = true;
158:
159: if (oldImpl != null) {
160: if (!oldImpl.equals(""))
161: valid = true;
162: }
163:
164: return valid;
165: }
166:
167: /**
168: * helper method to get the new full qualified
169: * name of the compilation unit.
170: *
171: * @return the full qualified name of the
172: * compilation unit or null of no was found
173: */
174: private String getNewImplementation() {
175: // get new implementation
176: String newUnitName = this .arguments.getNewName();
177: // it is possible to assume the selection is a CompilationUnit
178: ICompilationUnit u = (ICompilationUnit) this .selection;
179:
180: // get the package
181: PackageFragment packageFrag = ((PackageFragment) u.getParent());
182: String packageName = null;
183: if (packageFrag != null && !packageFrag.equals("")) {
184: packageName = packageFrag.getElementName();
185: }
186:
187: // get new implementation
188: newUnitName = this .arguments.getNewName();
189: String classname = Util.packagename(newUnitName);
190:
191: if (!packageName.equals(""))
192: packageName += ".";
193:
194: // return the new implementation
195: return packageName + classname;
196: }
197:
198: /**
199: * helper method to get the old full qualified
200: * name of the compilation unit.
201: *
202: * @return the old full qualified name of the
203: * compilation unit or null of no was found
204: */
205: private String getOldImplementation() {
206: String oldUnitName = ((ICompilationUnit) this .selection)
207: .getElementName();
208: String oldClassName = Util.packagename(oldUnitName);
209: String newClassName = Util.packagename(this .arguments
210: .getNewName());
211: return getNewImplementation().replace(newClassName,
212: oldClassName);
213: }
214:
215: /**
216: * The undo change class which is used by the CUModelChange participant.
217: * If a model modification will be undone, an instance of that class
218: * will be returned to set the original state.
219: *
220: * @author sh
221: *
222: */
223: private class CUUndoModelChange extends Change {
224: // the old implementation value to change
225: private String oldImplementation = null;
226: // the new implementation value to set
227: private String newImplementation = null;
228:
229: public Object getModifiedElement() {
230: return null;
231: }
232:
233: public String getName() {
234: return "Undo implementation change";
235: }
236:
237: public void initializeValidationData(IProgressMonitor pm) {
238: }
239:
240: public CUUndoModelChange(String oldImplementation,
241: String newImplementation) {
242: super ();
243: this .oldImplementation = oldImplementation;
244: this .newImplementation = newImplementation;
245: }
246:
247: /**
248: * check if properties are set.
249: */
250: @Override
251: public RefactoringStatus isValid(IProgressMonitor pm)
252: throws CoreException, OperationCanceledException {
253:
254: if (this .oldImplementation == null
255: || this .newImplementation == null) {
256: String message = "unable to undo the model implementation modification";
257: ILog logger = LoggingPlugin.getDefault().getLog();
258: String symName = LoggingPlugin.getDefault().getBundle()
259: .getSymbolicName();
260: logger.log(new Status(IStatus.ERROR, symName, 0,
261: message, null));
262: throw new RuntimeException(message);
263: }
264: return new RefactoringStatus();
265: }
266:
267: /**
268: * perform the undo operation
269: */
270: @Override
271: public Change perform(IProgressMonitor pm) throws CoreException {
272: new ModelModifier().doImplementationModification(
273: oldImplementation, newImplementation);
274: log.info("Model: Implementation change undone");
275: return null;
276: }
277: }
278: }
|