Skip to main content
summaryrefslogtreecommitdiffstats
blob: 9185d4f97d99cf570f3c486825278ce514750838 (plain) (blame)
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*******************************************************************************
 * Copyright (c) 2000, 2003 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.jface.text.source;


import java.util.Iterator;

import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.VerifyEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;

import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ISlaveDocumentManager;
import org.eclipse.jface.text.ITextViewerExtension3;
import org.eclipse.jface.text.Position;
import org.eclipse.jface.text.ProjectionDocument;
import org.eclipse.jface.text.ProjectionDocumentManager;


/**
 * A projection source viewer is a source viewer which does not support the concept of a visible region. Instead it supports
 * to dynamically hide and show regions of its document. Uses <code>ProjectionDocumentManager</code> as it internal slave document manager.<p>
 * This class is for internal use only.
 * 
 * @since 2.1
 */
public class ProjectionSourceViewer extends SourceViewer implements ISourceViewer, ITextViewerExtension3 {
	
	/** The projection annotation model */
	private IAnnotationModel fProjectionAnnotationModel;
	
	/**
	 * Creates a new projection source viewer.
	 * 
	 * @param parent the SWT parent control
	 * @param ruler the vertical ruler
	 * @param styles the SWT style bits
	 */
	public ProjectionSourceViewer(Composite parent, IVerticalRuler ruler, int styles) {
		super(parent, ruler, styles);
	}
	
	/*
	 * @see ISourceViewer#setDocument(IDocument, IAnnotationModel, int, int)
	 */
	public void setDocument(IDocument document, IAnnotationModel annotationModel, int visibleRegionOffset, int visibleRegionLength) {
		if (getDocument() != null && fProjectionAnnotationModel != null)
			fProjectionAnnotationModel.disconnect(getDocument());
		
		super.setDocument(document, annotationModel, visibleRegionOffset, visibleRegionLength);
		
		if (getDocument() != null && fProjectionAnnotationModel != null)
			fProjectionAnnotationModel.connect(getDocument());
	}
	
	/*
	 * @see TextViewer#handleDispose()
	 */
	protected void handleDispose() {
		
		if (getDocument() != null && fProjectionAnnotationModel != null) {
			fProjectionAnnotationModel.disconnect(getDocument());
			fProjectionAnnotationModel= null;
		}

		super.handleDispose();
	}
	
	/**
	 * Returns the projection annotation model.
	 * 
	 * @return the projection annotation model
	 */
	public IAnnotationModel getProjectionAnnotationModel() {
		return fProjectionAnnotationModel;
	}

	/**
	 * Sets the projection annotation model.
	 * 
	 * @param projectionAnnotationModel the projection annotation model 
	 */
	public void setProjectionAnnotationModel(IAnnotationModel projectionAnnotationModel) {
		fProjectionAnnotationModel= projectionAnnotationModel;
	}
	
	/*
	 * @see org.eclipse.jface.text.TextViewer#createSlaveDocumentManager()
	 */
	protected ISlaveDocumentManager createSlaveDocumentManager() {
		return new ProjectionDocumentManager();
	}

	/*
	 * @see org.eclipse.jface.text.TextViewer#updateVisibleDocument(org.eclipse.jface.text.IDocument, int, int)
	 */
	protected boolean updateVisibleDocument(IDocument visibleDocument, int visibleRegionOffset, int visibleRegionLength) throws BadLocationException {
		if (visibleDocument instanceof ProjectionDocument) {
			ProjectionDocument document= (ProjectionDocument) visibleDocument;
			document.addFragment(visibleRegionOffset, visibleRegionLength);
		}
		return true;
	}
	
