comparison dwtx/jface/text/revisions/RevisionInformation.d @ 129:eb30df5ca28b

Added JFace Text sources
author Frank Benoit <benoit@tionex.de>
date Sat, 23 Aug 2008 19:10:48 +0200
parents
children c4fb132a086c
comparison
equal deleted inserted replaced
128:8df1d4193877 129:eb30df5ca28b
1 /*******************************************************************************
2 * Copyright (c) 2000, 2008 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 * Port to the D programming language:
11 * Frank Benoit <benoit@tionex.de>
12 *******************************************************************************/
13 module dwtx.jface.text.revisions.RevisionInformation;
14
15 import dwt.dwthelper.utils;
16
17 import java.util.ArrayList;
18 import java.util.Collections;
19 import java.util.Comparator;
20 import java.util.Iterator;
21 import java.util.List;
22
23 import dwtx.core.runtime.Assert;
24 import dwtx.jface.internal.text.revisions.Hunk;
25 import dwtx.jface.text.IInformationControlCreator;
26 import dwtx.jface.text.ITextHoverExtension;
27 import dwtx.jface.text.information.IInformationProviderExtension2;
28
29 /**
30 * Encapsulates revision information for one line-based document.
31 * <p>
32 * Clients may instantiate.
33 * </p>
34 *
35 * @since 3.2
36 * @see Revision
37 */
38 public final class RevisionInformation : ITextHoverExtension, IInformationProviderExtension2 {
39 /** The revisions, element type: {@link Revision}. */
40 private final List fRevisions= new ArrayList();
41 /** A unmodifiable view of <code>fRevisions</code>. */
42 private final List fRORevisions= Collections.unmodifiableList(fRevisions);
43 /**
44 * The flattened list of {@link RevisionRange}s, unmodifiable. <code>null</code> if the list
45 * must be re-computed.
46 *
47 * @since 3.3
48 */
49 private List fRanges= null;
50
51 /**
52 * The hover control creator. Can be <code>null</code>.
53 *
54 * @since 3.3
55 */
56 private IInformationControlCreator fHoverControlCreator;
57
58 /**
59 * The information presenter control creator. Can be <code>null</code>.
60 *
61 * @since 3.3
62 */
63 private IInformationControlCreator fInformationPresenterControlCreator;
64
65 /**
66 * Creates a new revision information model.
67 */
68 public RevisionInformation() {
69 }
70
71 /**
72 * Adds a revision.
73 *
74 * @param revision a revision
75 */
76 public void addRevision(Revision revision) {
77 Assert.isLegal(revision !is null);
78 fRevisions.add(revision);
79 }
80
81 /**
82 * Returns the contained revisions.
83 *
84 * @return an unmodifiable view of the contained revisions (element type: {@link Revision})
85 */
86 public List getRevisions() {
87 return fRORevisions;
88 }
89
90 /**
91 * Returns the line ranges of this revision information. The returned information is only valid
92 * at the moment it is returned, and may change as the annotated document is modified. See
93 * {@link IRevisionListener} for a way to be informed when the revision information changes. The
94 * returned list is sorted by document offset.
95 *
96 * @return an unmodifiable view of the line ranges (element type: {@link RevisionRange})
97 * @see IRevisionListener
98 * @since 3.3
99 */
100 public List getRanges() {
101 if (fRanges is null) {
102 List ranges= new ArrayList(fRevisions.size() * 2); // wild size guess
103 for (Iterator it= fRevisions.iterator(); it.hasNext();) {
104 Revision revision= (Revision) it.next();
105 ranges.addAll(revision.getRegions());
106 }
107
108 // sort by start line
109 Collections.sort(ranges, new Comparator() {
110 public int compare(Object o1, Object o2) {
111 RevisionRange r1= (RevisionRange) o1;
112 RevisionRange r2= (RevisionRange) o2;
113
114 return r1.getStartLine() - r2.getStartLine();
115 }
116 });
117
118 fRanges= Collections.unmodifiableList(ranges);
119 }
120 return fRanges;
121 }
122
123 /**
124 * Adjusts the revision information to the given diff information. Any previous diff information is discarded. <strong>Note</strong>: This is an internal framework method and must not be called by clients.
125 *
126 * @param hunks the diff hunks to adjust the revision information to
127 * @since 3.3
128 * @noreference This method is not intended to be referenced by clients.
129 */
130 public void applyDiff(Hunk[] hunks) {
131 fRanges= null; // mark for recomputation
132 for (Iterator revisions= getRevisions().iterator(); revisions.hasNext();)
133 ((Revision) revisions.next()).applyDiff(hunks);
134 }
135
136 /*
137 * @see dwtx.jface.text.ITextHoverExtension#getHoverControlCreator()
138 * @since 3.3
139 */
140 public IInformationControlCreator getHoverControlCreator() {
141 return fHoverControlCreator;
142 }
143
144 /**
145 * {@inheritDoc}
146 * @return the information control creator or <code>null</code>
147 * @since 3.3
148 */
149 public IInformationControlCreator getInformationPresenterControlCreator() {
150 return fInformationPresenterControlCreator;
151 }
152
153 /**
154 * Sets the hover control creator.
155 * <p>
156 * <strong>Note:</strong> The created information control must be able to display the object
157 * returned by the concrete implementation of {@link Revision#getHoverInfo()}.
158 * </p>
159 *
160 * @param creator the control creator
161 * @since 3.3
162 */
163 public void setHoverControlCreator(IInformationControlCreator creator) {
164 fHoverControlCreator= creator;
165 }
166
167 /**
168 * Sets the information presenter control creator.
169 *
170 * @param creator the control creator
171 * @since 3.3
172 */
173 public void setInformationPresenterControlCreator(IInformationControlCreator creator) {
174 fInformationPresenterControlCreator= creator;
175 }
176 }