KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > junit > ui > CompareResultDialog


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.junit.ui;
12
13 import java.io.ByteArrayInputStream JavaDoc;
14 import java.io.InputStream JavaDoc;
15 import java.io.UnsupportedEncodingException JavaDoc;
16
17 import org.eclipse.core.runtime.CoreException;
18
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.custom.StyleRange;
21 import org.eclipse.swt.events.DisposeEvent;
22 import org.eclipse.swt.events.DisposeListener;
23 import org.eclipse.swt.graphics.Image;
24 import org.eclipse.swt.layout.GridData;
25 import org.eclipse.swt.layout.GridLayout;
26 import org.eclipse.swt.widgets.Composite;
27 import org.eclipse.swt.widgets.Control;
28 import org.eclipse.swt.widgets.Display;
29 import org.eclipse.swt.widgets.Shell;
30
31 import org.eclipse.jface.dialogs.IDialogConstants;
32 import org.eclipse.jface.dialogs.IDialogSettings;
33 import org.eclipse.jface.dialogs.TrayDialog;
34
35 import org.eclipse.jface.text.DocumentEvent;
36 import org.eclipse.jface.text.IDocument;
37 import org.eclipse.jface.text.IRegion;
38 import org.eclipse.jface.text.ITypedRegion;
39 import org.eclipse.jface.text.Region;
40 import org.eclipse.jface.text.TextAttribute;
41 import org.eclipse.jface.text.TextPresentation;
42 import org.eclipse.jface.text.TextViewer;
43 import org.eclipse.jface.text.presentation.IPresentationDamager;
44 import org.eclipse.jface.text.presentation.IPresentationReconciler;
45 import org.eclipse.jface.text.presentation.IPresentationRepairer;
46 import org.eclipse.jface.text.presentation.PresentationReconciler;
47 import org.eclipse.jface.text.source.ISourceViewer;
48 import org.eclipse.jface.text.source.SourceViewer;
49 import org.eclipse.jface.text.source.SourceViewerConfiguration;
50
51 import org.eclipse.ui.PlatformUI;
52
53 import org.eclipse.compare.CompareConfiguration;
54 import org.eclipse.compare.CompareViewerPane;
55 import org.eclipse.compare.IEncodedStreamContentAccessor;
56 import org.eclipse.compare.ITypedElement;
57 import org.eclipse.compare.contentmergeviewer.TextMergeViewer;
58 import org.eclipse.compare.structuremergeviewer.DiffNode;
59
60 import org.eclipse.jdt.internal.junit.model.TestElement;
61
62 public class CompareResultDialog extends TrayDialog {
63     private static final String JavaDoc PREFIX_SUFFIX_PROPERTY= "org.eclipse.jdt.internal.junit.ui.CompareResultDialog.prefixSuffix"; //$NON-NLS-1$
64

65     private static class CompareResultMergeViewer extends TextMergeViewer {
66          private CompareResultMergeViewer(Composite parent, int style, CompareConfiguration configuration) {
67              super(parent, style, configuration);
68          }
69          
70         protected void createControls(Composite composite) {
71             super.createControls(composite);
72             PlatformUI.getWorkbench().getHelpSystem().setHelp(composite, IJUnitHelpContextIds.RESULT_COMPARE_DIALOG);
73         }
74          
75 // protected void createToolItems(ToolBarManager tbm) {
76
// ResourceBundle bundle= CompareUI.getResourceBundle();
77
// tbm.add(new IgnoreWhiteSpaceAction(bundle, getCompareConfiguration()));
78
// super.createToolItems(tbm);
79
// }
80

81         protected void configureTextViewer(TextViewer textViewer) {
82             if (textViewer instanceof SourceViewer) {
83                 int[] prefixSuffixOffsets= (int[]) getCompareConfiguration().getProperty(PREFIX_SUFFIX_PROPERTY);
84                 ((SourceViewer)textViewer).configure(new CompareResultViewerConfiguration(prefixSuffixOffsets));
85             }
86         }
87     }
88     
89     private static class CompareResultViewerConfiguration extends SourceViewerConfiguration {
90         private static class SimpleDamagerRepairer implements IPresentationDamager, IPresentationRepairer {
91             private IDocument fDocument;
92             private final int[] fPrefixSuffixOffsets2;
93
94             public SimpleDamagerRepairer(int[] prefixSuffixOffsets) {
95                 fPrefixSuffixOffsets2= prefixSuffixOffsets;
96             }
97
98             public void setDocument(IDocument document) {
99                 fDocument= document;
100             }
101
102             public IRegion getDamageRegion(ITypedRegion partition, DocumentEvent event, boolean changed) {
103                 return new Region(0, fDocument.getLength());
104             }
105
106             public void createPresentation(TextPresentation presentation, ITypedRegion damage) {
107                 int prefix= fPrefixSuffixOffsets2[0];
108                 int suffix= fPrefixSuffixOffsets2[1];
109                 TextAttribute attr= new TextAttribute(Display.getDefault().getSystemColor(SWT.COLOR_RED), null, SWT.BOLD);
110                 presentation.addStyleRange(new StyleRange(prefix, fDocument.getLength()-suffix-prefix, attr.getForeground(), attr.getBackground(), attr.getStyle()));
111             }
112         }
113         
114         private final int[] fPrefixSuffixOffsets;
115         
116         public CompareResultViewerConfiguration(int[] prefixSuffixOffsets) {
117             fPrefixSuffixOffsets= prefixSuffixOffsets;
118         }
119
120         public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
121             PresentationReconciler reconciler= new PresentationReconciler();
122             SimpleDamagerRepairer dr= new SimpleDamagerRepairer(fPrefixSuffixOffsets);
123             reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
124             reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
125             return reconciler;
126         }
127     }
128         
129     private static class CompareElement implements ITypedElement, IEncodedStreamContentAccessor {
130         private String JavaDoc fContent;
131         
132         public CompareElement(String JavaDoc content) {
133             fContent= content;
134         }
135         public String JavaDoc getName() {
136             return "<no name>"; //$NON-NLS-1$
137
}
138         public Image getImage() {
139             return null;
140         }
141         public String JavaDoc getType() {
142             return "txt"; //$NON-NLS-1$
143
}
144         public InputStream JavaDoc getContents() {
145             try {
146                 return new ByteArrayInputStream JavaDoc(fContent.getBytes("UTF-8")); //$NON-NLS-1$
147
} catch (UnsupportedEncodingException JavaDoc e) {
148                 return new ByteArrayInputStream JavaDoc(fContent.getBytes());
149             }
150         }
151         public String JavaDoc getCharset() throws CoreException {
152             return "UTF-8"; //$NON-NLS-1$
153
}
154     }
155
156     private TextMergeViewer fViewer;
157     private String JavaDoc fExpected;
158     private String JavaDoc fActual;
159     private String JavaDoc fTestName;
160     
161     /**
162      * Lengths of common prefix and suffix.
163      * Note: this array is passed to the DamagerRepairer and
164      * the lengths are updated on content change.
165      */

