KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > impl > highlighting > GuardedBlocksHighlighting


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.modules.editor.impl.highlighting;
21
22 import java.util.Collection JavaDoc;
23 import java.util.ConcurrentModificationException JavaDoc;
24 import java.util.NoSuchElementException JavaDoc;
25 import java.util.logging.Level JavaDoc;
26 import java.util.logging.Logger JavaDoc;
27 import javax.swing.text.AttributeSet JavaDoc;
28 import javax.swing.text.Document JavaDoc;
29 import javax.swing.text.SimpleAttributeSet JavaDoc;
30 import org.netbeans.api.editor.mimelookup.MimeLookup;
31 import org.netbeans.api.editor.mimelookup.MimePath;
32 import org.netbeans.api.editor.settings.AttributesUtilities;
33 import org.netbeans.api.editor.settings.FontColorNames;
34 import org.netbeans.api.editor.settings.FontColorSettings;
35 import org.netbeans.editor.GuardedDocument;
36 import org.netbeans.editor.MarkBlock;
37 import org.netbeans.editor.MarkBlockChain;
38 import org.netbeans.spi.editor.highlighting.HighlightsSequence;
39 import org.netbeans.spi.editor.highlighting.support.AbstractHighlightsContainer;
40 import org.openide.util.Lookup;
41 import org.openide.util.LookupEvent;
42 import org.openide.util.LookupListener;
43
44 /**
45  *
46  * @author Vita Stejskal
47  */

