]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-before/ui/org/eclipse/jdt/ui/actions/FindBreakContinueTargetOccurrencesAction.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-before / ui / org / eclipse / jdt / ui / actions / FindBreakContinueTargetOccurrencesAction.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  *******************************************************************************/
11 package org.eclipse.jdt.ui.actions;
12
13 import org.eclipse.swt.widgets.Shell;
14
15 import org.eclipse.jface.viewers.IStructuredSelection;
16
17 import org.eclipse.jface.text.ITextSelection;
18
19 import org.eclipse.ui.IWorkbenchSite;
20 import org.eclipse.ui.PlatformUI;
21
22 import org.eclipse.ui.texteditor.IEditorStatusLine;
23
24 import org.eclipse.jdt.core.ITypeRoot;
25 import org.eclipse.jdt.core.JavaModelException;
26 import org.eclipse.jdt.core.dom.CompilationUnit;
27
28 import org.eclipse.jdt.ui.JavaUI;
29
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;
38
39 /**
40  * Action to find all break/continue targets for a given break or continue statement.
41  * <p>
42  * This class may be instantiated; it is not intended to be subclassed.
43  * </p>
44  *
45  * @since 3.4
46  *
47  * @noextend This class is not intended to be subclassed by clients.
48  */
49 public class FindBreakContinueTargetOccurrencesAction extends SelectionDispatchAction {
50
51         private JavaEditor fEditor;
52
53         /**
54          * Note: This constructor is for internal use only. Clients should not call this constructor.
55          *
56          * @param editor the Java editor
57          *
58          * @noreference This constructor is not intended to be referenced by clients.
59          */
60         public FindBreakContinueTargetOccurrencesAction(JavaEditor editor) {
61                 this(editor.getEditorSite());
62                 fEditor= editor;
63                 setEnabled(getEditorInput(editor) != null);
64         }
65
66         /**
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>.
70          *
71          * @param site the site providing context information for this action
72          */
73         public FindBreakContinueTargetOccurrencesAction(IWorkbenchSite site) {
74                 super(site);
75                 setText(ActionMessages.FindBreakContinueTargetOccurrencesAction_label);
76                 setToolTipText(ActionMessages.FindBreakContinueTargetOccurrencesAction_tooltip);
77                 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.FIND_BREAK_CONTINUE_TARGET_OCCURRENCES);
78         }
79
80         //---- Text Selection ----------------------------------------------------------------------
81
82         /**
83          * {@inheritDoc}
84          */
85         @Override
86         public void selectionChanged(ITextSelection selection) {
87                 setEnabled(true);
88         }
89
90         /**
91          * Note: This method is for internal use only. Clients should not call this method.
92          *
93          * @param selection the selection
94          *
95          * @noreference This method is not intended to be referenced by clients.
96          */
97         @Override
98         public void selectionChanged(JavaTextSelection selection) {
99                 CompilationUnit astRoot= selection.resolvePartialAstAtOffset();
100                 setEnabled(astRoot != null && new BreakContinueTargetFinder().initialize(astRoot, selection.getOffset(), selection.getLength()) == null);
101         }
102
103         /**
104          * {@inheritDoc}
105          */
106         @Override
107         public void selectionChanged(IStructuredSelection selection) {
108                 setEnabled(false);
109         }
110
111         /* (non-JavaDoc)
112          * Method declared in SelectionDispatchAction.
113          */
114         @Override
115         public final void run(ITextSelection ts) {
116                 ITypeRoot input= getEditorInput(fEditor);
117                 if (!ActionUtil.isProcessable(getShell(), input))
118                         return;
119                 FindOccurrencesEngine engine= FindOccurrencesEngine.create(new BreakContinueTargetFinder());
120                 try {
121                         String result= engine.run(input, ts.getOffset(), ts.getLength());
122                         if (result != null)
123                                 showMessage(getShell(), fEditor, result);
124                 } catch (JavaModelException e) {
125                         JavaPlugin.log(e);
126                 }
127         }
128
129         private static ITypeRoot getEditorInput(JavaEditor editor) {
130                 return JavaUI.getEditorInputTypeRoot(editor.getEditorInput());
131         }
132
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();
138         }
139 }