KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > text > projection > ProjectionDocumentManager


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.projection;
12
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Map JavaDoc;
19
20 import org.eclipse.jface.text.DocumentEvent;
21 import org.eclipse.jface.text.IDocument;
22 import org.eclipse.jface.text.IDocumentInformationMapping;
23 import org.eclipse.jface.text.IDocumentListener;
24 import org.eclipse.jface.text.ISlaveDocumentManager;
25 import org.eclipse.jface.text.ISlaveDocumentManagerExtension;
26
27
28 /**
29  * A <code>ProjectionDocumentManager</code> is one particular implementation
30  * of {@link org.eclipse.jface.text.ISlaveDocumentManager}. This manager
31  * creates so called projection documents (see
32  * {@link org.eclipse.jface.text.projection.ProjectionDocument}as slave
33  * documents for given master documents.
34  * <p>
35  * A projection document represents a particular projection of the master
36  * document and is accordingly adapted to changes of the master document. Vice
37  * versa, the master document is accordingly adapted to changes of its slave
38  * documents. The manager does not maintain any particular management structure
39  * but utilizes mechanisms given by {@link org.eclipse.jface.text.IDocument}
40  * such as position categories and position updaters.
41  * <p>
42  * Clients can instantiate this class. This class is not intended to be
43  * subclassed.</p>
44  *
45  * @since 3.0
46  */

