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.internal.core.PackageFragment;
017: import org.eclipse.ltk.core.refactoring.Change;
018: import org.eclipse.ltk.core.refactoring.RefactoringStatus;
019: import org.eclipse.ltk.core.refactoring.participants.RenameArguments;
020:
021: /**
022: * This change class is responsible for updating the implementation
023: * property in the process models. It is used by the package renaming
024: * refactoring. It checks which model element has the same implementation
025: * statement. If so the new string will be set. It also returns the
026: * undo operation for this modification.
027: *
028: * @author sh
029: */
030: public class PackageModelChange extends Change {
031: // the selected element to refacter
032: private Object selection = null;
033:
034: // the input of the refactoring dialog
035: private RenameArguments arguments = null;
036:
037: private Log log = LogFactory.getLog(getClass());
038:
039: /**
040: * Constructor
041: *
042: * @param selection the element to refacter
043: * @param arguments the input data of the refactoring dialog
044: */
045: public PackageModelChange(Object selection,
046: RenameArguments arguments) {
047: super ();
048: this .selection = selection;
049: this .arguments = arguments;
050: }
051:
052: /**
053: * Returns the element modified by this <code>Change</code>. The method may return
054: * <code>null</code> if the change isn't related to an element.
055: *
056: * @return the element modified by this change
057: */
058: @Override
059: public Object getModifiedElement() {
060: return null;
061: }
062:
063: /* (non-Javadoc)
064: * @see org.eclipse.ltk.core.refactoring.Change#getName()
065: */
066: @Override
067: public String getName() {
068: return new String("Implementation change");
069: }
070:
071: /* (non-Javadoc)
072: * @see org.eclipse.ltk.core.refactoring.Change#initializeValidationData(org.eclipse.core.runtime.IProgressMonitor)
073: */
074: @Override
075: public void initializeValidationData(IProgressMonitor pm) {
076: }
077:
078: /* (non-Javadoc)
079: * @see org.eclipse.ltk.core.refactoring.Change#isValid(org.eclipse.core.runtime.IProgressMonitor)
080: */
081: @Override
082: public RefactoringStatus isValid(IProgressMonitor pm)
083: throws CoreException, OperationCanceledException {
084: // if an fatal error will return, the refactoring will stoped
085: RefactoringStatus result = new RefactoringStatus();
086:
087: // TODO: check if some model could be read
088:
089: return result;
090: }
091:
092: /**
093: * Performs this change after the method isValid has been called. If isValid
094: * returns an fatal error or the change is disabled this change will not be performed.
095: * Changes should in general not respond to {@link IProgressMonitor#isCanceled()}
096: * since canceling a change tree in the middle of its execution leaves the workspace
097: * in a half changed state.
098: *
099: * @param pm a progress monitor
100: * @return the undo change for this change object or <code>null</code> if no
101: * undo is provided
102: * @throws CoreException if an error occurred during change execution
103: */
104: @Override
105: public Change perform(IProgressMonitor pm) throws CoreException {
106: // get new implementation
107: String newImplementation = this .arguments.getNewName();
108:
109: // get old implementation
110: String oldImplementation = ((PackageFragment) this .selection)
111: .getElementName();
112:
113: // check if values are valid
114: if (!valuesValid(newImplementation, oldImplementation)) {
115: String message = "Model Update could't be done !";
116: ILog logger = LoggingPlugin.getDefault().getLog();
117: String symName = LoggingPlugin.getDefault().getBundle()
118: .getSymbolicName();
119: logger.log(new Status(IStatus.ERROR, symName, 0, message,
120: null));
121: throw new RuntimeException(message);
122: }
123:
124: // start the model update
125: new ModelModifier().doPackageModification(oldImplementation,
126: newImplementation);
127: log.info("Model: Implementation change performed");
128:
129: // for the transaction handling the undo change element is returned
130: // which reverts the implementation property change to the original state
131: String undoOldImpl = newImplementation;
132: String undoNewImpl = oldImplementation;
133: return new PackageUndoModelChange(undoOldImpl, undoNewImpl);
134: }
135:
136: /**
137: * check if the given values are valid, means they are initialized and
138: * the string is not empty.
139: *
140: * @param newImpl the full qualified name of a compilation unit
141: * @param oldImpl the full qualified name of a compilation unit
142: *
143: * @return true if all values pass the check
144: */
145: private boolean valuesValid(String newPackage, String oldPackage) {
146: boolean valid = false;
147:
148: if (newPackage != null)
149: if (!newPackage.equals(""))
150: valid = true;
151:
152: if (oldPackage != null) {
153: if (!oldPackage.equals(""))
154: valid = true;
155: }
156:
157: return valid;
158: }
159:
160: /**
161: * The undo change class which is used by the PackageModelChange participant.
162: * If a model modification will be undone, an instance of that class
163: * will be returned to set the original state.
164: *
165: * @author sh
166: *
167: */
168: private class PackageUndoModelChange extends Change {
169: // the old implementation value to change
170: private String oldImplementation = null;
171: // the new implementation value to set
172: private String newImplementation = null;
173:
174: public Object getModifiedElement() {
175: return null;
176: }
177:
178: public String getName() {
179: return "Undo implementation change";
180: }
181:
182: public void initializeValidationData(IProgressMonitor pm) {
183: }
184:
185: public PackageUndoModelChange(String oldImplementation,
186: String newImplementation) {
187: super ();
188: this .oldImplementation = oldImplementation;
189: this .newImplementation = newImplementation;
190: }
191:
192: /**
193: * check if properties are set.
194: */
195: @Override
196: public RefactoringStatus isValid(IProgressMonitor pm)
197: throws CoreException, OperationCanceledException {
198:
199: if (this .oldImplementation == null
200: || this .newImplementation == null) {
201: String message = "unable to undo the model implementation modification";
202: ILog logger = LoggingPlugin.getDefault().getLog();
203: String symName = LoggingPlugin.getDefault().getBundle()
204: .getSymbolicName();
205: logger.log(new Status(IStatus.ERROR, symName, 0,
206: message, null));
207: throw new RuntimeException(message);
208: }
209: return new RefactoringStatus();
210: }
211:
212: /**
213: * perform the undo operation
214: */
215: @Override
216: public Change perform(IProgressMonitor pm) throws CoreException {
217: new ModelModifier().doPackageModification(
218: oldImplementation, newImplementation);
219: log.info("Model: Implementation change undone");
220: return null;
221: }
222: }
223: }
|