166     private final int[] fPrefixSuffix= new int[2];
167     
168     public CompareResultDialog(Shell parentShell, TestElement element) {
169         super(parentShell);
170         setShellStyle(SWT.DIALOG_TRIM | SWT.RESIZE | SWT.MAX);
171         setFailedTest(element);
172     }
173     
174     private void setFailedTest(TestElement failedTest) {
175         fTestName= failedTest.getTestName();
176         fExpected= failedTest.getExpected();
177         fActual= failedTest.getActual();
178         computePrefixSuffix();
179     }
180     
181     /* (non-Javadoc)
182      * @see org.eclipse.jface.dialogs.Dialog#getDialogBoundsSettings()
183      */

184     protected IDialogSettings getDialogBoundsSettings() {
185         return JUnitPlugin.getDefault().getDialogSettingsSection(getClass().getName());
186     }
187         
188     private void computePrefixSuffix() {
189         int end= Math.min(fExpected.length(), fActual.length());
190         int i= 0;
191         for(; i < end; i++)
192             if (fExpected.charAt(i) != fActual.charAt(i))
193                 break;
194         fPrefixSuffix[0]= i;
195         
196         int j= fExpected.length()-1;
197         int k= fActual.length()-1;
198         int l= 0;
199         for (; k >= i && j >= i; k--,j--) {
200             if (fExpected.charAt(j) != fActual.charAt(k))
201                 break;
202             l++;
203         }
204         fPrefixSuffix[1]= l;
205     }
206
207     protected void configureShell(Shell newShell) {
208         super.configureShell(newShell);
209         newShell.setText(JUnitMessages.CompareResultDialog_title);
210         PlatformUI.getWorkbench().getHelpSystem().setHelp(newShell, IJUnitHelpContextIds.RESULT_COMPARE_DIALOG);
211     }
212
213     protected void createButtonsForButtonBar(Composite parent) {
214         createButton(parent, IDialogConstants.OK_ID, JUnitMessages.CompareResultDialog_labelOK, true);
215     }
216
217     protected Control createDialogArea(Composite parent) {
218         Composite composite = (Composite)super.createDialogArea(parent);
219         GridLayout layout= new GridLayout();
220         layout.numColumns= 1;
221         composite.setLayout(layout);
222         
223         CompareViewerPane pane = new CompareViewerPane(composite, SWT.BORDER | SWT.FLAT);
224         pane.setText(fTestName);
225         GridData data= new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL);
226         data.widthHint= convertWidthInCharsToPixels(120);
227         data.heightHint= convertHeightInCharsToPixels(13);
228         pane.setLayoutData(data);
229         
230         Control previewer= createPreviewer(pane);
231         pane.setContent(previewer);
232         GridData gd= new GridData(GridData.FILL_BOTH);
233         previewer.setLayoutData(gd);
234         applyDialogFont(parent);
235         return composite;
236     }
237     
238     private Control createPreviewer(Composite parent) {
239         final CompareConfiguration compareConfiguration= new CompareConfiguration();
240         compareConfiguration.setLeftLabel(JUnitMessages.CompareResultDialog_expectedLabel);
241         compareConfiguration.setLeftEditable(false);
242         compareConfiguration.setRightLabel(JUnitMessages.CompareResultDialog_actualLabel);
243         compareConfiguration.setRightEditable(false);
244         compareConfiguration.setProperty(CompareConfiguration.IGNORE_WHITESPACE, Boolean.FALSE);
245         compareConfiguration.setProperty(PREFIX_SUFFIX_PROPERTY, fPrefixSuffix);
246
247         fViewer= new CompareResultMergeViewer(parent, SWT.NONE, compareConfiguration);
248         setCompareViewerInput();
249
250         Control control= fViewer.getControl();
251         control.addDisposeListener(new DisposeListener() {
252             public void widgetDisposed(DisposeEvent e) {
253                 compareConfiguration.dispose();
254             }
255         });
256         return control;
257     }
258
259     private void setCompareViewerInput() {
260         if (! fViewer.getControl().isDisposed())
261             fViewer.setInput(new DiffNode(new CompareElement(fExpected), new CompareElement(fActual)));
262     }
263
264     public void setInput(TestElement failedTest) {
265         setFailedTest(failedTest);
266         setCompareViewerInput();
267     }
268 }
269
Popular Tags