Method: getEditor(IWorkbenchPart)

1: /*
2: * #%~
3: * org.overture.ide.debug
4: * %%
5: * Copyright (C) 2008 - 2014 Overture
6: * %%
7: * This program is free software: you can redistribute it and/or modify
8: * it under the terms of the GNU General Public License as
9: * published by the Free Software Foundation, either version 3 of the
10: * License, or (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public
18: * License along with this program. If not, see
19: * <http://www.gnu.org/licenses/gpl-3.0.html>.
20: * #~%
21: */
22: package org.overture.ide.debug.ui.model;
23:
24: import org.eclipse.core.resources.IFile;
25: import org.eclipse.core.resources.IResource;
26: import org.eclipse.core.runtime.CoreException;
27: import org.eclipse.core.runtime.IPath;
28: import org.eclipse.debug.core.DebugPlugin;
29: import org.eclipse.debug.core.model.IBreakpoint;
30: import org.eclipse.debug.core.model.ILineBreakpoint;
31: import org.eclipse.debug.ui.actions.IToggleBreakpointsTarget;
32: import org.eclipse.jface.text.BadLocationException;
33: import org.eclipse.jface.text.IDocument;
34: import org.eclipse.jface.text.IRegion;
35: import org.eclipse.jface.text.ITextSelection;
36: import org.eclipse.jface.viewers.ISelection;
37: import org.eclipse.ui.IWorkbenchPart;
38: import org.eclipse.ui.texteditor.ITextEditor;
39: import org.overture.ast.node.INode;
40: import org.overture.ide.core.IVdmElement;
41: import org.overture.ide.core.resources.IVdmSourceUnit;
42: import org.overture.ide.debug.core.IDebugConstants;
43: import org.overture.ide.debug.core.VdmDebugPlugin;
44: import org.overture.ide.debug.core.model.internal.VdmLineBreakpoint;
45: import org.overture.ide.debug.utils.ExecutableAnalysis;
46: import org.overture.ide.ui.editor.core.VdmEditor;
47:
48: public class VdmLineBreakpointAdapter implements IToggleBreakpointsTarget
49: {
50:
51:         public void toggleLineBreakpoints(IWorkbenchPart part, ISelection selection)
52:                         throws CoreException
53:         {
54:                 ITextEditor textEditor = getEditor(part);
55:                 IResource resource = (IResource) textEditor.getEditorInput().getAdapter(IResource.class);
56:                 ITextSelection textSelection = (ITextSelection) selection;
57:                 int lineNumber = textSelection.getStartLine() + 1;
58:
59:                 boolean executable = false;
60:
61:                 if (textEditor != null)
62:                 {
63:                         if (textEditor instanceof VdmEditor)
64:                         {
65:                                 VdmEditor vEditor = (VdmEditor) textEditor;
66:                                 IVdmElement element = vEditor.getInputVdmElement();
67:                                 if (element != null && element instanceof IVdmSourceUnit)
68:                                 {
69:                                         IVdmSourceUnit sourceUnti = (IVdmSourceUnit) element;
70:                                         for (INode node : sourceUnti.getParseList())
71:                                         {
72:                                                 executable = ExecutableAnalysis.isExecutable(node, lineNumber, true);
73:
74:                                                 if (executable)
75:                                                 {
76:                                                         break;
77:                                                 }
78:                                         }
79:                                 }
80:                         }
81:
82:                         IBreakpoint[] breakpoints = DebugPlugin.getDefault().getBreakpointManager().getBreakpoints(IDebugConstants.ID_VDM_DEBUG_MODEL);
83:                         for (int i = 0; i < breakpoints.length; i++)
84:                         {
85:                                 IBreakpoint breakpoint = breakpoints[i];
86:                                 if (resource.equals(breakpoint.getMarker().getResource()))
87:                                 {
88:                                         if (((ILineBreakpoint) breakpoint).getLineNumber() == lineNumber)
89:                                         {
90:                                                 breakpoint.delete();
91:                                                 return;
92:                                         }
93:                                 }
94:                         }
95:
96:                         if (!executable)
97:                         {
98:                                 return;
99:                         }
100:
101:                         IDocument document = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput());
102:
103:                         IRegion line;
104:                         try
105:                         {
106:                                 line = document.getLineInformation(textSelection.getStartLine());
107:                                 int start = line.getOffset();
108:                                 int end = start + line.getLength();
109:                                 String debugModelId = IDebugConstants.ID_VDM_DEBUG_MODEL;// getDebugModelId(textEditor,
110:                                 // resource);
111:                                 if (debugModelId == null)
112:                                 {
113:                                         return;
114:                                 }
115:                                 IPath location = resource.getFullPath();
116:
117:                                 VdmLineBreakpoint lineBreakpoint = new VdmLineBreakpoint(IDebugConstants.ID_VDM_DEBUG_MODEL, resource, location, lineNumber, start, end, false);
118:
119:                                 StringBuilder message = new StringBuilder();
120:                                 message.append("Line breakpoint:");
121:                                 message.append(location.lastSegment());
122:                                 message.append("[line:" + lineNumber + "]");
123:
124:                                 lineBreakpoint.setMessage(message.toString());
125:
126:                                 DebugPlugin.getDefault().getBreakpointManager().addBreakpoint(lineBreakpoint);
127:
128:                         } catch (BadLocationException e)
129:                         {
130:                                 VdmDebugPlugin.log(e);
131:                         }
132:
133:                 }
134:         }
135:
136:         /*
137:          * (non-Javadoc)
138:          * @seeorg.eclipse.debug.ui.actions.IToggleBreakpointsTarget# canToggleLineBreakpoints(org.eclipse.ui.
139:          * IWorkbenchPart, org.eclipse.jface.viewers.ISelection)
140:          */
141:         public boolean canToggleLineBreakpoints(IWorkbenchPart part,
142:                         ISelection selection)
143:         {
144:                 return getEditor(part) != null;
145:         }
146:
147:         /**
148:          * Returns the editor being used to edit a PDA file, associated with the given part, or <code>null</code> if none.
149:          *
150:          * @param part
151:          * workbench part
152:          * @return the editor being used to edit a PDA file, associated with the given part, or <code>null</code> if none
153:          */
154:         private ITextEditor getEditor(IWorkbenchPart part)
155:         {
156:•                if (part instanceof ITextEditor)
157:                 {
158:                         ITextEditor editorPart = (ITextEditor) part;
159:                         IResource resource = (IResource) editorPart.getEditorInput().getAdapter(IResource.class);
160:•                        if (resource != null && resource instanceof IFile)
161:                         {
162:                                 return editorPart;
163:                                 // IFile file = (IFile) resource;
164:                                 // try
165:                                 // {
166:                                 // String contentTypeId = file.getContentDescription().getContentType().getId();
167:                                 // if (SourceViewerEditorManager.getInstance().getContentTypeIds().contains(contentTypeId))
168:                                 // {
169:                                 // return editorPart;
170:                                 // }
171:                                 // } catch (CoreException e)
172:                                 // {
173:                                 // // TODO Auto-generated catch block
174:                                 // e.printStackTrace();
175:                                 // }
176:                         }
177:                 }
178:                 return null;
179:         }
180:
181:         /*
182:          * (non-Javadoc)
183:          * @see org.eclipse.debug.ui.actions.IToggleBreakpointsTarget#toggleMethodBreakpoints (org.eclipse.ui.IWorkbenchPart
184:          * , org.eclipse.jface.viewers.ISelection)
185:          */
186:         public void toggleMethodBreakpoints(IWorkbenchPart part,
187:                         ISelection selection) throws CoreException
188:         {
189:         }
190:
191:         // private static IWorkspaceRoot getWorkspaceRoot()
192:         // {
193:         // return ResourcesPlugin.getWorkspace().getRoot();
194:         // }
195:
196:         // private static IResource getBreakpointResource(ITextEditor textEditor)
197:         // {
198:         // return getBreakpointResource(textEditor.getEditorInput());
199:         // }
200:
201:         // private static IResource getBreakpointResource(
202:         // final IEditorInput editorInput)
203:         // {
204:         // IResource resource = (IResource) editorInput.getAdapter(IResource.class);
205:         // if (resource == null)
206:         // resource = getWorkspaceRoot();
207:         // return resource;
208:         // }
209:
210:         /*
211:          * (non-Javadoc)
212:          * @seeorg.eclipse.debug.ui.actions.IToggleBreakpointsTarget# canToggleMethodBreakpoints(org.eclipse.ui.
213:          * IWorkbenchPart, org.eclipse.jface.viewers.ISelection)
214:          */
215:         public boolean canToggleMethodBreakpoints(IWorkbenchPart part,
216:                         ISelection selection)
217:         {
218:                 return false;
219:         }
220:
221:         /*
222:          * (non-Javadoc)
223:          * @see org.eclipse.debug.ui.actions.IToggleBreakpointsTarget#toggleWatchpoints (org.eclipse.ui.IWorkbenchPart,
224:          * org.eclipse.jface.viewers.ISelection)
225:          */
226:         public void toggleWatchpoints(IWorkbenchPart part, ISelection selection)
227:                         throws CoreException
228:         {
229:         }
230:
231:         /*
232:          * (non-Javadoc)
233:          * @see org.eclipse.debug.ui.actions.IToggleBreakpointsTarget#canToggleWatchpoints (org.eclipse.ui.IWorkbenchPart ,
234:          * org.eclipse.jface.viewers.ISelection)
235:          */
236:         public boolean canToggleWatchpoints(IWorkbenchPart part,
237:                         ISelection selection)
238:         {
239:                 return false;
240:         }
241: }