]> git.uio.no Git - ifi-stolz-refaktor.git/blame - case-study/jdt-after/ui/org/eclipse/jdt/ui/actions/SurroundWithTryCatchAction.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-after / ui / org / eclipse / jdt / ui / actions / SurroundWithTryCatchAction.java
CommitLineData
1b2798f6
EK
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 *******************************************************************************/
12package org.eclipse.jdt.ui.actions;
13
14import java.lang.reflect.InvocationTargetException;
15
16import org.eclipse.core.runtime.CoreException;
17import org.eclipse.core.runtime.NullProgressMonitor;
18
19import org.eclipse.jface.dialogs.MessageDialog;
20
21import org.eclipse.jface.text.BadLocationException;
22import org.eclipse.jface.text.ITextSelection;
23
24import org.eclipse.ui.PlatformUI;
25
26import org.eclipse.ltk.core.refactoring.Change;
27import org.eclipse.ltk.core.refactoring.PerformChangeOperation;
28import org.eclipse.ltk.core.refactoring.RefactoringStatus;
29import org.eclipse.ltk.core.refactoring.RefactoringStatusEntry;
30
31import org.eclipse.jdt.core.ICompilationUnit;
32import org.eclipse.jdt.core.ISourceRange;
33
34import org.eclipse.jdt.internal.corext.refactoring.base.JavaStatusContext;
35import org.eclipse.jdt.internal.corext.refactoring.surround.SurroundWithTryCatchRefactoring;
36
37import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
38import org.eclipse.jdt.internal.ui.actions.ActionUtil;
39import org.eclipse.jdt.internal.ui.actions.SelectionConverter;
40import org.eclipse.jdt.internal.ui.actions.WorkbenchRunnableAdapter;
41import org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitEditor;
42import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages;
43import org.eclipse.jdt.internal.ui.util.ElementValidator;
44import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
45import 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 */
58public 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}