KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > spi > editor > highlighting > HighlightsLayer


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.spi.editor.highlighting;
21
22 import java.util.Collection JavaDoc;
23 import java.util.List JavaDoc;
24 import javax.swing.text.Document JavaDoc;
25 import javax.swing.text.JTextComponent JavaDoc;
26 import org.netbeans.modules.editor.lib2.highlighting.HighlightingSpiPackageAccessor;
27 import org.netbeans.modules.editor.lib2.highlighting.HighlightsLayerAccessor;
28 import org.openide.util.TopologicalSortException;
29
30 /**
31  * The highlight layer defines a set of highlights used for rendering a document.
32  *
33  * <p>There can be multiple highlight layers participating in rendering one
34  * document. Each highlight layer provides its z-order, which defines an order
35  * in which the layers are used. The higher the z-order the more 'visible' the
36  * layer is. In other word highlights provided by a layer with higher
37  * z-order can hide highlights provided by a layer with lower z-order
38  * for the same part of a document.
39  *
40  * <p>The highlights provided by a <code>HighlightsLayer</code> can
41  * define any attributes affecting text rendering including attributes that affect the
42  * size of the view containing the rendered text. The layers that use those attributes
43  * have to participate in the view layout and therefore need to be treated in a
44  * special way. The <code>isFixedSize</code> attribute can be <code>true</code>
45  * if and only if layer's highlights will never contain attributes that influence the
46  * layout of a view (e.g. font size).
47  *
48  * <div class="nonnormative">
49  * <p>For example attributes changing foreground or background color such as
50  * {@link javax.swing.text.StyleConstants#Foreground}
51  * or {@link javax.swing.text.StyleConstants#Background}
52  * do not affect metrics while the following attributes do affect it
53  * {@link javax.swing.text.StyleConstants#FontFamily},
54  * {@link javax.swing.text.StyleConstants#FontSize},
55  * {@link javax.swing.text.StyleConstants#Bold},
56  * {@link javax.swing.text.StyleConstants#Italic}.
57  * </div>
58  *
59  * @author Miloslav Metelka
60  * @author Vita Stejskal
61  * @version 1.00
62  */

63
64 public final class HighlightsLayer {
65
66     /**
67      * Creates a new <code>HighlightsLayer</code> with contents defined by
68      * <code>HighlightsContainer</code>.
69      *
70      * @param layerTypeId The unique identifier of the new layer. This id is
71      * used for identifying this layer among other layers that may be created
72      * for the same <code>Document</code> and it is used for example for
73      * the purpose of z-order.
74      * @param zOrder The layer's z-order.
75      * @param fixedSize Whether this layer defines any attributes
76      * affecting text rendering; including attributes that affect the size
77      * of the view containing the rendered text. Pass in <code>true</code>
78      * <b>if and only if</b> the layer does not change metrics of rendered
79      * text.
80      * @param container The <code>HighlightsContainer</code> that should be used
81      * as a contents of this layer.
82      *
83      * @see org.netbeans.spi.editor.highlighting.ZOrder
84      * @see org.netbeans.spi.editor.highlighting.HighlightsContainer
85      */

86     public static HighlightsLayer create(
87         String JavaDoc layerTypeId,
88         ZOrder zOrder,
89         boolean fixedSize,
90         HighlightsContainer container
91     ) {
92         return new HighlightsLayer(layerTypeId, zOrder, fixedSize, container);
93     }
94     
95     private final String JavaDoc layerTypeId;
96     private final ZOrder zOrder;
97     private final boolean fixedSize;
98
99     private HighlightsContainer container;
100
101     private HighlightsLayerAccessor accessor;
102     
103     /**
104      * Creates a new <code>HighlightsLayer</code> as a proxy to another
105      * <code>HighlightsContainer</code>.
106      *
107      * @param layerTypeId The unique identifier of the new layer. This id is
108      * used for identifying this layer among other layers that may be created
109      * for the same <code>Document</code> and it is used for example for
110      * the purpose of z-order.
111      * @param zOrder The layer's z-order.
112      * @param fixedSize Whether this layer defines any attributes
113      * affecting text rendering including attributes that affect the size
114      * of the view containing the rendered text.
115      * @param container <code>HighlightsContainer</code> that should be used
116      * as a contents of this layer.
117      *
118      * @see org.netbeans.spi.editor.highlighting.ZOrder
119      */

120     private HighlightsLayer(
121         String JavaDoc layerTypeId,
122         ZOrder zOrder,
123         boolean fixedSize,
124         HighlightsContainer container
125     ) {
126         assert layerTypeId != null : "The layerId parameter must not be null.";
127         assert zOrder != null : "The zOrder parameter must not be null.";
128         
129         this.layerTypeId = layerTypeId;
130         this.zOrder = zOrder;
131         this.fixedSize = fixedSize;
132         this.container = container;
133     }
134     
135     /**
136      * Gets the unique identifier of this layer.
137      *
138      * @return The layer id.
139      * @see org.netbeans.spi.editor.highlighting.ZOrder
140      */

141     /* package */ String JavaDoc getLayerTypeId() {
142         return layerTypeId;
143     }
144     
145     /**
146      * Gets the z-order of this layer. The z-order defines a relative position
147      * of this layer among other layers participating in drawing the same
148      * document. The higher the z-order the higher this layer will be placed
149      * and the 'more visible' its highlights will be.
150      *
151      * @return The layer's z-order.
152      */

153     /* package */ ZOrder getZOrder() {
154         return zOrder;
155     }
156     
157     /**
158      * Specifies whether highlights from this layer affect size of the rendered text.
159      *
160      * <p>If this layer provides highlights that change the size
161      * of the view containing the rendered text (e.g. the font size) the infrastructure
162      * needs to know about it and use the layer for laying out the views. Such
163      * a layer should return <code>true</code> from this method.
164      *
165      * <p>Layers at the top (according to z-order) with highlights
166      * that do not change metrics can be skipped during the views creation phase
167      * and used later when drawing is done.
168      *
169      * @return <code>false</code> if the layer does not provide any highlights
170      * that would alter the size of the text; otherwise <code>true</code>.
171      */

172     /* package */ boolean isFixedSize() {
173         return fixedSize;
174     }
175     
176     /* package */ HighlightsContainer getContainer() {
177         return container;
178     }
179     
180     static {
181         HighlightingSpiPackageAccessor.register(new PackageAccessor());
182     }
183
184     private static final class PackageAccessor extends HighlightingSpiPackageAccessor {
185
186         /** Creates a new instance of PackageAccessor */
187         public PackageAccessor() {
188         }
189
190         public HighlightsLayerFactory.Context createFactoryContext(Document JavaDoc document, JTextComponent JavaDoc component) {
191             return new HighlightsLayerFactory.Context(document, component);
192         }
193
194         public List JavaDoc<? extends HighlightsLayer> sort(Collection JavaDoc<? extends HighlightsLayer> layers) throws TopologicalSortException {
195             return ZOrder.sort(layers);
196         }
197         
198         public HighlightsLayerAccessor getHighlightsLayerAccessor(final HighlightsLayer layer) {
199             if (layer.accessor == null) {
200                 layer.accessor = new HighlightsLayerAccessor() {
201                     public String JavaDoc getLayerTypeId() { return layer.getLayerTypeId(); }
202                     public boolean isFixedSize() { return layer.isFixedSize(); }
203                     public ZOrder getZOrder() { return layer.getZOrder(); }
204                     public HighlightsContainer getContainer() { return layer.getContainer(); }
205                 };
206             }
207             
208             return layer.accessor;
209         }
210     } // End of PackageAccessor class
211
}
212
Popular Tags