fix some warnings
[eclipsethinslicer.git] / Svelte / src / edu / berkeley / cs / bodik / svelte / plugin / SlicingActionDelegate.java
blobf4d2a9066aa1cbf51cad3bb6b77d2ff006776803
1 /*******************************************************************************
2 * This program and the accompanying materials
3 * are made available under the terms of the Eclipse Public License v1.0
4 * which accompanies this distribution, and is available at
5 * http://www.eclipse.org/legal/epl-v10.html.
6 *
7 * This file is a derivative of code released by the University of
8 * California under the terms listed below.
10 * Refinement Analysis Tools is Copyright ©2007 The Regents of the
11 * University of California (Regents). Provided that this notice and
12 * the following two paragraphs are included in any distribution of
13 * Refinement Analysis Tools or its derivative work, Regents agrees
14 * not to assert any of Regents' copyright rights in Refinement
15 * Analysis Tools against recipient for recipient’s reproduction,
16 * preparation of derivative works, public display, public
17 * performance, distribution or sublicensing of Refinement Analysis
18 * Tools and derivative works, in source code and object code form.
19 * This agreement not to assert does not confer, by implication,
20 * estoppel, or otherwise any license or rights in any intellectual
21 * property of Regents, including, but not limited to, any patents
22 * of Regents or Regents’ employees.
24 * IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT,
25 * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
26 * INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE
27 * AND ITS DOCUMENTATION, EVEN IF REGENTS HAS BEEN ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
30 * REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
31 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
32 * FOR A PARTICULAR PURPOSE AND FURTHER DISCLAIMS ANY STATUTORY
33 * WARRANTY OF NON-INFRINGEMENT. THE SOFTWARE AND ACCOMPANYING
34 * DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS
35 * IS". REGENTS HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT,
36 * UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
38 package edu.berkeley.cs.bodik.svelte.plugin;
40 import org.eclipse.core.resources.IFile;
41 import org.eclipse.core.resources.IProject;
42 import org.eclipse.core.resources.ResourcesPlugin;
43 import org.eclipse.jdt.core.ICompilationUnit;
44 import org.eclipse.jdt.core.dom.ASTNode;
45 import org.eclipse.jdt.core.dom.PackageDeclaration;
46 import org.eclipse.jdt.core.dom.TypeDeclaration;
47 import org.eclipse.jdt.ui.IWorkingCopyManager;
48 import org.eclipse.jdt.ui.JavaUI;
49 import org.eclipse.jface.action.IAction;
50 import org.eclipse.jface.dialogs.MessageDialogWithToggle;
51 import org.eclipse.jface.text.ITextSelection;
52 import org.eclipse.jface.text.Position;
53 import org.eclipse.jface.viewers.ISelection;
54 import org.eclipse.ui.IEditorActionDelegate;
55 import org.eclipse.ui.IEditorPart;
56 import org.eclipse.ui.IFileEditorInput;
57 import org.eclipse.ui.IWorkbenchPage;
58 import org.eclipse.ui.IWorkbenchWindow;
59 import org.eclipse.ui.PlatformUI;
60 import org.eclipse.ui.texteditor.AbstractTextEditor;
62 import edu.berkeley.cs.bodik.svelte.Slicing.SliceType;
64 /**
65 * Base class for the various *EditorActionDelegates that provides the bootstrapping required to
66 * take a slice, and then takes it.
68 * @author evan
71 public abstract class SlicingActionDelegate implements IEditorActionDelegate {
72 private IEditorPart editor;
73 protected ICompilationUnit cu;
75 public void selectionChanged(IAction action, ISelection selection) {
78 public static Position getPositionFromLineNum(String s, int linenum) {
79 int start = 0;
80 int l = 0;
81 int i = 0;
82 for (i = 0; i < s.length(); i++) {
83 if (s.charAt(i) == '\n') {
84 l++;
85 if (l == linenum)
86 start = i + 1;
87 if (l == linenum + 1)
88 return new Position(start, i - start);
91 return new Position(start, i - start);
94 public static String getPackagePlusClassName(ASTNode root, ASTNode selected) {
95 ASTNode iter = selected;
96 while (iter != null) {
97 if (iter.getNodeType() == ASTNode.TYPE_DECLARATION) {
98 TypeDeclaration td = (TypeDeclaration) iter;
99 if (td.isInterface()) {
100 return null; // error: in an interface
101 } else {
102 String classname = td.getName().getFullyQualifiedName();
103 if (root instanceof org.eclipse.jdt.core.dom.CompilationUnit) {
104 PackageDeclaration packkage = ((org.eclipse.jdt.core.dom.CompilationUnit) root)
105 .getPackage();
106 if (packkage != null)
107 return packkage.getName().getFullyQualifiedName() + "." + classname;
109 return classname;
112 iter = iter.getParent();
114 return null;
117 public void run(IAction action, SliceType slicerType) {
118 run(action, slicerType, "edu.berkeley.cs.bodik.svelte.plugin.sliceMarker", 16);
121 public void run(IAction action, SliceType slicerType, String markerType, int depth) {
122 ITextSelection its = (ITextSelection) ((AbstractTextEditor) editor).getSelectionProvider()
123 .getSelection();
124 int line_num = its.getStartLine();
125 line_num = line_num + 1; // their line numbers start at 0.
126 IFile file = ((IFileEditorInput) editor.getEditorInput()).getFile();
127 IProject proj = file.getProject();
129 // check for dirty editors in the project. This isn't strictly necessary (handled by
130 // saveAllEditors), but helps us out later when we want to know whether we should prompt
131 // whether to rebuild CG
132 boolean dirty = false;
133 for (IWorkbenchWindow window : PlatformUI.getWorkbench().getWorkbenchWindows()) {
134 for (IWorkbenchPage page : window.getPages()) {
135 for (IEditorPart editor : page.getDirtyEditors()) {
136 if (editor.getEditorInput() instanceof IFileEditorInput
137 && ((IFileEditorInput) editor.getEditorInput()).getFile().getProject() == proj) {
138 dirty = true;
139 break;
142 if (dirty)
143 break;
145 if (dirty)
146 break;
148 // ask if we want to save.
149 if (dirty)
150 if (PlatformUI.getWorkbench().saveAllEditors(true) == false)
151 return;
153 // may need to rebuild call graph
154 if (SliceEnvironmentCache.getInstance().isProjectOutdated(file.getProject())) {
156 if (dirty) // the files have just changed because we saved them (probably). don't even
157 // ask, just rebuild the call graph.
158 SliceEnvironmentCache.getInstance().expireProject(file.getProject());
159 else {
160 MessageDialogWithToggle rebuildDia = MessageDialogWithToggle
161 .openYesNoCancelQuestion(
162 PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
163 "Rebuild project call graph?",
164 "The project source files have changed since the call graph was last built. As a result slicing may not be synchronized with the latest source code. Do you wish to rebuild the callgraph?",
165 null, false, null, null);
166 // TODO: preferences
167 int result = rebuildDia.getReturnCode();
168 if (result == 2) {
169 SliceEnvironmentCache.getInstance().expireProject(file.getProject());
170 } else if (result == MessageDialogWithToggle.CANCEL || result == -1) {
171 return;
176 System.out.println("Slicing from: " + file.getLocation().toString());
178 System.out.println(" **** OK, BEGINNING SLICING OPERATION **** ");
180 SlicingJob job = new SlicingJob(slicerType, depth, editor, its);
181 job.setRule(ResourcesPlugin.getWorkspace().getRuleFactory().modifyRule(file.getProject()));
182 job.schedule();
184 System.out.println(" **** END SLICING OPERATION **** ");
188 public void setActiveEditor(IAction action, IEditorPart editor) {
189 cu = JavaUI.getWorkingCopyManager().getWorkingCopy(editor.getEditorInput());
190 // how the heck am I supposed to know the above?
191 this.editor = editor;
192 IWorkingCopyManager wcm = JavaUI.getWorkingCopyManager();
193 wcm.getWorkingCopy(editor.getEditorInput());