KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > text > link > LinkedModeManager


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  *******************************************************************************/

11 package org.eclipse.jface.text.link;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.HashSet JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.Map JavaDoc;
17 import java.util.Set JavaDoc;
18 import java.util.Stack JavaDoc;
19
20 import org.eclipse.core.runtime.Assert;
21
22 import org.eclipse.jface.text.IDocument;
23
24
25 /**
26  * A linked mode manager ensures exclusive access of linked position infrastructures to documents. There
27  * is at most one <code>LinkedModeManager</code> installed on the same document. The <code>getManager</code>
28  * methods will return the existing instance if any of the specified documents already have an installed
29  * manager.
30  *
31  * @since 3.0
32  */

33 class LinkedModeManager {
34
35     /**
36      * Our implementation of <code>ILinkedModeListener</code>.
37      */

38     private class Listener implements ILinkedModeListener {
39
40         /*
41          * @see org.eclipse.jdt.internal.ui.text.link2.LinkedModeModel.ILinkedModeListener#left(org.eclipse.jdt.internal.ui.text.link2.LinkedModeModel, int)
42          */

43         public void left(LinkedModeModel model, int flags) {
44             LinkedModeManager.this.left(model, flags);
45         }
46
47         /*
48          * @see org.eclipse.jdt.internal.ui.text.link2.LinkedModeModel.ILinkedModeListener#suspend(org.eclipse.jdt.internal.ui.text.link2.LinkedModeModel)
49          */

50         public void suspend(LinkedModeModel model) {
51             // not interested
52
}
53
54         /*
55          * @see org.eclipse.jdt.internal.ui.text.link2.LinkedModeModel.ILinkedModeListener#resume(org.eclipse.jdt.internal.ui.text.link2.LinkedModeModel, int)
56          */

57         public void resume(LinkedModeModel model, int flags) {
58             // not interested
59
}
60
61     }
62
63     /** Global map from documents to managers. */
64     private static Map JavaDoc fgManagers= new HashMap JavaDoc();
65
66     /**
67      * Returns whether there exists a <code>LinkedModeManager</code> on <code>document</code>.
68      *
69      * @param document the document of interest
70      * @return <code>true</code> if there exists a <code>LinkedModeManager</code> on <code>document</code>, <code>false</code> otherwise
71      */

72     public static boolean hasManager(IDocument document) {
73         return fgManagers.get(document) != null;
74     }
75
76     /**
77      * Returns whether there exists a <code>LinkedModeManager</code> on any of the <code>documents</code>.
78      *
79      * @param documents the documents of interest
80      * @return <code>true</code> if there exists a <code>LinkedModeManager</code> on any of the <code>documents</code>, <code>false</code> otherwise
81      */

82     public static boolean hasManager(IDocument[] documents) {
83         for (int i= 0; i < documents.length; i++) {
84             if (hasManager(documents[i]))
85                 return true;
86         }
87         return false;
88     }
89
90     /**
91      * Returns the manager for the given documents. If <code>force</code> is
92      * <code>true</code>, any existing conflicting managers are canceled, otherwise,
93      * the method may return <code>null</code> if there are conflicts.
94      *
95      * @param documents the documents of interest
96      * @param force whether to kill any conflicting managers
97      * @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>
98      */

99     public static LinkedModeManager getLinkedManager(IDocument[] documents, boolean force) {
100         if (documents == null || documents.length == 0)
101             return null;
102
103         Set JavaDoc mgrs= new HashSet JavaDoc();
104         LinkedModeManager mgr= null;
105         for (int i= 0; i < documents.length; i++) {
106             mgr= (LinkedModeManager) fgManagers.get(documents[i]);
107             if (mgr != null)
108                 mgrs.add(mgr);
109         }
110         if (mgrs.size() > 1)
111             if (force) {
112                 for (Iterator JavaDoc it= mgrs.iterator(); it.hasNext(); ) {
113                     LinkedModeManager m= (LinkedModeManager) it.next();
114                     m.closeAllEnvironments();
115                 }
116             } else {
117                 return null;
118             }
119
120         if (mgrs.size() == 0)
121             mgr= new LinkedModeManager();
122
123         for (int i= 0; i < documents.length; i++)
124             fgManagers.put(documents[i], mgr);
125
126         return mgr;
127     }
128
129     /**
130      * Cancels any linked mode manager for the specified document.
131      *
132      * @param document the document whose <code>LinkedModeManager</code> should be canceled
133      */

134     public static void cancelManager(IDocument document) {
135         LinkedModeManager mgr= (LinkedModeManager) fgManagers.get(document);
136         if (mgr != null)
137             mgr.closeAllEnvironments();
138     }
139
140     /** The hierarchy of environments managed by this manager. */
141     private Stack JavaDoc fEnvironments= new Stack JavaDoc();
142     private Listener fListener= new Listener();
143
144     /**
145      * Notify the manager about a leaving model.
146      *
147      * @param model
148      * @param flags
149      */

150     private void left(LinkedModeModel model, int flags) {
151         if (!fEnvironments.contains(model))
152             return;
153
154         while (!fEnvironments.isEmpty()) {
155             LinkedModeModel env= (LinkedModeModel) fEnvironments.pop();
156             if (env == model)
157                 break;
158             env.exit(ILinkedModeListener.NONE);
159         }
160
161         if (fEnvironments.isEmpty()) {
162             removeManager();
163         }
164     }
165
166     private void closeAllEnvironments() {
167         while (!fEnvironments.isEmpty()) {
168             LinkedModeModel env= (LinkedModeModel) fEnvironments.pop();
169             env.exit(ILinkedModeListener.NONE);
170         }
171
172         removeManager();
173     }
174
175     private void removeManager() {
176         for (Iterator JavaDoc it= fgManagers.keySet().iterator(); it.hasNext();) {
177             IDocument doc= (IDocument) it.next();
178             if (fgManagers.get(doc) == this)
179                 it.remove();
180         }
181     }
182
183     /**
184      * Tries to nest the given <code>LinkedModeModel</code> onto the top of
185      * the stack of environments managed by the receiver. If <code>force</code>
186      * is <code>true</code>, any environments on the stack that create a conflict
187      * are killed.
188      *
189      * @param model the model to nest
190      * @param force whether to force the addition of the model
191      * @return <code>true</code> if nesting was successful, <code>false</code> otherwise (only possible if <code>force</code> is <code>false</code>
192      */

193     public boolean nestEnvironment(LinkedModeModel model, boolean force) {
194         Assert.isNotNull(model);
195
196         try {
197             while (true) {
198                 if (fEnvironments.isEmpty()) {
199                     model.addLinkingListener(fListener);
200                     fEnvironments.push(model);
201                     return true;
202                 }
203
204                 LinkedModeModel top= (LinkedModeModel) fEnvironments.peek();
205                 if (model.canNestInto(top)) {
206                     model.addLinkingListener(fListener);
207                     fEnvironments.push(model);
208                     return true;
209                 } else if (!force) {
210                     return false;
211                 } else { // force
212
fEnvironments.pop();
213                     top.exit(ILinkedModeListener.NONE);
214                     // continue;
215
}
216             }
217         } finally {
218             // if we remove any, make sure the new one got inserted
219
Assert.isTrue(fEnvironments.size() > 0);
220         }
221     }
222
223     /**
224      * Returns the <code>LinkedModeModel</code> that is on top of the stack of
225      * environments managed by the receiver.
226      *
227      * @return the topmost <code>LinkedModeModel</code>
228      */

229     public LinkedModeModel getTopEnvironment() {
230         if (fEnvironments.isEmpty())
231             return null;
232         return (LinkedModeModel) fEnvironments.peek();
233     }
234 }
235
Popular Tags