47 public class ProjectionDocumentManager implements IDocumentListener, ISlaveDocumentManager, ISlaveDocumentManagerExtension {
48
49     /** Registry for master documents and their projection documents. */
50     private Map JavaDoc fProjectionRegistry= new HashMap JavaDoc();
51
52     /**
53      * Registers the given projection document for the given master document.
54      *
55      * @param master the master document
56      * @param projection the projection document
57      */

58     private void add(IDocument master, ProjectionDocument projection) {
59         List JavaDoc list= (List JavaDoc) fProjectionRegistry.get(master);
60         if (list == null) {
61             list= new ArrayList JavaDoc(1);
62             fProjectionRegistry.put(master, list);
63         }
64         list.add(projection);
65     }
66
67     /**
68      * Unregisters the given projection document from its master.
69      *
70      * @param master the master document
71      * @param projection the projection document
72      */

73     private void remove(IDocument master, ProjectionDocument projection) {
74         List JavaDoc list= (List JavaDoc) fProjectionRegistry.get(master);
75         if (list != null) {
76             list.remove(projection);
77             if (list.size() == 0)
78                 fProjectionRegistry.remove(master);
79         }
80     }
81
82     /**
83      * Returns whether the given document is a master document.
84      *
85      * @param master the document
86      * @return <code>true</code> if the given document is a master document known to this manager
87      */

88     private boolean hasProjection(IDocument master) {
89         return (fProjectionRegistry.get(master) instanceof List JavaDoc);
90     }
91
92     /**
93      * Returns an iterator enumerating all projection documents registered for the given document or
94      * <code>null</code> if the document is not a known master document.
95      *
96      * @param master the document
97      * @return an iterator for all registered projection documents or <code>null</code>
98      */

99     private Iterator JavaDoc getProjectionsIterator(IDocument master) {
100         List JavaDoc list= (List JavaDoc) fProjectionRegistry.get(master);
101         if (list != null)
102             return list.iterator();
103         return null;
104     }
105
106     /**
107      * Informs all projection documents of the master document that issued the given document event.
108      *
109      * @param about indicates whether the change is about to happen or happened already
110      * @param masterEvent the document event which will be processed to inform the projection documents
111      */

112     protected void fireDocumentEvent(boolean about, DocumentEvent masterEvent) {
113         IDocument master= masterEvent.getDocument();
114         Iterator JavaDoc e= getProjectionsIterator(master);
115         if (e == null)
116             return;
117
118         while (e.hasNext()) {
119             ProjectionDocument document= (ProjectionDocument) e.next();
120             if (about)
121                 document.masterDocumentAboutToBeChanged(masterEvent);
122             else
123                 document.masterDocumentChanged(masterEvent);
124         }
125     }
126
127     /*
128      * @see org.eclipse.jface.text.IDocumentListener#documentChanged(org.eclipse.jface.text.DocumentEvent)
129      */

130     public void documentChanged(DocumentEvent event) {
131         fireDocumentEvent(false, event);
132     }
133
134     /*
135      * @see org.eclipse.jface.text.IDocumentListener#documentAboutToBeChanged(org.eclipse.jface.text.DocumentEvent)
136      */

137     public void documentAboutToBeChanged(DocumentEvent event) {
138         fireDocumentEvent(true, event);
139     }
140
141     /*
142      * @see org.eclipse.jface.text.ISlaveDocumentManager#createMasterSlaveMapping(org.eclipse.jface.text.IDocument)
143      */

144     public IDocumentInformationMapping createMasterSlaveMapping(IDocument slave) {
145         if (slave instanceof ProjectionDocument) {
146             ProjectionDocument projectionDocument= (ProjectionDocument) slave;
147             return projectionDocument.getProjectionMapping();
148         }
149         return null;
150     }
151
152     /*
153      * @see org.eclipse.jface.text.ISlaveDocumentManager#createSlaveDocument(org.eclipse.jface.text.IDocument)
154      */

155     public IDocument createSlaveDocument(IDocument master) {
156         if (!hasProjection(master))
157             master.addDocumentListener(this);
158         ProjectionDocument slave= createProjectionDocument(master);
159         add(master, slave);
160         return slave;
161     }
162
163     /**
164      * Factory method for projection documents.
165      *
166      * @param master the master document
167      * @return the newly created projection document
168      */

169     protected ProjectionDocument createProjectionDocument(IDocument master) {
170         return new ProjectionDocument(master);
171     }
172
173     /*
174      * @see org.eclipse.jface.text.ISlaveDocumentManager#freeSlaveDocument(org.eclipse.jface.text.IDocument)
175      */

176     public void freeSlaveDocument(IDocument slave) {
177         if (slave instanceof ProjectionDocument) {
178             ProjectionDocument projectionDocument= (ProjectionDocument) slave;
179             IDocument master= projectionDocument.getMasterDocument();
180             remove(master, projectionDocument);
181             projectionDocument.dispose();
182             if (!hasProjection(master))
183                 master.removeDocumentListener(this);
184         }
185     }
186
187     /*
188      * @see org.eclipse.jface.text.ISlaveDocumentManager#getMasterDocument(org.eclipse.jface.text.IDocument)
189      */

190     public IDocument getMasterDocument(IDocument slave) {
191         if (slave instanceof ProjectionDocument)
192             return ((ProjectionDocument) slave).getMasterDocument();
193         return null;
194     }
195
196     /*
197      * @see org.eclipse.jface.text.ISlaveDocumentManager#isSlaveDocument(org.eclipse.jface.text.IDocument)
198      */

199     public boolean isSlaveDocument(IDocument document) {
200         return (document instanceof ProjectionDocument);
201     }
202
203     /*
204      * @see org.eclipse.jface.text.ISlaveDocumentManager#setAutoExpandMode(org.eclipse.jface.text.IDocument, boolean)
205      */

206     public void setAutoExpandMode(IDocument slave, boolean autoExpanding) {
207         if (slave instanceof ProjectionDocument)
208             ((ProjectionDocument) slave).setAutoExpandMode(autoExpanding);
209     }
210
211     /*
212      * @see org.eclipse.jface.text.ISlaveDocumentManagerExtension#getSlaveDocuments(org.eclipse.jface.text.IDocument)
213      */

214     public IDocument[] getSlaveDocuments(IDocument master) {
215         List JavaDoc list= (List JavaDoc) fProjectionRegistry.get(master);
216         if (list != null) {
217             IDocument[] result= new IDocument[list.size()];
218             list.toArray(result);
219             return result;
220         }
221         return null;
222     }
223 }
224
Popular Tags