001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jface.text.source;
011:
012: import org.eclipse.swt.graphics.Color;
013: import org.eclipse.swt.graphics.GC;
014: import org.eclipse.swt.widgets.Composite;
015: import org.eclipse.swt.widgets.Control;
016:
017: import org.eclipse.core.runtime.Assert;
018:
019: import org.eclipse.jface.text.revisions.IRevisionListener;
020: import org.eclipse.jface.text.revisions.IRevisionRulerColumn;
021: import org.eclipse.jface.text.revisions.IRevisionRulerColumnExtension;
022: import org.eclipse.jface.text.revisions.RevisionInformation;
023:
024: import org.eclipse.jface.internal.text.revisions.RevisionPainter;
025: import org.eclipse.jface.internal.text.source.DiffPainter;
026: import org.eclipse.jface.viewers.ISelectionProvider;
027:
028: /**
029: * A vertical ruler column displaying line numbers and serving as a UI for quick diff.
030: * Clients usually instantiate and configure object of this class.
031: *
032: * @since 3.0
033: */
034: public final class LineNumberChangeRulerColumn extends
035: LineNumberRulerColumn implements IVerticalRulerInfo,
036: IVerticalRulerInfoExtension, IChangeRulerColumn,
037: IRevisionRulerColumn, IRevisionRulerColumnExtension {
038: /** The ruler's annotation model. */
039: private IAnnotationModel fAnnotationModel;
040: /** <code>true</code> if changes should be displayed using character indications instead of background colors. */
041: private boolean fCharacterDisplay;
042: /**
043: * The revision painter strategy.
044: *
045: * @since 3.2
046: */
047: private final RevisionPainter fRevisionPainter;
048: /**
049: * The diff information painter strategy.
050: *
051: * @since 3.2
052: */
053: private final DiffPainter fDiffPainter;
054: /**
055: * Whether to show number or to behave like a change ruler column.
056: * @since 3.3
057: */
058: private boolean fShowNumbers = true;
059:
060: /**
061: * Creates a new instance.
062: *
063: * @param sharedColors the shared colors provider to use
064: */
065: public LineNumberChangeRulerColumn(ISharedTextColors sharedColors) {
066: Assert.isNotNull(sharedColors);
067: fRevisionPainter = new RevisionPainter(this , sharedColors);
068: fDiffPainter = new DiffPainter(this , sharedColors);
069: }
070:
071: /*
072: * @see org.eclipse.jface.text.source.LineNumberRulerColumn#createControl(org.eclipse.jface.text.source.CompositeRuler, org.eclipse.swt.widgets.Composite)
073: */
074: public Control createControl(CompositeRuler parentRuler,
075: Composite parentControl) {
076: Control control = super .createControl(parentRuler,
077: parentControl);
078: fRevisionPainter.setParentRuler(parentRuler);
079: fDiffPainter.setParentRuler(parentRuler);
080: return control;
081: }
082:
083: /*
084: * @see org.eclipse.jface.text.source.IVerticalRulerInfo#getLineOfLastMouseButtonActivity()
085: */
086: public int getLineOfLastMouseButtonActivity() {
087: return getParentRuler().getLineOfLastMouseButtonActivity();
088: }
089:
090: /*
091: * @see org.eclipse.jface.text.source.IVerticalRulerInfo#toDocumentLineNumber(int)
092: */
093: public int toDocumentLineNumber(int y_coordinate) {
094: return getParentRuler().toDocumentLineNumber(y_coordinate);
095: }
096:
097: /*
098: * @see IVerticalRulerColumn#setModel(IAnnotationModel)
099: */
100: public void setModel(IAnnotationModel model) {
101: setAnnotationModel(model);
102: fRevisionPainter.setModel(model);
103: fDiffPainter.setModel(model);
104: updateNumberOfDigits();
105: computeIndentations();
106: layout(true);
107: postRedraw();
108: }
109:
110: private void setAnnotationModel(IAnnotationModel model) {
111: if (fAnnotationModel != model)
112: fAnnotationModel = model;
113: }
114:
115: /**
116: * Sets the display mode of the ruler. If character mode is set to <code>true</code>, diff
117: * information will be displayed textually on the line number ruler.
118: *
119: * @param characterMode <code>true</code> if diff information is to be displayed textually.
120: */
121: public void setDisplayMode(boolean characterMode) {
122: if (characterMode != fCharacterDisplay) {
123: fCharacterDisplay = characterMode;
124: updateNumberOfDigits();
125: computeIndentations();
126: layout(true);
127: }
128: }
129:
130: /*
131: * @see org.eclipse.jface.text.source.IVerticalRulerInfoExtension#getModel()
132: */
133: public IAnnotationModel getModel() {
134: return fAnnotationModel;
135: }
136:
137: /*
138: * @see org.eclipse.jface.text.source.LineNumberRulerColumn#createDisplayString(int)
139: */
140: protected String createDisplayString(int line) {
141: StringBuffer buffer = new StringBuffer();
142: if (fShowNumbers)
143: buffer.append(super .createDisplayString(line));
144: if (fCharacterDisplay && getModel() != null)
145: buffer.append(fDiffPainter.getDisplayCharacter(line));
146: return buffer.toString();
147: }
148:
149: /*
150: * @see org.eclipse.jface.text.source.LineNumberRulerColumn#computeNumberOfDigits()
151: */
152: protected int computeNumberOfDigits() {
153: int digits;
154: if (fCharacterDisplay && getModel() != null) {
155: if (fShowNumbers)
156: digits = super .computeNumberOfDigits() + 1;
157: else
158: digits = 1;
159: } else {
160: if (fShowNumbers)
161: digits = super .computeNumberOfDigits();
162: else
163: digits = 0;
164: }
165: if (fRevisionPainter.hasInformation())
166: digits += fRevisionPainter.getRequiredWidth();
167: return digits;
168: }
169:
170: /*
171: * @see org.eclipse.jface.text.source.IVerticalRulerInfoExtension#addVerticalRulerListener(org.eclipse.jface.text.source.IVerticalRulerListener)
172: */
173: public void addVerticalRulerListener(IVerticalRulerListener listener) {
174: throw new UnsupportedOperationException();
175: }
176:
177: /*
178: * @see org.eclipse.jface.text.source.IVerticalRulerInfoExtension#removeVerticalRulerListener(org.eclipse.jface.text.source.IVerticalRulerListener)
179: */
180: public void removeVerticalRulerListener(
181: IVerticalRulerListener listener) {
182: throw new UnsupportedOperationException();
183: }
184:
185: /*
186: * @see org.eclipse.jface.text.source.LineNumberRulerColumn#doPaint(org.eclipse.swt.graphics.GC)
187: */
188: void doPaint(GC gc, ILineRange visibleLines) {
189: Color foreground = gc.getForeground();
190: if (visibleLines != null) {
191: if (fRevisionPainter.hasInformation())
192: fRevisionPainter.paint(gc, visibleLines);
193: else if (fDiffPainter.hasInformation()) // don't paint quick diff colors if revisions are painted
194: fDiffPainter.paint(gc, visibleLines);
195: }
196: gc.setForeground(foreground);
197: if (fShowNumbers || fCharacterDisplay)
198: super .doPaint(gc, visibleLines);
199: }
200:
201: /*
202: * @see org.eclipse.jface.text.source.IVerticalRulerInfoExtension#getHover()
203: */
204: public IAnnotationHover getHover() {
205: int activeLine = getParentRuler()
206: .getLineOfLastMouseButtonActivity();
207: if (fRevisionPainter.hasHover(activeLine))
208: return fRevisionPainter.getHover();
209: if (fDiffPainter.hasHover(activeLine))
210: return fDiffPainter.getHover();
211: return null;
212: }
213:
214: /*
215: * @see org.eclipse.jface.text.source.IChangeRulerColumn#setHover(org.eclipse.jface.text.source.IAnnotationHover)
216: */
217: public void setHover(IAnnotationHover hover) {
218: fRevisionPainter.setHover(hover);
219: fDiffPainter.setHover(hover);
220: }
221:
222: /*
223: * @see org.eclipse.jface.text.source.IChangeRulerColumn#setBackground(org.eclipse.swt.graphics.Color)
224: */
225: public void setBackground(Color background) {
226: super .setBackground(background);
227: fRevisionPainter.setBackground(background);
228: fDiffPainter.setBackground(background);
229: }
230:
231: /*
232: * @see org.eclipse.jface.text.source.IChangeRulerColumn#setAddedColor(org.eclipse.swt.graphics.Color)
233: */
234: public void setAddedColor(Color addedColor) {
235: fDiffPainter.setAddedColor(addedColor);
236: }
237:
238: /*
239: * @see org.eclipse.jface.text.source.IChangeRulerColumn#setChangedColor(org.eclipse.swt.graphics.Color)
240: */
241: public void setChangedColor(Color changedColor) {
242: fDiffPainter.setChangedColor(changedColor);
243: }
244:
245: /*
246: * @see org.eclipse.jface.text.source.IChangeRulerColumn#setDeletedColor(org.eclipse.swt.graphics.Color)
247: */
248: public void setDeletedColor(Color deletedColor) {
249: fDiffPainter.setDeletedColor(deletedColor);
250: }
251:
252: /*
253: * @see org.eclipse.jface.text.revisions.IRevisionRulerColumn#setRevisionInformation(org.eclipse.jface.text.revisions.RevisionInformation)
254: */
255: public void setRevisionInformation(RevisionInformation info) {
256: fRevisionPainter.setRevisionInformation(info);
257: updateNumberOfDigits();
258: computeIndentations();
259: layout(true);
260: postRedraw();
261: }
262:
263: /*
264: * @see org.eclipse.jface.text.revisions.IRevisionRulerColumnExtension#getRevisionSelectionProvider()
265: * @since 3.2
266: */
267: public ISelectionProvider getRevisionSelectionProvider() {
268: return fRevisionPainter.getRevisionSelectionProvider();
269: }
270:
271: /*
272: * @see org.eclipse.jface.text.revisions.IRevisionRulerColumnExtension#setRenderingMode(org.eclipse.jface.text.revisions.IRevisionRulerColumnExtension.RenderingMode)
273: * @since 3.3
274: */
275: public void setRevisionRenderingMode(RenderingMode renderingMode) {
276: fRevisionPainter.setRenderingMode(renderingMode);
277: }
278:
279: /**
280: * Sets the line number display mode.
281: *
282: * @param showNumbers <code>true</code> to show numbers, <code>false</code> to only show
283: * diff / revision info.
284: * @since 3.3
285: */
286: public void showLineNumbers(boolean showNumbers) {
287: if (fShowNumbers != showNumbers) {
288: fShowNumbers = showNumbers;
289: updateNumberOfDigits();
290: computeIndentations();
291: layout(true);
292: }
293: }
294:
295: /*
296: * @see org.eclipse.jface.text.source.LineNumberRulerColumn#getWidth()
297: * @since 3.3
298: */
299: public int getWidth() {
300: int width = super .getWidth();
301: return width > 0 ? width : 8; // minimal width to display quick diff / revisions if no textual info is shown
302: }
303:
304: /**
305: * Returns <code>true</code> if the ruler is showing line numbers, <code>false</code>
306: * otherwise
307: *
308: * @return <code>true</code> if line numbers are shown, <code>false</code> otherwise
309: * @since 3.3
310: */
311: public boolean isShowingLineNumbers() {
312: return fShowNumbers;
313: }
314:
315: /**
316: * Returns <code>true</code> if the ruler is showing revision information, <code>false</code>
317: * otherwise
318: *
319: * @return <code>true</code> if revision information is shown, <code>false</code> otherwise
320: * @since 3.3
321: */
322: public boolean isShowingRevisionInformation() {
323: return fRevisionPainter.hasInformation();
324: }
325:
326: /**
327: * Returns <code>true</code> if the ruler is showing change information, <code>false</code>
328: * otherwise
329: *
330: * @return <code>true</code> if change information is shown, <code>false</code> otherwise
331: * @since 3.3
332: */
333: public boolean isShowingChangeInformation() {
334: return fDiffPainter.hasInformation();
335: }
336:
337: /*
338: * @see org.eclipse.jface.text.revisions.IRevisionRulerColumnExtension#showRevisionAuthor(boolean)
339: * @since 3.3
340: */
341: public void showRevisionAuthor(boolean show) {
342: fRevisionPainter.showRevisionAuthor(show);
343: updateNumberOfDigits();
344: computeIndentations();
345: layout(true);
346: postRedraw();
347: }
348:
349: /*
350: * @see org.eclipse.jface.text.revisions.IRevisionRulerColumnExtension#showRevisionId(boolean)
351: * @since 3.3
352: */
353: public void showRevisionId(boolean show) {
354: fRevisionPainter.showRevisionId(show);
355: updateNumberOfDigits();
356: computeIndentations();
357: layout(true);
358: postRedraw();
359: }
360:
361: /*
362: * @see org.eclipse.jface.text.revisions.IRevisionRulerColumnExtension#addRevisionListener(org.eclipse.jface.text.revisions.IRevisionListener)
363: * @since 3.3
364: */
365: public void addRevisionListener(IRevisionListener listener) {
366: fRevisionPainter.addRevisionListener(listener);
367: }
368:
369: /*
370: * @see org.eclipse.jface.text.revisions.IRevisionRulerColumnExtension#removeRevisionListener(org.eclipse.jface.text.revisions.IRevisionListener)
371: * @since 3.3
372: */
373: public void removeRevisionListener(IRevisionListener listener) {
374: fRevisionPainter.removeRevisionListener(listener);
375: }
376:
377: /*
378: * @see org.eclipse.jface.text.source.LineNumberRulerColumn#handleDispose()
379: * @since 3.3
380: */
381: protected void handleDispose() {
382: fRevisionPainter.setParentRuler(null);
383: fRevisionPainter.setModel(null);
384: fDiffPainter.setParentRuler(null);
385: fDiffPainter.setModel(null);
386: super.handleDispose();
387: }
388: }
|