]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-after/ui/org/eclipse/jdt/ui/actions/SurroundWithTryCatchAction.java
060abde76ecd93fed196ddd05349a7eefd21e284
[ifi-stolz-refaktor.git] / case-study / jdt-after / ui / org / eclipse / jdt / ui / actions / SurroundWithTryCatchAction.java
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
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *     Sebastian Davids - Fixed bug 25898
11  *******************************************************************************/
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.jface.dialogs.MessageDialog;
20
21 import org.eclipse.jface.text.BadLocationException;
22 import org.eclipse.jface.text.ITextSelection;
23
24 import org.eclipse.ui.PlatformUI;
25
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;
30
31 import org.eclipse.jdt.core.ICompilationUnit;
32 import org.eclipse.jdt.core.ISourceRange;
33
34 import org.eclipse.jdt.internal.corext.refactoring.base.JavaStatusContext;
35 import org.eclipse.jdt.internal.corext.refactoring.surround.SurroundWithTryCatchRefactoring;
36
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;
46
47 /**
48  * Action to surround a set of statements with a try/catch block.
49  *
50  * <p>
51  * This class may be instantiated; it is not intended to be subclassed.
52  * </p>
53  *
54  * @since 2.0
55  *
56  * @noextend This class is not intended to be subclassed by clients.
57  */
58 public class SurroundWithTryCatchAction extends SelectionDispatchAction {
59
60         CompilationUnitEditor fEditor;
61
62         /**
63          * Note: This constructor is for internal use only. Clients should not call this constructor.
64          * @param editor the compilation unit editor
65          *
66          * @noreference This constructor is not intended to be referenced by clients.
67          */
68         public SurroundWithTryCatchAction(CompilationUnitEditor editor) {
69                 super(editor.getEditorSite());
70                 setText(RefactoringMessages.SurroundWithTryCatchAction_label);
71                 fEditor= editor;
72                 setEnabled(isApplicable());
73                 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.SURROUND_WITH_TRY_CATCH_ACTION);
74         }
75
76         @Override
77         public void run(ITextSelection selection) {
78                 if (!ActionUtil.isEditable(fEditor))
79                         return;
80                 ICompilationUnit cu= SelectionConverter.getInputAsCompilationUnit(fEditor);
81                 if (cu == null || !ElementValidator.checkValidateEdit(cu, getShell(), getDialogTitle()))
82                         return;
83                 SurroundWithTryCatchRefactoring refactoring= createRefactoring(selection, cu);
84
85                 if (refactoring == null)
86                         return;
87                 try {
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);
96                                 }
97                                 return;
98                         }
99                         if (refactoring.stopExecution())
100                                 return;
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();
107
108                         new LinkedProposalModelPresenter().enterLinkedMode(fEditor.getViewer(), fEditor, false, refactoring.getLinkedProposalModel());
109
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) {
115                         // not cancelable
116                 } catch (BadLocationException e) {
117                         // ignore
118                 }
119         }
120
121         SurroundWithTryCatchRefactoring createRefactoring(ITextSelection selection, ICompilationUnit cu) {
122                 return SurroundWithTryCatchRefactoring.create(cu, selection);
123         }
124
125         @Override
126         public void selectionChanged(ITextSelection selection) {
127                 setEnabled(selection.getLength() > 0 && isApplicable());
128         }
129
130         boolean isApplicable() {
131                 return fEditor != null && SelectionConverter.getInputAsCompilationUnit(fEditor) != null;
132         }
133
134         String getDialogTitle() {
135                 return RefactoringMessages.SurroundWithTryCatchAction_dialog_title;
136         }
137 }