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