001: // THIS SOFTWARE IS PROVIDED BY SOFTARIS PTY.LTD. AND OTHER METABOSS
002: // CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
003: // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
004: // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTARIS PTY.LTD.
005: // OR OTHER METABOSS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
006: // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
007: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
008: // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
009: // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
010: // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
011: // EVEN IF SOFTARIS PTY.LTD. OR OTHER METABOSS CONTRIBUTORS ARE ADVISED OF THE
012: // POSSIBILITY OF SUCH DAMAGE.
013: //
014: // Copyright 2000-2005 © Softaris Pty.Ltd. All Rights Reserved.
015: package com.metaboss.applications.designstudio.components;
016:
017: import java.io.File;
018: import java.util.Enumeration;
019: import java.util.Hashtable;
020:
021: public class ModelFileFilter extends javax.swing.filechooser.FileFilter {
022: private static String TYPE_UNKNOWN = "Type Unknown";
023: private static String HIDDEN_FILE = "Hidden File";
024:
025: private Hashtable filters = null;
026: private String description = null;
027: private String fullDescription = null;
028: private boolean useExtensionsInDescription = true;
029:
030: public ModelFileFilter() {
031: this .filters = new Hashtable();
032: }
033:
034: public ModelFileFilter(String extension) {
035: this (extension, null);
036: }
037:
038: public ModelFileFilter(String extension, String description) {
039: this ();
040: if (extension != null)
041: addExtension(extension);
042: if (description != null)
043: setDescription(description);
044: }
045:
046: public ModelFileFilter(String[] filters) {
047: this (filters, null);
048: }
049:
050: public ModelFileFilter(String[] filters, String description) {
051: this ();
052: for (int i = 0; i < filters.length; i++) {
053: // add filters one by one
054: addExtension(filters[i]);
055: }
056: if (description != null)
057: setDescription(description);
058: }
059:
060: public boolean accept(File f) {
061: if (f != null) {
062: if (f.isDirectory()) {
063: return true;
064: }
065: String extension = getExtension(f);
066: if (extension != null
067: && filters.get(getExtension(f)) != null) {
068: return true;
069: }
070: ;
071: }
072: return false;
073: }
074:
075: public String getExtension(File f) {
076: if (f != null) {
077: String filename = f.getName();
078: int i = filename.lastIndexOf('.');
079: if (i > 0 && i < filename.length() - 1) {
080: return filename.substring(i + 1).toLowerCase();
081: }
082: ;
083: }
084: return null;
085: }
086:
087: public void addExtension(String extension) {
088: if (filters == null) {
089: filters = new Hashtable(5);
090: }
091: filters.put(extension.toLowerCase(), this );
092: fullDescription = null;
093: }
094:
095: public String getDescription() {
096: if (fullDescription == null) {
097: if (description == null || isExtensionListInDescription()) {
098: fullDescription = description == null ? "("
099: : description + " (";
100: // build the description from the extension list
101: Enumeration extensions = filters.keys();
102: if (extensions != null) {
103: fullDescription += "."
104: + (String) extensions.nextElement();
105: while (extensions.hasMoreElements()) {
106: fullDescription += ", ."
107: + (String) extensions.nextElement();
108: }
109: }
110: fullDescription += ")";
111: } else {
112: fullDescription = description;
113: }
114: }
115: return fullDescription;
116: }
117:
118: public void setDescription(String description) {
119: this .description = description;
120: fullDescription = null;
121: }
122:
123: public void setExtensionListInDescription(boolean b) {
124: useExtensionsInDescription = b;
125: fullDescription = null;
126: }
127:
128: public boolean isExtensionListInDescription() {
129: return useExtensionsInDescription;
130: }
131:
132: }
|