KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > HiddenLines


1 /*
2  * HiddenLines.java
3  *
4  * Copyright (C) 2002 Peter Graves
5  * $Id: HiddenLines.java,v 1.1.1.1 2002/09/24 16:09:22 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
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
22 package org.armedbear.j;
23
24 import java.util.ArrayList JavaDoc;
25
26 public final class HiddenLines
27 {
28     private final Buffer buffer;
29     private final ArrayList JavaDoc list;
30
31     public HiddenLines(Editor editor)
32     {
33         buffer = editor.getBuffer();
34         list = new ArrayList JavaDoc();
35         int count = 0;
36         int hidden = -1;
37         for (Line line = buffer.getFirstLine(); line != null; line = line.next()) {
38             if (hidden == line.getHidden()) {
39                 ++count;
40             } else {
41                 if (count > 0)
42                     addEntry(hidden, count);
43                 hidden = line.getHidden();
44                 count = 1;
45             }
46         }
47         if (count > 0)
48             addEntry(hidden, count);
49     }
50
51     public void restore()
52     {
53         Line line = buffer.getFirstLine();
54         for (int i = 0; i < list.size(); i++) {
55             HiddenLinesEntry entry = (HiddenLinesEntry) list.get(i);
56             for (int j = 0; j < entry.getCount(); j++) {
57                 line.setHidden(entry.getHidden());
58                 line = line.next();
59             }
60         }
61         buffer.renumber();
62     }
63
64     private final void addEntry(int hidden, int count)
65     {
66         list.add(new HiddenLinesEntry(hidden, count));
67     }
68
69     private static final class HiddenLinesEntry
70     {
71         private final int hidden;
72         private final int count;
73
74         private HiddenLinesEntry(int hidden, int count)
75         {
76             this.hidden = hidden;
77             this.count = count;
78         }
79
80         private final int getHidden()
81         {
82             return hidden;
83         }
84
85         private final int getCount()
86         {
87             return count;
88         }
89     }
90 }
91
Popular Tags