001: /*
002: * ExtensionManager.java - Handles 'layers'
003: * :tabSize=8:indentSize=8:noTabs=false:
004: * :folding=explicit:collapseFolds=1:
005: *
006: * Copyright (C) 2002, 2003 Slava Pestov
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public License
010: * as published by the Free Software Foundation; either version 2
011: * of the License, or any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021: */
022:
023: package org.gjt.sp.jedit.textarea;
024:
025: import java.awt.Graphics2D;
026: import java.util.*;
027: import org.gjt.sp.util.Log;
028:
029: /**
030: * Manage the extensions for the gutter and the textarea.
031: *
032: * @author Slava Pestov
033: * @version $Id: ExtensionManager.java 7156 2006-10-02 21:33:17Z kpouer $
034: */
035: class ExtensionManager {
036: //{{{ addExtension() method
037: /**
038: * Add an extension.
039: * See {@link Gutter} and {@link TextAreaPainter} to know the layers
040: *
041: * @param layer the layer. It could be defined in Gutter or TextAreaPainter
042: * @param ext the extension to add
043: */
044: void addExtension(int layer, TextAreaExtension ext) {
045: Entry entry = new Entry(layer, ext);
046:
047: int i = 0;
048: for (Entry extension : extensions) {
049: int _layer = extension.layer;
050: if (layer < _layer) {
051: extensions.add(i, entry);
052: return;
053: }
054: i++;
055: }
056:
057: extensions.add(entry);
058: } //}}}
059:
060: //{{{ removeExtension() method
061: void removeExtension(TextAreaExtension ext) {
062: Iterator<Entry> iter = extensions.iterator();
063: while (iter.hasNext()) {
064: if (iter.next().ext == ext) {
065: iter.remove();
066: return;
067: }
068: }
069: } //}}}
070:
071: //{{{ getExtensions() method
072: TextAreaExtension[] getExtensions() {
073: TextAreaExtension[] retVal = new TextAreaExtension[extensions
074: .size()];
075: Iterator<Entry> iter = extensions.iterator();
076: int i = 0;
077: while (iter.hasNext())
078: retVal[i++] = iter.next().ext;
079:
080: return retVal;
081: } //}}}
082:
083: //{{{ paintScreenLineRange() method
084: void paintScreenLineRange(TextArea textArea, Graphics2D gfx,
085: int firstLine, int lastLine, int y, int lineHeight) {
086: try {
087: int[] physicalLines = new int[lastLine - firstLine + 1];
088: int[] start = new int[physicalLines.length];
089: int[] end = new int[physicalLines.length];
090:
091: for (int i = 0; i < physicalLines.length; i++) {
092: int screenLine = i + firstLine;
093: ChunkCache.LineInfo lineInfo = textArea.chunkCache
094: .getLineInfo(screenLine);
095:
096: if (lineInfo.physicalLine == -1)
097: physicalLines[i] = -1;
098: else {
099: physicalLines[i] = lineInfo.physicalLine;
100: start[i] = textArea
101: .getScreenLineStartOffset(screenLine);
102: end[i] = textArea
103: .getScreenLineEndOffset(screenLine);
104: }
105: }
106:
107: paintScreenLineRange(gfx, firstLine, lastLine,
108: physicalLines, start, end, y, lineHeight);
109: } catch (Exception e) {
110: Log.log(Log.ERROR, this , "Error repainting line"
111: + " range {" + firstLine + ',' + lastLine + "}:");
112: Log.log(Log.ERROR, this , e);
113: }
114: } //}}}
115:
116: //{{{ getToolTipText() method
117: String getToolTipText(int x, int y) {
118: for (int i = 0; i < extensions.size(); i++) {
119: TextAreaExtension ext = extensions.get(i).ext;
120: String toolTip = ext.getToolTipText(x, y);
121: if (toolTip != null)
122: return toolTip;
123: }
124:
125: return null;
126: } //}}}
127:
128: //{{{ Private members
129: private final List<Entry> extensions = new LinkedList<Entry>();
130:
131: //{{{ paintScreenLineRange() method
132: private void paintScreenLineRange(Graphics2D gfx, int firstLine,
133: int lastLine, int[] physicalLines, int[] start, int[] end,
134: int y, int lineHeight) {
135: Iterator<Entry> iter = extensions.iterator();
136: while (iter.hasNext()) {
137: TextAreaExtension ext = iter.next().ext;
138: try {
139: ext.paintScreenLineRange(gfx, firstLine, lastLine,
140: physicalLines, start, end, y, lineHeight);
141: } catch (Throwable t) {
142: Log.log(Log.ERROR, this , t);
143:
144: // remove it so editor can continue
145: // functioning
146: iter.remove();
147: }
148: }
149: } //}}}
150:
151: //}}}
152:
153: //{{{ Entry class
154: /** This class represents an extension with his layer. */
155: static class Entry {
156: int layer;
157: TextAreaExtension ext;
158:
159: Entry(int layer, TextAreaExtension ext) {
160: this .layer = layer;
161: this .ext = ext;
162: }
163: } //}}}
164: }
|