KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > editor > DrawLayerList


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.editor;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Arrays JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.Set JavaDoc;
27 import java.util.logging.Level JavaDoc;
28 import java.util.logging.Logger JavaDoc;
29 import org.netbeans.modules.editor.lib.HighlightingDrawLayer;
30
31 /** Draw layer list stores multiple draw-layers sorted
32 * according to their visibility which is the integer giving the z-order
33 * in which the layers are sorted. It also provides an iterator
34 * to go through the draw layer members.
35 *
36 * @author Miloslav Metelka
37 * @version 1.00
38 */

39
40
41 class DrawLayerList {
42
43     private static final Logger JavaDoc LOG = Logger.getLogger(DrawLayerList.class.getName());
44
45     private static final DrawLayer[] EMPTY = new DrawLayer[0];
46
47     private DrawLayer[] layers = EMPTY;
48
49     private static final Set JavaDoc<String JavaDoc> ISSUED_WARNINGS = Collections.synchronizedSet(new HashSet JavaDoc<String JavaDoc>());
50     
51     private final ArrayList JavaDoc visibilityList = new ArrayList JavaDoc();
52
53     /** Add the new layer to the list depending on visibility.
54     * @param layer layer to add to the layer list
55     * @return true when new layer was added false otherwise. The layer
56     * is not added if there is already a layer with the same name.
57     * There can be a layer with the same visibility like the layer
58     * being added.
59     */

60     synchronized boolean add(DrawLayer layer, int visibility) {
61         if (indexOf(layer.getName()) >= 0) { // already layer with that name
62
return false;
63         }
64
65         if (!(layer instanceof HighlightingDrawLayer)) {
66             String JavaDoc layerId = layer.getClass().getName() + layer.getName();
67             if (!ISSUED_WARNINGS.contains(layerId)) {
68                 ISSUED_WARNINGS.add(layerId);
69                 LOG.log(Level.WARNING, "Using deprecated DrawLayer: " + layer.getName() + //NOI18N
70
" z-order: " + visibility + //NOI18N
71
" class: " + layer.getClass().getName() + " " + layer); //NOI18N
72
}
73         }
74         
75         int indAdd = layers.length;
76         for (int i = 0; i < layers.length; i++) {
77             if (((Integer JavaDoc)visibilityList.get(i)).intValue() > visibility) {
78                 indAdd = i;
79                 break;
80             }
81         }
82
83         ArrayList JavaDoc l = new ArrayList JavaDoc(Arrays.asList(layers));
84         l.add(indAdd, layer);
85         layers = new DrawLayer[layers.length + 1];
86         l.toArray(layers);
87
88         visibilityList.add(indAdd, new Integer JavaDoc(visibility));
89
90         return true;
91     }
92
93     synchronized void add(DrawLayerList l) {
94         DrawLayer[] lta = l.layers;
95         for (int i = 0; i < lta.length; i++) {
96             add(lta[i], ((Integer JavaDoc)l.visibilityList.get(i)).intValue());
97         }
98     }
99
100     /** Remove layer specified by layerName from layer list.
101     * @param layer layer to remove from the layer list
102     */

103     synchronized DrawLayer remove(String JavaDoc layerName) {
104         int ind = indexOf(layerName);
105         DrawLayer removed = null;
106
107         if (ind >= 0) {
108             removed = layers[ind];
109             ArrayList JavaDoc l = new ArrayList JavaDoc(Arrays.asList(layers));
110             l.remove(ind);
111             layers = new DrawLayer[layers.length - 1];
112             l.toArray(layers);
113
114             visibilityList.remove(ind);
115         }
116
117         return removed;
118     }
119
120     synchronized void remove(DrawLayerList l) {
121         DrawLayer[] lta = l.layers;
122         for (int i = 0; i < lta.length; i++) {
123             remove(lta[i].getName());
124         }
125     }
126
127     synchronized DrawLayer findLayer(String JavaDoc layerName) {
128         int ind = indexOf(layerName);
129         return (ind >= 0) ? layers[ind] : null;
130     }
131
132     /** Get the snapshot of the current layers. This is useful
133     * for drawing process that would otherwise have to hold
134     * a lock on editorUI so that no layer would be added or removed
135     * during the drawing.
136     */

137     synchronized DrawLayer[] currentLayers() {
138         return (DrawLayer[])layers.clone();
139     }
140
141     private int indexOf(String JavaDoc layerName) {
142         for (int i = 0; i < layers.length; i++) {
143             if (layerName.equals(layers[i].getName())) {
144                 return i;
145             }
146         }
147         return -1;
148     }
149
150     public String JavaDoc toString() {
151         switch (layers.length) {
152         case 0:
153             return "No layers"; // NOI18N
154
case 1:
155             return "Standalone " + layers[0]; // NOI18N
156
default:
157             return "Layers:\n" + EditorDebug.debugArray(layers); // NOI18N
158
}
159     }
160
161 }
162
Popular Tags