1 19 20 package org.netbeans.spi.editor.highlighting; 21 22 import java.util.ArrayList ; 23 import java.util.Arrays ; 24 import java.util.Collection ; 25 import java.util.Collections ; 26 import java.util.HashMap ; 27 import java.util.HashSet ; 28 import java.util.List ; 29 import java.util.logging.Logger ; 30 import org.openide.util.TopologicalSortException; 31 import org.openide.util.Utilities; 32 33 72 public final class ZOrder { 73 74 private static final Logger LOG = Logger.getLogger(ZOrder.class.getName()); 75 76 private static final Collection <String > EMPTY = new HashSet <String >(); 77 78 private static final String TOP_RACK_MARKER = "org-netbeans-spi-editor-highlighting-ZOrder-TOP-RACK"; private static final String SHOW_OFF_RACK_MARKER = "org-netbeans-spi-editor-highlighting-ZOrder-SHOW-OFF-RACK"; private static final String DEFAULT_RACK_MARKER = "org-netbeans-spi-editor-highlighting-ZOrder-DEFAULT-RACK"; private static final String CARET_RACK_MARKER = "org-netbeans-spi-editor-highlighting-ZOrder-CARET-RACK"; private static final String SYNTAX_RACK_MARKER = "org-netbeans-spi-editor-highlighting-ZOrder-SYNTAX-RACK"; 84 88 public static final ZOrder TOP_RACK = ZOrder.above(TOP_RACK_MARKER); 89 90 95 public static final ZOrder SHOW_OFF_RACK = new ZOrder( 96 Collections.singleton(TOP_RACK_MARKER), Collections.singleton(SHOW_OFF_RACK_MARKER)); 97 98 101 public static final ZOrder DEFAULT_RACK = new ZOrder( 102 Collections.singleton(SHOW_OFF_RACK_MARKER), Collections.singleton(DEFAULT_RACK_MARKER)); 103 104 public static final ZOrder CARET_RACK = new ZOrder( 105 Collections.singleton(DEFAULT_RACK_MARKER), Collections.singleton(CARET_RACK_MARKER)); 106 107 112 public static final ZOrder SYNTAX_RACK = new ZOrder( 113 Collections.singleton(CARET_RACK_MARKER), Collections.singleton(SYNTAX_RACK_MARKER)); 114 115 119 public static final ZOrder BOTTOM_RACK = ZOrder.below(SYNTAX_RACK_MARKER); 120 121 130 public static ZOrder above(String ... layerIds) { 131 return new ZOrder(null, Arrays.asList(layerIds)); 132 } 133 134 143 public static ZOrder below(String ... layerIds) { 144 return new ZOrder(Arrays.asList(layerIds), null); 145 } 146 147 158 static HighlightsLayer[] sort(HighlightsLayer[] layers) throws TopologicalSortException { 159 List <? extends HighlightsLayer> list = sort(Arrays.asList(layers)); 160 return list.toArray(new HighlightsLayer [list.size()]); 161 } 162 163 176 static List <? extends HighlightsLayer> sort(Collection <? extends HighlightsLayer> layers) throws TopologicalSortException { 177 HashMap <String , HighlightsLayer> id2layer = new HashMap <String , HighlightsLayer>(); 178 HashSet <String > vertices = new HashSet <String >(); 179 HashMap <String , List <String >> edges = new HashMap <String , List <String >>(); 180 181 vertices.add(TOP_RACK_MARKER); 182 vertices.add(SHOW_OFF_RACK_MARKER); 183 vertices.add(DEFAULT_RACK_MARKER); 184 vertices.add(CARET_RACK_MARKER); 185 vertices.add(SYNTAX_RACK_MARKER); 186 187 edges.put(SYNTAX_RACK_MARKER, new ArrayList <String >(Collections.singleton(CARET_RACK_MARKER))); 188 edges.put(CARET_RACK_MARKER, new ArrayList <String >(Collections.singleton(DEFAULT_RACK_MARKER))); 189 edges.put(DEFAULT_RACK_MARKER, new ArrayList <String >(Collections.singleton(SHOW_OFF_RACK_MARKER))); 190 edges.put(SHOW_OFF_RACK_MARKER, new ArrayList <String >(Collections.singleton(TOP_RACK_MARKER))); 191 192 for (HighlightsLayer layer : layers) { 193 id2layer.put(layer.getLayerTypeId(), layer); 194 195 for (String belowLayerId : layer.getZOrder().layersBelow) { 197 vertices.add(belowLayerId); 198 List <String > verticeEdges = edges.get(belowLayerId); 199 if (verticeEdges == null) { 200 verticeEdges = new ArrayList <String >(); 201 edges.put(belowLayerId, verticeEdges); 202 } 203 verticeEdges.add(layer.getLayerTypeId()); 204 LOG.finest(belowLayerId + " < " + layer.getLayerTypeId()); 205 } 206 207 vertices.add(layer.getLayerTypeId()); 209 List <String > verticeEdges = edges.get(layer.getLayerTypeId()); 210 if (verticeEdges == null) { 211 verticeEdges = new ArrayList <String >(); 212 edges.put(layer.getLayerTypeId(), verticeEdges); 213 } 214 for (String aboveLayerId : layer.getZOrder().layersAbove) { 215 verticeEdges.add(aboveLayerId); 216 LOG.finest(layer.getLayerTypeId() + " < " + aboveLayerId); 217 } 218 } 219 220 List <String > sortedLayerIds = Utilities.topologicalSort(vertices, edges); 222 List <HighlightsLayer> sortedLayers = new ArrayList <HighlightsLayer>(); 223 224 LOG.finest("Sorted layer Ids: "); 226 for (String layerId : sortedLayerIds) { 227 LOG.finest(" " + layerId); 228 HighlightsLayer layer = id2layer.get(layerId); 229 if (layer != null) { 230 sortedLayers.add(layer); 231 } 232 } 233 LOG.finest("End of Sorted layer Ids: -----------------------"); 234 235 return sortedLayers; 236 } 237 238 final Collection <String > layersAbove; 239 final Collection <String > layersBelow; 240 241 242 private ZOrder(Collection <String > aboveLayers, Collection <String > belowLayers) { 243 this.layersAbove = aboveLayers == null ? EMPTY : aboveLayers; 244 this.layersBelow = belowLayers == null ? EMPTY : belowLayers; 245 } 246 247 257 public ZOrder aboveLayers(String ... layerIds) { 258 HashSet <String > newLayersAbove = new HashSet <String >(layersAbove); 259 HashSet <String > newLayersBelow = new HashSet <String >(layersBelow.size() + layerIds.length); 260 newLayersBelow.addAll(layersBelow); 261 newLayersBelow.addAll(Arrays.asList(layerIds)); 262 return new ZOrder(newLayersAbove, newLayersBelow); 263 } 264 265 275 public ZOrder belowLayers(String ... layerIds) { 276 HashSet <String > newLayersBelow = new HashSet <String >(layersBelow); 277 HashSet <String > newLayersAbove = new HashSet <String >(layersAbove.size() + layerIds.length); 278 newLayersAbove.addAll(layersAbove); 279 newLayersAbove.addAll(Arrays.asList(layerIds)); 280 return new ZOrder(newLayersAbove, newLayersBelow); 281 } 282 } 283 | Popular Tags |