Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

FilesystemContainer.java 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.data.util;
  5. import java.io.File;
  6. import java.io.FilenameFilter;
  7. import java.io.IOException;
  8. import java.io.Serializable;
  9. import java.lang.reflect.Method;
  10. import java.util.ArrayList;
  11. import java.util.Arrays;
  12. import java.util.Collection;
  13. import java.util.Collections;
  14. import java.util.Date;
  15. import java.util.Iterator;
  16. import java.util.LinkedList;
  17. import java.util.List;
  18. import com.vaadin.data.Container;
  19. import com.vaadin.data.Item;
  20. import com.vaadin.data.Property;
  21. import com.vaadin.service.FileTypeResolver;
  22. import com.vaadin.terminal.Resource;
  23. /**
  24. * A hierarchical container wrapper for a filesystem.
  25. *
  26. * @author Vaadin Ltd.
  27. * @since 3.0
  28. */
  29. @SuppressWarnings("serial")
  30. public class FilesystemContainer implements Container.Hierarchical {
  31. /**
  32. * String identifier of a file's "name" property.
  33. */
  34. public static String PROPERTY_NAME = "Name";
  35. /**
  36. * String identifier of a file's "size" property.
  37. */
  38. public static String PROPERTY_SIZE = "Size";
  39. /**
  40. * String identifier of a file's "icon" property.
  41. */
  42. public static String PROPERTY_ICON = "Icon";
  43. /**
  44. * String identifier of a file's "last modified" property.
  45. */
  46. public static String PROPERTY_LASTMODIFIED = "Last Modified";
  47. /**
  48. * List of the string identifiers for the available properties.
  49. */
  50. public static Collection<String> FILE_PROPERTIES;
  51. private final static Method FILEITEM_LASTMODIFIED;
  52. private final static Method FILEITEM_NAME;
  53. private final static Method FILEITEM_ICON;
  54. private final static Method FILEITEM_SIZE;
  55. static {
  56. FILE_PROPERTIES = new ArrayList<String>();
  57. FILE_PROPERTIES.add(PROPERTY_NAME);
  58. FILE_PROPERTIES.add(PROPERTY_ICON);
  59. FILE_PROPERTIES.add(PROPERTY_SIZE);
  60. FILE_PROPERTIES.add(PROPERTY_LASTMODIFIED);
  61. FILE_PROPERTIES = Collections.unmodifiableCollection(FILE_PROPERTIES);
  62. try {
  63. FILEITEM_LASTMODIFIED = FileItem.class.getMethod("lastModified",
  64. new Class[] {});
  65. FILEITEM_NAME = FileItem.class.getMethod("getName", new Class[] {});
  66. FILEITEM_ICON = FileItem.class.getMethod("getIcon", new Class[] {});
  67. FILEITEM_SIZE = FileItem.class.getMethod("getSize", new Class[] {});
  68. } catch (final NoSuchMethodException e) {
  69. throw new RuntimeException(
  70. "Internal error finding methods in FilesystemContainer");
  71. }
  72. }
  73. private File[] roots = new File[] {};
  74. private FilenameFilter filter = null;
  75. private boolean recursive = true;
  76. /**
  77. * Constructs a new <code>FileSystemContainer</code> with the specified file
  78. * as the root of the filesystem. The files are included recursively.
  79. *
  80. * @param root
  81. * the root file for the new file-system container. Null values
  82. * are ignored.
  83. */
  84. public FilesystemContainer(File root) {
  85. if (root != null) {
  86. roots = new File[] { root };
  87. }
  88. }
  89. /**
  90. * Constructs a new <code>FileSystemContainer</code> with the specified file
  91. * as the root of the filesystem. The files are included recursively.
  92. *
  93. * @param root
  94. * the root file for the new file-system container.
  95. * @param recursive
  96. * should the container recursively contain subdirectories.
  97. */
  98. public FilesystemContainer(File root, boolean recursive) {
  99. this(root);
  100. setRecursive(recursive);
  101. }
  102. /**
  103. * Constructs a new <code>FileSystemContainer</code> with the specified file
  104. * as the root of the filesystem.
  105. *
  106. * @param root
  107. * the root file for the new file-system container.
  108. * @param extension
  109. * the Filename extension (w/o separator) to limit the files in
  110. * container.
  111. * @param recursive
  112. * should the container recursively contain subdirectories.
  113. */
  114. public FilesystemContainer(File root, String extension, boolean recursive) {
  115. this(root);
  116. this.setFilter(extension);
  117. setRecursive(recursive);
  118. }
  119. /**
  120. * Constructs a new <code>FileSystemContainer</code> with the specified root
  121. * and recursivity status.
  122. *
  123. * @param root
  124. * the root file for the new file-system container.
  125. * @param filter
  126. * the Filename filter to limit the files in container.
  127. * @param recursive
  128. * should the container recursively contain subdirectories.
  129. */
  130. public FilesystemContainer(File root, FilenameFilter filter,
  131. boolean recursive) {
  132. this(root);
  133. this.setFilter(filter);
  134. setRecursive(recursive);
  135. }
  136. /**
  137. * Adds new root file directory. Adds a file to be included as root file
  138. * directory in the <code>FilesystemContainer</code>.
  139. *
  140. * @param root
  141. * the File to be added as root directory. Null values are
  142. * ignored.
  143. */
  144. public void addRoot(File root) {
  145. if (root != null) {
  146. final File[] newRoots = new File[roots.length + 1];
  147. for (int i = 0; i < roots.length; i++) {
  148. newRoots[i] = roots[i];
  149. }
  150. newRoots[roots.length] = root;
  151. roots = newRoots;
  152. }
  153. }
  154. /**
  155. * Tests if the specified Item in the container may have children. Since a
  156. * <code>FileSystemContainer</code> contains files and directories, this
  157. * method returns <code>true</code> for directory Items only.
  158. *
  159. * @param itemId
  160. * the id of the item.
  161. * @return <code>true</code> if the specified Item is a directory,
  162. * <code>false</code> otherwise.
  163. */
  164. @Override
  165. public boolean areChildrenAllowed(Object itemId) {
  166. return itemId instanceof File && ((File) itemId).canRead()
  167. && ((File) itemId).isDirectory();
  168. }
  169. /*
  170. * Gets the ID's of all Items who are children of the specified Item. Don't
  171. * add a JavaDoc comment here, we use the default documentation from
  172. * implemented interface.
  173. */
  174. @Override
  175. public Collection<File> getChildren(Object itemId) {
  176. if (!(itemId instanceof File)) {
  177. return Collections.unmodifiableCollection(new LinkedList<File>());
  178. }
  179. File[] f;
  180. if (filter != null) {
  181. f = ((File) itemId).listFiles(filter);
  182. } else {
  183. f = ((File) itemId).listFiles();
  184. }
  185. if (f == null) {
  186. return Collections.unmodifiableCollection(new LinkedList<File>());
  187. }
  188. final List<File> l = Arrays.asList(f);
  189. Collections.sort(l);
  190. return Collections.unmodifiableCollection(l);
  191. }
  192. /*
  193. * Gets the parent item of the specified Item. Don't add a JavaDoc comment
  194. * here, we use the default documentation from implemented interface.
  195. */
  196. @Override
  197. public Object getParent(Object itemId) {
  198. if (!(itemId instanceof File)) {
  199. return null;
  200. }
  201. return ((File) itemId).getParentFile();
  202. }
  203. /*
  204. * Tests if the specified Item has any children. Don't add a JavaDoc comment
  205. * here, we use the default documentation from implemented interface.
  206. */
  207. @Override
  208. public boolean hasChildren(Object itemId) {
  209. if (!(itemId instanceof File)) {
  210. return false;
  211. }
  212. String[] l;
  213. if (filter != null) {
  214. l = ((File) itemId).list(filter);
  215. } else {
  216. l = ((File) itemId).list();
  217. }
  218. return (l != null) && (l.length > 0);
  219. }
  220. /*
  221. * Tests if the specified Item is the root of the filesystem. Don't add a
  222. * JavaDoc comment here, we use the default documentation from implemented
  223. * interface.
  224. */
  225. @Override
  226. public boolean isRoot(Object itemId) {
  227. if (!(itemId instanceof File)) {
  228. return false;
  229. }
  230. for (int i = 0; i < roots.length; i++) {
  231. if (roots[i].equals(itemId)) {
  232. return true;
  233. }
  234. }
  235. return false;
  236. }
  237. /*
  238. * Gets the ID's of all root Items in the container. Don't add a JavaDoc
  239. * comment here, we use the default documentation from implemented
  240. * interface.
  241. */
  242. @Override
  243. public Collection<File> rootItemIds() {
  244. File[] f;
  245. // in single root case we use children
  246. if (roots.length == 1) {
  247. if (filter != null) {
  248. f = roots[0].listFiles(filter);
  249. } else {
  250. f = roots[0].listFiles();
  251. }
  252. } else {
  253. f = roots;
  254. }
  255. if (f == null) {
  256. return Collections.unmodifiableCollection(new LinkedList<File>());
  257. }
  258. final List<File> l = Arrays.asList(f);
  259. Collections.sort(l);
  260. return Collections.unmodifiableCollection(l);
  261. }
  262. /**
  263. * Returns <code>false</code> when conversion from files to directories is
  264. * not supported.
  265. *
  266. * @param itemId
  267. * the ID of the item.
  268. * @param areChildrenAllowed
  269. * the boolean value specifying if the Item can have children or
  270. * not.
  271. * @return <code>true</code> if the operaton is successful otherwise
  272. * <code>false</code>.
  273. * @throws UnsupportedOperationException
  274. * if the setChildrenAllowed is not supported.
  275. */
  276. @Override
  277. public boolean setChildrenAllowed(Object itemId, boolean areChildrenAllowed)
  278. throws UnsupportedOperationException {
  279. throw new UnsupportedOperationException(
  280. "Conversion file to/from directory is not supported");
  281. }
  282. /**
  283. * Returns <code>false</code> when moving files around in the filesystem is
  284. * not supported.
  285. *
  286. * @param itemId
  287. * the ID of the item.
  288. * @param newParentId
  289. * the ID of the Item that's to be the new parent of the Item
  290. * identified with itemId.
  291. * @return <code>true</code> if the operation is successful otherwise
  292. * <code>false</code>.
  293. * @throws UnsupportedOperationException
  294. * if the setParent is not supported.
  295. */
  296. @Override
  297. public boolean setParent(Object itemId, Object newParentId)
  298. throws UnsupportedOperationException {
  299. throw new UnsupportedOperationException("File moving is not supported");
  300. }
  301. /*
  302. * Tests if the filesystem contains the specified Item. Don't add a JavaDoc
  303. * comment here, we use the default documentation from implemented
  304. * interface.
  305. */
  306. @Override
  307. public boolean containsId(Object itemId) {
  308. if (!(itemId instanceof File)) {
  309. return false;
  310. }
  311. boolean val = false;
  312. // Try to match all roots
  313. for (int i = 0; i < roots.length; i++) {
  314. try {
  315. val |= ((File) itemId).getCanonicalPath().startsWith(
  316. roots[i].getCanonicalPath());
  317. } catch (final IOException e) {
  318. // Exception ignored
  319. }
  320. }
  321. if (val && filter != null) {
  322. val &= filter.accept(((File) itemId).getParentFile(),
  323. ((File) itemId).getName());
  324. }
  325. return val;
  326. }
  327. /*
  328. * Gets the specified Item from the filesystem. Don't add a JavaDoc comment
  329. * here, we use the default documentation from implemented interface.
  330. */
  331. @Override
  332. public Item getItem(Object itemId) {
  333. if (!(itemId instanceof File)) {
  334. return null;
  335. }
  336. return new FileItem((File) itemId);
  337. }
  338. /**
  339. * Internal recursive method to add the files under the specified directory
  340. * to the collection.
  341. *
  342. * @param col
  343. * the collection where the found items are added
  344. * @param f
  345. * the root file where to start adding files
  346. */
  347. private void addItemIds(Collection<File> col, File f) {
  348. File[] l;
  349. if (filter != null) {
  350. l = f.listFiles(filter);
  351. } else {
  352. l = f.listFiles();
  353. }
  354. if (l == null) {
  355. // File.listFiles returns null if File does not exist or if there
  356. // was an IO error (permission denied)
  357. return;
  358. }
  359. final List<File> ll = Arrays.asList(l);
  360. Collections.sort(ll);
  361. for (final Iterator<File> i = ll.iterator(); i.hasNext();) {
  362. final File lf = i.next();
  363. col.add(lf);
  364. if (lf.isDirectory()) {
  365. addItemIds(col, lf);
  366. }
  367. }
  368. }
  369. /*
  370. * Gets the IDs of Items in the filesystem. Don't add a JavaDoc comment
  371. * here, we use the default documentation from implemented interface.
  372. */
  373. @Override
  374. public Collection<File> getItemIds() {
  375. if (recursive) {
  376. final Collection<File> col = new ArrayList<File>();
  377. for (int i = 0; i < roots.length; i++) {
  378. addItemIds(col, roots[i]);
  379. }
  380. return Collections.unmodifiableCollection(col);
  381. } else {
  382. File[] f;
  383. if (roots.length == 1) {
  384. if (filter != null) {
  385. f = roots[0].listFiles(filter);
  386. } else {
  387. f = roots[0].listFiles();
  388. }
  389. } else {
  390. f = roots;
  391. }
  392. if (f == null) {
  393. return Collections
  394. .unmodifiableCollection(new LinkedList<File>());
  395. }
  396. final List<File> l = Arrays.asList(f);
  397. Collections.sort(l);
  398. return Collections.unmodifiableCollection(l);
  399. }
  400. }
  401. /**
  402. * Gets the specified property of the specified file Item. The available
  403. * file properties are "Name", "Size" and "Last Modified". If propertyId is
  404. * not one of those, <code>null</code> is returned.
  405. *
  406. * @param itemId
  407. * the ID of the file whose property is requested.
  408. * @param propertyId
  409. * the property's ID.
  410. * @return the requested property's value, or <code>null</code>
  411. */
  412. @Override
  413. public Property<?> getContainerProperty(Object itemId, Object propertyId) {
  414. if (!(itemId instanceof File)) {
  415. return null;
  416. }
  417. if (propertyId.equals(PROPERTY_NAME)) {
  418. return new MethodProperty<Object>(getType(propertyId),
  419. new FileItem((File) itemId), FILEITEM_NAME, null);
  420. }
  421. if (propertyId.equals(PROPERTY_ICON)) {
  422. return new MethodProperty<Object>(getType(propertyId),
  423. new FileItem((File) itemId), FILEITEM_ICON, null);
  424. }
  425. if (propertyId.equals(PROPERTY_SIZE)) {
  426. return new MethodProperty<Object>(getType(propertyId),
  427. new FileItem((File) itemId), FILEITEM_SIZE, null);
  428. }
  429. if (propertyId.equals(PROPERTY_LASTMODIFIED)) {
  430. return new MethodProperty<Object>(getType(propertyId),
  431. new FileItem((File) itemId), FILEITEM_LASTMODIFIED, null);
  432. }
  433. return null;
  434. }
  435. /**
  436. * Gets the collection of available file properties.
  437. *
  438. * @return Unmodifiable collection containing all available file properties.
  439. */
  440. @Override
  441. public Collection<String> getContainerPropertyIds() {
  442. return FILE_PROPERTIES;
  443. }
  444. /**
  445. * Gets the specified property's data type. "Name" is a <code>String</code>,
  446. * "Size" is a <code>Long</code>, "Last Modified" is a <code>Date</code>. If
  447. * propertyId is not one of those, <code>null</code> is returned.
  448. *
  449. * @param propertyId
  450. * the ID of the property whose type is requested.
  451. * @return data type of the requested property, or <code>null</code>
  452. */
  453. @Override
  454. public Class<?> getType(Object propertyId) {
  455. if (propertyId.equals(PROPERTY_NAME)) {
  456. return String.class;
  457. }
  458. if (propertyId.equals(PROPERTY_ICON)) {
  459. return Resource.class;
  460. }
  461. if (propertyId.equals(PROPERTY_SIZE)) {
  462. return Long.class;
  463. }
  464. if (propertyId.equals(PROPERTY_LASTMODIFIED)) {
  465. return Date.class;
  466. }
  467. return null;
  468. }
  469. /**
  470. * Internal method to recursively calculate the number of files under a root
  471. * directory.
  472. *
  473. * @param f
  474. * the root to start counting from.
  475. */
  476. private int getFileCounts(File f) {
  477. File[] l;
  478. if (filter != null) {
  479. l = f.listFiles(filter);
  480. } else {
  481. l = f.listFiles();
  482. }
  483. if (l == null) {
  484. return 0;
  485. }
  486. int ret = l.length;
  487. for (int i = 0; i < l.length; i++) {
  488. if (l[i].isDirectory()) {
  489. ret += getFileCounts(l[i]);
  490. }
  491. }
  492. return ret;
  493. }
  494. /**
  495. * Gets the number of Items in the container. In effect, this is the
  496. * combined amount of files and directories.
  497. *
  498. * @return Number of Items in the container.
  499. */
  500. @Override
  501. public int size() {
  502. if (recursive) {
  503. int counts = 0;
  504. for (int i = 0; i < roots.length; i++) {
  505. counts += getFileCounts(roots[i]);
  506. }
  507. return counts;
  508. } else {
  509. File[] f;
  510. if (roots.length == 1) {
  511. if (filter != null) {
  512. f = roots[0].listFiles(filter);
  513. } else {
  514. f = roots[0].listFiles();
  515. }
  516. } else {
  517. f = roots;
  518. }
  519. if (f == null) {
  520. return 0;
  521. }
  522. return f.length;
  523. }
  524. }
  525. /**
  526. * A Item wrapper for files in a filesystem.
  527. *
  528. * @author Vaadin Ltd.
  529. * @version
  530. * @VERSION@
  531. * @since 3.0
  532. */
  533. public class FileItem implements Item {
  534. /**
  535. * The wrapped file.
  536. */
  537. private final File file;
  538. /**
  539. * Constructs a FileItem from a existing file.
  540. */
  541. private FileItem(File file) {
  542. this.file = file;
  543. }
  544. /*
  545. * Gets the specified property of this file. Don't add a JavaDoc comment
  546. * here, we use the default documentation from implemented interface.
  547. */
  548. @Override
  549. public Property<?> getItemProperty(Object id) {
  550. return getContainerProperty(file, id);
  551. }
  552. /*
  553. * Gets the IDs of all properties available for this item Don't add a
  554. * JavaDoc comment here, we use the default documentation from
  555. * implemented interface.
  556. */
  557. @Override
  558. public Collection<String> getItemPropertyIds() {
  559. return getContainerPropertyIds();
  560. }
  561. /**
  562. * Calculates a integer hash-code for the Property that's unique inside
  563. * the Item containing the Property. Two different Properties inside the
  564. * same Item contained in the same list always have different
  565. * hash-codes, though Properties in different Items may have identical
  566. * hash-codes.
  567. *
  568. * @return A locally unique hash-code as integer
  569. */
  570. @Override
  571. public int hashCode() {
  572. return file.hashCode() ^ FilesystemContainer.this.hashCode();
  573. }
  574. /**
  575. * Tests if the given object is the same as the this object. Two
  576. * Properties got from an Item with the same ID are equal.
  577. *
  578. * @param obj
  579. * an object to compare with this object.
  580. * @return <code>true</code> if the given object is the same as this
  581. * object, <code>false</code> if not
  582. */
  583. @Override
  584. public boolean equals(Object obj) {
  585. if (obj == null || !(obj instanceof FileItem)) {
  586. return false;
  587. }
  588. final FileItem fi = (FileItem) obj;
  589. return fi.getHost() == getHost() && fi.file.equals(file);
  590. }
  591. /**
  592. * Gets the host of this file.
  593. */
  594. private FilesystemContainer getHost() {
  595. return FilesystemContainer.this;
  596. }
  597. /**
  598. * Gets the last modified date of this file.
  599. *
  600. * @return Date
  601. */
  602. public Date lastModified() {
  603. return new Date(file.lastModified());
  604. }
  605. /**
  606. * Gets the name of this file.
  607. *
  608. * @return file name of this file.
  609. */
  610. public String getName() {
  611. return file.getName();
  612. }
  613. /**
  614. * Gets the icon of this file.
  615. *
  616. * @return the icon of this file.
  617. */
  618. public Resource getIcon() {
  619. return FileTypeResolver.getIcon(file);
  620. }
  621. /**
  622. * Gets the size of this file.
  623. *
  624. * @return size
  625. */
  626. public long getSize() {
  627. if (file.isDirectory()) {
  628. return 0;
  629. }
  630. return file.length();
  631. }
  632. /**
  633. * @see java.lang.Object#toString()
  634. */
  635. @Override
  636. public String toString() {
  637. if ("".equals(file.getName())) {
  638. return file.getAbsolutePath();
  639. }
  640. return file.getName();
  641. }
  642. /**
  643. * Filesystem container does not support adding new properties.
  644. *
  645. * @see com.vaadin.data.Item#addItemProperty(Object, Property)
  646. */
  647. @Override
  648. public boolean addItemProperty(Object id, Property property)
  649. throws UnsupportedOperationException {
  650. throw new UnsupportedOperationException("Filesystem container "
  651. + "does not support adding new properties");
  652. }
  653. /**
  654. * Filesystem container does not support removing properties.
  655. *
  656. * @see com.vaadin.data.Item#removeItemProperty(Object)
  657. */
  658. @Override
  659. public boolean removeItemProperty(Object id)
  660. throws UnsupportedOperationException {
  661. throw new UnsupportedOperationException(
  662. "Filesystem container does not support property removal");
  663. }
  664. }
  665. /**
  666. * Generic file extension filter for displaying only files having certain
  667. * extension.
  668. *
  669. * @author Vaadin Ltd.
  670. * @version
  671. * @VERSION@
  672. * @since 3.0
  673. */
  674. public class FileExtensionFilter implements FilenameFilter, Serializable {
  675. private final String filter;
  676. /**
  677. * Constructs a new FileExtensionFilter using given extension.
  678. *
  679. * @param fileExtension
  680. * the File extension without the separator (dot).
  681. */
  682. public FileExtensionFilter(String fileExtension) {
  683. filter = "." + fileExtension;
  684. }
  685. /**
  686. * Allows only files with the extension and directories.
  687. *
  688. * @see java.io.FilenameFilter#accept(File, String)
  689. */
  690. @Override
  691. public boolean accept(File dir, String name) {
  692. if (name.endsWith(filter)) {
  693. return true;
  694. }
  695. return new File(dir, name).isDirectory();
  696. }
  697. }
  698. /**
  699. * Returns the file filter used to limit the files in this container.
  700. *
  701. * @return Used filter instance or null if no filter is assigned.
  702. */
  703. public FilenameFilter getFilter() {
  704. return filter;
  705. }
  706. /**
  707. * Sets the file filter used to limit the files in this container.
  708. *
  709. * @param filter
  710. * The filter to set. <code>null</code> disables filtering.
  711. */
  712. public void setFilter(FilenameFilter filter) {
  713. this.filter = filter;
  714. }
  715. /**
  716. * Sets the file filter used to limit the files in this container.
  717. *
  718. * @param extension
  719. * the Filename extension (w/o separator) to limit the files in
  720. * container.
  721. */
  722. public void setFilter(String extension) {
  723. filter = new FileExtensionFilter(extension);
  724. }
  725. /**
  726. * Is this container recursive filesystem.
  727. *
  728. * @return <code>true</code> if container is recursive, <code>false</code>
  729. * otherwise.
  730. */
  731. public boolean isRecursive() {
  732. return recursive;
  733. }
  734. /**
  735. * Sets the container recursive property. Set this to false to limit the
  736. * files directly under the root file.
  737. * <p>
  738. * Note : This is meaningful only if the root really is a directory.
  739. * </p>
  740. *
  741. * @param recursive
  742. * the New value for recursive property.
  743. */
  744. public void setRecursive(boolean recursive) {
  745. this.recursive = recursive;
  746. }
  747. /*
  748. * (non-Javadoc)
  749. *
  750. * @see com.vaadin.data.Container#addContainerProperty(java.lang.Object,
  751. * java.lang.Class, java.lang.Object)
  752. */
  753. @Override
  754. public boolean addContainerProperty(Object propertyId, Class<?> type,
  755. Object defaultValue) throws UnsupportedOperationException {
  756. throw new UnsupportedOperationException(
  757. "File system container does not support this operation");
  758. }
  759. /*
  760. * (non-Javadoc)
  761. *
  762. * @see com.vaadin.data.Container#addItem()
  763. */
  764. @Override
  765. public Object addItem() throws UnsupportedOperationException {
  766. throw new UnsupportedOperationException(
  767. "File system container does not support this operation");
  768. }
  769. /*
  770. * (non-Javadoc)
  771. *
  772. * @see com.vaadin.data.Container#addItem(java.lang.Object)
  773. */
  774. @Override
  775. public Item addItem(Object itemId) throws UnsupportedOperationException {
  776. throw new UnsupportedOperationException(
  777. "File system container does not support this operation");
  778. }
  779. /*
  780. * (non-Javadoc)
  781. *
  782. * @see com.vaadin.data.Container#removeAllItems()
  783. */
  784. @Override
  785. public boolean removeAllItems() throws UnsupportedOperationException {
  786. throw new UnsupportedOperationException(
  787. "File system container does not support this operation");
  788. }
  789. /*
  790. * (non-Javadoc)
  791. *
  792. * @see com.vaadin.data.Container#removeItem(java.lang.Object)
  793. */
  794. @Override
  795. public boolean removeItem(Object itemId)
  796. throws UnsupportedOperationException {
  797. throw new UnsupportedOperationException(
  798. "File system container does not support this operation");
  799. }
  800. /*
  801. * (non-Javadoc)
  802. *
  803. * @see com.vaadin.data.Container#removeContainerProperty(java.lang.Object )
  804. */
  805. @Override
  806. public boolean removeContainerProperty(Object propertyId)
  807. throws UnsupportedOperationException {
  808. throw new UnsupportedOperationException(
  809. "File system container does not support this operation");
  810. }
  811. }