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 *******************************************************************************/
11 package org.eclipse.jdt.ui.actions;
13 import org.eclipse.swt.widgets.Shell;
15 import org.eclipse.jface.viewers.IStructuredSelection;
17 import org.eclipse.jface.text.ITextSelection;
19 import org.eclipse.ui.IWorkbenchSite;
20 import org.eclipse.ui.PlatformUI;
22 import org.eclipse.ui.texteditor.IEditorStatusLine;
24 import org.eclipse.jdt.core.ITypeRoot;
25 import org.eclipse.jdt.core.JavaModelException;
26 import org.eclipse.jdt.core.dom.CompilationUnit;
28 import org.eclipse.jdt.ui.JavaUI;
30 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
31 import org.eclipse.jdt.internal.ui.JavaPlugin;
32 import org.eclipse.jdt.internal.ui.actions.ActionMessages;
33 import org.eclipse.jdt.internal.ui.actions.ActionUtil;
34 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
35 import org.eclipse.jdt.internal.ui.javaeditor.JavaTextSelection;
36 import org.eclipse.jdt.internal.ui.search.BreakContinueTargetFinder;
37 import org.eclipse.jdt.internal.ui.search.FindOccurrencesEngine;
40 * Action to find all break/continue targets for a given break or continue statement.
42 * This class may be instantiated; it is not intended to be subclassed.
47 * @noextend This class is not intended to be subclassed by clients.
49 public class FindBreakContinueTargetOccurrencesAction extends SelectionDispatchAction {
51 private JavaEditor fEditor;
54 * Note: This constructor is for internal use only. Clients should not call this constructor.
56 * @param editor the Java editor
58 * @noreference This constructor is not intended to be referenced by clients.
60 public FindBreakContinueTargetOccurrencesAction(JavaEditor editor) {
61 this(editor.getEditorSite());
63 setEnabled(getEditorInput(editor) != null);
67 * Creates a new {@link FindBreakContinueTargetOccurrencesAction}. The action
68 * requires that the selection provided by the site's selection provider is of type
69 * <code>IStructuredSelection</code>.
71 * @param site the site providing context information for this action
73 public FindBreakContinueTargetOccurrencesAction(IWorkbenchSite site) {
75 setText(ActionMessages.FindBreakContinueTargetOccurrencesAction_label);
76 setToolTipText(ActionMessages.FindBreakContinueTargetOccurrencesAction_tooltip);
77 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.FIND_BREAK_CONTINUE_TARGET_OCCURRENCES);
80 //---- Text Selection ----------------------------------------------------------------------
86 public void selectionChanged(ITextSelection selection) {
91 * Note: This method is for internal use only. Clients should not call this method.
93 * @param selection the selection
95 * @noreference This method is not intended to be referenced by clients.
98 public void selectionChanged(JavaTextSelection selection) {
99 CompilationUnit astRoot= selection.resolvePartialAstAtOffset();
100 setEnabled(astRoot != null && new BreakContinueTargetFinder().initialize(astRoot, selection.getOffset(), selection.getLength()) == null);
107 public void selectionChanged(IStructuredSelection selection) {
112 * Method declared in SelectionDispatchAction.
115 public final void run(ITextSelection ts) {
116 ITypeRoot input= getEditorInput(fEditor);
117 if (!ActionUtil.isProcessable(getShell(), input))
119 FindOccurrencesEngine engine= FindOccurrencesEngine.create(new BreakContinueTargetFinder());
121 String result= engine.run(input, ts.getOffset(), ts.getLength());
123 showMessage(getShell(), fEditor, result);
124 } catch (JavaModelException e) {
129 private static ITypeRoot getEditorInput(JavaEditor editor) {
130 return JavaUI.getEditorInputTypeRoot(editor.getEditorInput());
133 private static void showMessage(Shell shell, JavaEditor editor, String msg) {
134 IEditorStatusLine statusLine= (IEditorStatusLine) editor.getAdapter(IEditorStatusLine.class);
135 if (statusLine != null)
136 statusLine.setMessage(true, msg, null);
137 shell.getDisplay().beep();