KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > ui > actions > SurroundWithTryCatchAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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 JavaDoc;
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 /**
54  * Action to surround a set of statements with a try/catch block.
55  *
56  * <p>
57  * This class may be instantiated; it is not intended to be subclassed.
58  * </p>
59  *
60  * @since 2.0
61  */

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, // accept the default window icon
74
RefactoringMessages.SurroundWithTryCatchAction_no_exceptions,
75                 MessageDialog.QUESTION,
76                 new String JavaDoc[] {IDialogConstants.YES_LABEL, IDialogConstants.NO_LABEL},
77                 1) {
78                     // Work around for http://dev.eclipse.org/bugs/show_bug.cgi?id=18303
79
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; // yes selected
87
}
88     }
89
90     /**
91      * Note: This constructor is for internal use only. Clients should not call this constructor.
92      * @param editor the compilation unit editor
93      */

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             // must be fork == false since file buffers can't be manipulated in a different thread.
130
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 JavaDoc e) {
136             ExceptionHandler.handle(e, getDialogTitle(), RefactoringMessages.SurroundWithTryCatchAction_exception);
137         } catch (InterruptedException JavaDoc e) {
138             // not cancelable
139
}
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 JavaDoc getDialogTitle() {
147         return RefactoringMessages.SurroundWithTryCatchAction_dialog_title;
148     }
149 }
150
Popular Tags