KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > propertypages > BreakpointConditionEditor


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.internal.debug.ui.propertypages;
12
13 import org.eclipse.core.commands.AbstractHandler;
14 import org.eclipse.core.commands.ExecutionEvent;
15 import org.eclipse.core.commands.IHandler;
16 import org.eclipse.core.resources.IMarker;
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.jdt.core.IClassFile;
19 import org.eclipse.jdt.core.ICompilationUnit;
20 import org.eclipse.jdt.core.IType;
21 import org.eclipse.jdt.debug.core.IJavaLineBreakpoint;
22 import org.eclipse.jdt.internal.debug.ui.BreakpointUtils;
23 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin;
24 import org.eclipse.jdt.internal.debug.ui.JDISourceViewer;
25 import org.eclipse.jdt.internal.debug.ui.contentassist.IJavaDebugContentAssistContext;
26 import org.eclipse.jdt.internal.debug.ui.contentassist.JavaDebugContentAssistProcessor;
27 import org.eclipse.jdt.internal.debug.ui.contentassist.TypeContext;
28 import org.eclipse.jdt.internal.debug.ui.display.DisplayViewerConfiguration;
29 import org.eclipse.jdt.ui.text.IJavaPartitions;
30 import org.eclipse.jface.text.BadLocationException;
31 import org.eclipse.jface.text.Document;
32 import org.eclipse.jface.text.DocumentEvent;
33 import org.eclipse.jface.text.IDocument;
34 import org.eclipse.jface.text.IDocumentListener;
35 import org.eclipse.jface.text.TextViewerUndoManager;
36 import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
37 import org.eclipse.jface.text.source.ISourceViewer;
38 import org.eclipse.swt.SWT;
39 import org.eclipse.swt.graphics.Color;
40 import org.eclipse.swt.layout.GridData;
41 import org.eclipse.swt.widgets.Composite;
42 import org.eclipse.ui.PlatformUI;
43 import org.eclipse.ui.handlers.IHandlerActivation;
44 import org.eclipse.ui.handlers.IHandlerService;
45 import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds;
46
47 /**
48  * The widget for the conditional editor on the breakpoints properties page
49  */

