/bundles/plugins-trunk/RFCReader/src/gatchan/jedit/rfcreader/RFCPageFoldHandler.java

# · Java · 102 lines · 64 code · 10 blank · 28 comment · 20 complexity · b29df1c4009a6e2a299bed1e1d2a03f2 MD5 · raw file

  1. /*
  2. * RFCPageFoldHandler.java
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 2007 Matthieu Casanova
  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. package gatchan.jedit.rfcreader;
  23. import org.gjt.sp.jedit.buffer.FoldHandler;
  24. import org.gjt.sp.jedit.buffer.JEditBuffer;
  25. import javax.swing.text.Segment;
  26. /**
  27. * @author Matthieu Casanova
  28. * @version $Id: Server.java,v 1.33 2007/01/05 15:15:17 matthieu Exp $
  29. */
  30. public class RFCPageFoldHandler extends FoldHandler
  31. {
  32. //{{{ RFCPageFoldHandler constructor
  33. public RFCPageFoldHandler()
  34. {
  35. super("rfc-page");
  36. } //}}}
  37. //{{{ getFoldLevel() method
  38. public int getFoldLevel(JEditBuffer buffer, int lineIndex, Segment seg)
  39. {
  40. if(lineIndex == 0)
  41. return 0;
  42. int foldLevel = buffer.getFoldLevel(lineIndex - 1);
  43. buffer.getLineText(lineIndex - 1,seg);
  44. if (segmentIsPage(seg))
  45. {
  46. return foldLevel + 1;
  47. }
  48. else if (seg.count == 0)
  49. {
  50. buffer.getLineText(lineIndex,seg);
  51. if (segmentIsPage(seg))
  52. {
  53. return Math.max(foldLevel - 1,0);
  54. }
  55. }
  56. return foldLevel;
  57. } //}}}
  58. //{{{ segmentIsPage() method
  59. private boolean segmentIsPage(Segment seg)
  60. {
  61. int offset = seg.offset;
  62. int count = seg.count;
  63. if (count < 8)
  64. return false;
  65. char[] chars = seg.array;
  66. if (chars[offset + count - 1] != ']')
  67. return false;
  68. int i;
  69. for (i = count - 2;i>0;i--)
  70. {
  71. char c = chars[offset + i];
  72. if (Character.isWhitespace(c))
  73. {
  74. i--;
  75. break;
  76. }
  77. if (!Character.isDigit(c))
  78. return false;
  79. }
  80. if (i < 5)
  81. return false;
  82. if (chars[offset + i] == 'e' &&
  83. chars[offset + i-1] == 'g' &&
  84. chars[offset + i-2] == 'a' &&
  85. chars[offset + i-3] == 'P' &&
  86. chars[offset + i-4] == '[')
  87. {
  88. return true;
  89. }
  90. return false;
  91. } //}}}
  92. }