PageRenderTime 149ms CodeModel.GetById 128ms app.highlight 17ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre4/org/gjt/sp/jedit/browser/VFSDirectoryEntryTableModel.java

#
Java | 290 lines | 180 code | 45 blank | 65 comment | 22 complexity | 3ac46c680f39ebac8a3fdbf37455387d MD5 | raw file
  1/*
  2 * VFSDirectoryEntryTableModel.java - VFS directory entry table model
  3 * :tabSize=8:indentSize=8:noTabs=false:
  4 * :folding=explicit:collapseFolds=1:
  5 *
  6 * Copyright (C) 2003 Slava Pestov
  7 *
  8 * This program is free software; you can redistribute it and/or
  9 * modify it under the terms of the GNU General Public License
 10 * as published by the Free Software Foundation; either version 2
 11 * of the License, or any later version.
 12 *
 13 * This program is distributed in the hope that it will be useful,
 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 16 * GNU General Public License for more details.
 17 *
 18 * You should have received a copy of the GNU General Public License
 19 * along with this program; if not, write to the Free Software
 20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 21 */
 22
 23package org.gjt.sp.jedit.browser;
 24
 25import javax.swing.table.*;
 26import javax.swing.*;
 27import java.util.*;
 28import org.gjt.sp.jedit.io.VFS;
 29import org.gjt.sp.jedit.io.VFSManager;
 30import org.gjt.sp.jedit.*;
 31import org.gjt.sp.util.Log;
 32
 33/**
 34 * @author Slava Pestov
 35 * @version $Id: VFSDirectoryEntryTableModel.java 4740 2003-05-29 19:02:57Z spestov $
 36 * @since jEdit 4.2pre1
 37 */
 38public class VFSDirectoryEntryTableModel extends AbstractTableModel
 39{
 40	//{{{ VFSDirectoryEntryTableModel constructor
 41	public VFSDirectoryEntryTableModel()
 42	{
 43		extAttrs = new ArrayList();
 44	} //}}}
 45
 46	//{{{ setRoot() method
 47	public void setRoot(VFS vfs, ArrayList list)
 48	{
 49		extAttrs.clear();
 50		addExtendedAttributes(vfs);
 51
 52		/* if(files != null && files.length != 0)
 53			fireTableRowsDeleted(0,files.length - 1); */
 54
 55		files = new Entry[list.size()];
 56		for(int i = 0; i < files.length; i++)
 57		{
 58			files[i] = new Entry((VFS.DirectoryEntry)list.get(i),0);
 59		}
 60
 61		/* if(files.length != 0)
 62			fireTableRowsInserted(0,files.length - 1); */
 63
 64		fireTableStructureChanged();
 65	} //}}}
 66
 67	//{{{ expand() method
 68	public int expand(VFS vfs, Entry entry, ArrayList list)
 69	{
 70		int startIndex = -1;
 71		for(int i = 0; i < files.length; i++)
 72		{
 73			if(files[i] == entry)
 74				startIndex = i;
 75		}
 76
 77		collapse(vfs,startIndex);
 78
 79		addExtendedAttributes(vfs);
 80		entry.expanded = true;
 81
 82		if(list != null)
 83		{
 84			Entry[] newFiles = new Entry[files.length + list.size()];
 85			System.arraycopy(files,0,newFiles,0,startIndex + 1);
 86			for(int i = 0; i < list.size(); i++)
 87			{
 88				newFiles[startIndex + i + 1] = new Entry(
 89					(VFS.DirectoryEntry)list.get(i),
 90					entry.level + 1);
 91			}
 92			System.arraycopy(files,startIndex + 1,
 93				newFiles,startIndex + list.size() + 1,
 94				files.length - startIndex - 1);
 95			this.files = newFiles;
 96
 97			/* fireTableRowsInserted(startIndex + 1,
 98				startIndex + list.size() + 1); */
 99		}
100
101		/* fireTableRowsUpdated(startIndex,startIndex); */
102
103		fireTableStructureChanged();
104
105		return startIndex;
106	} //}}}
107
108	//{{{ collapse() method
109	public void collapse(VFS vfs, int index)
110	{
111		Entry entry = files[index];
112		if(!entry.expanded)
113			return;
114
115		entry.expanded = false;
116
117		int lastIndex = index + 1;
118		while(lastIndex < files.length)
119		{
120			Entry e = files[lastIndex];
121
122			if(e.expanded)
123			{
124				removeExtendedAttributes(VFSManager.getVFSForPath(
125					e.dirEntry.path));
126			}
127
128			if(e.level <= entry.level)
129				break;
130			else
131				lastIndex++;
132		}
133
134		removeExtendedAttributes(vfs);
135
136		Entry[] newFiles = new Entry[files.length - lastIndex + index + 1];
137		System.arraycopy(files,0,newFiles,0,index + 1);
138		System.arraycopy(files,lastIndex,newFiles,index + 1,
139			files.length - lastIndex);
140
141		files = newFiles;
142
143		/* fireTableRowsUpdated(index,index);
144		fireTableRowsDeleted(index + 1,lastIndex); */
145
146		fireTableStructureChanged();
147	} //}}}
148
149	//{{{ getColumnCount() method
150	public int getColumnCount()
151	{
152		return 1 + extAttrs.size();
153	} //}}}
154
155	//{{{ getRowCount() method
156	public int getRowCount()
157	{
158		if(files == null)
159			return 0;
160		else
161			return files.length;
162	} //}}}
163
164	//{{{ getColumnName() method
165	public String getColumnName(int col)
166	{
167		if(col == 0)
168			return jEdit.getProperty("vfs.browser.name");
169		else
170			return jEdit.getProperty("vfs.browser." + getExtendedAttribute(col - 1));
171	} //}}}
172
173	//{{{ getColumnClass() method
174	public Class getColumnClass(int col)
175	{
176		return Entry.class;
177	} //}}}
178
179	//{{{ getValueAt() method
180	public Object getValueAt(int row, int col)
181	{
182		if(files == null)
183			return null;
184		else
185			return files[row];
186	} //}}}
187
188	//{{{ getExtendedAttribute() method
189	public String getExtendedAttribute(int index)
190	{
191		return ((ExtendedAttribute)extAttrs.get(index)).name;
192	} //}}}
193
194	//{{{ Package-private members
195	Entry[] files;
196	//}}}
197
198	//{{{ Private members
199	private List extAttrs;
200
201	//{{{ addExtendedAttributes() method
202	private void addExtendedAttributes(VFS vfs)
203	{
204		String[] attrs = vfs.getExtendedAttributes();
205vfs_attr_loop:	for(int i = 0; i < attrs.length; i++)
206		{
207			Iterator iter = extAttrs.iterator();
208			while(iter.hasNext())
209			{
210				ExtendedAttribute attr = (ExtendedAttribute)
211					iter.next();
212				if(attrs[i].equals(attr.name))
213				{
214					attr.ref++;
215					continue vfs_attr_loop;
216				}
217			}
218
219			// this vfs has an extended attribute which is not
220			// in the list. add it to the end with a ref count
221			// of 1
222			extAttrs.add(new ExtendedAttribute(attrs[i]));
223		}
224	} //}}}
225
226	//{{{ removeExtendedAttributes() method
227	private void removeExtendedAttributes(VFS vfs)
228	{
229		String[] attrs = vfs.getExtendedAttributes();
230vfs_attr_loop:	for(int i = 0; i < attrs.length; i++)
231		{
232			Iterator iter = extAttrs.iterator();
233			while(iter.hasNext())
234			{
235				ExtendedAttribute attr = (ExtendedAttribute)
236					iter.next();
237				if(attrs[i].equals(attr.name))
238				{
239					if(--attr.ref == 0)
240					{
241						// we no longer have any
242						// dirs using this extended
243						// attribute
244						iter.remove();
245					}
246
247					continue vfs_attr_loop;
248				}
249			}
250
251			// this vfs has an extended attribute which is not
252			// in the list ???
253			Log.log(Log.WARNING,this,"We forgot about " + attrs[i]);
254		}
255	} //}}}
256
257	//}}}
258
259	//{{{ Entry class
260	static class Entry
261	{
262		VFS.DirectoryEntry dirEntry;
263		boolean expanded;
264		// how deeply we are nested
265		int level;
266
267		Entry(VFS.DirectoryEntry dirEntry, int level)
268		{
269			this.dirEntry = dirEntry;
270			this.level = level;
271		}
272	} //}}}
273
274	//{{{ ExtendedAttribute class
275	static class ExtendedAttribute
276	{
277		/* reference counter allows us to remove a column from
278		 * the table when no directory using this column is
279		 * visible */
280		int ref;
281
282		String name;
283
284		ExtendedAttribute(String name)
285		{
286			this.name = name;
287			ref = 1;
288		}
289	} //}}}
290}