KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * CheckinBuffer.java
3  *
4  * Copyright (C) 1998-2003 Peter Graves
5  * $Id: CheckinBuffer.java,v 1.5 2003/07/01 17:23:38 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 CheckinBuffer extends Buffer implements Constants
25 {
26     private final int vcType;
27     private final boolean editOnly;
28
29     private int commentIndex = -1;
30
31     public CheckinBuffer(Buffer parentBuffer, int vcType)
32     {
33         this(parentBuffer, vcType, false);
34     }
35
36     public CheckinBuffer(Buffer parentBuffer, int vcType, boolean editOnly)
37     {
38         super();
39         this.parentBuffer = parentBuffer;
40         this.vcType = vcType;
41         this.editOnly = editOnly;
42         initializeUndo();
43         type = TYPE_NORMAL;
44         isUntitled = true;
45         mode = CheckinMode.getMode();
46         try {
47             lockWrite();
48         }
49         catch (InterruptedException JavaDoc e) {
50             Log.debug(e);
51             return;
52         }
53         try {
54             appendLine("");
55             renumber();
56         }
57         finally {
58             unlockWrite();
59         }
60         setLoaded(true);
61         setInitialized(true);
62     }
63
64     public final File getCurrentDirectory()
65     {
66         if (parentBuffer != null)
67             return parentBuffer.getCurrentDirectory();
68         return Directories.getUserHomeDirectory();
69     }
70
71     public final int getVCType()
72     {
73         return vcType;
74     }
75
76     public final boolean isEditOnly()
77     {
78         return editOnly;
79     }
80
81     public String JavaDoc getFileNameForDisplay()
82     {
83         return title != null ? title : "";
84     }
85
86     public static void previousComment()
87     {
88         final Editor editor = Editor.currentEditor();
89         final Buffer buffer = editor.getBuffer();
90         if (buffer instanceof CheckinBuffer)
91             ((CheckinBuffer)buffer).retrieveComment(editor, -1);
92     }
93
94     public static void nextComment()
95     {
96         final Editor editor = Editor.currentEditor();
97         final Buffer buffer = editor.getBuffer();
98         if (buffer instanceof CheckinBuffer)
99             ((CheckinBuffer)buffer).retrieveComment(editor, +1);
100     }
101
102     private void retrieveComment(Editor editor, int arg)
103     {
104         final CommentRing commentRing = CommentRing.getInstance();
105         if (commentIndex < 0)
106             commentIndex = commentRing.size();
107         int index = commentIndex + arg;
108         if (index > commentRing.size()-1) {
109             // Wrap.
110
index = 0;
111         } else if (index < 0) {
112             // Wrap.
113
index = commentRing.size()-1;
114         }
115         final String JavaDoc comment = commentRing.get(index);
116         if (comment == null) {
117             // Comment ring is empty.
118
return;
119         }
120         commentIndex = index;
121         switch (vcType) {
122             case VC_CVS:
123                 CVS.replaceComment(editor, comment);
124                 break;
125             case VC_P4:
126                 P4.replaceComment(editor, comment);
127                 break;
128             default:
129                 Debug.bug();
130                 break;
131         }
132     }
133
134     public static void finish()
135     {
136         final Editor editor = Editor.currentEditor();
137         final Buffer buffer = editor.getBuffer();
138         if (buffer instanceof CheckinBuffer) {
139             CheckinBuffer cb = (CheckinBuffer) buffer;
140             switch (cb.getVCType()) {
141                 case VC_CVS:
142                     CommentRing.getInstance().appendNew(CVS.extractComment(cb));
143                     CVS.finish(editor, cb);
144                     break;
145                 case VC_P4:
146                     CommentRing.getInstance().appendNew(P4.extractComment(cb));
147                     P4.finish(editor, cb);
148                     break;
149                 default:
150                     break;
151             }
152         }
153     }
154
155     public Expansion getExpansion(Position dot)
156     {
157         Expansion e =
158             new Expansion(dot, Editor.getModeList().getMode(PLAIN_TEXT_MODE));
159         if (parentBuffer != null && e.getPrefix() != null) {
160             // Look for diff output buffer for same parent buffer.
161
for (BufferIterator it = new BufferIterator(); it.hasNext();) {
162                 Buffer b = it.nextBuffer();
163                 if (b instanceof DiffOutputBuffer) {
164                     if (((DiffOutputBuffer)b).getParentBuffer() == parentBuffer) {
165                         // Add candidates from diff output buffer.
166
Expansion d =
167                             new Expansion(b, e.getPrefix(), e.getCurrent());
168                         e.appendCandidates(d.getCandidates());
169                         break; // There should be one diff output buffer at most.
170
}
171                 }
172             }
173             // Add candidates from parent buffer.
174
Expansion p =
175                 new Expansion(parentBuffer, e.getPrefix(), e.getCurrent());
176             e.appendCandidates(p.getCandidates());
177         }
178         return e;
179     }
180 }
181
Popular Tags