001: /**
002: *
003: */package gui.actions;
004:
005: import gui.wizards.RenameImplementationWizard;
006: import logging.LoggingPlugin;
007: import model.PropertyUtil;
008:
009: import org.apache.commons.logging.Log;
010: import org.apache.commons.logging.LogFactory;
011: import org.eclipse.core.runtime.ILog;
012: import org.eclipse.core.runtime.IStatus;
013: import org.eclipse.core.runtime.Status;
014: import org.eclipse.gef.EditPart;
015: import org.eclipse.jface.action.Action;
016: import org.eclipse.jface.action.IAction;
017: import org.eclipse.jface.dialogs.MessageDialog;
018: import org.eclipse.jface.viewers.ISelection;
019: import org.eclipse.jface.viewers.IStructuredSelection;
020: import org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor;
021: import org.eclipse.ltk.ui.refactoring.RefactoringWizardOpenOperation;
022: import org.eclipse.ui.IActionDelegate;
023:
024: import core.ModelInfo;
025: import core.RenameImplementationProcessor;
026: import core.RenameProcessorHolder;
027:
028: /**
029: * This action will be called from the Context Menu in the Editor.
030: * It starts the refactor implementation process, which changes
031: * the implementation property for a model element. After
032: * that the source code will be adapted to the changes
033: * in the model
034: *
035: * @author sh
036: */
037: public class RenameImplementationAction extends Action implements
038: IActionDelegate {
039:
040: // the current selection
041: private EditPart selectedEditPart = null;
042:
043: // the Info Object to refacter
044: private ModelInfo info = new ModelInfo();
045:
046: private Log log = LogFactory.getLog(getClass());
047:
048: /* (non-Javadoc)
049: * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
050: */
051: @Override
052: public void run(IAction action) {
053: if (selectedEditPart == null) {
054: refuse();
055: } else {
056: applySelection();
057: if (ActionUtil.saveAll()) {
058: openWizard();
059: }
060: }
061: }
062:
063: /**
064: * helper method which is called when the current
065: * selection cannot be used for that refactoring.
066: */
067: private void refuse() {
068: String title = "Rename Implementation";
069: String message = "This refactoring is not available for the selected Element.";
070: MessageDialog.openInformation(ActionUtil.getShell(), title,
071: message);
072: }
073:
074: /**
075: * helper method which applies the selection
076: * and stores it in the info object.
077: */
078: private void applySelection() {
079: info.setOldImplementation(PropertyUtil
080: .getImplementation(selectedEditPart));
081: info.setNewImplementation(PropertyUtil
082: .getImplementation(selectedEditPart));
083: info.setSelectedEditPart(selectedEditPart);
084: }
085:
086: /**
087: * helper method for opening the type refactoring wizard.
088: */
089: private void openWizard() {
090: RefactoringProcessor processor = new RenameImplementationProcessor(
091: info);
092: RenameProcessorHolder ref = new RenameProcessorHolder(processor);
093: RenameImplementationWizard wizard = new RenameImplementationWizard(
094: ref, info);
095: RefactoringWizardOpenOperation op = new RefactoringWizardOpenOperation(
096: wizard);
097: try {
098: String titleForFailedChecks = "";
099: op.run(ActionUtil.getShell(), titleForFailedChecks);
100: } catch (final InterruptedException irex) {
101: String message = " operation was canceled";
102: ILog logger = LoggingPlugin.getDefault().getLog();
103: String symName = LoggingPlugin.getDefault().getBundle()
104: .getSymbolicName();
105: logger.log(new Status(IStatus.ERROR, symName, 0, message,
106: irex));
107: throw new RuntimeException(irex + message);
108: }
109: }
110:
111: /* (non-Javadoc)
112: * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
113: */
114: @Override
115: public void selectionChanged(IAction action, ISelection selection) {
116: if (selection instanceof IStructuredSelection == false)
117: return;
118:
119: IStructuredSelection structuredSelection = (IStructuredSelection) selection;
120: if (structuredSelection.getFirstElement() instanceof EditPart == false)
121: return;
122:
123: // check if the selected EditPart is supported
124: boolean isSupported = PropertyUtil
125: .isEditPartSupported((EditPart) structuredSelection
126: .getFirstElement());
127: if (isSupported)
128: selectedEditPart = (EditPart) structuredSelection
129: .getFirstElement();
130: }
131: }
|