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