1 19 20 package org.netbeans.modules.registry; 21 22 import org.netbeans.api.registry.Context; 23 import org.netbeans.api.registry.ContextException; 24 import org.netbeans.spi.registry.BasicContext; 25 import org.openide.ErrorManager; 26 import org.openide.util.TopologicalSortException; 27 import org.openide.util.Utilities; 28 29 import java.util.*; 30 31 42 public class OrderingSupport { 43 44 private static final char SEP = '/'; 45 46 public static final OrderingSupport DEFAULT = new OrderingSupport(); 47 48 private OrderingSupport() { 49 } 50 51 55 public List getOrderedNames(Context ctx) { 56 57 String ordered = ctx.getAttribute(null, "ordered", null); 59 if (ordered == null || (!ordered.equals("true"))) { 60 } 63 64 List l = getItems(ctx); 65 Map constraints = getOrderingConstraints(ctx); 66 if (constraints.keySet().size() != 0) { 67 try { 68 l = Utilities.topologicalSort(l, constraints); 69 } catch (TopologicalSortException ex) { 70 List corrected = ex.partialSort(); 71 ErrorManager.getDefault().log(ErrorManager.WARNING, "Note: context [" + ctx + "] cannot be consistently sorted due to ordering conflicts."); ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 73 ErrorManager.getDefault().log(ErrorManager.WARNING, "Using partial sort: " + corrected); l = corrected; 75 } 76 } 77 78 String fullorder = ctx.getAttribute(null, "fullorder", null); 80 if (fullorder != null) { 81 List list = new ArrayList(); 82 StringTokenizer tok = new StringTokenizer(fullorder, ","); 85 while (tok.hasMoreTokens()) { 86 String token = tok.nextToken(); 87 if (l.remove(token)) { 88 list.add(token); 89 } 90 } 91 list.addAll(l); 92 l = list; 93 } 94 return l; 95 } 96 97 private List getItems(Context ctx) { 99 List l = new ArrayList(); 100 Iterator i = ctx.getBindingNames().iterator(); 101 while (i.hasNext()) { 102 l.add((String )i.next()); 103 } 104 i = ctx.getSubcontextNames().iterator(); 105 while (i.hasNext()) { 106 l.add(((String )i.next())+"/"); 107 } 108 return l; 109 } 110 111 private Map getOrderingConstraints(Context ctx) { 112 Map map = new HashMap(); 113 try { 114 addPositions(map, ctx); 115 } catch (ContextException ex) { 116 ErrorManager.getDefault().log(ErrorManager.EXCEPTION, ex.toString()); 117 } 118 addPartials(map, ctx); 119 return map; 120 } 121 122 123 130 private void addPartials(Map m, Context ctx) { 131 Iterator i = ctx.getAttributeNames(null).iterator(); 132 while (i.hasNext()) { 133 String attrName = (String )i.next(); 134 if (attrName.indexOf (SEP) != -1) { 135 String value = ctx.getAttribute(null, attrName, ""); 137 if (value.equals("true")) { 138 int idx = attrName.indexOf(SEP); 139 String a = attrName.substring(0, idx); 140 String b = attrName.substring(idx + 1); 141 142 idx = a.lastIndexOf('.'); 146 if (idx != -1) { 147 a = a.substring(0, idx); 149 } else { 150 a = a + "/"; 152 } 153 idx = b.lastIndexOf('.'); 154 if (idx != -1) { 155 b = b.substring(0, idx); 157 } else { 158 b = b + "/"; 160 } 161 162 List l = (List)m.get(a); 163 if (l == null) { 164 m.put(a, l = new LinkedList()); 165 } 166 l.add(b); 167 168 } 169 } 170 } 171 } 172 173 180 private void addPositions(Map m, Context ctx) throws ContextException { 181 Map map = new HashMap(); 182 BasicContext basicCtx = ApiContextFactory.DEFAULT.getBasicContext(ctx); 183 Iterator i = ctx.getBindingNames().iterator(); 184 while (i.hasNext()) { 185 String bindingName = (String )i.next(); 186 String position = ctx.getAttribute(bindingName, "position", null); 187 if (position != null) { 188 try { 189 Float f = new Float (position); 190 String bnd = (String )map.put(f, bindingName); 191 if (bnd != null) { 192 ErrorManager.getDefault().log(ErrorManager.WARNING, "Ordering Error: Two items in context ["+ctx+ 193 "] has the same position ["+position+"]: "+bnd+" "+bindingName); 194 } 195 } catch (NumberFormatException ex) { 196 } 198 } else { 199 } 202 } 203 i = ctx.getSubcontextNames().iterator(); 204 while (i.hasNext()) { 205 String subcontextName = (String )i.next(); 206 Context subCtx = ctx.getSubcontext(subcontextName); 207 if (subCtx == null) { 208 continue; 209 } 210 String position = subCtx.getAttribute(null, "position", null); 211 if (position != null) { 212 try { 213 Float f = new Float (position); 214 String sub = (String )map.put(f, subcontextName+"/"); 215 if (sub != null) { 216 ErrorManager.getDefault().log(ErrorManager.WARNING, "Ordering Error: Two items in context ["+ctx+ 217 "] has the same position ["+position+"]: "+sub+" "+subcontextName); 218 } 219 } catch (NumberFormatException ex) { 220 } 222 } else { 223 } 226 } 227 List l = new ArrayList(map.keySet()); 228 Collections.sort(l); 229 for (int k=0; k<l.size(); k++) { 230 String itemA = (String )map.get(l.get(k)); 231 List list = (List)m.get(itemA); 232 if (list == null) { 233 m.put(itemA, list = new LinkedList()); 234 } 235 for (int j=k+1; j<l.size(); j++) { 236 String itemB = (String )map.get(l.get(j)); 237 list.add(itemB); 238 } 239 } 240 } 241 242 243 } 244 | Popular Tags |