KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * BufferList.java
3  *
4  * Copyright (C) 1998-2004 Peter Graves, Mike Rutter
5  * $Id: BufferList.java,v 1.4 2004/06/27 14:37:03 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 import java.util.ArrayList JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.Comparator JavaDoc;
27 import java.util.Iterator JavaDoc;
28
29 public final class BufferList implements Constants, PreferencesChangeListener
30 {
31     private final ArrayList JavaDoc list = new ArrayList JavaDoc();
32
33     private boolean alpha; // Sort alphabetically?
34
private boolean reorder;
35     private boolean modified;
36
37     public BufferList()
38     {
39         Preferences p = Editor.preferences();
40         if (p != null) {
41             alpha = p.getBooleanProperty(Property.SORT_BUFFER_LIST);
42             if (alpha)
43                 reorder = false;
44             else
45                 reorder = p.getIntegerProperty(Property.REORDER_BUFFERS) > 0;
46             p.addPreferencesChangeListener(this);
47         } else
48             Debug.bug();
49     }
50
51     public synchronized Iterator JavaDoc iterator()
52     {
53         if (alpha && modified)
54             sort();
55         return list.iterator();
56     }
57
58     public synchronized void add(Buffer buf)
59     {
60         list.add(buf);
61         modified = true;
62     }
63
64     public synchronized boolean remove(Buffer buf)
65     {
66         return list.remove(buf);
67     }
68
69     public synchronized boolean move(Buffer buf, int index)
70     {
71         // If we're given an invalid index, return false.
72
if (index < 0 || index >= size())
73             return false;
74
75         // If the given index is not a change, or we're given an invalid
76
// Buffer, return false.
77
int previousIndex = list.indexOf(buf);
78         if (index == previousIndex || previousIndex < 0)
79             return false;
80
81         list.remove(previousIndex);
82         list.add(index, buf);
83         modified();
84         return true;
85     }
86
87     public synchronized int size()
88     {
89         return list.size();
90     }
91
92     public synchronized boolean contains(Buffer buf)
93     {
94         return indexOf(buf) >= 0;
95     }
96
97     public synchronized Buffer getFirstBuffer()
98     {
99         if (list.size() > 0) {
100             if (alpha && modified)
101                 sort();
102             return (Buffer) list.get(0);
103         }
104         return null;
105     }
106
107     public synchronized Buffer getNextPrimaryBuffer(Buffer buffer)
108     {
109         if (alpha && modified)
110             sort();
111         if (buffer.isSecondary()) {
112             Debug.assertTrue(buffer.getPrimary() != null);
113             Debug.assertTrue(buffer.getPrimary().isPrimary());
114             Debug.assertFalse(buffer.getPrimary().isSecondary());
115             return getNextPrimaryBuffer(buffer.getPrimary());
116         }
117         int index = indexOf(buffer);
118         if (index < 0)
119             return null;
120         while (true) {
121             if (index < size()-1)
122                 ++index;
123             else
124                 index = 0;
125             Buffer buf = (Buffer) list.get(index);
126             if (buf == buffer)
127                 break;
128             if (buf.isPrimary())
129                 return buf;
130         }
131         return null;
132     }
133
134     public synchronized Buffer getPreviousPrimaryBuffer(Buffer buffer)
135     {
136         if (alpha && modified)
137             sort();
138         if (buffer.isSecondary()) {
139             Debug.assertTrue(buffer.getPrimary() != null);
140             Debug.assertTrue(buffer.getPrimary().isPrimary());
141             Debug.assertFalse(buffer.getPrimary().isSecondary());
142             return getPreviousPrimaryBuffer(buffer.getPrimary());
143         }
144         int index = indexOf(buffer);
145         if (index < 0)
146             return null;
147         while (true) {
148             if (index > 0)
149                 --index;
150             else
151                 index = size() - 1;
152             Buffer buf = (Buffer) list.get(index);
153             if (buf == buffer)
154                 break;
155             if (buf.isPrimary())
156                 return buf;
157         }
158         return null;
159     }
160
161     public synchronized Buffer findBuffer(File f)
162     {
163         if (f != null) {
164             for (int i = list.size(); i-- > 0;) {
165                 Buffer buf = (Buffer) list.get(i);
166                 if (buf instanceof WebBuffer)
167                     continue;
168                 if (f.equals(buf.getFile()))
169                     return buf;
170             }
171         }
172         return null;
173     }
174
175     public synchronized void makeNext(final Buffer nextBuffer,
176         final Buffer currentBuffer)
177     {
178         if (!reorder)
179             return;
180         if (currentBuffer == null)
181             Log.debug("makeNext currentBuffer is null size = " + list.size());
182         if (currentBuffer != null)
183             Debug.assertTrue(list.contains(currentBuffer));
184         Debug.assertTrue(nextBuffer != null);
185         Debug.assertTrue(list.contains(nextBuffer));
186         if (nextBuffer == currentBuffer)
187             return; // Nothing to do.
188
remove(nextBuffer);
189         try {
190             for (int i = 0; i < list.size(); i++) {
191                 Buffer buf = (Buffer) list.get(i);
192                 if (buf == currentBuffer) {
193                     list.add(i+1, nextBuffer);
194                     return;
195                 }
196             }
197             Debug.assertTrue(currentBuffer == null || !list.contains(currentBuffer));
198             Debug.assertFalse(list.contains(nextBuffer));
199             list.add(nextBuffer);
200         }
201         finally {
202             if (currentBuffer != null)
203                 Debug.assertTrue(list.contains(currentBuffer));
204             Debug.assertTrue(list.contains(nextBuffer));
205         }
206     }
207
208     // Replace o (old) with n (new).
209
public synchronized void replace(Buffer o, Buffer n)
210     {
211         Debug.assertTrue(list.contains(o));
212         Debug.assertTrue(list.contains(n));
213         for (EditorIterator iter = new EditorIterator(); iter.hasNext();) {
214             Editor ed = iter.nextEditor();
215             if (ed.getBuffer() == o)
216                 ed.activate(n);
217             ed.views.remove(o);
218             if (ed.getBuffer() == n)
219                 ed.updateDisplay();
220         }
221         list.remove(n);
222         Debug.assertFalse(list.contains(n));
223         for (int i = list.size(); i-- > 0;) {
224             if (list.get(i) == o) {
225                 list.set(i, n);
226                 modified = true;
227                 break;
228             }
229         }
230         Debug.assertTrue(list.contains(n));
231         Debug.assertFalse(list.contains(o));
232         Sidebar.setUpdateFlagInAllFrames(SIDEBAR_BUFFER_LIST_CHANGED);
233     }
234
235     public synchronized final void modified()
236     {
237         modified = true;
238     }
239
240     public synchronized void preferencesChanged()
241     {
242         Preferences p = Editor.preferences();
243         boolean b = p.getBooleanProperty(Property.SORT_BUFFER_LIST);
244         if (b != alpha) {
245             alpha = b;
246             if (alpha) {
247                 sort();
248                 Sidebar.setUpdateFlagInAllFrames(SIDEBAR_BUFFER_LIST_CHANGED);
249             }
250         }
251         if (alpha)
252             reorder = false;
253         else
254             reorder = p.getIntegerProperty(Property.REORDER_BUFFERS) > 0;
255     }
256
257     private static final String JavaDoc userHome =
258         Platform.isPlatformUnix() ? Utilities.getUserHome() : null;
259
260     public synchronized String JavaDoc getUniqueName(Buffer buf)
261     {
262         final File file = buf.getFile();
263         final String JavaDoc name = file.getName();
264         boolean qualify = false;
265         Debug.assertTrue(file != null);
266         Debug.assertTrue(file.isLocal());
267         for (Iterator JavaDoc it = list.iterator(); it.hasNext();) {
268             Buffer b = (Buffer) it.next();
269             if (b == buf)
270                 continue;
271             if (b.getFile() != null && b.getFile().getName().equals(name)) {
272                 qualify = true;
273                 break;
274             }
275         }
276         if (qualify) {
277             FastStringBuffer sb = new FastStringBuffer(name);
278             sb.append(" [");
279             String JavaDoc dir = file.getParent();
280             if (userHome != null && userHome.length() > 0) {
281                 if (dir.equals(userHome))
282                     dir = "~";
283                 if (dir.startsWith(userHome.concat("/")))
284                     dir = "~".concat(dir.substring(userHome.length()));
285             }
286             sb.append(dir);
287             sb.append(']');
288             return sb.toString();
289         }
290         return name;
291     }
292
293     public int indexOf(Buffer buf)
294     {
295         for (int i = list.size(); i-- > 0;) {
296             if (list.get(i) == buf)
297                 return i;
298         }
299         return -1;
300     }
301
302     private static Comparator JavaDoc comparator;
303
304     private void sort()
305     {
306         if (alpha) {
307             if (comparator == null) {
308                 comparator = new Comparator JavaDoc() {
309                     public int compare(Object JavaDoc o1, Object JavaDoc o2)
310                     {
311                         return o1.toString().compareToIgnoreCase(o2.toString());
312                     }
313                 };
314             }
315             Collections.sort(list, comparator);
316         }
317         modified = false;
318     }
319 }
320
Popular Tags