1 /*******************************************************************************
2 * Copyright (c) 2000, 2011 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * IBM Corporation - initial API and implementation
10 * Sebastian Davids - Fixed bug 25898
11 *******************************************************************************/
12 package org.eclipse.jdt.ui.actions;
14 import java.lang.reflect.InvocationTargetException;
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.NullProgressMonitor;
19 import org.eclipse.jface.dialogs.MessageDialog;
21 import org.eclipse.jface.text.BadLocationException;
22 import org.eclipse.jface.text.ITextSelection;
24 import org.eclipse.ui.PlatformUI;
26 import org.eclipse.ltk.core.refactoring.Change;
27 import org.eclipse.ltk.core.refactoring.PerformChangeOperation;
28 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
29 import org.eclipse.ltk.core.refactoring.RefactoringStatusEntry;
31 import org.eclipse.jdt.core.ICompilationUnit;
32 import org.eclipse.jdt.core.ISourceRange;
34 import org.eclipse.jdt.internal.corext.refactoring.base.JavaStatusContext;
35 import org.eclipse.jdt.internal.corext.refactoring.surround.SurroundWithTryCatchRefactoring;
37 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
38 import org.eclipse.jdt.internal.ui.actions.ActionUtil;
39 import org.eclipse.jdt.internal.ui.actions.SelectionConverter;
40 import org.eclipse.jdt.internal.ui.actions.WorkbenchRunnableAdapter;
41 import org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitEditor;
42 import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages;
43 import org.eclipse.jdt.internal.ui.util.ElementValidator;
44 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
45 import org.eclipse.jdt.internal.ui.viewsupport.LinkedProposalModelPresenter;
48 * Action to surround a set of statements with a try/catch block.
51 * This class may be instantiated; it is not intended to be subclassed.
56 * @noextend This class is not intended to be subclassed by clients.
58 public class SurroundWithTryCatchAction extends SelectionDispatchAction {
60 CompilationUnitEditor fEditor;
63 * Note: This constructor is for internal use only. Clients should not call this constructor.
64 * @param editor the compilation unit editor
66 * @noreference This constructor is not intended to be referenced by clients.
68 public SurroundWithTryCatchAction(CompilationUnitEditor editor) {
69 super(editor.getEditorSite());
70 setText(RefactoringMessages.SurroundWithTryCatchAction_label);
72 setEnabled(isApplicable());
73 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.SURROUND_WITH_TRY_CATCH_ACTION);
77 public void run(ITextSelection selection) {
78 if (!ActionUtil.isEditable(fEditor))
80 ICompilationUnit cu= SelectionConverter.getInputAsCompilationUnit(fEditor);
81 if (cu == null || !ElementValidator.checkValidateEdit(cu, getShell(), getDialogTitle()))
83 SurroundWithTryCatchRefactoring refactoring= createRefactoring(selection, cu);
85 if (refactoring == null)
88 RefactoringStatus status= refactoring.checkInitialConditions(new NullProgressMonitor());
89 if (status.hasFatalError()) {
90 RefactoringStatusEntry entry= status.getEntryMatchingSeverity(RefactoringStatus.FATAL);
91 MessageDialog.openInformation(getShell(), getDialogTitle(), entry.getMessage());
92 if (entry.getContext() instanceof JavaStatusContext && fEditor != null) {
93 JavaStatusContext context= (JavaStatusContext)entry.getContext();
94 ISourceRange range= context.getSourceRange();
95 fEditor.setHighlightRange(range.getOffset(), range.getLength(), true);
99 if (refactoring.stopExecution())
101 Change change= refactoring.createChange(new NullProgressMonitor());
102 change.initializeValidationData(new NullProgressMonitor());
103 PerformChangeOperation op= new PerformChangeOperation(change);
104 // must be fork == false since file buffers can't be manipulated in a different thread.
105 WorkbenchRunnableAdapter adapter= new WorkbenchRunnableAdapter(op);
106 adapter.generated_4302978336927222909();
108 new LinkedProposalModelPresenter().enterLinkedMode(fEditor.getViewer(), fEditor, false, refactoring.getLinkedProposalModel());
110 } catch (CoreException e) {
111 ExceptionHandler.handle(e, getDialogTitle(), RefactoringMessages.SurroundWithTryCatchAction_exception);
112 } catch (InvocationTargetException e) {
113 ExceptionHandler.handle(e, getDialogTitle(), RefactoringMessages.SurroundWithTryCatchAction_exception);
114 } catch (InterruptedException e) {
116 } catch (BadLocationException e) {
121 SurroundWithTryCatchRefactoring createRefactoring(ITextSelection selection, ICompilationUnit cu) {
122 return SurroundWithTryCatchRefactoring.create(cu, selection);
126 public void selectionChanged(ITextSelection selection) {
127 setEnabled(selection.getLength() > 0 && isApplicable());
130 boolean isApplicable() {
131 return fEditor != null && SelectionConverter.getInputAsCompilationUnit(fEditor) != null;
134 String getDialogTitle() {
135 return RefactoringMessages.SurroundWithTryCatchAction_dialog_title;