129
|
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.projection.ProjectionDocumentManager;
|
|
14
|
|
15 import dwt.dwthelper.utils;
|
|
16
|
|
17 import java.util.ArrayList;
|
|
18 import java.util.HashMap;
|
|
19 import java.util.Iterator;
|
|
20 import java.util.List;
|
|
21 import java.util.Map;
|
|
22
|
|
23 import dwtx.jface.text.DocumentEvent;
|
|
24 import dwtx.jface.text.IDocument;
|
|
25 import dwtx.jface.text.IDocumentInformationMapping;
|
|
26 import dwtx.jface.text.IDocumentListener;
|
|
27 import dwtx.jface.text.ISlaveDocumentManager;
|
|
28 import dwtx.jface.text.ISlaveDocumentManagerExtension;
|
|
29
|
|
30
|
|
31 /**
|
|
32 * A <code>ProjectionDocumentManager</code> is one particular implementation
|
|
33 * of {@link dwtx.jface.text.ISlaveDocumentManager}. This manager
|
|
34 * creates so called projection documents (see
|
|
35 * {@link dwtx.jface.text.projection.ProjectionDocument}as slave
|
|
36 * documents for given master documents.
|
|
37 * <p>
|
|
38 * A projection document represents a particular projection of the master
|
|
39 * document and is accordingly adapted to changes of the master document. Vice
|
|
40 * versa, the master document is accordingly adapted to changes of its slave
|
|
41 * documents. The manager does not maintain any particular management structure
|
|
42 * but utilizes mechanisms given by {@link dwtx.jface.text.IDocument}
|
|
43 * such as position categories and position updaters.
|
|
44 * <p>
|
|
45 * Clients can instantiate this class. This class is not intended to be
|
|
46 * subclassed.</p>
|
|
47 *
|
|
48 * @since 3.0
|
|
49 * @noextend This class is not intended to be subclassed by clients.
|
|
50 */
|
|
51 public class ProjectionDocumentManager : IDocumentListener, ISlaveDocumentManager, ISlaveDocumentManagerExtension {
|
|
52
|
|
53 /** Registry for master documents and their projection documents. */
|
|
54 private Map fProjectionRegistry= new HashMap();
|
|
55
|
|
56 /**
|
|
57 * Registers the given projection document for the given master document.
|
|
58 *
|
|
59 * @param master the master document
|
|
60 * @param projection the projection document
|
|
61 */
|
|
62 private void add(IDocument master, ProjectionDocument projection) {
|
|
63 List list= (List) fProjectionRegistry.get(master);
|
|
64 if (list is null) {
|
|
65 list= new ArrayList(1);
|
|
66 fProjectionRegistry.put(master, list);
|
|
67 }
|
|
68 list.add(projection);
|
|
69 }
|
|
70
|
|
71 /**
|
|
72 * Unregisters the given projection document from its master.
|
|
73 *
|
|
74 * @param master the master document
|
|
75 * @param projection the projection document
|
|
76 */
|
|
77 private void remove(IDocument master, ProjectionDocument projection) {
|
|
78 List list= (List) fProjectionRegistry.get(master);
|
|
79 if (list !is null) {
|
|
80 list.remove(projection);
|
|
81 if (list.size() is 0)
|
|
82 fProjectionRegistry.remove(master);
|
|
83 }
|
|
84 }
|
|
85
|
|
86 /**
|
|
87 * Returns whether the given document is a master document.
|
|
88 *
|
|
89 * @param master the document
|
|
90 * @return <code>true</code> if the given document is a master document known to this manager
|
|
91 */
|
|
92 private bool hasProjection(IDocument master) {
|
|
93 return (fProjectionRegistry.get(master) instanceof List);
|
|
94 }
|
|
95
|
|
96 /**
|
|
97 * Returns an iterator enumerating all projection documents registered for the given document or
|
|
98 * <code>null</code> if the document is not a known master document.
|
|
99 *
|
|
100 * @param master the document
|
|
101 * @return an iterator for all registered projection documents or <code>null</code>
|
|
102 */
|
|
103 private Iterator getProjectionsIterator(IDocument master) {
|
|
104 List list= (List) fProjectionRegistry.get(master);
|
|
105 if (list !is null)
|
|
106 return list.iterator();
|
|
107 return null;
|
|
108 }
|
|
109
|
|
110 /**
|
|
111 * Informs all projection documents of the master document that issued the given document event.
|
|
112 *
|
|
113 * @param about indicates whether the change is about to happen or happened already
|
|
114 * @param masterEvent the document event which will be processed to inform the projection documents
|
|
115 */
|
|
116 protected void fireDocumentEvent(bool about, DocumentEvent masterEvent) {
|
|
117 IDocument master= masterEvent.getDocument();
|
|
118 Iterator e= getProjectionsIterator(master);
|
|
119 if (e is null)
|
|
120 return;
|
|
121
|
|
122 while (e.hasNext()) {
|
|
123 ProjectionDocument document= (ProjectionDocument) e.next();
|
|
124 if (about)
|
|
125 document.masterDocumentAboutToBeChanged(masterEvent);
|
|
126 else
|
|
127 document.masterDocumentChanged(masterEvent);
|
|
128 }
|
|
129 }
|
|
130
|
|
131 /*
|
|
132 * @see dwtx.jface.text.IDocumentListener#documentChanged(dwtx.jface.text.DocumentEvent)
|
|
133 */
|
|
134 public void documentChanged(DocumentEvent event) {
|
|
135 fireDocumentEvent(false, event);
|
|
136 }
|
|
137
|
|
138 /*
|
|
139 * @see dwtx.jface.text.IDocumentListener#documentAboutToBeChanged(dwtx.jface.text.DocumentEvent)
|
|
140 */
|
|
141 public void documentAboutToBeChanged(DocumentEvent event) {
|
|
142 fireDocumentEvent(true, event);
|
|
143 }
|
|
144
|
|
145 /*
|
|
146 * @see dwtx.jface.text.ISlaveDocumentManager#createMasterSlaveMapping(dwtx.jface.text.IDocument)
|
|
147 */
|
|
148 public IDocumentInformationMapping createMasterSlaveMapping(IDocument slave) {
|
|
149 if (slave instanceof ProjectionDocument) {
|
|
150 ProjectionDocument projectionDocument= (ProjectionDocument) slave;
|
|
151 return projectionDocument.getDocumentInformationMapping();
|
|
152 }
|
|
153 return null;
|
|
154 }
|
|
155
|
|
156 /*
|
|
157 * @see dwtx.jface.text.ISlaveDocumentManager#createSlaveDocument(dwtx.jface.text.IDocument)
|
|
158 */
|
|
159 public IDocument createSlaveDocument(IDocument master) {
|
|
160 if (!hasProjection(master))
|
|
161 master.addDocumentListener(this);
|
|
162 ProjectionDocument slave= createProjectionDocument(master);
|
|
163 add(master, slave);
|
|
164 return slave;
|
|
165 }
|
|
166
|
|
167 /**
|
|
168 * Factory method for projection documents.
|
|
169 *
|
|
170 * @param master the master document
|
|
171 * @return the newly created projection document
|
|
172 */
|
|
173 protected ProjectionDocument createProjectionDocument(IDocument master) {
|
|
174 return new ProjectionDocument(master);
|
|
175 }
|
|
176
|
|
177 /*
|
|
178 * @see dwtx.jface.text.ISlaveDocumentManager#freeSlaveDocument(dwtx.jface.text.IDocument)
|
|
179 */
|
|
180 public void freeSlaveDocument(IDocument slave) {
|
|
181 if (slave instanceof ProjectionDocument) {
|
|
182 ProjectionDocument projectionDocument= (ProjectionDocument) slave;
|
|
183 IDocument master= projectionDocument.getMasterDocument();
|
|
184 remove(master, projectionDocument);
|
|
185 projectionDocument.dispose();
|
|
186 if (!hasProjection(master))
|
|
187 master.removeDocumentListener(this);
|
|
188 }
|
|
189 }
|
|
190
|
|
191 /*
|
|
192 * @see dwtx.jface.text.ISlaveDocumentManager#getMasterDocument(dwtx.jface.text.IDocument)
|
|
193 */
|
|
194 public IDocument getMasterDocument(IDocument slave) {
|
|
195 if (slave instanceof ProjectionDocument)
|
|
196 return ((ProjectionDocument) slave).getMasterDocument();
|
|
197 return null;
|
|
198 }
|
|
199
|
|
200 /*
|
|
201 * @see dwtx.jface.text.ISlaveDocumentManager#isSlaveDocument(dwtx.jface.text.IDocument)
|
|
202 */
|
|
203 public bool isSlaveDocument(IDocument document) {
|
|
204 return (document instanceof ProjectionDocument);
|
|
205 }
|
|
206
|
|
207 /*
|
|
208 * @see dwtx.jface.text.ISlaveDocumentManager#setAutoExpandMode(dwtx.jface.text.IDocument, bool)
|
|
209 */
|
|
210 public void setAutoExpandMode(IDocument slave, bool autoExpanding) {
|
|
211 if (slave instanceof ProjectionDocument)
|
|
212 ((ProjectionDocument) slave).setAutoExpandMode(autoExpanding);
|
|
213 }
|
|
214
|
|
215 /*
|
|
216 * @see dwtx.jface.text.ISlaveDocumentManagerExtension#getSlaveDocuments(dwtx.jface.text.IDocument)
|
|
217 */
|
|
218 public IDocument[] getSlaveDocuments(IDocument master) {
|
|
219 List list= (List) fProjectionRegistry.get(master);
|
|
220 if (list !is null) {
|
|
221 IDocument[] result= new IDocument[list.size()];
|
|
222 list.toArray(result);
|
|
223 return result;
|
|
224 }
|
|
225 return null;
|
|
226 }
|
|
227 }
|