50 public class BreakpointConditionEditor {
51     
52     private JDISourceViewer fViewer;
53     private IContentAssistProcessor fCompletionProcessor;
54     private String JavaDoc fOldValue;
55     private String JavaDoc fErrorMessage;
56     private JavaLineBreakpointPage fPage;
57     private IJavaLineBreakpoint fBreakpoint;
58     private IHandlerService fHandlerService;
59     private IHandler fHandler;
60     private IHandlerActivation fActivation;
61     private IDocumentListener fDocumentListener;
62         
63     /**
64      * Constructor
65      * @param parent the parent to add this widget to
66      * @param page the page that is associated with this widget
67      */

68     public BreakpointConditionEditor(Composite parent, JavaLineBreakpointPage page) {
69         fPage = page;
70         fBreakpoint = (IJavaLineBreakpoint) fPage.getBreakpoint();
71         String JavaDoc condition = new String JavaDoc();
72         try {
73             condition = fBreakpoint.getCondition();
74             fErrorMessage = PropertyPageMessages.BreakpointConditionEditor_1;
75             fOldValue = ""; //$NON-NLS-1$
76

77             fViewer = new JDISourceViewer(parent, null, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
78             fViewer.setInput(parent);
79             IDocument document = new Document();
80             JDIDebugUIPlugin.getDefault().getJavaTextTools().setupJavaDocumentPartitioner(document, IJavaPartitions.JAVA_PARTITIONING);
81         // we can only do code assist if there is an associated type
82
IJavaDebugContentAssistContext context = null;
83             IType type = BreakpointUtils.getType(fBreakpoint);
84             if (type == null) {
85                 context = new TypeContext(null, -1);
86             }
87             else {
88                 try {
89                     String JavaDoc source = null;
90                     ICompilationUnit compilationUnit = type.getCompilationUnit();
91                     if (compilationUnit != null && compilationUnit.getJavaProject().getProject().exists()) {
92                         source = compilationUnit.getSource();
93                     }
94                     else {
95                         IClassFile classFile = type.getClassFile();
96                         if (classFile != null) {
97                             source = classFile.getSource();
98                         }
99                     }
100                     int lineNumber = fBreakpoint.getMarker().getAttribute(IMarker.LINE_NUMBER, -1);
101                     int position= -1;
102                     if (source != null && lineNumber != -1) {
103                         try {
104                             position = new Document(source).getLineOffset(lineNumber - 1);
105                         }
106                         catch (BadLocationException e) {JDIDebugUIPlugin.log(e);}
107                     }
108                     context = new TypeContext(type, position);
109                 }
110                 catch (CoreException e) {JDIDebugUIPlugin.log(e);}
111             }
112             fCompletionProcessor = new JavaDebugContentAssistProcessor(context);
113             fViewer.configure(new DisplayViewerConfiguration() {
114                 public IContentAssistProcessor getContentAssistantProcessor() {
115                         return fCompletionProcessor;
116                 }
117             });
118             fViewer.setEditable(true);
119         //if we don't check upstream tracing can throw assertion exceptions see bug 181914
120
document.set((condition == null ? "" : condition)); //$NON-NLS-1$
121
fViewer.setDocument(document);
122             fViewer.setUndoManager(new TextViewerUndoManager(10));
123             fViewer.getUndoManager().connect(fViewer);
124             fDocumentListener = new IDocumentListener() {
125                 public void documentAboutToBeChanged(DocumentEvent event) {
126                 }
127                 public void documentChanged(DocumentEvent event) {
128                     valueChanged();
129                 }
130             };
131             fViewer.getDocument().addDocumentListener(fDocumentListener);
132             GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
133             gd.heightHint = fPage.convertHeightInCharsToPixels(10);
134             gd.widthHint = fPage.convertWidthInCharsToPixels(40);
135             fViewer.getControl().setLayoutData(gd);
136             fHandler = new AbstractHandler() {
137                 public Object JavaDoc execute(ExecutionEvent event) throws org.eclipse.core.commands.ExecutionException {
138                     fViewer.doOperation(ISourceViewer.CONTENTASSIST_PROPOSALS);
139                     return null;
140                 }
141             };
142             fHandlerService = (IHandlerService) PlatformUI.getWorkbench().getAdapter(IHandlerService.class);
143         }
144         catch (CoreException exception) {JDIDebugUIPlugin.log(exception);}
145     }
146
147     /**
148      * Returns the condition defined in the source viewer.
149      * @return the contents of this condition editor
150      */

151     public String JavaDoc getCondition() {
152         return fViewer.getDocument().get();
153     }
154
155     /**
156      * @see org.eclipse.jface.preference.FieldEditor#refreshValidState()
157      */

158     protected void refreshValidState() {
159         if (!fViewer.isEditable()) {
160             fPage.removeErrorMessage(fErrorMessage);
161         } else {
162             String JavaDoc text = fViewer.getDocument().get();
163             if (!(text != null && text.trim().length() > 0)) {
164                 fPage.addErrorMessage(fErrorMessage);
165             } else {
166                 fPage.removeErrorMessage(fErrorMessage);
167             }
168         }
169     }
170
171     /**
172      * @see org.eclipse.jface.preference.FieldEditor#setEnabled(boolean, org.eclipse.swt.widgets.Composite)
173      */

174     public void setEnabled(boolean enabled) {
175         fViewer.setEditable(enabled);
176         fViewer.getTextWidget().setEnabled(enabled);
177         if (enabled) {
178             fViewer.updateViewerColors();
179             fViewer.getTextWidget().setFocus();
180             fActivation = fHandlerService.activateHandler(ITextEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS, fHandler);
181         } else {
182             Color color = fViewer.getControl().getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND);
183             fViewer.getTextWidget().setBackground(color);
184             if(fActivation != null) {
185                 fHandlerService.deactivateHandler(fActivation);
186             }
187         }
188         valueChanged();
189     }
190     
191     /**
192      * Handle that the value changed
193      */

194     protected void valueChanged() {
195         String JavaDoc newValue = fViewer.getDocument().get();
196         if (!newValue.equals(fOldValue)) {
197             fOldValue = newValue;
198         }
199         refreshValidState();
200     }
201     
202     /**
203      * Dispose of the handlers, etc
204      */

205     public void dispose() {
206         if (fViewer.isEditable()) {
207             fHandlerService.deactivateHandler(fActivation);
208         }
209         fViewer.getDocument().removeDocumentListener(fDocumentListener);
210         fViewer.dispose();
211     }
212 }
213
Popular Tags