KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > jedit > buffer > ExplicitFoldHandler


1 /*
2  * ExplicitFoldHandler.java - Explicit fold handler
3  * :tabSize=8:indentSize=8:noTabs=false:
4  * :folding=explicit:collapseFolds=1:
5  *
6  * Copyright (C) 2001 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
23 package org.gjt.sp.jedit.buffer;
24
25 import javax.swing.text.Segment JavaDoc;
26
27 /**
28  * A fold handler that folds lines based on markers ("{{{" and "}}}")
29  * embedded in the text.
30  *
31  * @author Slava Pestov
32  * @version $Id: ExplicitFoldHandler.java 5339 2006-01-25 23:12:07Z spestov $
33  * @since jEdit 4.0pre1
34  */

35 public class ExplicitFoldHandler extends FoldHandler
36 {
37     //{{{ ExplicitFoldHandler constructor
38
public ExplicitFoldHandler()
39     {
40         super("explicit");
41     } //}}}
42

43     //{{{ getFoldLevel() method
44
/**
45      * Returns the fold level of the specified line.
46      * @param buffer The buffer in question
47      * @param lineIndex The line index
48      * @param seg A segment the fold handler can use to obtain any
49      * text from the buffer, if necessary
50      * @return The fold level of the specified line
51      * @since jEdit 4.0pre1
52      */

53     public int getFoldLevel(JEditBuffer buffer, int lineIndex, Segment JavaDoc seg)
54     {
55         if(lineIndex == 0)
56             return 0;
57         else
58         {
59             int foldLevel = buffer.getFoldLevel(lineIndex - 1);
60
61             buffer.getLineText(lineIndex - 1,seg);
62
63             int offset = seg.offset;
64             int count = seg.count;
65
66             int openingBrackets = 0, closingBrackets = 0;
67             for(int i = 0; i < count; i++)
68             {
69                 switch(seg.array[offset + i])
70                 {
71                 case '{':
72                     closingBrackets = 0;
73                     openingBrackets++;
74                     if(openingBrackets == 3)
75                     {
76                         foldLevel++;
77                         openingBrackets = 0;
78                     }
79                     break;
80                 case '}':
81                     openingBrackets = 0;
82                     closingBrackets++;
83                     if(closingBrackets == 3)
84                     {
85                         if(foldLevel > 0)
86                             foldLevel--;
87                         closingBrackets = 0;
88                     }
89                     break;
90                 default:
91                     closingBrackets = openingBrackets = 0;
92                     break;
93                 }
94             }
95
96             return foldLevel;
97         }
98     } //}}}
99
}
100
Popular Tags