KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > jedit > textarea > ExtensionManager


1 /*
2  * ExtensionManager.java - Handles 'layers'
3  * :tabSize=8:indentSize=8:noTabs=false:
4  * :folding=explicit:collapseFolds=1:
5  *
6  * Copyright (C) 2002, 2003 Slava Pestov
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21  */

22
23 package org.gjt.sp.jedit.textarea;
24
25 import java.awt.Graphics2D JavaDoc;
26 import java.util.*;
27 import org.gjt.sp.util.Log;
28
29 /**
30  * Manage the extensions for the gutter and the textarea.
31  *
32  * @author Slava Pestov
33  * @version $Id: ExtensionManager.java 7156 2006-10-02 21:33:17Z kpouer $
34  */

35 class ExtensionManager
36 {
37     //{{{ addExtension() method
38
/**
39      * Add an extension.
40      * See {@link Gutter} and {@link TextAreaPainter} to know the layers
41      *
42      * @param layer the layer. It could be defined in Gutter or TextAreaPainter
43      * @param ext the extension to add
44      */

45     void addExtension(int layer, TextAreaExtension ext)
46     {
47         Entry entry = new Entry(layer,ext);
48
49         int i = 0;
50         for (Entry extension : extensions) {
51             int _layer = extension.layer;
52             if (layer < _layer) {
53                 extensions.add(i, entry);
54                 return;
55             }
56             i++;
57         }
58
59         extensions.add(entry);
60     } //}}}
61

62     //{{{ removeExtension() method
63
void removeExtension(TextAreaExtension ext)
64     {
65         Iterator<Entry> iter = extensions.iterator();
66         while(iter.hasNext())
67         {
68             if(iter.next().ext == ext)
69             {
70                 iter.remove();
71                 return;
72             }
73         }
74     } //}}}
75

76     //{{{ getExtensions() method
77
TextAreaExtension[] getExtensions()
78     {
79         TextAreaExtension[] retVal = new TextAreaExtension[
80             extensions.size()];
81         Iterator<Entry> iter = extensions.iterator();
82         int i = 0;
83         while(iter.hasNext())
84             retVal[i++] = iter.next().ext;
85
86         return retVal;
87     } //}}}
88

89     //{{{ paintScreenLineRange() method
90
void paintScreenLineRange(TextArea textArea, Graphics2D JavaDoc gfx,
91         int firstLine, int lastLine, int y, int lineHeight)
92     {
93         try
94         {
95             int[] physicalLines = new int[lastLine - firstLine + 1];
96             int[] start = new int[physicalLines.length];
97             int[] end = new int[physicalLines.length];
98
99             for(int i = 0; i < physicalLines.length; i++)
100             {
101                 int screenLine = i + firstLine;
102                 ChunkCache.LineInfo lineInfo = textArea
103                     .chunkCache.getLineInfo(screenLine);
104
105                 if(lineInfo.physicalLine == -1)
106                     physicalLines[i] = -1;
107                 else
108                 {
109                     physicalLines[i] = lineInfo.physicalLine;
110                     start[i] = textArea.getScreenLineStartOffset(screenLine);
111                     end[i] = textArea.getScreenLineEndOffset(screenLine);
112                 }
113             }
114
115             paintScreenLineRange(gfx,firstLine,lastLine,physicalLines,
116                 start,end,y,lineHeight);
117         }
118         catch(Exception JavaDoc e)
119         {
120             Log.log(Log.ERROR,this,"Error repainting line"
121                 + " range {" + firstLine + ','
122                     + lastLine + "}:");
123             Log.log(Log.ERROR,this,e);
124         }
125     } //}}}
126

127     //{{{ getToolTipText() method
128
String JavaDoc getToolTipText(int x, int y)
129     {
130         for(int i = 0; i < extensions.size(); i++)
131         {
132             TextAreaExtension ext = extensions.get(i).ext;
133             String JavaDoc toolTip = ext.getToolTipText(x,y);
134             if(toolTip != null)
135                 return toolTip;
136         }
137
138         return null;
139     } //}}}
140

141     //{{{ Private members
142
private final List<Entry> extensions = new LinkedList<Entry>();
143
144     //{{{ paintScreenLineRange() method
145
private void paintScreenLineRange(Graphics2D JavaDoc gfx, int firstLine,
146         int lastLine, int[] physicalLines, int[] start, int[] end,
147         int y, int lineHeight)
148     {
149         Iterator<Entry> iter = extensions.iterator();
150         while(iter.hasNext())
151         {
152             TextAreaExtension ext = iter.next().ext;
153             try
154             {
155                 ext.paintScreenLineRange(gfx,firstLine,lastLine,
156                     physicalLines,start,end,y,lineHeight);
157             }
158             catch(Throwable JavaDoc t)
159             {
160                 Log.log(Log.ERROR,this,t);
161
162                 // remove it so editor can continue
163
// functioning
164
iter.remove();
165             }
166         }
167     } //}}}
168

169     //}}}
170

171     //{{{ Entry class
172
/** This class represents an extension with his layer. */
173     static class Entry
174     {
175         int layer;
176         TextAreaExtension ext;
177
178         Entry(int layer, TextAreaExtension ext)
179         {
180             this.layer = layer;
181             this.ext = ext;
182         }
183     } //}}}
184
}
185
Popular Tags