Added
Link Here
|
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2009 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.compare.contentmergeviewer; |
12 |
|
13 |
import java.lang.reflect.Field; |
14 |
import java.lang.reflect.InvocationTargetException; |
15 |
import java.lang.reflect.Method; |
16 |
import java.util.ArrayList; |
17 |
import java.util.HashMap; |
18 |
import java.util.Iterator; |
19 |
import java.util.Map; |
20 |
|
21 |
import org.eclipse.compare.CompareConfiguration; |
22 |
import org.eclipse.compare.internal.CompareUIPlugin; |
23 |
import org.eclipse.core.runtime.Assert; |
24 |
import org.eclipse.core.runtime.CoreException; |
25 |
import org.eclipse.jface.text.ITextViewer; |
26 |
import org.eclipse.jface.text.TextViewer; |
27 |
import org.eclipse.jface.text.source.ISourceViewer; |
28 |
import org.eclipse.jface.text.source.SourceViewer; |
29 |
import org.eclipse.swt.events.DisposeEvent; |
30 |
import org.eclipse.swt.widgets.Composite; |
31 |
import org.eclipse.ui.IEditorInput; |
32 |
import org.eclipse.ui.IEditorSite; |
33 |
import org.eclipse.ui.IStorageEditorInput; |
34 |
import org.eclipse.ui.IWorkbenchPart; |
35 |
import org.eclipse.ui.IWorkbenchPartSite; |
36 |
import org.eclipse.ui.PartInitException; |
37 |
import org.eclipse.ui.editors.text.TextEditor; |
38 |
import org.eclipse.ui.part.FileEditorInput; |
39 |
import org.eclipse.ui.texteditor.AbstractTextEditor; |
40 |
|
41 |
/** |
42 |
* @since 3.5 |
43 |
*/ |
44 |
public class TextEditorMergeViewer extends TextMergeViewer { |
45 |
|
46 |
private Map /*<TextEditorAdapter>*/ fEditor; |
47 |
private ArrayList/*<SourceViewer>*/ fSourceViewer; |
48 |
private IWorkbenchPartSite fSavedSite; |
49 |
|
50 |
public TextEditorMergeViewer(Composite parent, CompareConfiguration configuration) { |
51 |
super(parent, configuration); |
52 |
} |
53 |
|
54 |
protected void handleDispose(DisposeEvent event) { |
55 |
fSourceViewer= null; |
56 |
if (fEditor != null) { |
57 |
for (Iterator iterator= fEditor.values().iterator(); iterator.hasNext();) { |
58 |
TextEditorAdapter editor= (TextEditorAdapter)iterator.next(); |
59 |
editor.dispose(); |
60 |
} |
61 |
fEditor= null; |
62 |
} |
63 |
super.handleDispose(event); |
64 |
} |
65 |
|
66 |
protected void configureTextViewer(TextViewer viewer) { |
67 |
if (viewer instanceof SourceViewer) { |
68 |
SourceViewer sourceViewer = (SourceViewer) viewer; |
69 |
if (fSourceViewer == null) |
70 |
fSourceViewer = new ArrayList(); |
71 |
if (!fSourceViewer.contains(sourceViewer)) |
72 |
fSourceViewer.add(sourceViewer); |
73 |
IEditorInput editorInput = getEditorInput(sourceViewer); |
74 |
sourceViewer.unconfigure(); |
75 |
if (editorInput == null) { |
76 |
// old-fashioned configuration |
77 |
super.configureTextViewer(viewer); |
78 |
return; |
79 |
} |
80 |
configureTextViewer(sourceViewer, editorInput); |
81 |
} |
82 |
} |
83 |
|
84 |
/* |
85 |
* @see org.eclipse.compare.contentmergeviewer.TextMergeViewer#setEditable(org.eclipse.jface.text.source.ISourceViewer, boolean) |
86 |
* @since 3.5 |
87 |
*/ |
88 |
protected void setEditable(ISourceViewer sourceViewer, boolean state) { |
89 |
super.setEditable(sourceViewer, state); |
90 |
if (fEditor != null) { |
91 |
Object editor= fEditor.get(sourceViewer); |
92 |
if (editor instanceof TextEditorAdapter) |
93 |
((TextEditorAdapter)editor).setEditable(state); |
94 |
} |
95 |
} |
96 |
|
97 |
/* |
98 |
* @see org.eclipse.compare.contentmergeviewer.TextMergeViewer#isEditorBacked(org.eclipse.jface.text.ITextViewer) |
99 |
* @since 3.5 |
100 |
*/ |
101 |
protected boolean isEditorBacked(ITextViewer textViewer) { |
102 |
return true; |
103 |
} |
104 |
|
105 |
protected IEditorInput getEditorInput(ISourceViewer sourceViewer) { |
106 |
IEditorInput editorInput= super.getEditorInput(sourceViewer); |
107 |
if (editorInput == null) |
108 |
return null; |
109 |
if (getSite() == null) |
110 |
return null; |
111 |
if (!isInputValid(editorInput)) |
112 |
return null; |
113 |
return editorInput; |
114 |
} |
115 |
|
116 |
private IWorkbenchPartSite getSite() { |
117 |
IWorkbenchPart workbenchPart= getCompareConfiguration().getContainer().getWorkbenchPart(); |
118 |
IWorkbenchPartSite site= null; |
119 |
if (workbenchPart != null) |
120 |
site = workbenchPart.getSite(); |
121 |
if (fSavedSite == null) |
122 |
fSavedSite = site; |
123 |
// TODO: this is a hack |
124 |
return fSavedSite; |
125 |
} |
126 |
|
127 |
private boolean isInputValid(IEditorInput editorInput) { |
128 |
if (editorInput instanceof FileEditorInput) { |
129 |
FileEditorInput fileEditorInput= (FileEditorInput)editorInput; |
130 |
return fileEditorInput.getFile().isAccessible(); |
131 |
} else if (editorInput instanceof IStorageEditorInput) { |
132 |
return true; |
133 |
} |
134 |
return false; |
135 |
} |
136 |
|
137 |
private void configureTextViewer(SourceViewer sourceViewer, IEditorInput editorInput) { |
138 |
if (editorInput != null) { |
139 |
// when input available, use editor |
140 |
TextEditorAdapter editor= (TextEditorAdapter)fEditor.get(sourceViewer); |
141 |
try { |
142 |
editor.init((IEditorSite)editor.getSite(), editorInput); |
143 |
editor.createActions(); |
144 |
} catch (PartInitException e) { |
145 |
CompareUIPlugin.log(e); |
146 |
} |
147 |
} |
148 |
} |
149 |
|
150 |
protected ISourceViewer createSourceViewer(Composite parent, int textOrientation) { |
151 |
ISourceViewer sourceViewer = super.createSourceViewer(parent, |
152 |
textOrientation); |
153 |
TextEditorAdapter editor = new TextEditorAdapter(); |
154 |
editor.setSourceViewer(sourceViewer); |
155 |
|
156 |
if (fEditor == null) |
157 |
fEditor = new HashMap(3); |
158 |
fEditor.put(sourceViewer, editor); |
159 |
|
160 |
if (fSourceViewer == null) |
161 |
fSourceViewer = new ArrayList(); |
162 |
fSourceViewer.add(sourceViewer); |
163 |
|
164 |
return sourceViewer; |
165 |
} |
166 |
|
167 |
protected void setActionsActivated(SourceViewer sourceViewer, boolean state) { |
168 |
if (fEditor != null) { |
169 |
Object editor= fEditor.get(sourceViewer); |
170 |
if (editor instanceof TextEditorAdapter) |
171 |
((TextEditorAdapter)editor).setActionsActivated(state); |
172 |
} |
173 |
} |
174 |
|
175 |
private class TextEditorAdapter extends TextEditor { |
176 |
private boolean fInputSet = false; |
177 |
private boolean fEditable; |
178 |
|
179 |
private void setEditable(boolean editable) { |
180 |
fEditable= editable; |
181 |
} |
182 |
public IWorkbenchPartSite getSite() { |
183 |
if (TextEditorMergeViewer.this != null) // when called from super(); |
184 |
return TextEditorMergeViewer.this.getSite(); |
185 |
// TODO: any side effects? |
186 |
return null; |
187 |
} |
188 |
public void createActions() { |
189 |
if (fInputSet) { |
190 |
super.createActions(); |
191 |
} |
192 |
// else do nothing, we will create actions later, when input is available |
193 |
} |
194 |
public void createPartControl(Composite composite) { |
195 |
// empty implementation |
196 |
} |
197 |
|
198 |
void setSourceViewer(ISourceViewer sourceViewer) { |
199 |
// no setter to private field AbstractTextEditor.fSourceViewer |
200 |
Field field= null; |
201 |
try { |
202 |
field= AbstractTextEditor.class.getDeclaredField("fSourceViewer"); //$NON-NLS-1$ |
203 |
} catch (SecurityException ex) { |
204 |
CompareUIPlugin.log(ex); |
205 |
} catch (NoSuchFieldException ex) { |
206 |
CompareUIPlugin.log(ex); |
207 |
} |
208 |
field.setAccessible(true); |
209 |
try { |
210 |
field.set(this, sourceViewer); |
211 |
} catch (IllegalArgumentException ex) { |
212 |
CompareUIPlugin.log(ex); |
213 |
} catch (IllegalAccessException ex) { |
214 |
CompareUIPlugin.log(ex); |
215 |
} |
216 |
|
217 |
getSelectionProvider().addSelectionChangedListener(getSelectionChangedListener()); |
218 |
} |
219 |
|
220 |
protected void doSetInput(IEditorInput input) throws CoreException { |
221 |
super.doSetInput(input); |
222 |
// the editor input has been explicitly set |
223 |
fInputSet = true; |
224 |
} |
225 |
public IEditorInput getEditorInput() { |
226 |
return fInputSet ? TextEditorMergeViewer.this.getEditorInput(getSourceViewer()) : super.getEditorInput(); |
227 |
} |
228 |
|
229 |
public ISourceViewer getViewer() { |
230 |
return getSourceViewer(); |
231 |
} |
232 |
// called by org.eclipse.ui.texteditor.TextEditorAction.canModifyEditor() |
233 |
public boolean isEditable() { |
234 |
return fEditable; |
235 |
} |
236 |
public boolean isEditorInputModifiable() { |
237 |
return fEditable; |
238 |
} |
239 |
public boolean isEditorInputReadOnly() { |
240 |
return !fEditable; |
241 |
} |
242 |
|
243 |
protected void setActionsActivated(boolean state) { |
244 |
Method method= null; |
245 |
try { |
246 |
method= AbstractTextEditor.class.getDeclaredMethod("setActionActivation", new Class[] { boolean.class }); //$NON-NLS-1$ |
247 |
} catch (SecurityException ex) { |
248 |
CompareUIPlugin.log(ex); |
249 |
} catch (NoSuchMethodException ex) { |
250 |
CompareUIPlugin.log(ex); |
251 |
} |
252 |
Assert.isNotNull(method); |
253 |
method.setAccessible(true); |
254 |
try { |
255 |
method.invoke(this, new Object[] { new Boolean(state) }); |
256 |
} catch (IllegalArgumentException ex) { |
257 |
CompareUIPlugin.log(ex); |
258 |
} catch (InvocationTargetException ex) { |
259 |
CompareUIPlugin.log(ex); |
260 |
} catch (IllegalAccessException ex) { |
261 |
CompareUIPlugin.log(ex); |
262 |
} |
263 |
} |
264 |
} |
265 |
|
266 |
|
267 |
} |