001: /*
002: * CheckinBuffer.java
003: *
004: * Copyright (C) 1998-2003 Peter Graves
005: * $Id: CheckinBuffer.java,v 1.5 2003/07/01 17:23:38 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 CheckinBuffer extends Buffer implements Constants {
025: private final int vcType;
026: private final boolean editOnly;
027:
028: private int commentIndex = -1;
029:
030: public CheckinBuffer(Buffer parentBuffer, int vcType) {
031: this (parentBuffer, vcType, false);
032: }
033:
034: public CheckinBuffer(Buffer parentBuffer, int vcType,
035: boolean editOnly) {
036: super ();
037: this .parentBuffer = parentBuffer;
038: this .vcType = vcType;
039: this .editOnly = editOnly;
040: initializeUndo();
041: type = TYPE_NORMAL;
042: isUntitled = true;
043: mode = CheckinMode.getMode();
044: try {
045: lockWrite();
046: } catch (InterruptedException e) {
047: Log.debug(e);
048: return;
049: }
050: try {
051: appendLine("");
052: renumber();
053: } finally {
054: unlockWrite();
055: }
056: setLoaded(true);
057: setInitialized(true);
058: }
059:
060: public final File getCurrentDirectory() {
061: if (parentBuffer != null)
062: return parentBuffer.getCurrentDirectory();
063: return Directories.getUserHomeDirectory();
064: }
065:
066: public final int getVCType() {
067: return vcType;
068: }
069:
070: public final boolean isEditOnly() {
071: return editOnly;
072: }
073:
074: public String getFileNameForDisplay() {
075: return title != null ? title : "";
076: }
077:
078: public static void previousComment() {
079: final Editor editor = Editor.currentEditor();
080: final Buffer buffer = editor.getBuffer();
081: if (buffer instanceof CheckinBuffer)
082: ((CheckinBuffer) buffer).retrieveComment(editor, -1);
083: }
084:
085: public static void nextComment() {
086: final Editor editor = Editor.currentEditor();
087: final Buffer buffer = editor.getBuffer();
088: if (buffer instanceof CheckinBuffer)
089: ((CheckinBuffer) buffer).retrieveComment(editor, +1);
090: }
091:
092: private void retrieveComment(Editor editor, int arg) {
093: final CommentRing commentRing = CommentRing.getInstance();
094: if (commentIndex < 0)
095: commentIndex = commentRing.size();
096: int index = commentIndex + arg;
097: if (index > commentRing.size() - 1) {
098: // Wrap.
099: index = 0;
100: } else if (index < 0) {
101: // Wrap.
102: index = commentRing.size() - 1;
103: }
104: final String comment = commentRing.get(index);
105: if (comment == null) {
106: // Comment ring is empty.
107: return;
108: }
109: commentIndex = index;
110: switch (vcType) {
111: case VC_CVS:
112: CVS.replaceComment(editor, comment);
113: break;
114: case VC_P4:
115: P4.replaceComment(editor, comment);
116: break;
117: default:
118: Debug.bug();
119: break;
120: }
121: }
122:
123: public static void finish() {
124: final Editor editor = Editor.currentEditor();
125: final Buffer buffer = editor.getBuffer();
126: if (buffer instanceof CheckinBuffer) {
127: CheckinBuffer cb = (CheckinBuffer) buffer;
128: switch (cb.getVCType()) {
129: case VC_CVS:
130: CommentRing.getInstance().appendNew(
131: CVS.extractComment(cb));
132: CVS.finish(editor, cb);
133: break;
134: case VC_P4:
135: CommentRing.getInstance().appendNew(
136: P4.extractComment(cb));
137: P4.finish(editor, cb);
138: break;
139: default:
140: break;
141: }
142: }
143: }
144:
145: public Expansion getExpansion(Position dot) {
146: Expansion e = new Expansion(dot, Editor.getModeList().getMode(
147: PLAIN_TEXT_MODE));
148: if (parentBuffer != null && e.getPrefix() != null) {
149: // Look for diff output buffer for same parent buffer.
150: for (BufferIterator it = new BufferIterator(); it.hasNext();) {
151: Buffer b = it.nextBuffer();
152: if (b instanceof DiffOutputBuffer) {
153: if (((DiffOutputBuffer) b).getParentBuffer() == parentBuffer) {
154: // Add candidates from diff output buffer.
155: Expansion d = new Expansion(b, e.getPrefix(), e
156: .getCurrent());
157: e.appendCandidates(d.getCandidates());
158: break; // There should be one diff output buffer at most.
159: }
160: }
161: }
162: // Add candidates from parent buffer.
163: Expansion p = new Expansion(parentBuffer, e.getPrefix(), e
164: .getCurrent());
165: e.appendCandidates(p.getCandidates());
166: }
167: return e;
168: }
169: }
|