PageRenderTime 1ms CodeModel.GetById 258ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/textarea/ElasticTabstopsTabExpander.java

#
Java | 93 lines | 61 code | 7 blank | 25 comment | 7 complexity | 4aaa0a5234df0fcab646a816f989ef96 MD5 | raw file
 1/*
 2 * jEdit - Programmer's Text Editor
 3 * :tabSize=8:indentSize=8:noTabs=false:
 4 * :folding=explicit:collapseFolds=1:
 5 *
 6 * Copyright Š 2010 jEdit contributors
 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 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20 */
21package org.gjt.sp.jedit.textarea;
22
23import java.awt.font.TextLayout;
24import java.util.Vector;
25
26import javax.swing.text.TabExpander;
27
28public class ElasticTabstopsTabExpander implements TabExpander 
29{
30	TextArea textArea;
31	
32	//{{{ ElasticTabstopsTabExpander() method
33	public ElasticTabstopsTabExpander(TextArea textArea)
34	{
35		this.textArea = textArea;
36	}//}}}
37	
38	//{{{ nextTabStop() method
39	@Override
40	public float nextTabStop(float x, int tabOffset) 
41	{
42		float _tabSize = 0;
43		if(textArea.buffer.getBooleanProperty("elasticTabstops")&&textArea.buffer.getColumnBlock()!=null)
44		{
45			int line = textArea.buffer.getLineOfOffset(tabOffset);
46			_tabSize = getTabSize(textArea.buffer.getColumnBlock().getColumnBlock(line, tabOffset),line);
47			if(_tabSize<0)
48			{
49				throw new IllegalArgumentException("Unaccounted tab at line "+textArea.buffer.getLineOfOffset(tabOffset)+" at index "+tabOffset);
50			}
51		}
52		//keep minimum tab size of  textArea.tabSize
53		_tabSize+= textArea.tabSize;
54		return (x+_tabSize);
55	}//}}}
56	
57	//{{{ getTabSize() method
58	private float getTabSize(ColumnBlock columnBlock, int line) 
59	{
60		float ret = -5;
61		if(columnBlock!=null)
62		{	
63			Vector lines = columnBlock.getLines();
64			if(columnBlock.areTabSizesDirty())
65			{
66				float colBlockWidth = -1;
67				for(int i= 0;i<lines.size();i++)
68				{
69					ColumnBlockLine colBlockLine = (ColumnBlockLine)lines.elementAt(i);
70					int startOffset = colBlockLine.getColumnStartIndex()+textArea.buffer.getLineStartOffset(colBlockLine.getLine());
71					String str = textArea.buffer.getText(startOffset,colBlockLine.getColumnEndIndex()-colBlockLine.getColumnStartIndex());
72					float width = 0;
73					if(str.length()!=0)
74					{	
75						TextLayout layout = new TextLayout(str,textArea.painter.getFont(),textArea.painter.getFontRenderContext());
76						width = layout.getAdvance();
77					}
78					colBlockLine.lineLength = width;
79					//colBlockLine.lineLength = textArea.painter.getFontMetrics().stringWidth(str);
80					if((colBlockWidth<0)||(colBlockLine.lineLength>colBlockWidth))
81					{
82						colBlockWidth = colBlockLine.lineLength;
83					}
84				}
85				columnBlock.columnBlockWidth = colBlockWidth;
86				columnBlock.setTabSizeDirtyStatus(false, false);
87			}
88			ret = columnBlock.columnBlockWidth-((ColumnBlockLine)lines.get(line-columnBlock.startLine)).lineLength;
89		}	
90		return ret;
91	}//}}}
92
93}