1 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 50 public class BreakpointConditionEditor { 51 52 private JDISourceViewer fViewer; 53 private IContentAssistProcessor fCompletionProcessor; 54 private String fOldValue; 55 private String 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 68 public BreakpointConditionEditor(Composite parent, JavaLineBreakpointPage page) { 69 fPage = page; 70 fBreakpoint = (IJavaLineBreakpoint) fPage.getBreakpoint(); 71 String condition = new String (); 72 try { 73 condition = fBreakpoint.getCondition(); 74 fErrorMessage = PropertyPageMessages.BreakpointConditionEditor_1; 75 fOldValue = ""; 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 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 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 document.set((condition == null ? "" : condition)); 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 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 151 public String getCondition() { 152 return fViewer.getDocument().get(); 153 } 154 155 158 protected void refreshValidState() { 159 if (!fViewer.isEditable()) { 160 fPage.removeErrorMessage(fErrorMessage); 161 } else { 162 String 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 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 194 protected void valueChanged() { 195 String newValue = fViewer.getDocument().get(); 196 if (!newValue.equals(fOldValue)) { 197 fOldValue = newValue; 198 } 199 refreshValidState(); 200 } 201 202 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 |