Mercurial > projects > dwt-addons
annotate dwtx/jface/text/link/LinkedModeManager.d @ 200:eb3414669eb0 default tip
fix for dmd 1.041 and tango 0.99.8
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sat, 28 Mar 2009 03:09:57 +0100 |
parents | 1a5b8f8129df |
children |
rev | line source |
---|---|
129 | 1 /******************************************************************************* |
2 * Copyright (c) 2000, 2006 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.link.LinkedModeManager; | |
14 | |
131 | 15 import dwtx.jface.text.link.LinkedModeModel; // packageimport |
16 import dwtx.jface.text.link.LinkedPosition; // packageimport | |
17 import dwtx.jface.text.link.ILinkedModeListener; // packageimport | |
18 import dwtx.jface.text.link.TabStopIterator; // packageimport | |
19 import dwtx.jface.text.link.LinkedModeUI; // packageimport | |
20 import dwtx.jface.text.link.InclusivePositionUpdater; // packageimport | |
21 import dwtx.jface.text.link.LinkedPositionGroup; // packageimport | |
22 import dwtx.jface.text.link.LinkedPositionAnnotations; // packageimport | |
23 import dwtx.jface.text.link.ProposalPosition; // packageimport | |
24 | |
25 | |
129 | 26 import dwt.dwthelper.utils; |
27 | |
153
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
137
diff
changeset
|
28 import dwtx.dwtxhelper.Collection; |
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
137
diff
changeset
|
29 |
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
137
diff
changeset
|
30 |
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
137
diff
changeset
|
31 |
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
137
diff
changeset
|
32 |
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
137
diff
changeset
|
33 |
129 | 34 |
35 import dwtx.core.runtime.Assert; | |
36 import dwtx.jface.text.IDocument; | |
37 | |
38 | |
39 /** | |
40 * A linked mode manager ensures exclusive access of linked position infrastructures to documents. There | |
41 * is at most one <code>LinkedModeManager</code> installed on the same document. The <code>getManager</code> | |
42 * methods will return the existing instance if any of the specified documents already have an installed | |
43 * manager. | |
44 * | |
45 * @since 3.0 | |
46 */ | |
47 class LinkedModeManager { | |
48 | |
49 /** | |
50 * Our implementation of <code>ILinkedModeListener</code>. | |
51 */ | |
52 private class Listener : ILinkedModeListener { | |
53 | |
54 /* | |
55 * @see dwtx.jdt.internal.ui.text.link2.LinkedModeModel.ILinkedModeListener#left(dwtx.jdt.internal.ui.text.link2.LinkedModeModel, int) | |
56 */ | |
57 public void left(LinkedModeModel model, int flags) { | |
137 | 58 this.outer.left(model, flags); |
129 | 59 } |
60 | |
61 /* | |
62 * @see dwtx.jdt.internal.ui.text.link2.LinkedModeModel.ILinkedModeListener#suspend(dwtx.jdt.internal.ui.text.link2.LinkedModeModel) | |
63 */ | |
64 public void suspend(LinkedModeModel model) { | |
65 // not interested | |
66 } | |
67 | |
68 /* | |
69 * @see dwtx.jdt.internal.ui.text.link2.LinkedModeModel.ILinkedModeListener#resume(dwtx.jdt.internal.ui.text.link2.LinkedModeModel, int) | |
70 */ | |
71 public void resume(LinkedModeModel model, int flags) { | |
72 // not interested | |
73 } | |
74 | |
75 } | |
76 | |
77 /** Global map from documents to managers. */ | |
162 | 78 private static Map fgManagers_; |
79 private static Map fgManagers(){ | |
80 if( fgManagers_ is null ){ | |
81 synchronized( LinkedModeManager.classinfo ){ | |
82 if( fgManagers_ is null ){ | |
83 fgManagers_= new HashMap(); | |
84 } | |
85 } | |
86 } | |
87 return fgManagers_; | |
88 } | |
129 | 89 /** |
90 * Returns whether there exists a <code>LinkedModeManager</code> on <code>document</code>. | |
91 * | |
92 * @param document the document of interest | |
93 * @return <code>true</code> if there exists a <code>LinkedModeManager</code> on <code>document</code>, <code>false</code> otherwise | |
94 */ | |
95 public static bool hasManager(IDocument document) { | |
162 | 96 return fgManagers.get(cast(Object)document) !is null; |
129 | 97 } |
98 | |
99 /** | |
100 * Returns whether there exists a <code>LinkedModeManager</code> on any of the <code>documents</code>. | |
101 * | |
102 * @param documents the documents of interest | |
103 * @return <code>true</code> if there exists a <code>LinkedModeManager</code> on any of the <code>documents</code>, <code>false</code> otherwise | |
104 */ | |
105 public static bool hasManager(IDocument[] documents) { | |
106 for (int i= 0; i < documents.length; i++) { | |
107 if (hasManager(documents[i])) | |
108 return true; | |
109 } | |
110 return false; | |
111 } | |
112 | |
113 /** | |
114 * Returns the manager for the given documents. If <code>force</code> is | |
115 * <code>true</code>, any existing conflicting managers are canceled, otherwise, | |
116 * the method may return <code>null</code> if there are conflicts. | |
117 * | |
118 * @param documents the documents of interest | |
119 * @param force whether to kill any conflicting managers | |
120 * @return a manager able to cover the requested documents, or <code>null</code> if there is a conflict and <code>force</code> was set to <code>false</code> | |
121 */ | |
122 public static LinkedModeManager getLinkedManager(IDocument[] documents, bool force) { | |
123 if (documents is null || documents.length is 0) | |
124 return null; | |
125 | |
126 Set mgrs= new HashSet(); | |
127 LinkedModeManager mgr= null; | |
128 for (int i= 0; i < documents.length; i++) { | |
162 | 129 mgr= cast(LinkedModeManager) fgManagers.get(cast(Object)documents[i]); |
129 | 130 if (mgr !is null) |
131 mgrs.add(mgr); | |
132 } | |
133 if (mgrs.size() > 1) | |
134 if (force) { | |
135 for (Iterator it= mgrs.iterator(); it.hasNext(); ) { | |
134 | 136 LinkedModeManager m= cast(LinkedModeManager) it.next(); |
129 | 137 m.closeAllEnvironments(); |
138 } | |
139 } else { | |
140 return null; | |
141 } | |
142 | |
143 if (mgrs.size() is 0) | |
144 mgr= new LinkedModeManager(); | |
145 | |
146 for (int i= 0; i < documents.length; i++) | |
162 | 147 fgManagers.put(cast(Object)documents[i], mgr); |
129 | 148 |
149 return mgr; | |
150 } | |
151 | |
152 /** | |
153 * Cancels any linked mode manager for the specified document. | |
154 * | |
155 * @param document the document whose <code>LinkedModeManager</code> should be canceled | |
156 */ | |
157 public static void cancelManager(IDocument document) { | |
162 | 158 LinkedModeManager mgr= cast(LinkedModeManager) fgManagers.get(cast(Object)document); |
129 | 159 if (mgr !is null) |
160 mgr.closeAllEnvironments(); | |
161 } | |
162 | |
163 /** The hierarchy of environments managed by this manager. */ | |
162 | 164 private Stack fEnvironments; |
165 private Listener fListener; | |
166 | |
167 this(){ | |
168 fEnvironments= new Stack(); | |
169 fListener= new Listener(); | |
170 } | |
129 | 171 |
172 /** | |
173 * Notify the manager about a leaving model. | |
174 * | |
175 * @param model | |
176 * @param flags | |
177 */ | |
178 private void left(LinkedModeModel model, int flags) { | |
179 if (!fEnvironments.contains(model)) | |
180 return; | |
181 | |
182 while (!fEnvironments.isEmpty()) { | |
134 | 183 LinkedModeModel env= cast(LinkedModeModel) fEnvironments.pop(); |
129 | 184 if (env is model) |
185 break; | |
186 env.exit(ILinkedModeListener.NONE); | |
187 } | |
188 | |
189 if (fEnvironments.isEmpty()) { | |
190 removeManager(); | |
191 } | |
192 } | |
193 | |
194 private void closeAllEnvironments() { | |
195 while (!fEnvironments.isEmpty()) { | |
134 | 196 LinkedModeModel env= cast(LinkedModeModel) fEnvironments.pop(); |
129 | 197 env.exit(ILinkedModeListener.NONE); |
198 } | |
199 | |
200 removeManager(); | |
201 } | |
202 | |
203 private void removeManager() { | |
204 for (Iterator it= fgManagers.keySet().iterator(); it.hasNext();) { | |
134 | 205 IDocument doc= cast(IDocument) it.next(); |
162 | 206 if (fgManagers.get(cast(Object)doc) is this) |
129 | 207 it.remove(); |
208 } | |
209 } | |
210 | |
211 /** | |
212 * Tries to nest the given <code>LinkedModeModel</code> onto the top of | |
213 * the stack of environments managed by the receiver. If <code>force</code> | |
214 * is <code>true</code>, any environments on the stack that create a conflict | |
215 * are killed. | |
216 * | |
217 * @param model the model to nest | |
218 * @param force whether to force the addition of the model | |
219 * @return <code>true</code> if nesting was successful, <code>false</code> otherwise (only possible if <code>force</code> is <code>false</code> | |
220 */ | |
221 public bool nestEnvironment(LinkedModeModel model, bool force) { | |
222 Assert.isNotNull(model); | |
223 | |
224 try { | |
225 while (true) { | |
226 if (fEnvironments.isEmpty()) { | |
227 model.addLinkingListener(fListener); | |
228 fEnvironments.push(model); | |
229 return true; | |
230 } | |
231 | |
134 | 232 LinkedModeModel top= cast(LinkedModeModel) fEnvironments.peek(); |
129 | 233 if (model.canNestInto(top)) { |
234 model.addLinkingListener(fListener); | |
235 fEnvironments.push(model); | |
236 return true; | |
237 } else if (!force) { | |
238 return false; | |
239 } else { // force | |
240 fEnvironments.pop(); | |
241 top.exit(ILinkedModeListener.NONE); | |
242 // continue; | |
243 } | |
244 } | |
245 } finally { | |
246 // if we remove any, make sure the new one got inserted | |
247 Assert.isTrue(fEnvironments.size() > 0); | |
248 } | |
249 } | |
250 | |
251 /** | |
252 * Returns the <code>LinkedModeModel</code> that is on top of the stack of | |
253 * environments managed by the receiver. | |
254 * | |
255 * @return the topmost <code>LinkedModeModel</code> | |
256 */ | |
257 public LinkedModeModel getTopEnvironment() { | |
258 if (fEnvironments.isEmpty()) | |
259 return null; | |
134 | 260 return cast(LinkedModeModel) fEnvironments.peek(); |
129 | 261 } |
262 } |