001: /*
002: * ListOccurrencesFormatter.java
003: *
004: * Copyright (C) 2000-2003 Peter Graves
005: * $Id: ListOccurrencesFormatter.java,v 1.2 2003/07/26 16:20:29 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j;
023:
024: public final class ListOccurrencesFormatter extends Formatter {
025: private static final byte FORMAT_TEXT = 0;
026: private static final byte FORMAT_COMMENT = 1;
027: private static final byte FORMAT_HEADER_NAME = 2;
028: private static final byte FORMAT_HEADER_VALUE = 4;
029: private static final byte FORMAT_MATCHING_TEXT = 5;
030: private static final byte FORMAT_STATUS = 6;
031: private static final byte FORMAT_VISITED = 7;
032:
033: private Search search;
034: private Mode parentMode;
035:
036: public ListOccurrencesFormatter(Buffer buffer) {
037: this .buffer = buffer;
038: search = (Search) ((ListOccurrences) buffer).getSearch()
039: .clone();
040: parentMode = ((ListOccurrences) buffer).getParentMode();
041: }
042:
043: public LineSegmentList formatLine(Line line) {
044: clearSegmentList();
045: if (line instanceof OccurrenceLine)
046: return formatOutputLine((OccurrenceLine) line);
047: else if (line instanceof FileLine)
048: return formatFileLine((FileLine) line);
049: else if (line instanceof ListOccurrencesStatusLine)
050: return formatStatusLine((ListOccurrencesStatusLine) line);
051: else
052: return formatHeaderLine(line);
053: }
054:
055: private LineSegmentList formatHeaderLine(Line line) {
056: final String text = getDetabbedText(line);
057: int index = text.indexOf(':');
058: if (index > 0) {
059: addSegment(text, 0, index + 1, FORMAT_HEADER_NAME);
060: addSegment(text, index + 1, FORMAT_HEADER_VALUE);
061: return segmentList;
062: }
063: addSegment(text, FORMAT_TEXT);
064: return segmentList;
065: }
066:
067: private LineSegmentList formatOutputLine(OccurrenceLine line) {
068: final String text = getDetabbedText(line);
069: // Include the ':' in the first segment.
070: int index = text.indexOf(':') + 1;
071: addSegment(text, 0, index, FORMAT_COMMENT);
072: Position start = new Position(line, index);
073: int startCol = index;
074: while (true) {
075: Position pos = search.findInLine(parentMode, start);
076: if (pos == null)
077: break;
078: int matchCol = buffer.getCol(pos);
079: if (matchCol != startCol)
080: addSegment(text, startCol, matchCol, FORMAT_TEXT);
081: int length;
082: if (search.getMatch() != null)
083: length = search.getMatch().toString().length();
084: else
085: length = search.getPatternLength();
086: startCol = buffer.getCol(pos.getLine(), pos.getOffset()
087: + length);
088: addSegment(text, matchCol, startCol, FORMAT_MATCHING_TEXT);
089: start = new Position(pos.getLine(), pos.getOffset()
090: + length);
091: }
092: startCol = buffer.getCol(start);
093: addSegment(text, startCol, FORMAT_TEXT);
094: return segmentList;
095: }
096:
097: private LineSegmentList formatFileLine(FileLine line) {
098: final String text = getDetabbedText(line);
099: int index = text.indexOf(':');
100: if (index > 0) {
101: addSegment(text, 0, index + 1, FORMAT_HEADER_NAME);
102: addSegment(text, index + 1, FORMAT_HEADER_VALUE);
103: } else
104: addSegment(text, line.visited() ? FORMAT_VISITED
105: : FORMAT_TEXT);
106: return segmentList;
107: }
108:
109: private LineSegmentList formatStatusLine(
110: ListOccurrencesStatusLine line) {
111: final String text = getDetabbedText(line);
112: addSegment(text, FORMAT_STATUS);
113: return segmentList;
114: }
115:
116: public FormatTable getFormatTable() {
117: if (formatTable == null) {
118: formatTable = new FormatTable("ListOccurrencesMode");
119: formatTable.addEntryFromPrefs(FORMAT_TEXT, "text");
120: formatTable.addEntryFromPrefs(FORMAT_COMMENT, "comment");
121: formatTable.addEntryFromPrefs(FORMAT_HEADER_NAME,
122: "headerName", "keyword");
123: formatTable.addEntryFromPrefs(FORMAT_HEADER_VALUE,
124: "headerValue", "operator");
125: formatTable.addEntryFromPrefs(FORMAT_MATCHING_TEXT,
126: "matchingText", "function");
127: formatTable.addEntryFromPrefs(FORMAT_STATUS, "status",
128: "comment");
129: formatTable.addEntryFromPrefs(FORMAT_VISITED, "visited",
130: "disabled");
131: }
132: return formatTable;
133: }
134: }
|