KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > jdb > JdbFormatter


1 /*
2  * JdbFormatter.java
3  *
4  * Copyright (C) 2000-2003 Peter Graves
5  * $Id: JdbFormatter.java,v 1.4 2003/05/23 17:42:47 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.jdb;
23
24 import org.armedbear.j.Buffer;
25 import org.armedbear.j.Editor;
26 import org.armedbear.j.FormatTable;
27 import org.armedbear.j.Formatter;
28 import org.armedbear.j.Line;
29 import org.armedbear.j.LineSegmentList;
30 import org.armedbear.j.Utilities;
31
32 public final class JdbFormatter extends Formatter
33 {
34     private static final String JavaDoc prompt = Jdb.getPrompt();
35     private static final int promptLength = prompt.length();
36
37     // Formats.
38
public static final byte JDB_FORMAT_TEXT = 0;
39     public static final byte JDB_FORMAT_PROMPT = 1;
40     public static final byte JDB_FORMAT_INPUT = 2;
41     public static final byte JDB_FORMAT_OUTPUT = 3;
42     public static final byte JDB_FORMAT_LOG = 4;
43
44     public JdbFormatter(Buffer buffer)
45     {
46         this.buffer = buffer;
47     }
48
49     public LineSegmentList formatLine(Line line)
50     {
51         clearSegmentList();
52         if (line == null) {
53             addSegment("", JDB_FORMAT_TEXT);
54             return segmentList;
55         }
56         int flags = line.flags();
57         String JavaDoc text = getDetabbedText(line);
58         if (flags == JDB_FORMAT_OUTPUT) {
59             addSegment(text, JDB_FORMAT_OUTPUT);
60         } else if (text.startsWith(prompt)) {
61             addSegment(text, 0, promptLength, JDB_FORMAT_PROMPT);
62             if (text.length() > promptLength)
63                 addSegment(text, promptLength, JDB_FORMAT_INPUT);
64         } else
65             addSegment(text, JDB_FORMAT_LOG);
66         return segmentList;
67     }
68
69     public FormatTable getFormatTable()
70     {
71         if (formatTable == null) {
72             formatTable = new FormatTable("JdbMode");
73             formatTable.addEntryFromPrefs(JDB_FORMAT_TEXT, "text");
74             formatTable.addEntryFromPrefs(JDB_FORMAT_PROMPT, "prompt");
75             formatTable.addEntryFromPrefs(JDB_FORMAT_INPUT, "input");
76             formatTable.addEntryFromPrefs(JDB_FORMAT_OUTPUT, "output", "text");
77             formatTable.addEntryFromPrefs(JDB_FORMAT_LOG, "log", "comment");
78         }
79         return formatTable;
80     }
81 }
82
Popular Tags