KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * ListRegistersBuffer.java
3  *
4  * Copyright (C) 2002 Peter Graves
5  * $Id: ListRegistersBuffer.java,v 1.2 2002/10/11 16:29:37 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.Arrays JavaDoc;
25
26 public final class ListRegistersBuffer extends Buffer
27 {
28     public ListRegistersBuffer()
29     {
30         supportsUndo = false;
31         type = TYPE_OUTPUT;
32         mode = ListRegistersMode.getMode();
33         formatter = mode.getFormatter(this);
34         lineSeparator = System.getProperty("line.separator");
35         readOnly = true;
36         setTransient(true);
37         setProperty(Property.VERTICAL_RULE, 0);
38         setProperty(Property.SHOW_LINE_NUMBERS, false);
39         setProperty(Property.HIGHLIGHT_MATCHING_BRACKET, false);
40         setProperty(Property.HIGHLIGHT_BRACKETS, false);
41         setInitialized(true);
42     }
43
44     public int load()
45     {
46         if (!isLoaded()) {
47             loadInternal();
48             if (!isLoaded())
49                 Debug.bug();
50         }
51         return LOAD_COMPLETED;
52     }
53
54     public void reload()
55     {
56         for (EditorIterator it = new EditorIterator(); it.hasNext();) {
57             Editor ed = it.nextEditor();
58             if (ed.getBuffer() == this) {
59                 ed.setWaitCursor();
60                 ed.saveView();
61             }
62         }
63         empty();
64         loadInternal();
65         for (EditorIterator it = new EditorIterator(); it.hasNext();) {
66             Editor ed = it.nextEditor();
67             if (ed.getBuffer() == this) {
68                 View view = ed.getView(this);
69                 if (view != null) {
70                     Line dotLine = getLine(view.getDotLineNumber());
71                     if (dotLine == null)
72                         dotLine = getFirstLine();
73                     if (dotLine != null) {
74                         ed.setDot(dotLine, 0);
75                         ed.moveCaretToDotCol();
76                     }
77                     Line topLine = getLine(view.getTopLineNumber());
78                     if (topLine == null)
79                         topLine = dotLine;
80                     ed.setTopLine(topLine);
81                 } else {
82                     ed.setDot(getFirstLine(), 0);
83                     ed.moveCaretToDotCol();
84                     ed.setTopLine(getFirstLine());
85                 }
86                 ed.setUpdateFlag(REPAINT);
87                 ed.setDefaultCursor();
88                 ed.updateDisplay();
89             }
90         }
91     }
92
93     private void loadInternal()
94     {
95         String JavaDoc[] names = null;
96         File directory = Directories.getRegistersDirectory();
97         if (directory != null)
98             names = directory.list();
99         try {
100             lockWrite();
101         }
102         catch (InterruptedException JavaDoc e) {
103             Log.debug(e);
104             return;
105         }
106         try {
107             if (names != null && names.length > 0) {
108                 Arrays.sort(names);
109                 final int MAX_LINES = 100;
110                 for (int i = 0; i < names.length; i++) {
111                     final String JavaDoc name = names[i];
112                     FastStringBuffer sb = new FastStringBuffer();
113                     sb.append("Register ");
114                     sb.append(name);
115                     String JavaDoc text = Registers.getText(name, MAX_LINES);
116                     if (text == null || text.length() == 0)
117                         continue;
118                     int lineCount = Utilities.countLines(text);
119                     appendLine(
120                         new ListRegistersLine(sb.toString(), name));
121                     append(text);
122                     if (lineCount == MAX_LINES)
123                         appendLine(new ListRegistersLine("[...]", name));
124                 }
125             } else
126                 appendLine(new ListRegistersLine("No registers in use", null));
127             renumber();
128             setLoaded(true);
129         }
130         finally {
131             unlockWrite();
132         }
133     }
134
135     public String JavaDoc getFileNameForDisplay()
136     {
137         return "listRegisters";
138     }
139
140     public String JavaDoc toString()
141     {
142         return "listRegisters";
143     }
144 }
145
Popular Tags