001: /**
002: *
003: */package gui.actions;
004:
005: import gui.section.IEnvEntryViewer;
006: import gui.wizards.RenameTypeWizard;
007:
008: import java.util.Iterator;
009: import java.util.List;
010: import java.util.Set;
011:
012: import logging.LoggingPlugin;
013: import model.ModelUtil;
014:
015: import org.apache.commons.logging.Log;
016: import org.apache.commons.logging.LogFactory;
017: import org.eclipse.core.runtime.ILog;
018: import org.eclipse.core.runtime.IStatus;
019: import org.eclipse.core.runtime.Status;
020: import org.eclipse.gef.EditPart;
021: import org.eclipse.jdt.core.ICompilationUnit;
022: import org.eclipse.jdt.core.dom.FieldDeclaration;
023: import org.eclipse.jface.action.Action;
024: import org.eclipse.jface.dialogs.MessageDialog;
025: import org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor;
026: import org.eclipse.ltk.ui.refactoring.RefactoringWizardOpenOperation;
027:
028: import core.EnvEntryProcessor;
029: import core.FieldDeclarationParser;
030: import core.RefactorInfo;
031: import core.RenameProcessorHolder;
032: import core.Util;
033: import diagram.section.EnvEntry;
034:
035: /**
036: * This action will be called from the Environment Entry
037: * property sheet page. It startes the refactor type
038: * process, which refactors a variable declaration type.
039: * It openes a refactoring wizard and adapts the changes
040: * to the source and the model. This currently only workes
041: * when the file in the workspace is selected.
042: *
043: * @author sh
044: */
045: public class RefactorEnvEntryAction extends Action {
046:
047: // the selected EditPart
048: private EditPart selectedEditPart = null;
049:
050: // the selected environment entry
051: private EnvEntry envEntry = null;
052:
053: // the Info Object to refacter
054: private RefactorInfo info = new RefactorInfo();
055:
056: private Log log = LogFactory.getLog(getClass());
057:
058: // FIXME
059: // the viewer set is needed to execute the table model update
060: // after an environment entry has been changed. It will be
061: // transmitted from EnvEntryList, to RefactorEnvEntryAction,
062: // to EnvEntryProcessor and finally to EnvEntryDelegate where the
063: // update will be performed.
064: private Set<IEnvEntryViewer> changeListeners = null;
065:
066: /**
067: * Constructor
068: *
069: * @param selectedEditPart the selected EditPart
070: * @param envEntry the selected environment entry
071: */
072: public RefactorEnvEntryAction(EditPart selectedEditPart,
073: EnvEntry envEntry, Set<IEnvEntryViewer> changeListeners) {
074: super ();
075: this .selectedEditPart = selectedEditPart;
076: this .envEntry = envEntry;
077: this .changeListeners = changeListeners;
078: }
079:
080: /*
081: * Starts the action. Before the refactorig wizard appears we check
082: * if a java class exists for the selected model element.
083: * If so we check if the selected environment entry exists.
084: * If so the source code refactoring will be performed, otherwise
085: * the environment entry source variable will be created.
086: *
087: * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
088: */
089: @Override
090: public void run() {
091: super .run();
092:
093: // check if the context is ok
094: if (selectedEditPart == null || envEntry == null) {
095: refuse();
096: } else {
097: // apply the values of the current environment entry
098: applySelection();
099:
100: // check if source code is available
101: ICompilationUnit unit = getCompilationUnit();
102: if (unit == null) {
103: noSourceCodeMessage();
104: return;
105: } else {
106: // add the source code file to the info object
107: info.setUnit(unit);
108: }
109:
110: // check if the environment entry already exist
111: FieldDeclaration fDecl = new FieldDeclarationParser()
112: .parseFieldDeclarations(unit, envEntry);
113: info.setElement(fDecl);
114:
115: if (ActionUtil.saveAll()) {
116: openWizard();
117: }
118: }
119: }
120:
121: /**
122: * helper method which is called if the current
123: * selection cannot be used for that refactoring.
124: */
125: private void refuse() {
126: String title = "Environment Entry Refactoring";
127: String message = "This refactoring is only available on Java field properties.";
128: MessageDialog.openInformation(ActionUtil.getShell(), title,
129: message);
130: }
131:
132: /**
133: * helper method wich is called if no underlying java source
134: * file has been found for the model element.
135: */
136: private void noSourceCodeMessage() {
137: String title = "Environment Entry Refactoring";
138: String message = "No concerning Java source file available.";
139: MessageDialog.openInformation(ActionUtil.getShell(), title,
140: message);
141: }
142:
143: /**
144: * helper method which applies the selection
145: * and stores it in the info object.
146: */
147: private void applySelection() {
148: info.setNewName(envEntry.getType());
149: info.setOldName(envEntry.getType());
150: info.setNewVarName(envEntry.getName());
151: info.setOldVarName(envEntry.getName());
152: }
153:
154: /**
155: * helper method which searchs the compilation
156: * unit for the selected EditPart.
157: *
158: * @return unit the compilation unit
159: */
160: private ICompilationUnit getCompilationUnit() {
161: // get the implementation statement of the EditPart
162: String epImpl = ModelUtil
163: .getModelImplProperty(selectedEditPart);
164: if (epImpl == null)
165: return null;
166:
167: // get all compilation units
168: List<ICompilationUnit> units = Util.getICompilationUnits();
169: for (Iterator<ICompilationUnit> iterator = units.iterator(); iterator
170: .hasNext();) {
171: ICompilationUnit unit = (ICompilationUnit) iterator.next();
172: String sourceImpl = Util.getImplementation(unit);
173:
174: // check the concerning cu has been found
175: if (epImpl.equals(sourceImpl))
176: return unit;
177: }
178: return null;
179: }
180:
181: /**
182: * helper method for opening the type refactoring wizard.
183: */
184: private void openWizard() {
185: RefactoringProcessor processor = new EnvEntryProcessor(info,
186: envEntry, changeListeners);
187: RenameProcessorHolder ref = new RenameProcessorHolder(processor);
188: RenameTypeWizard wizard = new RenameTypeWizard(ref, info);
189: RefactoringWizardOpenOperation op = new RefactoringWizardOpenOperation(
190: wizard);
191: try {
192: String titleForFailedChecks = "";
193: op.run(ActionUtil.getShell(), titleForFailedChecks);
194: } catch (final InterruptedException irex) {
195: String message = "operation was canceled";
196: ILog logger = LoggingPlugin.getDefault().getLog();
197: String symName = LoggingPlugin.getDefault().getBundle()
198: .getSymbolicName();
199: logger.log(new Status(IStatus.ERROR, symName, 0, message,
200: irex));
201: throw new RuntimeException(irex + message);
202: }
203: }
204: }
|