001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: * Sebastian Davids - Fixed bug 25898
011: *******************************************************************************/package org.eclipse.jdt.ui.actions;
012:
013: import java.lang.reflect.InvocationTargetException;
014:
015: import org.eclipse.core.runtime.CoreException;
016: import org.eclipse.core.runtime.NullProgressMonitor;
017:
018: import org.eclipse.swt.widgets.Button;
019: import org.eclipse.swt.widgets.Composite;
020: import org.eclipse.swt.widgets.Shell;
021:
022: import org.eclipse.jface.dialogs.IDialogConstants;
023: import org.eclipse.jface.dialogs.MessageDialog;
024:
025: import org.eclipse.jface.text.ITextSelection;
026:
027: import org.eclipse.ui.PlatformUI;
028:
029: import org.eclipse.ltk.core.refactoring.Change;
030: import org.eclipse.ltk.core.refactoring.PerformChangeOperation;
031: import org.eclipse.ltk.core.refactoring.RefactoringStatus;
032: import org.eclipse.ltk.core.refactoring.RefactoringStatusEntry;
033: import org.eclipse.ltk.ui.refactoring.RefactoringUI;
034:
035: import org.eclipse.jdt.core.ICompilationUnit;
036: import org.eclipse.jdt.core.ISourceRange;
037:
038: import org.eclipse.jdt.internal.corext.refactoring.base.JavaStatusContext;
039: import org.eclipse.jdt.internal.corext.refactoring.surround.ISurroundWithTryCatchQuery;
040: import org.eclipse.jdt.internal.corext.refactoring.surround.SurroundWithTryCatchRefactoring;
041:
042: import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
043: import org.eclipse.jdt.internal.ui.actions.ActionUtil;
044: import org.eclipse.jdt.internal.ui.actions.SelectionConverter;
045: import org.eclipse.jdt.internal.ui.actions.WorkbenchRunnableAdapter;
046: import org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitEditor;
047: import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages;
048: import org.eclipse.jdt.internal.ui.util.BusyIndicatorRunnableContext;
049: import org.eclipse.jdt.internal.ui.util.ElementValidator;
050: import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
051:
052: /**
053: * Action to surround a set of statements with a try/catch block.
054: *
055: * <p>
056: * This class may be instantiated; it is not intended to be subclassed.
057: * </p>
058: *
059: * @since 2.0
060: */
061: public class SurroundWithTryCatchAction extends SelectionDispatchAction {
062:
063: private CompilationUnitEditor fEditor;
064:
065: private static class Query implements ISurroundWithTryCatchQuery {
066: private Shell fParent;
067:
068: public Query(Shell shell) {
069: fParent = shell;
070: }
071:
072: public boolean catchRuntimeException() {
073: MessageDialog dialog = new MessageDialog(
074: fParent,
075: getDialogTitle(),
076: null, // accept the default window icon
077: RefactoringMessages.SurroundWithTryCatchAction_no_exceptions,
078: MessageDialog.QUESTION, new String[] {
079: IDialogConstants.YES_LABEL,
080: IDialogConstants.NO_LABEL }, 1) {
081: // Work around for http://dev.eclipse.org/bugs/show_bug.cgi?id=18303
082: protected void createButtonsForButtonBar(
083: Composite parent) {
084: super .createButtonsForButtonBar(parent);
085: Button button = getButton(1);
086: if (button != null)
087: button.setFocus();
088: }
089: };
090: return dialog.open() == 0; // yes selected
091: }
092: }
093:
094: /**
095: * Note: This constructor is for internal use only. Clients should not call this constructor.
096: * @param editor the compilation unit editor
097: */
098: public SurroundWithTryCatchAction(CompilationUnitEditor editor) {
099: super (editor.getEditorSite());
100: setText(RefactoringMessages.SurroundWithTryCatchAction_label);
101: fEditor = editor;
102: setEnabled((fEditor != null && SelectionConverter
103: .getInputAsCompilationUnit(fEditor) != null));
104: PlatformUI.getWorkbench().getHelpSystem().setHelp(this ,
105: IJavaHelpContextIds.SURROUND_WITH_TRY_CATCH_ACTION);
106: }
107:
108: public void run(ITextSelection selection) {
109: if (!ActionUtil.isEditable(fEditor))
110: return;
111: ICompilationUnit cu = SelectionConverter
112: .getInputAsCompilationUnit(fEditor);
113: if (cu == null
114: || !ElementValidator.checkValidateEdit(cu, getShell(),
115: getDialogTitle()))
116: return;
117: SurroundWithTryCatchRefactoring refactoring = SurroundWithTryCatchRefactoring
118: .create(cu, selection, new Query(getShell()));
119:
120: if (refactoring == null)
121: return;
122: try {
123: RefactoringStatus status = refactoring
124: .checkInitialConditions(new NullProgressMonitor());
125: if (status.hasFatalError()) {
126: RefactoringStatusEntry entry = status
127: .getEntryMatchingSeverity(RefactoringStatus.FATAL);
128: MessageDialog.openInformation(getShell(),
129: getDialogTitle(), entry.getMessage());
130: if (entry.getContext() instanceof JavaStatusContext
131: && fEditor != null) {
132: JavaStatusContext context = (JavaStatusContext) entry
133: .getContext();
134: ISourceRange range = context.getSourceRange();
135: fEditor.setHighlightRange(range.getOffset(), range
136: .getLength(), true);
137: }
138: return;
139: }
140: if (refactoring.stopExecution())
141: return;
142: Change change = refactoring
143: .createChange(new NullProgressMonitor());
144: change.initializeValidationData(new NullProgressMonitor());
145: PerformChangeOperation op = RefactoringUI
146: .createUIAwareChangeOperation(change);
147: // must be fork == false since file buffers can't be manipulated in a different thread.
148: WorkbenchRunnableAdapter adapter = new WorkbenchRunnableAdapter(
149: op);
150: PlatformUI.getWorkbench().getProgressService().runInUI(
151: new BusyIndicatorRunnableContext(), adapter,
152: adapter.getSchedulingRule());
153: } catch (CoreException e) {
154: ExceptionHandler
155: .handle(
156: e,
157: getDialogTitle(),
158: RefactoringMessages.SurroundWithTryCatchAction_exception);
159: } catch (InvocationTargetException e) {
160: ExceptionHandler
161: .handle(
162: e,
163: getDialogTitle(),
164: RefactoringMessages.SurroundWithTryCatchAction_exception);
165: } catch (InterruptedException e) {
166: // not cancelable
167: }
168: }
169:
170: public void selectionChanged(ITextSelection selection) {
171: setEnabled(selection.getLength() > 0
172: && (fEditor != null && SelectionConverter
173: .getInputAsCompilationUnit(fEditor) != null));
174: }
175:
176: private static String getDialogTitle() {
177: return RefactoringMessages.SurroundWithTryCatchAction_dialog_title;
178: }
179: }
|