KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > editor > codetemplates > CodeTemplateDrawLayer


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.lib.editor.codetemplates;
21
22 import java.awt.Color JavaDoc;
23 import javax.swing.text.Document JavaDoc;
24 import javax.swing.text.JTextComponent JavaDoc;
25 import javax.swing.text.Position JavaDoc;
26 import org.netbeans.editor.Coloring;
27 import org.netbeans.editor.DrawContext;
28 import org.netbeans.editor.DrawLayer;
29 import org.netbeans.editor.MarkFactory;
30 import org.netbeans.lib.editor.util.swing.PositionRegion;
31
32 /**
33  * Code template drawing layer allows to render frames around the text
34  * and highlight regions of the current parameter.
35  *
36  * @author Miloslav Metelka
37  */

38 final class CodeTemplateDrawLayer extends DrawLayer.AbstractLayer {
39     
40     public static final String JavaDoc NAME = "code-template-draw-layer"; // NOI18N
41

42     public static final int VISIBILITY = 5000;
43     
44     private static final Coloring COLORING = new Coloring(null, null, new Color JavaDoc(138, 191, 236));
45     
46     private static int instanceCounter;
47     
48     private CodeTemplateParameterImpl paramImpl;
49     
50     private CodeTemplateInsertHandler handler;
51     
52     private int regionIndex;
53     
54     private Position JavaDoc regionStartPosition;
55     
56     private Position JavaDoc regionEndPosition;
57     
58     private boolean colorBackground;
59     
60     private Coloring coloring;
61     
62     private boolean textFramePropertyAssigned;
63     
64     CodeTemplateDrawLayer(CodeTemplateParameterImpl paramImpl) {
65         super(NAME + instanceCounter++); // must have distinct names
66
this.paramImpl = paramImpl;
67
68         handler = paramImpl.getHandler();
69     }
70     
71     public void init(DrawContext ctx) {
72         coloring = null;
73         JTextComponent JavaDoc c = ctx.getEditorUI().getComponent();
74         regionStartPosition = null;
75         if (c != null) {
76             if (handler.getActiveMasterImpl() == paramImpl) {
77                 colorBackground = true;
78                 int startOffset = ctx.getStartOffset();
79                 regionIndex = 0;
80                 SyncDocumentRegion syncRegion = paramImpl.getRegion();
81                 int regionCount = syncRegion.getRegionCount();
82                 while (regionIndex < regionCount) {
83                     PositionRegion region = syncRegion.getSortedRegion(regionIndex);
84                     Position JavaDoc startPos = region.getStartPosition();
85                     if (startOffset <= startPos.getOffset()) {
86                         regionStartPosition = startPos;
87                         regionEndPosition = region.getEndPosition();
88                         setNextActivityChangeOffset(regionStartPosition.getOffset());
89                         break;
90                     }
91                     regionIndex++;
92                 }
93             } else {
94                 colorBackground = false;
95             }
96         }
97     }
98
99     public boolean isActive(DrawContext ctx, MarkFactory.DrawMark mark) {
100         if (regionStartPosition != null) {
101             int regionStartOffset = regionStartPosition.getOffset();
102             int regionEndOffset = regionEndPosition.getOffset();
103             int fragmentOffset = ctx.getFragmentOffset();
104             SyncDocumentRegion syncRegion = paramImpl.getRegion();
105             if (fragmentOffset == regionStartOffset && regionStartOffset != regionEndOffset) {
106                 // Cannot set earlier as there could be other text frames
107
// located before this one
108
if (regionStartOffset == syncRegion.getFirstRegionStartOffset()) {
109                     JTextComponent JavaDoc c = ctx.getEditorUI().getComponent();
110                     c.putClientProperty(DrawLayer.TEXT_FRAME_START_POSITION_COMPONENT_PROPERTY,
111                             regionStartPosition);
112                     c.putClientProperty(DrawLayer.TEXT_FRAME_END_POSITION_COMPONENT_PROPERTY,
113                             regionEndPosition);
114                     textFramePropertyAssigned = true;
115                 }
116                 coloring = colorBackground ? COLORING : null;
117                 setNextActivityChangeOffset(regionEndOffset);
118
119             } else if (fragmentOffset == regionEndOffset) {
120                 coloring = null;
121                 regionIndex++;
122                 if (regionIndex < syncRegion.getRegionCount()) {
123                     PositionRegion region = syncRegion.getSortedRegion(regionIndex);
124                     regionStartPosition = region.getStartPosition();
125                     regionEndPosition = region.getEndPosition();
126                     setNextActivityChangeOffset(regionStartPosition.getOffset());
127                 } else {
128                     regionStartPosition = null;
129                     regionEndPosition = null;
130                 }
131                 JTextComponent JavaDoc c = ctx.getEditorUI().getComponent();
132                 resetTextFrameProperties(c);
133             }
134             return true;
135
136         } else {
137             return false;
138         }
139     }
140     
141     void resetTextFrameProperties(JTextComponent JavaDoc c) {
142         if (textFramePropertyAssigned) {
143             c.putClientProperty(DrawLayer.TEXT_FRAME_START_POSITION_COMPONENT_PROPERTY, null);
144             c.putClientProperty(DrawLayer.TEXT_FRAME_END_POSITION_COMPONENT_PROPERTY, null);
145             textFramePropertyAssigned = false;
146         }
147     }
148     
149     public void updateContext(DrawContext ctx) {
150         if (coloring != null) {
151             coloring.apply(ctx);
152         }
153     }
154
155 }
156
Popular Tags