Skip to content

Commit 28f0a63

Browse files
oleosterhagenmickaelistria
authored andcommitted
Provide a new viewer for the Restore from Local History action #1879
The new viewer implementation is used when the viewer factory is invoked without a compare configuration. This is the case when it is called from the "Restore from Local History" action. The existing merge viewer cannot be used in this situation.
1 parent afe3422 commit 28f0a63

File tree

2 files changed

+206
-2
lines changed

2 files changed

+206
-2
lines changed

bundles/org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/compare/CompareViewerCreator.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* SPDX-License-Identifier: EPL-2.0
1010
*
1111
* Contributors:
12+
* - Ole Osterhagen - Issue #1879
1213
* - Mickael Istria (Red Hat Inc.)
1314
*******************************************************************************/
1415
package org.eclipse.ui.internal.genericeditor.compare;
@@ -20,8 +21,10 @@
2021

2122
public class CompareViewerCreator implements IViewerCreator {
2223

23-
@Override public Viewer createViewer(Composite parent, CompareConfiguration compareConfiguration) {
24-
return new GenericEditorMergeViewer(parent, compareConfiguration);
24+
@Override
25+
public Viewer createViewer(Composite parent, CompareConfiguration compareConfiguration) {
26+
return compareConfiguration != null ? new GenericEditorMergeViewer(parent, compareConfiguration)
27+
: new GenericEditorViewer(parent);
2528
}
2629

2730
}
Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2024 Ole Osterhagen and others.
3+
*
4+
* This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License 2.0
6+
* which accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
*
11+
* Contributors:
12+
* Ole Osterhagen - initial API and implementation
13+
*******************************************************************************/
14+
package org.eclipse.ui.internal.genericeditor.compare;
15+
16+
import java.io.InputStream;
17+
18+
import org.eclipse.compare.IEncodedStreamContentAccessor;
19+
import org.eclipse.compare.ITypedElement;
20+
import org.eclipse.compare.SharedDocumentAdapter;
21+
import org.eclipse.compare.contentmergeviewer.TextMergeViewer;
22+
import org.eclipse.core.resources.IEncodedStorage;
23+
import org.eclipse.core.resources.IStorage;
24+
import org.eclipse.core.runtime.CoreException;
25+
import org.eclipse.core.runtime.IPath;
26+
import org.eclipse.core.runtime.IStatus;
27+
import org.eclipse.core.runtime.PlatformObject;
28+
import org.eclipse.core.runtime.Status;
29+
import org.eclipse.jface.preference.IPreferenceStore;
30+
import org.eclipse.jface.resource.ImageDescriptor;
31+
import org.eclipse.jface.resource.JFaceResources;
32+
import org.eclipse.jface.text.source.SourceViewer;
33+
import org.eclipse.jface.viewers.ISelection;
34+
import org.eclipse.jface.viewers.StructuredSelection;
35+
import org.eclipse.jface.viewers.Viewer;
36+
import org.eclipse.swt.SWT;
37+
import org.eclipse.swt.widgets.Composite;
38+
import org.eclipse.swt.widgets.Control;
39+
import org.eclipse.ui.IEditorInput;
40+
import org.eclipse.ui.IPersistableElement;
41+
import org.eclipse.ui.IStorageEditorInput;
42+
import org.eclipse.ui.PlatformUI;
43+
import org.eclipse.ui.editors.text.EditorsUI;
44+
import org.eclipse.ui.internal.genericeditor.ExtensionBasedTextViewerConfiguration;
45+
import org.eclipse.ui.internal.genericeditor.GenericEditorPlugin;
46+
import org.eclipse.ui.texteditor.ChainedPreferenceStore;
47+
import org.eclipse.ui.texteditor.IDocumentProvider;
48+
49+
public class GenericEditorViewer extends Viewer {
50+
51+
private final SourceViewer sourceViewer;
52+
53+
private IEditorInput editorInput;
54+
55+
public GenericEditorViewer(Composite parent) {
56+
sourceViewer = new SourceViewer(parent, null, SWT.H_SCROLL | SWT.V_SCROLL);
57+
sourceViewer.setEditable(false);
58+
59+
// use the same font as the TextMergeViewer
60+
sourceViewer.getTextWidget().setFont(JFaceResources.getFont(TextMergeViewer.class.getName()));
61+
sourceViewer.getTextWidget().addDisposeListener(e -> disconnect());
62+
}
63+
64+
@Override
65+
public Control getControl() {
66+
return sourceViewer.getControl();
67+
}
68+
69+
@Override
70+
public Object getInput() {
71+
return editorInput;
72+
}
73+
74+
@Override
75+
public ISelection getSelection() {
76+
return StructuredSelection.EMPTY;
77+
}
78+
79+
@Override
80+
public void refresh() {
81+
// empty implementation
82+
}
83+
84+
@Override
85+
public void setInput(Object input) {
86+
disconnect();
87+
88+
if (!(input instanceof ITypedElement && input instanceof IEncodedStreamContentAccessor)) {
89+
return;
90+
}
91+
92+
IStorage storage = new Storage<>((ITypedElement & IEncodedStreamContentAccessor) input);
93+
editorInput = new StorageEditorInput(storage);
94+
95+
IDocumentProvider documentProvider = SharedDocumentAdapter.getDocumentProvider(editorInput);
96+
try {
97+
documentProvider.connect(editorInput);
98+
} catch (CoreException ex) {
99+
GenericEditorPlugin.getDefault().getLog()
100+
.log(new Status(IStatus.ERROR, GenericEditorPlugin.BUNDLE_ID, ex.getMessage(), ex));
101+
}
102+
103+
sourceViewer.setDocument(documentProvider.getDocument(editorInput));
104+
105+
ExtensionBasedTextViewerConfiguration configuration = new ExtensionBasedTextViewerConfiguration(null,
106+
new ChainedPreferenceStore(new IPreferenceStore[] { EditorsUI.getPreferenceStore(),
107+
GenericEditorPlugin.getDefault().getPreferenceStore() }));
108+
sourceViewer.unconfigure();
109+
sourceViewer.configure(configuration);
110+
}
111+
112+
@Override
113+
public void setSelection(ISelection selection, boolean reveal) {
114+
// empty implementation
115+
}
116+
117+
private void disconnect() {
118+
if (editorInput != null) {
119+
sourceViewer.setDocument(null);
120+
SharedDocumentAdapter.getDocumentProvider(editorInput).disconnect(editorInput);
121+
editorInput = null;
122+
}
123+
}
124+
125+
private static class Storage<T extends ITypedElement & IEncodedStreamContentAccessor> extends PlatformObject
126+
implements IEncodedStorage {
127+
128+
private final T element;
129+
130+
public Storage(T element) {
131+
this.element = element;
132+
}
133+
134+
@Override
135+
public InputStream getContents() throws CoreException {
136+
return element.getContents();
137+
}
138+
139+
@Override
140+
public IPath getFullPath() {
141+
return null;
142+
}
143+
144+
@Override
145+
public String getName() {
146+
return element.getName();
147+
}
148+
149+
@Override
150+
public boolean isReadOnly() {
151+
return true;
152+
}
153+
154+
@Override
155+
public String getCharset() throws CoreException {
156+
return element.getCharset();
157+
}
158+
159+
}
160+
161+
private static class StorageEditorInput extends PlatformObject implements IStorageEditorInput {
162+
163+
private final IStorage storage;
164+
165+
public StorageEditorInput(IStorage storage) {
166+
this.storage = storage;
167+
}
168+
169+
@Override
170+
public boolean exists() {
171+
return false;
172+
}
173+
174+
@Override
175+
public ImageDescriptor getImageDescriptor() {
176+
return PlatformUI.getWorkbench().getEditorRegistry().getImageDescriptor(storage.getName());
177+
}
178+
179+
@Override
180+
public String getName() {
181+
return storage.getName();
182+
}
183+
184+
@Override
185+
public IPersistableElement getPersistable() {
186+
return null;
187+
}
188+
189+
@Override
190+
public String getToolTipText() {
191+
return storage.getName();
192+
}
193+
194+
@Override
195+
public IStorage getStorage() throws CoreException {
196+
return storage;
197+
}
198+
199+
}
200+
201+
}

0 commit comments

Comments
 (0)