1 12 package org.eclipse.jdt.ui.actions; 13 14 import java.lang.reflect.InvocationTargetException ; 15 16 import org.eclipse.core.runtime.CoreException; 17 import org.eclipse.core.runtime.NullProgressMonitor; 18 19 import org.eclipse.swt.widgets.Button; 20 import org.eclipse.swt.widgets.Composite; 21 import org.eclipse.swt.widgets.Shell; 22 23 import org.eclipse.jface.dialogs.IDialogConstants; 24 import org.eclipse.jface.dialogs.MessageDialog; 25 26 import org.eclipse.jface.text.ITextSelection; 27 28 import org.eclipse.ui.PlatformUI; 29 30 import org.eclipse.ltk.core.refactoring.Change; 31 import org.eclipse.ltk.core.refactoring.PerformChangeOperation; 32 import org.eclipse.ltk.core.refactoring.RefactoringStatus; 33 import org.eclipse.ltk.core.refactoring.RefactoringStatusEntry; 34 import org.eclipse.ltk.ui.refactoring.RefactoringUI; 35 36 import org.eclipse.jdt.core.ICompilationUnit; 37 import org.eclipse.jdt.core.ISourceRange; 38 39 import org.eclipse.jdt.internal.corext.refactoring.base.JavaStatusContext; 40 import org.eclipse.jdt.internal.corext.refactoring.surround.ISurroundWithTryCatchQuery; 41 import org.eclipse.jdt.internal.corext.refactoring.surround.SurroundWithTryCatchRefactoring; 42 43 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; 44 import org.eclipse.jdt.internal.ui.actions.ActionUtil; 45 import org.eclipse.jdt.internal.ui.actions.SelectionConverter; 46 import org.eclipse.jdt.internal.ui.actions.WorkbenchRunnableAdapter; 47 import org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitEditor; 48 import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages; 49 import org.eclipse.jdt.internal.ui.util.BusyIndicatorRunnableContext; 50 import org.eclipse.jdt.internal.ui.util.ElementValidator; 51 import org.eclipse.jdt.internal.ui.util.ExceptionHandler; 52 53 62 public class SurroundWithTryCatchAction extends SelectionDispatchAction { 63 64 private CompilationUnitEditor fEditor; 65 66 private static class Query implements ISurroundWithTryCatchQuery { 67 private Shell fParent; 68 public Query(Shell shell) { 69 fParent= shell; 70 } 71 public boolean catchRuntimeException() { 72 MessageDialog dialog = new MessageDialog( 73 fParent, getDialogTitle(), null, RefactoringMessages.SurroundWithTryCatchAction_no_exceptions, 75 MessageDialog.QUESTION, 76 new String [] {IDialogConstants.YES_LABEL, IDialogConstants.NO_LABEL}, 77 1) { 78 protected void createButtonsForButtonBar(Composite parent) { 80 super.createButtonsForButtonBar(parent); 81 Button button= getButton(1); 82 if (button != null) 83 button.setFocus(); 84 } 85 }; 86 return dialog.open() == 0; } 88 } 89 90 94 public SurroundWithTryCatchAction(CompilationUnitEditor editor) { 95 super(editor.getEditorSite()); 96 setText(RefactoringMessages.SurroundWithTryCatchAction_label); 97 fEditor= editor; 98 setEnabled((fEditor != null && SelectionConverter.getInputAsCompilationUnit(fEditor) != null)); 99 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.SURROUND_WITH_TRY_CATCH_ACTION); 100 } 101 102 public void run(ITextSelection selection) { 103 if (!ActionUtil.isEditable(fEditor)) 104 return; 105 ICompilationUnit cu= SelectionConverter.getInputAsCompilationUnit(fEditor); 106 if (cu == null || !ElementValidator.checkValidateEdit(cu, getShell(), getDialogTitle())) 107 return; 108 SurroundWithTryCatchRefactoring refactoring= SurroundWithTryCatchRefactoring.create(cu, selection, new Query(getShell())); 109 110 if (refactoring == null) 111 return; 112 try { 113 RefactoringStatus status= refactoring.checkInitialConditions(new NullProgressMonitor()); 114 if (status.hasFatalError()) { 115 RefactoringStatusEntry entry= status.getEntryMatchingSeverity(RefactoringStatus.FATAL); 116 MessageDialog.openInformation(getShell(), getDialogTitle(), entry.getMessage()); 117 if (entry.getContext() instanceof JavaStatusContext && fEditor != null) { 118 JavaStatusContext context= (JavaStatusContext)entry.getContext(); 119 ISourceRange range= context.getSourceRange(); 120 fEditor.setHighlightRange(range.getOffset(), range.getLength(), true); 121 } 122 return; 123 } 124 if (refactoring.stopExecution()) 125 return; 126 Change change= refactoring.createChange(new NullProgressMonitor()); 127 change.initializeValidationData(new NullProgressMonitor()); 128 PerformChangeOperation op= RefactoringUI.createUIAwareChangeOperation(change); 129 WorkbenchRunnableAdapter adapter= new WorkbenchRunnableAdapter(op); 131 PlatformUI.getWorkbench().getProgressService().runInUI( 132 new BusyIndicatorRunnableContext(), adapter, adapter.getSchedulingRule()); 133 } catch (CoreException e) { 134 ExceptionHandler.handle(e, getDialogTitle(), RefactoringMessages.SurroundWithTryCatchAction_exception); 135 } catch (InvocationTargetException e) { 136 ExceptionHandler.handle(e, getDialogTitle(), RefactoringMessages.SurroundWithTryCatchAction_exception); 137 } catch (InterruptedException e) { 138 } 140 } 141 142 public void selectionChanged(ITextSelection selection) { 143 setEnabled(selection.getLength() > 0 && (fEditor != null && SelectionConverter.getInputAsCompilationUnit(fEditor) != null)); 144 } 145 146 private static String getDialogTitle() { 147 return RefactoringMessages.SurroundWithTryCatchAction_dialog_title; 148 } 149 } 150 | Popular Tags |