Package: BreakpointConditionEditor$1

BreakpointConditionEditor$1

nameinstructionbranchcomplexitylinemethod
documentAboutToBeChanged(DocumentEvent)
M: 1 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
documentChanged(DocumentEvent)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
{...}
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

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.propertypages;
23:
24: import org.eclipse.core.commands.AbstractHandler;
25: import org.eclipse.core.commands.ExecutionEvent;
26: import org.eclipse.core.commands.IHandler;
27: import org.eclipse.core.runtime.CoreException;
28: import org.eclipse.jface.text.Document;
29: import org.eclipse.jface.text.DocumentEvent;
30: import org.eclipse.jface.text.IDocument;
31: import org.eclipse.jface.text.IDocumentListener;
32: import org.eclipse.jface.text.TextViewerUndoManager;
33: import org.eclipse.jface.text.source.ISourceViewer;
34: import org.eclipse.swt.SWT;
35: import org.eclipse.swt.events.FocusAdapter;
36: import org.eclipse.swt.events.FocusEvent;
37: import org.eclipse.swt.graphics.Color;
38: import org.eclipse.swt.layout.GridData;
39: import org.eclipse.swt.widgets.Composite;
40: import org.eclipse.ui.PlatformUI;
41: import org.eclipse.ui.handlers.IHandlerActivation;
42: import org.eclipse.ui.handlers.IHandlerService;
43: import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds;
44: import org.overture.ast.lex.Dialect;
45: import org.overture.ide.debug.core.model.IVdmLineBreakpoint;
46: import org.overture.ide.ui.editor.core.VdmSourceViewer;
47: import org.overture.parser.lex.LexException;
48: import org.overture.parser.lex.LexTokenReader;
49: import org.overture.parser.messages.Console;
50: import org.overture.parser.syntax.ExpressionReader;
51: import org.overture.parser.syntax.ParserException;
52:
53: public class BreakpointConditionEditor
54: {
55:         private VdmSourceViewer fViewer;
56:         // private IContentAssistProcessor fCompletionProcessor;
57:         private String fOldValue;
58:         private String fErrorMessage;
59:         private VdmLineBreakpointPropertyPage fPage;
60:         private IVdmLineBreakpoint fBreakpoint;
61:         private IHandlerService fHandlerService;
62:         private IHandler fHandler;
63:         private IHandlerActivation fActivation;
64:         private IDocumentListener fDocumentListener;
65:         private String fSyntaxErrorMessage;
66:
67:         /**
68:          * Constructor
69:          *
70:          * @param parent
71:          * the parent to add this widget to
72:          * @param page
73:          * the page that is associated with this widget
74:          */
75:         public BreakpointConditionEditor(Composite parent,
76:                         VdmLineBreakpointPropertyPage page)
77:         {
78:                 fPage = page;
79:                 fBreakpoint = (IVdmLineBreakpoint) fPage.getBreakpoint();
80:                 String condition = new String();
81:                 try
82:                 {
83:                         condition = fBreakpoint.getExpression();
84:                         fErrorMessage = "Enter a condition";
85:
86:                         fOldValue = ""; //$NON-NLS-1$
87:
88:                         fViewer = new VdmSourceViewer(parent, null, null, false, SWT.BORDER
89:                                         | SWT.V_SCROLL | SWT.H_SCROLL | SWT.LEFT_TO_RIGHT);
90:                         fViewer.setInput(parent);
91:                         IDocument document = new Document();
92:                         // JDIDebugUIPlugin.getDefault().getJavaTextTools().setupJavaDocumentPartitioner(document,
93:                         // IJavaPartitions.JAVA_PARTITIONING);
94:                         // we can only do code assist if there is an associated type
95:                         // IJavaDebugContentAssistContext context = null;
96:                         // IType type = BreakpointUtils.getType(fBreakpoint);
97:                         // if (type == null) {
98:                         // context = new TypeContext(null, -1);
99:                         // }
100:                         // else {
101:                         // try {
102:                         // String source = null;
103:                         // ICompilationUnit compilationUnit = type.getCompilationUnit();
104:                         // if (compilationUnit != null && compilationUnit.getJavaProject().getProject().exists()) {
105:                         // source = compilationUnit.getSource();
106:                         // }
107:                         // else {
108:                         // IClassFile classFile = type.getClassFile();
109:                         // if (classFile != null) {
110:                         // source = classFile.getSource();
111:                         // }
112:                         // }
113:                         // int lineNumber = fBreakpoint.getMarker().getAttribute(IMarker.LINE_NUMBER, -1);
114:                         // int position= -1;
115:                         // if (source != null && lineNumber != -1) {
116:                         // try {
117:                         // position = new Document(source).getLineOffset(lineNumber - 1);
118:                         // }
119:                         // catch (BadLocationException e) {JDIDebugUIPlugin.log(e);}
120:                         // }
121:                         // context = new TypeContext(type, position);
122:                         // }
123:                         // catch (CoreException e) {JDIDebugUIPlugin.log(e);}
124:                         // }
125:                         // fCompletionProcessor = new JavaDebugContentAssistProcessor(context);
126:                         // fViewer.configure(new DisplayViewerConfiguration() {
127:                         // public IContentAssistProcessor getContentAssistantProcessor() {
128:                         // return fCompletionProcessor;
129:                         // }
130:                         // });
131:                         fViewer.setEditable(true);
132:                         // if we don't check upstream tracing can throw assertion exceptions see bug 181914
133:                         document.set(condition == null ? "" : condition); //$NON-NLS-1$
134:                         fViewer.setDocument(document);
135:                         fViewer.setUndoManager(new TextViewerUndoManager(10));
136:                         fViewer.getUndoManager().connect(fViewer);
137:                         fDocumentListener = new IDocumentListener()
138:                         {
139:                                 public void documentAboutToBeChanged(DocumentEvent event)
140:                                 {
141:                                 }
142:
143:                                 public void documentChanged(DocumentEvent event)
144:                                 {
145:                                         valueChanged();
146:                                 }
147:                         };
148:                         fViewer.getDocument().addDocumentListener(fDocumentListener);
149:                         GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
150:                         gd.heightHint = fPage.convertHeightInCharsToPixels(10);
151:                         gd.widthHint = fPage.convertWidthInCharsToPixels(40);
152:                         fViewer.getControl().setLayoutData(gd);
153:                         fHandler = new AbstractHandler()
154:                         {
155:                                 public Object execute(ExecutionEvent event)
156:                                                 throws org.eclipse.core.commands.ExecutionException
157:                                 {
158:                                         fViewer.doOperation(ISourceViewer.CONTENTASSIST_PROPOSALS);
159:                                         return null;
160:                                 }
161:                         };
162:                         fHandlerService = (IHandlerService) PlatformUI.getWorkbench().getAdapter(IHandlerService.class);
163:
164:                         fViewer.getControl().addFocusListener(new FocusAdapter()
165:                         {
166:                                 public void focusGained(FocusEvent e)
167:                                 {
168:                                         activateContentAssist();
169:                                 }
170:
171:                                 public void focusLost(FocusEvent e)
172:                                 {
173:                                         deactivateContentAssist();
174:                                 }
175:                         });
176:                 } catch (CoreException exception)
177:                 {
178:                 }
179:         }
180:
181:         /**
182:          * Returns the condition defined in the source viewer.
183:          *
184:          * @return the contents of this condition editor
185:          */
186:         public String getCondition()
187:         {
188:                 return fViewer.getDocument().get();
189:         }
190:
191:         /**
192:          * @see org.eclipse.jface.preference.FieldEditor#refreshValidState()
193:          */
194:         protected void refreshValidState()
195:         {
196:                 if (!fViewer.isEditable())
197:                 {
198:                         fPage.removeErrorMessage(fErrorMessage);
199:                         fPage.removeErrorMessage(fSyntaxErrorMessage);
200:                 } else
201:                 {
202:                         String text = fViewer.getDocument().get();
203:                         if (!(text != null && text.trim().length() > 0))
204:                         {
205:                                 fPage.addErrorMessage(fErrorMessage);
206:                                 fPage.removeErrorMessage(fSyntaxErrorMessage);
207:                         } else
208:                         {
209:                                 fPage.removeErrorMessage(fErrorMessage);
210:                                 if (validateSyntax())
211:                                 {
212:                                         fPage.removeErrorMessage(fSyntaxErrorMessage);
213:                                 } else
214:                                 {
215:                                         fPage.addErrorMessage(fSyntaxErrorMessage);
216:                                 }
217:
218:                         }
219:
220:                 }
221:         }
222:
223:         /**
224:          * @see org.eclipse.jface.preference.FieldEditor#setEnabled(boolean, org.eclipse.swt.widgets.Composite)
225:          */
226:         public void setEnabled(boolean enabled)
227:         {
228:                 fViewer.setEditable(enabled);
229:                 fViewer.getTextWidget().setEnabled(enabled);
230:                 if (enabled)
231:                 {
232:                         Color color = fViewer.getControl().getDisplay().getSystemColor(SWT.COLOR_WHITE);
233:                         fViewer.getTextWidget().setBackground(color);
234:                         fViewer.getTextWidget().setFocus();
235:                 } else
236:                 {
237:                         Color color = fViewer.getControl().getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND);
238:                         fViewer.getTextWidget().setBackground(color);
239:                 }
240:                 valueChanged();
241:         }
242:
243:         /**
244:          * Handle that the value changed
245:          */
246:         protected void valueChanged()
247:         {
248:                 String newValue = fViewer.getDocument().get();
249:                 if (!newValue.equals(fOldValue))
250:                 {
251:                         fOldValue = newValue;
252:
253:                 }
254:                 refreshValidState();
255:         }
256:
257:         private boolean validateSyntax()
258:         {
259:                 LexTokenReader ltr;
260:                 ltr = new LexTokenReader(fOldValue, Dialect.VDM_RT, Console.charset);
261:
262:                 ExpressionReader reader = new ExpressionReader(ltr);
263:
264:                 try
265:                 {
266:                         reader.readExpression();
267:                 } catch (ParserException e)
268:                 {
269:                         this.fSyntaxErrorMessage = e.getMessage();
270:                         return false;
271:
272:                 } catch (LexException e)
273:                 {
274:                         this.fSyntaxErrorMessage = e.getMessage();
275:                         return false;
276:
277:                 }
278:                 return true;
279:
280:         }
281:
282:         /**
283:          * Dispose of the handlers, etc
284:          */
285:         public void dispose()
286:         {
287:                 deactivateContentAssist();
288:
289:                 fViewer.getDocument().removeDocumentListener(fDocumentListener);
290:                 // fViewer.dispose();
291:         }
292:
293:         private void activateContentAssist()
294:         {
295:                 fActivation = fHandlerService.activateHandler(ITextEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS, fHandler);
296:         }
297:
298:         private void deactivateContentAssist()
299:         {
300:                 if (fActivation != null)
301:                 {
302:                         fHandlerService.deactivateHandler(fActivation);
303:                         fActivation = null;
304:                 }
305:         }
306: }