1 11 package org.eclipse.jface.text.link; 12 13 import java.util.HashMap ; 14 import java.util.HashSet ; 15 import java.util.Iterator ; 16 import java.util.Map ; 17 import java.util.Set ; 18 import java.util.Stack ; 19 20 import org.eclipse.core.runtime.Assert; 21 22 import org.eclipse.jface.text.IDocument; 23 24 25 33 class LinkedModeManager { 34 35 38 private class Listener implements ILinkedModeListener { 39 40 43 public void left(LinkedModeModel model, int flags) { 44 LinkedModeManager.this.left(model, flags); 45 } 46 47 50 public void suspend(LinkedModeModel model) { 51 } 53 54 57 public void resume(LinkedModeModel model, int flags) { 58 } 60 61 } 62 63 64 private static Map fgManagers= new HashMap (); 65 66 72 public static boolean hasManager(IDocument document) { 73 return fgManagers.get(document) != null; 74 } 75 76 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 99 public static LinkedModeManager getLinkedManager(IDocument[] documents, boolean force) { 100 if (documents == null || documents.length == 0) 101 return null; 102 103 Set mgrs= new HashSet (); 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 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 134 public static void cancelManager(IDocument document) { 135 LinkedModeManager mgr= (LinkedModeManager) fgManagers.get(document); 136 if (mgr != null) 137 mgr.closeAllEnvironments(); 138 } 139 140 141 private Stack fEnvironments= new Stack (); 142 private Listener fListener= new Listener(); 143 144 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 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 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 { fEnvironments.pop(); 213 top.exit(ILinkedModeListener.NONE); 214 } 216 } 217 } finally { 218 Assert.isTrue(fEnvironments.size() > 0); 220 } 221 } 222 223 229 public LinkedModeModel getTopEnvironment() { 230 if (fEnvironments.isEmpty()) 231 return null; 232 return (LinkedModeModel) fEnvironments.peek(); 233 } 234 } 235 | Popular Tags |