PageRenderTime 277ms CodeModel.GetById 263ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre4/org/gjt/sp/jedit/textarea/ExtensionManager.java

#
Java | 175 lines | 123 code | 20 blank | 32 comment | 9 complexity | 91e4e91c8f58f618496724da9816e2a4 MD5 | raw file
  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
 23package org.gjt.sp.jedit.textarea;
 24
 25import java.awt.Graphics2D;
 26import java.util.*;
 27import org.gjt.sp.util.Log;
 28
 29class ExtensionManager
 30{
 31	//{{{ addExtension() method
 32	void addExtension(int layer, TextAreaExtension ext)
 33	{
 34		Entry entry = new Entry(layer,ext);
 35
 36		int i = 0;
 37		Iterator iter = extensions.iterator();
 38		while(iter.hasNext())
 39		{
 40			int _layer = ((Entry)iter.next()).layer;
 41			if(layer < _layer)
 42			{
 43				extensions.add(i,entry);
 44				return;
 45			}
 46			i++;
 47		}
 48
 49		extensions.add(entry);
 50	} //}}}
 51
 52	//{{{ removeExtension() method
 53	void removeExtension(TextAreaExtension ext)
 54	{
 55		Iterator iter = extensions.iterator();
 56		while(iter.hasNext())
 57		{
 58			if(((Entry)iter.next()).ext == ext)
 59			{
 60				iter.remove();
 61				return;
 62			}
 63		}
 64	} //}}}
 65
 66	//{{{ getExtensions() method
 67	TextAreaExtension[] getExtensions()
 68	{
 69		TextAreaExtension[] retVal = new TextAreaExtension[
 70			extensions.size()];
 71		Iterator iter = extensions.iterator();
 72		int i = 0;
 73		while(iter.hasNext())
 74		{
 75			retVal[i++] = ((Entry)iter.next()).ext;
 76		}
 77		return retVal;
 78	} //}}}
 79
 80	//{{{ paintScreenLineRange() method
 81	void paintScreenLineRange(JEditTextArea textArea, Graphics2D gfx,
 82		int firstLine, int lastLine, int y, int lineHeight)
 83	{
 84		try
 85		{
 86			int[] physicalLines = new int[lastLine - firstLine + 1];
 87			int[] start = new int[physicalLines.length];
 88			int[] end = new int[physicalLines.length];
 89
 90			for(int i = 0; i < physicalLines.length; i++)
 91			{
 92				int screenLine = i + firstLine;
 93				ChunkCache.LineInfo lineInfo = textArea
 94					.chunkCache.getLineInfo(screenLine);
 95
 96				if(lineInfo.physicalLine == -1)
 97					physicalLines[i] = -1;
 98				else
 99				{
100					physicalLines[i] = lineInfo.physicalLine;
101					start[i] = textArea.getScreenLineStartOffset(screenLine);
102					end[i] = textArea.getScreenLineEndOffset(screenLine);
103				}
104			}
105
106			paintScreenLineRange(gfx,firstLine,lastLine,physicalLines,
107				start,end,y,lineHeight);
108		}
109		catch(Exception e)
110		{
111			Log.log(Log.ERROR,this,"Error repainting line"
112				+ " range {" + firstLine + ","
113				+ lastLine + "}:");
114			Log.log(Log.ERROR,this,e);
115		}
116	} //}}}
117
118	//{{{ getToolTipText() method
119	String getToolTipText(int x, int y)
120	{
121		for(int i = 0; i < extensions.size(); i++)
122		{
123			TextAreaExtension ext = ((Entry)extensions.get(i)).ext;
124			String toolTip = ext.getToolTipText(x,y);
125			if(toolTip != null)
126				return toolTip;
127		}
128
129		return null;
130	} //}}}
131
132	//{{{ Private members
133	private List extensions = new LinkedList();
134
135	//{{{ paintScreenLineRange() method
136	private void paintScreenLineRange(Graphics2D gfx, int firstLine,
137		int lastLine, int[] physicalLines, int[] start, int[] end,
138		int y, int lineHeight)
139	{
140		Iterator iter = extensions.iterator();
141		while(iter.hasNext())
142		{
143			TextAreaExtension ext = ((Entry)iter.next()).ext;
144			try
145			{
146				ext.paintScreenLineRange(gfx,firstLine,lastLine,
147					physicalLines,start,end,y,lineHeight);
148			}
149			
150			catch(Throwable t)
151			{
152				Log.log(Log.ERROR,this,t);
153
154				// remove it so editor can continue
155				// functioning
156				iter.remove();
157			}
158		}
159	} //}}}
160
161	//}}}
162
163	//{{{ Entry class
164	static class Entry
165	{
166		int layer;
167		TextAreaExtension ext;
168
169		Entry(int layer, TextAreaExtension ext)
170		{
171			this.layer = layer;
172			this.ext = ext;
173		}
174	} //}}}
175}