KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > text > revisions > RevisionInformation


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.jface.text.revisions;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Collections JavaDoc;
15 import java.util.Comparator JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18
19 import org.eclipse.core.runtime.Assert;
20
21 import org.eclipse.jface.internal.text.revisions.Hunk;
22
23 import org.eclipse.jface.text.IInformationControlCreator;
24 import org.eclipse.jface.text.ITextHoverExtension;
25 import org.eclipse.jface.text.information.IInformationProviderExtension2;
26
27 /**
28  * Encapsulates revision information for one line-based document.
29  * <p>
30  * Clients may instantiate.
31  * </p>
32  *
33  * @since 3.2
34  * @see Revision
35  */

36 public final class RevisionInformation implements ITextHoverExtension, IInformationProviderExtension2 {
37     /** The revisions, element type: {@link Revision}. */
38     private final List JavaDoc fRevisions= new ArrayList JavaDoc();
39     /** A unmodifiable view of <code>fRevisions</code>. */
40     private final List JavaDoc fRORevisions= Collections.unmodifiableList(fRevisions);
41     /**
42      * The flattened list of {@link RevisionRange}s, unmodifiable. <code>null</code> if the list
43      * must be re-computed.
44      *
45      * @since 3.3
46      */

47     private List JavaDoc fRanges= null;
48     
49     /**
50      * The hover control creator. Can be <code>null</code>.
51      *
52      * @since 3.3
53      */

54     private IInformationControlCreator fHoverControlCreator;
55     
56     /**
57      * The information presenter control creator. Can be <code>null</code>.
58      *
59      * @since 3.3
60      */

61     private IInformationControlCreator fInformationPresenterControlCreator;
62
63     /**
64      * Creates a new revision information model.
65      */

66     public RevisionInformation() {
67     }
68
69     /**
70      * Adds a revision.
71      *
72      * @param revision a revision
73      */

74     public void addRevision(Revision revision) {
75         Assert.isLegal(revision != null);
76         fRevisions.add(revision);
77     }
78
79     /**
80      * Returns the contained revisions.
81      *
82      * @return an unmodifiable view of the contained revisions (element type: {@link Revision})
83      */

84     public List JavaDoc getRevisions() {
85         return fRORevisions;
86     }
87
88     /**
89      * Returns the line ranges of this revision information. The returned information is only valid
90      * at the moment it is returned, and may change as the annotated document is modified. See
91      * {@link IRevisionListener} for a way to be informed when the revision information changes. The
92      * returned list is sorted by document offset.
93      *
94      * @return an unmodifiable view of the line ranges (element type: {@link RevisionRange})
95      * @see IRevisionListener
96      * @since 3.3
97      */

98     public List JavaDoc getRanges() {
99         if (fRanges == null) {
100             List JavaDoc ranges= new ArrayList JavaDoc(fRevisions.size() * 2); // wild size guess
101
for (Iterator JavaDoc it= fRevisions.iterator(); it.hasNext();) {
102                 Revision revision= (Revision) it.next();
103                 ranges.addAll(revision.getRegions());
104             }
105
106             // sort by start line
107
Collections.sort(ranges, new Comparator JavaDoc() {
108                 public int compare(Object JavaDoc o1, Object JavaDoc o2) {
109                     RevisionRange r1= (RevisionRange) o1;
110                     RevisionRange r2= (RevisionRange) o2;
111
112                     return r1.getStartLine() - r2.getStartLine();
113                 }
114             });
115
116             fRanges= Collections.unmodifiableList(ranges);
117         }
118         return fRanges;
119     }
120
121     /**
122      * Adjusts the revision information to the given diff information. Any previous diff information
123      * is discarded. <strong>Note</strong>: This is an internal framework method and must not be
124      * called by clients.
125      *
126      * @param hunks the diff hunks to adjust the revision information to
127      * @since 3.3
128      */

129     public void applyDiff(Hunk[] hunks) {
130         fRanges= null; // mark for recomputation
131
for (Iterator JavaDoc revisions= getRevisions().iterator(); revisions.hasNext();)
132             ((Revision) revisions.next()).applyDiff(hunks);
133     }
134
135     /*
136      * @see org.eclipse.jface.text.ITextHoverExtension#getHoverControlCreator()
137      * @since 3.3
138      */

139     public IInformationControlCreator getHoverControlCreator() {
140         return fHoverControlCreator;
141     }
142
143     /**
144      * {@inheritDoc}
145      * @return the information control creator or <code>null</code>
146      * @since 3.3
147      */

148     public IInformationControlCreator getInformationPresenterControlCreator() {
149         return fInformationPresenterControlCreator;
150     }
151     
152     /**
153      * Sets the hover control creator.
154      *
155      * @param creator the control creator
156      * @since 3.3
157      */

158     public void setHoverControlCreator(IInformationControlCreator creator) {
159         fHoverControlCreator= creator;
160     }
161
162     /**
163      * Sets the information presenter control creator.
164      *
165      * @param creator the control creator
166      * @since 3.3
167      */

168     public void setInformationPresenterControlCreator(IInformationControlCreator creator) {
169         fInformationPresenterControlCreator= creator;
170     }
171 }
172
Popular Tags