	/**
	 * Hides the given range by collapsing it.
	 *
	 * @param offset the offset of the range to hide
	 * @param length the length of the range to hide
	 */
	public void collapse(int offset, int length) {
		
		IDocument previous= getVisibleDocument();
		IDocument slave= createSlaveDocument(previous);
		
		if (slave instanceof ProjectionDocument) {
			
			StyledText textWidget= getTextWidget();
			try {
			
				if (textWidget != null)
					textWidget.setRedraw(false);
					
				
				int topIndex= getTopIndex();
				Point selection= getSelectedRange();
				
				// adapt selection
				int selectionEnd= selection.x + selection.y;
				if (offset < selectionEnd && selectionEnd <= offset + length) {
					
					int lineEnd= offset;
					
					try {
						IDocument document= getDocument();
						int line= document.getLineOfOffset(offset);
						IRegion lineInfo= document.getLineInformation(Math.max(line -1, 0));
						lineEnd= lineInfo.getOffset() + lineInfo.getLength();
					} catch (BadLocationException x) {
					}
					
					if (offset <= selection.x && selection.x < offset + length) {
						selection.x= lineEnd;
						selection.y= 0;
					} else {
						selection.y= Math.max(lineEnd - selection.x, 0);
					}
					
				} else if (offset <= selection.x && selection.x < offset + length) {
					int delta= offset + length - selection.x;
					selection.x= offset + length;
					selection.y -= delta;
				} 
				
				((ProjectionDocument) slave).hide(offset, length);				
				setVisibleDocument(slave);
				
				setSelectedRange(selection.x, selection.y);
				setTopIndex(topIndex);
				
			} finally {
				if(textWidget != null)
					textWidget.setRedraw(true);
			}
		}
	}

	/**
	 * Makes all hidden ranges in the given range visible again.
	 *
	 * @param offset the offset of the range
	 * @param length the length of the range
	 */
	public void expand(int offset, int length) {
		if (getVisibleDocument() instanceof ProjectionDocument) {
			ProjectionDocument document= (ProjectionDocument) getVisibleDocument();
			
			StyledText textWidget= getTextWidget();
			try {
				
				if (textWidget != null)
					textWidget.setRedraw(false);
			
				Point selection= getSelectedRange();
				int topIndex= getTopIndex();
				
				document.show(offset, length);
				setVisibleDocument(document);
				
				setSelectedRange(selection.x, selection.y);
				setTopIndex(topIndex);
			
			} finally {
				if (textWidget != null)
					textWidget.setRedraw(true);
			}
		}
	}

	/*
	 * @see org.eclipse.jface.text.ITextViewer#getVisibleRegion()
	 */
	public IRegion getVisibleRegion() {
		return null;
	}

	/*
	 * @see org.eclipse.jface.text.TextViewer#getVisibleRegionOffset()
	 */
	protected int getVisibleRegionOffset() {
		return -1;
	}

	/*
	 * @see org.eclipse.jface.text.TextViewer#internalGetVisibleRegion()
	 */
	protected IRegion internalGetVisibleRegion() {
		return null;
	}

	/*
	 * @see org.eclipse.jface.text.ITextViewer#overlapsWithVisibleRegion(int,int)
	 */
	public boolean overlapsWithVisibleRegion(int offset, int length) {
		return false;
	}
	
	/*
	 * @see org.eclipse.jface.text.TextViewer#getVisibleDocument()
	 */
	public IDocument getVisibleDocument() {
		return super.getVisibleDocument();
	}
	
	/*
	 * @see org.eclipse.jface.text.TextViewer#handleVerifyEvent(org.eclipse.swt.events.VerifyEvent)
	 */
	protected void handleVerifyEvent(VerifyEvent e) {
		IRegion modelRange= event2ModelRange(e);
		Iterator iterator= fProjectionAnnotationModel.getAnnotationIterator();
		while (iterator.hasNext()) {
			ProjectionAnnotation annotation= (ProjectionAnnotation) iterator.next();
			if (annotation.isFolded()) {
				Position position= fProjectionAnnotationModel.getPosition(annotation);
				if (position.overlapsWith(modelRange.getOffset(), modelRange.getLength()) /* || is a delete at the boundary */ ) {
					e.doit= false;
					annotation.run(this);
				}
			}
		}
	}
}

Back to the top