comparison org.eclipse.text/src/org/eclipse/jface/text/link/LinkedModeManager.d @ 12:bc29606a740c

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