001 /*
002 * This file is part of muCommander, http://www.mucommander.com
003 * Copyright (C) 2002-2008 Maxence Bernard
004 *
005 * muCommander is free software; you can redistribute it and/or modify
006 * it under the terms of the GNU General Public License as published by
007 * the Free Software Foundation; either version 3 of the License, or
008 * (at your option) any later version.
009 *
010 * muCommander is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013 * GNU General Public License for more details.
014 *
015 * You should have received a copy of the GNU General Public License
016 * along with this program. If not, see <http://www.gnu.org/licenses/>.
017 */
018
019 package com.mucommander.file.filter;
020
021 import com.mucommander.file.AbstractArchiveFile;
022 import com.mucommander.file.AbstractFile;
023
024 /**
025 * <code>AttributeFileFilter</code> matches files which have a specific attribute set.
026 * Here's a list of supported file attributes:
027 * <ul>
028 * <li>{@link #DIRECTORY}</li>
029 * <li>{@link #FILE}</li>
030 * <li>{@link #BROWSABLE}</li>
031 * <li>{@link #ARCHIVE}</li>
032 * <li>{@link #SYMLINK}</li>
033 * <li>{@link #HIDDEN}</li>
034 * <li>{@link #ROOT}</li>
035 * </ul>
036 *
037 * <p>Only one attribute can be matched at a time. To match several attributes, combine them using a
038 * {@link com.mucommander.file.filter.ChainedFileFilter}.</p>
039 *
040 * @author Maxence Bernard
041 */
042 public class AttributeFileFilter extends FileFilter {
043
044 /** Tests if the file is a directory, cf {@link com.mucommander.file.AbstractFile#isDirectory()}. */
045 public static final int DIRECTORY = 0;
046
047 /** Tests if the file is a regular file, i.e. not a directory. Note that this is equivalent to inverting {@link #DIRECTORY}. */
048 public static final int FILE = 1;
049
050 /** Tests if the file is browsable, cf {@link com.mucommander.file.AbstractFile#isBrowsable()}. */
051 public static final int BROWSABLE = 2;
052
053 /** Tests if the file is an archive. */
054 public static final int ARCHIVE = 3;
055
056 /** Tests if the file is a symlink, cf {@link com.mucommander.file.AbstractFile#isSymlink()}. */
057 public static final int SYMLINK = 4;
058
059 /** Tests if the file is hidden, cf {@link com.mucommander.file.AbstractFile#isHidden()}. */
060 public static final int HIDDEN = 5;
061
062 /** Tests if the file is a root folder, cf {@link com.mucommander.file.AbstractFile#isRoot()}. */
063 public static final int ROOT = 6;
064
065
066 /** The attribute to test files against */
067 private int attribute;
068
069
070 /**
071 * Creates a new <code>AttributeFileFilter</code> that matches files which have the specified attribute set.
072 * This filter will operate in normal, non-inverted mode.
073 *
074 * @param attribute the attribute to test files against
075 */
076 public AttributeFileFilter(int attribute) {
077 this(attribute, false);
078 }
079
080 /**
081 * Creates a new <code>AttributeFileFilter</code> that matches files which have the specified attribute set.
082 * This filter operates in the specified mode.
083 *
084 * @param attribute the attribute to test files against
085 * @param inverted if true, this filter will operate in inverted mode.
086 */
087 public AttributeFileFilter(int attribute, boolean inverted) {
088 super(inverted);
089 this.attribute = attribute;
090 }
091
092
093 /**
094 * Returns the attribute which files are tested against.
095 *
096 * @return the attribute which files are tested against.
097 */
098 public int getAttribute() {
099 return attribute;
100 }
101
102 /**
103 * Sets the attribute which files are tested against.
104 *
105 * @param attribute the attribute which files are tested against.
106 */
107 public void setAttribute(int attribute) {
108 this.attribute = attribute;
109 }
110
111
112 ///////////////////////////////
113 // FileFilter implementation //
114 ///////////////////////////////
115
116 public boolean accept(AbstractFile file) {
117 switch(attribute) {
118 case DIRECTORY:
119 return file.isDirectory();
120 case FILE:
121 return !file.isDirectory();
122 case BROWSABLE:
123 return file.isBrowsable();
124 case ARCHIVE:
125 return file.hasAncestor(AbstractArchiveFile.class);
126 case SYMLINK:
127 return file.isSymlink();
128 case HIDDEN:
129 return file.isHidden();
130 case ROOT:
131 return file.isRoot();
132 }
133 return true;
134 }
135 }
136