You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FilesystemContainer.java 27KB

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