001: /*
002: * DirectoryFormatter.java
003: *
004: * Copyright (C) 1998-2003 Peter Graves
005: * $Id: DirectoryFormatter.java,v 1.2 2003/05/12 02:07:32 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: import gnu.regexp.REMatch;
025:
026: public final class DirectoryFormatter extends Formatter {
027: // Formats.
028: private static final byte DIRECTORY_FORMAT_TEXT = 0;
029: private static final byte DIRECTORY_FORMAT_DIRECTORY = 1;
030: private static final byte DIRECTORY_FORMAT_SYMLINK = 2;
031: private static final byte DIRECTORY_FORMAT_TAGGED = 3;
032:
033: private final Directory directory;
034:
035: public DirectoryFormatter(Buffer buffer) {
036: this .buffer = buffer;
037: directory = (Directory) buffer;
038: }
039:
040: public LineSegmentList formatLine(Line line) {
041: clearSegmentList();
042: if (line == null || line.length() == 0) {
043: addSegment("", DIRECTORY_FORMAT_TEXT);
044: return segmentList;
045: }
046: final String text = line.getText();
047: if (text.charAt(0) == 'T') {
048: // Tagged file.
049: int nameOffset = getNameOffset(text);
050: addSegment("T", DIRECTORY_FORMAT_TAGGED);
051: addSegment(text, 1, nameOffset, DIRECTORY_FORMAT_TEXT);
052: addSegment(text, nameOffset, DIRECTORY_FORMAT_TAGGED);
053: return segmentList;
054: }
055: final int index = text.indexOf(" -> ");
056: if (index >= 0) {
057: // Symbolic link.
058: int nameOffset = getNameOffset(text);
059: addSegment(text, 0, nameOffset, DIRECTORY_FORMAT_TEXT);
060: addSegment(text, nameOffset, index,
061: DIRECTORY_FORMAT_SYMLINK);
062: addSegment(text, index, DIRECTORY_FORMAT_TEXT);
063: return segmentList;
064: }
065: final String trim = text.trim();
066: if (trim.startsWith("d") || trim.startsWith("<DIR>")) {
067: // Directory.
068: int nameOffset = getNameOffset(text);
069: addSegment(text, 0, nameOffset, DIRECTORY_FORMAT_TEXT);
070: addSegment(text, nameOffset, DIRECTORY_FORMAT_DIRECTORY);
071: return segmentList;
072: }
073: addSegment(text, DIRECTORY_FORMAT_TEXT);
074: return segmentList;
075: }
076:
077: private int getNameOffset(String text) {
078: REMatch match;
079: if (directory.isUsingNativeFormat())
080: match = Directory.getNativeMoveToFilenameRegExp().getMatch(
081: text);
082: else
083: match = Directory.getInternalMoveToFilenameRegExp()
084: .getMatch(text);
085: return (match != null) ? match.getEndIndex() : text.length();
086: }
087:
088: public FormatTable getFormatTable() {
089: if (formatTable == null) {
090: formatTable = new FormatTable("DirectoryMode");
091: formatTable
092: .addEntryFromPrefs(DIRECTORY_FORMAT_TEXT, "text");
093: formatTable.addEntryFromPrefs(DIRECTORY_FORMAT_DIRECTORY,
094: "directory");
095: formatTable.addEntryFromPrefs(DIRECTORY_FORMAT_SYMLINK,
096: "symlink");
097: formatTable.addEntryFromPrefs(DIRECTORY_FORMAT_TAGGED,
098: "marked");
099: }
100: return formatTable;
101: }
102: }
|