Mercurial > projects > dwt-addons
annotate dwtx/jface/text/link/LinkedModeManager.d @ 153:f70d9508c95c
Fix java Collection imports
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Mon, 25 Aug 2008 00:27:31 +0200 |
parents | 25170b5a8951 |
children | 1a5b8f8129df |
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. */ | |
78 private static Map fgManagers= new HashMap(); | |
79 | |
80 /** | |
81 * Returns whether there exists a <code>LinkedModeManager</code> on <code>document</code>. | |
82 * | |
83 * @param document the document of interest | |
84 * @return <code>true</code> if there exists a <code>LinkedModeManager</code> on <code>document</code>, <code>false</code> otherwise | |
85 */ | |
86 public static bool hasManager(IDocument document) { | |
87 return fgManagers.get(document) !is null; | |
88 } | |
89 | |
90 /** | |
91 * Returns whether there exists a <code>LinkedModeManager</code> on any of the <code>documents</code>. | |
92 * | |
93 * @param documents the documents of interest | |
94 * @return <code>true</code> if there exists a <code>LinkedModeManager</code> on any of the <code>documents</code>, <code>false</code> otherwise | |
95 */ | |
96 public static bool hasManager(IDocument[] documents) { | |
97 for (int i= 0; i < documents.length; i++) { | |
98 if (hasManager(documents[i])) | |
99 return true; | |
100 } | |
101 return false; | |
102 } | |
103 | |
104 /** | |
105 * Returns the manager for the given documents. If <code>force</code> is | |
106 * <code>true</code>, any existing conflicting managers are canceled, otherwise, | |
107 * the method may return <code>null</code> if there are conflicts. | |
108 * | |
109 * @param documents the documents of interest | |
110 * @param force whether to kill any conflicting managers | |
111 * @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> | |
112 */ | |
113 public static LinkedModeManager getLinkedManager(IDocument[] documents, bool force) { | |
114 if (documents is null || documents.length is 0) | |
115 return null; | |
116 | |
117 Set mgrs= new HashSet(); | |
118 LinkedModeManager mgr= null; | |
119 for (int i= 0; i < documents.length; i++) { | |
134 | 120 mgr= cast(LinkedModeManager) fgManagers.get(documents[i]); |
129 | 121 if (mgr !is null) |
122 mgrs.add(mgr); | |
123 } | |
124 if (mgrs.size() > 1) | |
125 if (force) { | |
126 for (Iterator it= mgrs.iterator(); it.hasNext(); ) { | |
134 | 127 LinkedModeManager m= cast(LinkedModeManager) it.next(); |
129 | 128 m.closeAllEnvironments(); |
129 } | |
130 } else { | |
131 return null; | |
132 } | |
133 | |
134 if (mgrs.size() is 0) | |
135 mgr= new LinkedModeManager(); | |
136 | |
137 for (int i= 0; i < documents.length; i++) | |
138 fgManagers.put(documents[i], mgr); | |
139 | |
140 return mgr; | |
141 } | |
142 | |
143 /** | |
144 * Cancels any linked mode manager for the specified document. | |
145 * | |
146 * @param document the document whose <code>LinkedModeManager</code> should be canceled | |
147 */ | |
148 public static void cancelManager(IDocument document) { | |
134 | 149 LinkedModeManager mgr= cast(LinkedModeManager) fgManagers.get(document); |
129 | 150 if (mgr !is null) |
151 mgr.closeAllEnvironments(); | |
152 } | |
153 | |
154 /** The hierarchy of environments managed by this manager. */ | |
155 private Stack fEnvironments= new Stack(); | |
156 private Listener fListener= new Listener(); | |
157 | |
158 /** | |
159 * Notify the manager about a leaving model. | |
160 * | |
161 * @param model | |
162 * @param flags | |
163 */ | |
164 private void left(LinkedModeModel model, int flags) { | |
165 if (!fEnvironments.contains(model)) | |
166 return; | |
167 | |
168 while (!fEnvironments.isEmpty()) { | |
134 | 169 LinkedModeModel env= cast(LinkedModeModel) fEnvironments.pop(); |
129 | 170 if (env is model) |
171 break; | |
172 env.exit(ILinkedModeListener.NONE); | |
173 } | |
174 | |
175 if (fEnvironments.isEmpty()) { | |
176 removeManager(); | |
177 } | |
178 } | |
179 | |
180 private void closeAllEnvironments() { | |
181 while (!fEnvironments.isEmpty()) { | |
134 | 182 LinkedModeModel env= cast(LinkedModeModel) fEnvironments.pop(); |
129 | 183 env.exit(ILinkedModeListener.NONE); |
184 } | |
185 | |
186 removeManager(); | |
187 } | |
188 | |
189 private void removeManager() { | |
190 for (Iterator it= fgManagers.keySet().iterator(); it.hasNext();) { | |
134 | 191 IDocument doc= cast(IDocument) it.next(); |
129 | 192 if (fgManagers.get(doc) is this) |
193 it.remove(); | |
194 } | |
195 } | |
196 | |
197 /** | |
198 * Tries to nest the given <code>LinkedModeModel</code> onto the top of | |
199 * the stack of environments managed by the receiver. If <code>force</code> | |
200 * is <code>true</code>, any environments on the stack that create a conflict | |
201 * are killed. | |
202 * | |
203 * @param model the model to nest | |
204 * @param force whether to force the addition of the model | |
205 * @return <code>true</code> if nesting was successful, <code>false</code> otherwise (only possible if <code>force</code> is <code>false</code> | |
206 */ | |
207 public bool nestEnvironment(LinkedModeModel model, bool force) { | |
208 Assert.isNotNull(model); | |
209 | |
210 try { | |
211 while (true) { | |
212 if (fEnvironments.isEmpty()) { | |
213 model.addLinkingListener(fListener); | |
214 fEnvironments.push(model); | |
215 return true; | |
216 } | |
217 | |
134 | 218 LinkedModeModel top= cast(LinkedModeModel) fEnvironments.peek(); |
129 | 219 if (model.canNestInto(top)) { |
220 model.addLinkingListener(fListener); | |
221 fEnvironments.push(model); | |
222 return true; | |
223 } else if (!force) { | |
224 return false; | |
225 } else { // force | |
226 fEnvironments.pop(); | |
227 top.exit(ILinkedModeListener.NONE); | |
228 // continue; | |
229 } | |
230 } | |
231 } finally { | |
232 // if we remove any, make sure the new one got inserted | |
233 Assert.isTrue(fEnvironments.size() > 0); | |
234 } | |
235 } | |
236 | |
237 /** | |
238 * Returns the <code>LinkedModeModel</code> that is on top of the stack of | |
239 * environments managed by the receiver. | |
240 * | |
241 * @return the topmost <code>LinkedModeModel</code> | |
242 */ | |
243 public LinkedModeModel getTopEnvironment() { | |
244 if (fEnvironments.isEmpty()) | |
245 return null; | |
134 | 246 return cast(LinkedModeModel) fEnvironments.peek(); |
129 | 247 } |
248 } |