KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * ListOccurrencesFormatter.java
3  *
4  * Copyright (C) 2000-2003 Peter Graves
5  * $Id: ListOccurrencesFormatter.java,v 1.2 2003/07/26 16:20:29 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 public final class ListOccurrencesFormatter extends Formatter
25 {
26     private static final byte FORMAT_TEXT = 0;
27     private static final byte FORMAT_COMMENT = 1;
28     private static final byte FORMAT_HEADER_NAME = 2;
29     private static final byte FORMAT_HEADER_VALUE = 4;
30     private static final byte FORMAT_MATCHING_TEXT = 5;
31     private static final byte FORMAT_STATUS = 6;
32     private static final byte FORMAT_VISITED = 7;
33
34     private Search search;
35     private Mode parentMode;
36
37     public ListOccurrencesFormatter(Buffer buffer)
38     {
39         this.buffer = buffer;
40         search = (Search) ((ListOccurrences)buffer).getSearch().clone();
41         parentMode = ((ListOccurrences)buffer).getParentMode();
42     }
43
44     public LineSegmentList formatLine(Line line)
45     {
46         clearSegmentList();
47         if (line instanceof OccurrenceLine)
48             return formatOutputLine((OccurrenceLine)line);
49         else if (line instanceof FileLine)
50             return formatFileLine((FileLine)line);
51         else if (line instanceof ListOccurrencesStatusLine)
52             return formatStatusLine((ListOccurrencesStatusLine)line);
53         else
54             return formatHeaderLine(line);
55     }
56
57     private LineSegmentList formatHeaderLine(Line line)
58     {
59         final String JavaDoc text = getDetabbedText(line);
60         int index = text.indexOf(':');
61         if (index > 0) {
62             addSegment(text, 0, index+1, FORMAT_HEADER_NAME);
63             addSegment(text, index+1, FORMAT_HEADER_VALUE);
64             return segmentList;
65         }
66         addSegment(text, FORMAT_TEXT);
67         return segmentList;
68     }
69
70     private LineSegmentList formatOutputLine(OccurrenceLine line)
71     {
72         final String JavaDoc text = getDetabbedText(line);
73         // Include the ':' in the first segment.
74
int index = text.indexOf(':') + 1;
75         addSegment(text, 0, index, FORMAT_COMMENT);
76         Position start = new Position(line, index);
77         int startCol = index;
78         while (true) {
79             Position pos = search.findInLine(parentMode, start);
80             if (pos == null)
81                 break;
82             int matchCol = buffer.getCol(pos);
83             if (matchCol != startCol)
84                 addSegment(text, startCol, matchCol, FORMAT_TEXT);
85             int length;
86             if (search.getMatch() != null)
87                 length = search.getMatch().toString().length();
88             else
89                 length = search.getPatternLength();
90             startCol = buffer.getCol(pos.getLine(), pos.getOffset() + length);
91             addSegment(text, matchCol, startCol, FORMAT_MATCHING_TEXT);
92             start = new Position(pos.getLine(), pos.getOffset() + length);
93         }
94         startCol = buffer.getCol(start);
95         addSegment(text, startCol, FORMAT_TEXT);
96         return segmentList;
97     }
98
99     private LineSegmentList formatFileLine(FileLine line)
100     {
101         final String JavaDoc text = getDetabbedText(line);
102         int index = text.indexOf(':');
103         if (index > 0) {
104             addSegment(text, 0, index+1, FORMAT_HEADER_NAME);
105             addSegment(text, index+1, FORMAT_HEADER_VALUE);
106         } else
107             addSegment(text, line.visited() ? FORMAT_VISITED : FORMAT_TEXT);
108         return segmentList;
109     }
110
111     private LineSegmentList formatStatusLine(ListOccurrencesStatusLine line)
112     {
113         final String JavaDoc text = getDetabbedText(line);
114         addSegment(text, FORMAT_STATUS);
115         return segmentList;
116     }
117
118     public FormatTable getFormatTable()
119     {
120         if (formatTable == null) {
121             formatTable = new FormatTable("ListOccurrencesMode");
122             formatTable.addEntryFromPrefs(FORMAT_TEXT, "text");
123             formatTable.addEntryFromPrefs(FORMAT_COMMENT, "comment");
124             formatTable.addEntryFromPrefs(FORMAT_HEADER_NAME, "headerName", "keyword");
125             formatTable.addEntryFromPrefs(FORMAT_HEADER_VALUE, "headerValue", "operator");
126             formatTable.addEntryFromPrefs(FORMAT_MATCHING_TEXT, "matchingText", "function");
127             formatTable.addEntryFromPrefs(FORMAT_STATUS, "status", "comment");
128             formatTable.addEntryFromPrefs(FORMAT_VISITED, "visited", "disabled");
129         }
130         return formatTable;
131     }
132 }
133
Popular Tags