Repository navigation
Expand file tree
/
Copy pathCompareResultDialog.java
More file actions
215 lines (180 loc) · 7.85 KB
/
Copy pathCompareResultDialog.java
File metadata and controls
215 lines (180 loc) · 7.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package code2code.ui.compare;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import org.eclipse.compare.CompareConfiguration;
import org.eclipse.compare.CompareViewerPane;
import org.eclipse.compare.IEncodedStreamContentAccessor;
import org.eclipse.compare.ITypedElement;
import org.eclipse.compare.contentmergeviewer.TextMergeViewer;
import org.eclipse.compare.structuremergeviewer.DiffNode;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.dialogs.TrayDialog;
import org.eclipse.jface.text.DocumentEvent;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ITypedRegion;
import org.eclipse.jface.text.Region;
import org.eclipse.jface.text.TextAttribute;
import org.eclipse.jface.text.TextPresentation;
import org.eclipse.jface.text.TextViewer;
import org.eclipse.jface.text.presentation.IPresentationDamager;
import org.eclipse.jface.text.presentation.IPresentationReconciler;
import org.eclipse.jface.text.presentation.IPresentationRepairer;
import org.eclipse.jface.text.presentation.PresentationReconciler;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.jface.text.source.SourceViewer;
import org.eclipse.jface.text.source.SourceViewerConfiguration;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class CompareResultDialog extends TrayDialog {
private static final String PREFIX_SUFFIX_PROPERTY= "org.eclipse.jdt.internal.junit.ui.CompareResultDialog.prefixSuffix"; //$NON-NLS-1$
private static class CompareResultMergeViewer extends TextMergeViewer {
private CompareResultMergeViewer(Composite parent, int style, CompareConfiguration configuration) {
super(parent, style, configuration);
}
protected void createControls(Composite composite) {
super.createControls(composite);
}
protected void configureTextViewer(TextViewer textViewer) {
if (textViewer instanceof SourceViewer) {
int[] prefixSuffixOffsets= (int[]) getCompareConfiguration().getProperty(PREFIX_SUFFIX_PROPERTY);
((SourceViewer)textViewer).configure(new CompareResultViewerConfiguration(prefixSuffixOffsets));
}
}
}
private static class CompareResultViewerConfiguration extends SourceViewerConfiguration {
private static class SimpleDamagerRepairer implements IPresentationDamager, IPresentationRepairer {
private IDocument fDocument;
private final int[] fPrefixSuffixOffsets2;
public SimpleDamagerRepairer(int[] prefixSuffixOffsets) {
fPrefixSuffixOffsets2= prefixSuffixOffsets;
}
public void setDocument(IDocument document) {
fDocument= document;
}
public IRegion getDamageRegion(ITypedRegion partition, DocumentEvent event, boolean changed) {
return new Region(0, fDocument.getLength());
}
public void createPresentation(TextPresentation presentation, ITypedRegion damage) {
presentation.setDefaultStyleRange(new StyleRange(0, fDocument.getLength(), null, null));
int prefix= fPrefixSuffixOffsets2[0];
int suffix= fPrefixSuffixOffsets2[1];
TextAttribute attr= new TextAttribute(Display.getDefault().getSystemColor(SWT.COLOR_RED), null, SWT.BOLD);
presentation.addStyleRange(new StyleRange(prefix, fDocument.getLength()-suffix-prefix, attr.getForeground(), attr.getBackground(), attr.getStyle()));
}
}
private final int[] fPrefixSuffixOffsets;
public CompareResultViewerConfiguration(int[] prefixSuffixOffsets) {
fPrefixSuffixOffsets= prefixSuffixOffsets;
}
public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
PresentationReconciler reconciler= new PresentationReconciler();
SimpleDamagerRepairer dr= new SimpleDamagerRepairer(fPrefixSuffixOffsets);
reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
return reconciler;
}
}
private static class CompareElement implements ITypedElement, IEncodedStreamContentAccessor {
private String fContent;
public CompareElement(String content) {
fContent= content;
}
public String getName() {
return "<no name>"; //$NON-NLS-1$
}
public Image getImage() {
return null;
}
public String getType() {
return "txt"; //$NON-NLS-1$
}
public InputStream getContents() {
try {
return new ByteArrayInputStream(fContent.getBytes("UTF-8")); //$NON-NLS-1$
} catch (UnsupportedEncodingException e) {
return new ByteArrayInputStream(fContent.getBytes());
}
}
public String getCharset() throws CoreException {
return "UTF-8"; //$NON-NLS-1$
}
}
private TextMergeViewer fViewer;
private String fExpected;
private String fActual;
private String fTestName;
/**
* Lengths of common prefix and suffix.
* Note: this array is passed to the DamagerRepairer and
* the lengths are updated on content change.
*/
private final int[] fPrefixSuffix= new int[2];
public CompareResultDialog(Shell parentShell, String element) {
super(parentShell);
fExpected = element;
fActual = element;
setShellStyle((getShellStyle() & ~SWT.APPLICATION_MODAL) | SWT.TOOL);
}
/* (non-Javadoc)
* @see org.eclipse.jface.dialogs.Dialog#isResizable()
* @since 3.4
*/
protected boolean isResizable() {
return true;
}
protected void configureShell(Shell newShell) {
super.configureShell(newShell);
}
protected Control createDialogArea(Composite parent) {
Composite composite = (Composite)super.createDialogArea(parent);
GridLayout layout= new GridLayout();
layout.numColumns= 1;
composite.setLayout(layout);
CompareViewerPane pane = new CompareViewerPane(composite, SWT.BORDER | SWT.FLAT);
pane.setText(fTestName);
GridData data= new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL);
data.widthHint= convertWidthInCharsToPixels(120);
data.heightHint= convertHeightInCharsToPixels(13);
pane.setLayoutData(data);
Control previewer= createPreviewer(pane);
pane.setContent(previewer);
GridData gd= new GridData(GridData.FILL_BOTH);
previewer.setLayoutData(gd);
applyDialogFont(parent);
return composite;
}
private Control createPreviewer(Composite parent) {
final CompareConfiguration compareConfiguration= new CompareConfiguration();
compareConfiguration.setLeftEditable(false);
compareConfiguration.setRightEditable(false);
compareConfiguration.setProperty(CompareConfiguration.IGNORE_WHITESPACE, Boolean.FALSE);
compareConfiguration.setProperty(PREFIX_SUFFIX_PROPERTY, fPrefixSuffix);
fViewer= new CompareResultMergeViewer(parent, SWT.NONE, compareConfiguration);
setCompareViewerInput();
Control control= fViewer.getControl();
control.addDisposeListener(new DisposeListener() {
public void widgetDisposed(DisposeEvent e) {
compareConfiguration.dispose();
}
});
return control;
}
private void setCompareViewerInput() {
if (! fViewer.getControl().isDisposed())
fViewer.setInput(new DiffNode(new CompareElement(fExpected), new CompareElement(fActual)));
}
public void setInput(String failedTest) {
setCompareViewerInput();
}
}