PageRenderTime 39ms CodeModel.GetById 17ms RepoModel.GetById 0ms 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
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  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. */
  21. package org.gjt.sp.jedit.textarea;
  22. import java.awt.font.TextLayout;
  23. import java.util.Vector;
  24. import javax.swing.text.TabExpander;
  25. public class ElasticTabstopsTabExpander implements TabExpander
  26. {
  27. TextArea textArea;
  28. //{{{ ElasticTabstopsTabExpander() method
  29. public ElasticTabstopsTabExpander(TextArea textArea)
  30. {
  31. this.textArea = textArea;
  32. }//}}}
  33. //{{{ nextTabStop() method
  34. @Override
  35. public float nextTabStop(float x, int tabOffset)
  36. {
  37. float _tabSize = 0;
  38. if(textArea.buffer.getBooleanProperty("elasticTabstops")&&textArea.buffer.getColumnBlock()!=null)
  39. {
  40. int line = textArea.buffer.getLineOfOffset(tabOffset);
  41. _tabSize = getTabSize(textArea.buffer.getColumnBlock().getColumnBlock(line, tabOffset),line);
  42. if(_tabSize<0)
  43. {
  44. throw new IllegalArgumentException("Unaccounted tab at line "+textArea.buffer.getLineOfOffset(tabOffset)+" at index "+tabOffset);
  45. }
  46. }
  47. //keep minimum tab size of textArea.tabSize
  48. _tabSize+= textArea.tabSize;
  49. return (x+_tabSize);
  50. }//}}}
  51. //{{{ getTabSize() method
  52. private float getTabSize(ColumnBlock columnBlock, int line)
  53. {
  54. float ret = -5;
  55. if(columnBlock!=null)
  56. {
  57. Vector lines = columnBlock.getLines();
  58. if(columnBlock.areTabSizesDirty())
  59. {
  60. float colBlockWidth = -1;
  61. for(int i= 0;i<lines.size();i++)
  62. {
  63. ColumnBlockLine colBlockLine = (ColumnBlockLine)lines.elementAt(i);
  64. int startOffset = colBlockLine.getColumnStartIndex()+textArea.buffer.getLineStartOffset(colBlockLine.getLine());
  65. String str = textArea.buffer.getText(startOffset,colBlockLine.getColumnEndIndex()-colBlockLine.getColumnStartIndex());
  66. float width = 0;
  67. if(str.length()!=0)
  68. {
  69. TextLayout layout = new TextLayout(str,textArea.painter.getFont(),textArea.painter.getFontRenderContext());
  70. width = layout.getAdvance();
  71. }
  72. colBlockLine.lineLength = width;
  73. //colBlockLine.lineLength = textArea.painter.getFontMetrics().stringWidth(str);
  74. if((colBlockWidth<0)||(colBlockLine.lineLength>colBlockWidth))
  75. {
  76. colBlockWidth = colBlockLine.lineLength;
  77. }
  78. }
  79. columnBlock.columnBlockWidth = colBlockWidth;
  80. columnBlock.setTabSizeDirtyStatus(false, false);
  81. }
  82. ret = columnBlock.columnBlockWidth-((ColumnBlockLine)lines.get(line-columnBlock.startLine)).lineLength;
  83. }
  84. return ret;
  85. }//}}}
  86. }