48 public final class GuardedBlocksHighlighting extends AbstractHighlightsContainer implements LookupListener {
49     
50     private static final Logger JavaDoc LOG = Logger.getLogger(GuardedBlocksHighlighting.class.getName());
51     public static final String JavaDoc LAYER_TYPE_ID = "org.netbeans.modules.editor.oldlibbridge.GuardedBlocksHighlighting"; //NOI18N
52

53     private final Document JavaDoc document;
54     private final MimePath mimePath;
55     private final Lookup.Result<FontColorSettings> lookupResult;
56
57     private long version = 0;
58     private AttributeSet JavaDoc attribs = null;
59     
60     /** Creates a new instance of NonLexerSytaxHighlighting */
61     public GuardedBlocksHighlighting(Document JavaDoc document, String JavaDoc mimeType) {
62         this.document = document;
63         this.mimePath = MimePath.parse(mimeType);
64         this.lookupResult = MimeLookup.getLookup(mimePath).lookup(new Lookup.Template<FontColorSettings>(FontColorSettings.class));
65         this.lookupResult.addLookupListener(this);
66     }
67
68     public HighlightsSequence getHighlights(int startOffset, int endOffset) {
69         synchronized (this) {
70             if (document instanceof GuardedDocument) {
71                 MarkBlockChain guardedBlocks = ((GuardedDocument) document).getGuardedBlockChain();
72                 return new HSImpl(version, guardedBlocks, startOffset, endOffset);
73             } else {
74                 return HighlightsSequence.EMPTY;
75             }
76         }
77     }
78     
79     // ----------------------------------------------------------------------
80
// LookupListener implementation
81
// ----------------------------------------------------------------------
82

83     public void resultChanged(LookupEvent ev) {
84         synchronized (this) {
85             attribs = null;
86             version++;
87         }
88         
89         document.render(new Runnable JavaDoc() {
90             public void run() {
91                 fireHighlightsChange(0, Integer.MAX_VALUE);
92             }
93         });
94     }
95
96     // ----------------------------------------------------------------------
97
// Private implementation
98
// ----------------------------------------------------------------------
99

100     private final class HSImpl implements HighlightsSequence {
101         
102         private final long version;
103         private final MarkBlockChain guardedBlocks;
104         private final int startOffset;
105         private final int endOffset;
106
107         private boolean init = false;
108         private MarkBlock block;
109         
110         public HSImpl(long version, MarkBlockChain guardedBlocks, int startOffset, int endOffset) {
111             this.version = version;
112             this.guardedBlocks = guardedBlocks;
113             this.startOffset = startOffset;
114             this.endOffset = endOffset;
115         }
116         
117         public boolean moveNext() {
118             if (!init) {
119                 init = true;
120
121                 block = guardedBlocks.getChain();
122                 
123                 while(null != block) {
124                     if (block.getEndOffset() > startOffset) {
125                         break;
126                     }
127
128                     if (LOG.isLoggable(Level.FINE)) {
129                         LOG.fine("Skipping block: " + block + //NOI18N
130
", blockStart = " + block.getStartOffset() + //NOI18N
131
", blockEnd = " + block.getEndOffset() + //NOI18N
132
", startOffset = " + startOffset + //NOI18N
133
", endOffset = " + endOffset //NOI18N
134
);
135                     }
136                     
137                     block = block.getNext();
138                 }
139             } else if (block != null) {
140                 block = block.getNext();
141             }
142             
143             if (block != null && block.getStartOffset() > endOffset) {
144                 block = null;
145             }
146             
147             if (LOG.isLoggable(Level.FINE)) {
148                 if (block != null) {
149                     LOG.fine("Next block: " + block + //NOI18N
150
", blockStart = " + block.getStartOffset() + //NOI18N
151
", blockEnd = " + block.getEndOffset() + //NOI18N
152
", startOffset = " + startOffset + //NOI18N
153
", endOffset = " + endOffset //NOI18N
154
);
155                 } else {
156                     LOG.fine("Next block: null"); //NOI18N
157
}
158             }
159             
160             return block != null;
161         }
162
163         public int getStartOffset() {
164             synchronized (GuardedBlocksHighlighting.this) {
165                 checkVersion();
166                 
167                 if (!init) {
168                     throw new NoSuchElementException JavaDoc("Call moveNext() first."); //NOI18N
169
} else if (block == null) {
170                     throw new NoSuchElementException JavaDoc();
171                 }
172
173                 return Math.max(block.getStartOffset(), startOffset);
174             }
175         }
176
177         public int getEndOffset() {
178             synchronized (GuardedBlocksHighlighting.this) {
179                 checkVersion();
180                 
181                 if (!init) {
182                     throw new NoSuchElementException JavaDoc("Call moveNext() first."); //NOI18N
183
} else if (block == null) {
184                     throw new NoSuchElementException JavaDoc();
185                 }
186
187                 return Math.min(block.getEndOffset(), endOffset);
188             }
189         }
190
191         public AttributeSet JavaDoc getAttributes() {
192             synchronized (GuardedBlocksHighlighting.this) {
193                 checkVersion();
194                 
195                 if (!init) {
196                     throw new NoSuchElementException JavaDoc("Call moveNext() first."); //NOI18N
197
} else if (block == null) {
198                     throw new NoSuchElementException JavaDoc();
199                 }
200
201                 if (attribs == null) {
202                     Collection JavaDoc<? extends FontColorSettings> allFcs = lookupResult.allInstances();
203                     if (!allFcs.isEmpty()) {
204                         FontColorSettings fcs = allFcs.iterator().next();
205                         attribs = fcs.getFontColors(FontColorNames.GUARDED_COLORING);
206                     }
207
208                     if (attribs == null) {
209                         attribs = SimpleAttributeSet.EMPTY;
210                     } else {
211                         attribs = AttributesUtilities.createImmutable(
212                             attribs,
213                             AttributesUtilities.createImmutable(ATTR_EXTENDS_EOL, Boolean.TRUE)
214                         );
215                     }
216                 }
217                 
218                 return attribs;
219             }
220         }
221         
222         private void checkVersion() {
223             if (this.version != GuardedBlocksHighlighting.this.version) {
224                 throw new ConcurrentModificationException JavaDoc();
225             }
226         }
227     } // End of HSImpl class
228

229 }
230
Popular Tags