PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms 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
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  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. package org.gjt.sp.jedit.browser;
  23. import javax.swing.table.*;
  24. import javax.swing.*;
  25. import java.util.*;
  26. import org.gjt.sp.jedit.io.VFS;
  27. import org.gjt.sp.jedit.io.VFSManager;
  28. import org.gjt.sp.jedit.*;
  29. import org.gjt.sp.util.Log;
  30. /**
  31. * @author Slava Pestov
  32. * @version $Id: VFSDirectoryEntryTableModel.java 4740 2003-05-29 19:02:57Z spestov $
  33. * @since jEdit 4.2pre1
  34. */
  35. public class VFSDirectoryEntryTableModel extends AbstractTableModel
  36. {
  37. //{{{ VFSDirectoryEntryTableModel constructor
  38. public VFSDirectoryEntryTableModel()
  39. {
  40. extAttrs = new ArrayList();
  41. } //}}}
  42. //{{{ setRoot() method
  43. public void setRoot(VFS vfs, ArrayList list)
  44. {
  45. extAttrs.clear();
  46. addExtendedAttributes(vfs);
  47. /* if(files != null && files.length != 0)
  48. fireTableRowsDeleted(0,files.length - 1); */
  49. files = new Entry[list.size()];
  50. for(int i = 0; i < files.length; i++)
  51. {
  52. files[i] = new Entry((VFS.DirectoryEntry)list.get(i),0);
  53. }
  54. /* if(files.length != 0)
  55. fireTableRowsInserted(0,files.length - 1); */
  56. fireTableStructureChanged();
  57. } //}}}
  58. //{{{ expand() method
  59. public int expand(VFS vfs, Entry entry, ArrayList list)
  60. {
  61. int startIndex = -1;
  62. for(int i = 0; i < files.length; i++)
  63. {
  64. if(files[i] == entry)
  65. startIndex = i;
  66. }
  67. collapse(vfs,startIndex);
  68. addExtendedAttributes(vfs);
  69. entry.expanded = true;
  70. if(list != null)
  71. {
  72. Entry[] newFiles = new Entry[files.length + list.size()];
  73. System.arraycopy(files,0,newFiles,0,startIndex + 1);
  74. for(int i = 0; i < list.size(); i++)
  75. {
  76. newFiles[startIndex + i + 1] = new Entry(
  77. (VFS.DirectoryEntry)list.get(i),
  78. entry.level + 1);
  79. }
  80. System.arraycopy(files,startIndex + 1,
  81. newFiles,startIndex + list.size() + 1,
  82. files.length - startIndex - 1);
  83. this.files = newFiles;
  84. /* fireTableRowsInserted(startIndex + 1,
  85. startIndex + list.size() + 1); */
  86. }
  87. /* fireTableRowsUpdated(startIndex,startIndex); */
  88. fireTableStructureChanged();
  89. return startIndex;
  90. } //}}}
  91. //{{{ collapse() method
  92. public void collapse(VFS vfs, int index)
  93. {
  94. Entry entry = files[index];
  95. if(!entry.expanded)
  96. return;
  97. entry.expanded = false;
  98. int lastIndex = index + 1;
  99. while(lastIndex < files.length)
  100. {
  101. Entry e = files[lastIndex];
  102. if(e.expanded)
  103. {
  104. removeExtendedAttributes(VFSManager.getVFSForPath(
  105. e.dirEntry.path));
  106. }
  107. if(e.level <= entry.level)
  108. break;
  109. else
  110. lastIndex++;
  111. }
  112. removeExtendedAttributes(vfs);
  113. Entry[] newFiles = new Entry[files.length - lastIndex + index + 1];
  114. System.arraycopy(files,0,newFiles,0,index + 1);
  115. System.arraycopy(files,lastIndex,newFiles,index + 1,
  116. files.length - lastIndex);
  117. files = newFiles;
  118. /* fireTableRowsUpdated(index,index);
  119. fireTableRowsDeleted(index + 1,lastIndex); */
  120. fireTableStructureChanged();
  121. } //}}}
  122. //{{{ getColumnCount() method
  123. public int getColumnCount()
  124. {
  125. return 1 + extAttrs.size();
  126. } //}}}
  127. //{{{ getRowCount() method
  128. public int getRowCount()
  129. {
  130. if(files == null)
  131. return 0;
  132. else
  133. return files.length;
  134. } //}}}
  135. //{{{ getColumnName() method
  136. public String getColumnName(int col)
  137. {
  138. if(col == 0)
  139. return jEdit.getProperty("vfs.browser.name");
  140. else
  141. return jEdit.getProperty("vfs.browser." + getExtendedAttribute(col - 1));
  142. } //}}}
  143. //{{{ getColumnClass() method
  144. public Class getColumnClass(int col)
  145. {
  146. return Entry.class;
  147. } //}}}
  148. //{{{ getValueAt() method
  149. public Object getValueAt(int row, int col)
  150. {
  151. if(files == null)
  152. return null;
  153. else
  154. return files[row];
  155. } //}}}
  156. //{{{ getExtendedAttribute() method
  157. public String getExtendedAttribute(int index)
  158. {
  159. return ((ExtendedAttribute)extAttrs.get(index)).name;
  160. } //}}}
  161. //{{{ Package-private members
  162. Entry[] files;
  163. //}}}
  164. //{{{ Private members
  165. private List extAttrs;
  166. //{{{ addExtendedAttributes() method
  167. private void addExtendedAttributes(VFS vfs)
  168. {
  169. String[] attrs = vfs.getExtendedAttributes();
  170. vfs_attr_loop: for(int i = 0; i < attrs.length; i++)
  171. {
  172. Iterator iter = extAttrs.iterator();
  173. while(iter.hasNext())
  174. {
  175. ExtendedAttribute attr = (ExtendedAttribute)
  176. iter.next();
  177. if(attrs[i].equals(attr.name))
  178. {
  179. attr.ref++;
  180. continue vfs_attr_loop;
  181. }
  182. }
  183. // this vfs has an extended attribute which is not
  184. // in the list. add it to the end with a ref count
  185. // of 1
  186. extAttrs.add(new ExtendedAttribute(attrs[i]));
  187. }
  188. } //}}}
  189. //{{{ removeExtendedAttributes() method
  190. private void removeExtendedAttributes(VFS vfs)
  191. {
  192. String[] attrs = vfs.getExtendedAttributes();
  193. vfs_attr_loop: for(int i = 0; i < attrs.length; i++)
  194. {
  195. Iterator iter = extAttrs.iterator();
  196. while(iter.hasNext())
  197. {
  198. ExtendedAttribute attr = (ExtendedAttribute)
  199. iter.next();
  200. if(attrs[i].equals(attr.name))
  201. {
  202. if(--attr.ref == 0)
  203. {
  204. // we no longer have any
  205. // dirs using this extended
  206. // attribute
  207. iter.remove();
  208. }
  209. continue vfs_attr_loop;
  210. }
  211. }
  212. // this vfs has an extended attribute which is not
  213. // in the list ???
  214. Log.log(Log.WARNING,this,"We forgot about " + attrs[i]);
  215. }
  216. } //}}}
  217. //}}}
  218. //{{{ Entry class
  219. static class Entry
  220. {
  221. VFS.DirectoryEntry dirEntry;
  222. boolean expanded;
  223. // how deeply we are nested
  224. int level;
  225. Entry(VFS.DirectoryEntry dirEntry, int level)
  226. {
  227. this.dirEntry = dirEntry;
  228. this.level = level;
  229. }
  230. } //}}}
  231. //{{{ ExtendedAttribute class
  232. static class ExtendedAttribute
  233. {
  234. /* reference counter allows us to remove a column from
  235. * the table when no directory using this column is
  236. * visible */
  237. int ref;
  238. String name;
  239. ExtendedAttribute(String name)
  240. {
  241. this.name = name;
  242. ref = 1;
  243. }
  244. } //}}}
  245. }