001: /*
002: * BufferList.java
003: *
004: * Copyright (C) 1998-2004 Peter Graves, Mike Rutter
005: * $Id: BufferList.java,v 1.4 2004/06/27 14:37:03 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 java.util.ArrayList;
025: import java.util.Collections;
026: import java.util.Comparator;
027: import java.util.Iterator;
028:
029: public final class BufferList implements Constants,
030: PreferencesChangeListener {
031: private final ArrayList list = new ArrayList();
032:
033: private boolean alpha; // Sort alphabetically?
034: private boolean reorder;
035: private boolean modified;
036:
037: public BufferList() {
038: Preferences p = Editor.preferences();
039: if (p != null) {
040: alpha = p.getBooleanProperty(Property.SORT_BUFFER_LIST);
041: if (alpha)
042: reorder = false;
043: else
044: reorder = p
045: .getIntegerProperty(Property.REORDER_BUFFERS) > 0;
046: p.addPreferencesChangeListener(this );
047: } else
048: Debug.bug();
049: }
050:
051: public synchronized Iterator iterator() {
052: if (alpha && modified)
053: sort();
054: return list.iterator();
055: }
056:
057: public synchronized void add(Buffer buf) {
058: list.add(buf);
059: modified = true;
060: }
061:
062: public synchronized boolean remove(Buffer buf) {
063: return list.remove(buf);
064: }
065:
066: public synchronized boolean move(Buffer buf, int index) {
067: // If we're given an invalid index, return false.
068: if (index < 0 || index >= size())
069: return false;
070:
071: // If the given index is not a change, or we're given an invalid
072: // Buffer, return false.
073: int previousIndex = list.indexOf(buf);
074: if (index == previousIndex || previousIndex < 0)
075: return false;
076:
077: list.remove(previousIndex);
078: list.add(index, buf);
079: modified();
080: return true;
081: }
082:
083: public synchronized int size() {
084: return list.size();
085: }
086:
087: public synchronized boolean contains(Buffer buf) {
088: return indexOf(buf) >= 0;
089: }
090:
091: public synchronized Buffer getFirstBuffer() {
092: if (list.size() > 0) {
093: if (alpha && modified)
094: sort();
095: return (Buffer) list.get(0);
096: }
097: return null;
098: }
099:
100: public synchronized Buffer getNextPrimaryBuffer(Buffer buffer) {
101: if (alpha && modified)
102: sort();
103: if (buffer.isSecondary()) {
104: Debug.assertTrue(buffer.getPrimary() != null);
105: Debug.assertTrue(buffer.getPrimary().isPrimary());
106: Debug.assertFalse(buffer.getPrimary().isSecondary());
107: return getNextPrimaryBuffer(buffer.getPrimary());
108: }
109: int index = indexOf(buffer);
110: if (index < 0)
111: return null;
112: while (true) {
113: if (index < size() - 1)
114: ++index;
115: else
116: index = 0;
117: Buffer buf = (Buffer) list.get(index);
118: if (buf == buffer)
119: break;
120: if (buf.isPrimary())
121: return buf;
122: }
123: return null;
124: }
125:
126: public synchronized Buffer getPreviousPrimaryBuffer(Buffer buffer) {
127: if (alpha && modified)
128: sort();
129: if (buffer.isSecondary()) {
130: Debug.assertTrue(buffer.getPrimary() != null);
131: Debug.assertTrue(buffer.getPrimary().isPrimary());
132: Debug.assertFalse(buffer.getPrimary().isSecondary());
133: return getPreviousPrimaryBuffer(buffer.getPrimary());
134: }
135: int index = indexOf(buffer);
136: if (index < 0)
137: return null;
138: while (true) {
139: if (index > 0)
140: --index;
141: else
142: index = size() - 1;
143: Buffer buf = (Buffer) list.get(index);
144: if (buf == buffer)
145: break;
146: if (buf.isPrimary())
147: return buf;
148: }
149: return null;
150: }
151:
152: public synchronized Buffer findBuffer(File f) {
153: if (f != null) {
154: for (int i = list.size(); i-- > 0;) {
155: Buffer buf = (Buffer) list.get(i);
156: if (buf instanceof WebBuffer)
157: continue;
158: if (f.equals(buf.getFile()))
159: return buf;
160: }
161: }
162: return null;
163: }
164:
165: public synchronized void makeNext(final Buffer nextBuffer,
166: final Buffer currentBuffer) {
167: if (!reorder)
168: return;
169: if (currentBuffer == null)
170: Log.debug("makeNext currentBuffer is null size = "
171: + list.size());
172: if (currentBuffer != null)
173: Debug.assertTrue(list.contains(currentBuffer));
174: Debug.assertTrue(nextBuffer != null);
175: Debug.assertTrue(list.contains(nextBuffer));
176: if (nextBuffer == currentBuffer)
177: return; // Nothing to do.
178: remove(nextBuffer);
179: try {
180: for (int i = 0; i < list.size(); i++) {
181: Buffer buf = (Buffer) list.get(i);
182: if (buf == currentBuffer) {
183: list.add(i + 1, nextBuffer);
184: return;
185: }
186: }
187: Debug.assertTrue(currentBuffer == null
188: || !list.contains(currentBuffer));
189: Debug.assertFalse(list.contains(nextBuffer));
190: list.add(nextBuffer);
191: } finally {
192: if (currentBuffer != null)
193: Debug.assertTrue(list.contains(currentBuffer));
194: Debug.assertTrue(list.contains(nextBuffer));
195: }
196: }
197:
198: // Replace o (old) with n (new).
199: public synchronized void replace(Buffer o, Buffer n) {
200: Debug.assertTrue(list.contains(o));
201: Debug.assertTrue(list.contains(n));
202: for (EditorIterator iter = new EditorIterator(); iter.hasNext();) {
203: Editor ed = iter.nextEditor();
204: if (ed.getBuffer() == o)
205: ed.activate(n);
206: ed.views.remove(o);
207: if (ed.getBuffer() == n)
208: ed.updateDisplay();
209: }
210: list.remove(n);
211: Debug.assertFalse(list.contains(n));
212: for (int i = list.size(); i-- > 0;) {
213: if (list.get(i) == o) {
214: list.set(i, n);
215: modified = true;
216: break;
217: }
218: }
219: Debug.assertTrue(list.contains(n));
220: Debug.assertFalse(list.contains(o));
221: Sidebar.setUpdateFlagInAllFrames(SIDEBAR_BUFFER_LIST_CHANGED);
222: }
223:
224: public synchronized final void modified() {
225: modified = true;
226: }
227:
228: public synchronized void preferencesChanged() {
229: Preferences p = Editor.preferences();
230: boolean b = p.getBooleanProperty(Property.SORT_BUFFER_LIST);
231: if (b != alpha) {
232: alpha = b;
233: if (alpha) {
234: sort();
235: Sidebar
236: .setUpdateFlagInAllFrames(SIDEBAR_BUFFER_LIST_CHANGED);
237: }
238: }
239: if (alpha)
240: reorder = false;
241: else
242: reorder = p.getIntegerProperty(Property.REORDER_BUFFERS) > 0;
243: }
244:
245: private static final String userHome = Platform.isPlatformUnix() ? Utilities
246: .getUserHome()
247: : null;
248:
249: public synchronized String getUniqueName(Buffer buf) {
250: final File file = buf.getFile();
251: final String name = file.getName();
252: boolean qualify = false;
253: Debug.assertTrue(file != null);
254: Debug.assertTrue(file.isLocal());
255: for (Iterator it = list.iterator(); it.hasNext();) {
256: Buffer b = (Buffer) it.next();
257: if (b == buf)
258: continue;
259: if (b.getFile() != null
260: && b.getFile().getName().equals(name)) {
261: qualify = true;
262: break;
263: }
264: }
265: if (qualify) {
266: FastStringBuffer sb = new FastStringBuffer(name);
267: sb.append(" [");
268: String dir = file.getParent();
269: if (userHome != null && userHome.length() > 0) {
270: if (dir.equals(userHome))
271: dir = "~";
272: if (dir.startsWith(userHome.concat("/")))
273: dir = "~".concat(dir.substring(userHome.length()));
274: }
275: sb.append(dir);
276: sb.append(']');
277: return sb.toString();
278: }
279: return name;
280: }
281:
282: public int indexOf(Buffer buf) {
283: for (int i = list.size(); i-- > 0;) {
284: if (list.get(i) == buf)
285: return i;
286: }
287: return -1;
288: }
289:
290: private static Comparator comparator;
291:
292: private void sort() {
293: if (alpha) {
294: if (comparator == null) {
295: comparator = new Comparator() {
296: public int compare(Object o1, Object o2) {
297: return o1.toString().compareToIgnoreCase(
298: o2.toString());
299: }
300: };
301: }
302: Collections.sort(list, comparator);
303: }
304: modified = false;
305: }
306: }
|