001/* 002 * The contents of this file are subject to the terms of the Common Development and 003 * Distribution License (the License). You may not use this file except in compliance with the 004 * License. 005 * 006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the 007 * specific language governing permission and limitations under the License. 008 * 009 * When distributing Covered Software, include this CDDL Header Notice in each file and include 010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL 011 * Header, with the fields enclosed by brackets [] replaced by your own identifying 012 * information: "Portions Copyright [year] [name of copyright owner]". 013 * 014 * Copyright 2006-2008 Sun Microsystems, Inc. 015 * Portions Copyright 2014-2016 ForgeRock AS. 016 */ 017package org.opends.quicksetup.util; 018 019import java.io.File; 020 021import javax.swing.filechooser.FileFilter; 022import static com.forgerock.opendj.util.OperatingSystem.isWindows; 023 024/** 025 * This is a class used to be able to filter on certain type of files 026 * in the File Browser dialog. 027 */ 028public class ExtensionFileFilter extends FileFilter 029{ 030 private String extension; 031 032 private String description; 033 034 /** 035 * The ExtensionFiter constructor. 036 * @param extension the extension of the file we want to filter on. 037 * @param description the description for the extension. 038 */ 039 public ExtensionFileFilter(String extension, String description) 040 { 041 this.extension = extension; 042 this.description = description; 043 } 044 045 @Override 046 public boolean accept(File f) 047 { 048 boolean accept = false; 049 if (f != null) 050 { 051 if (f.isDirectory()) 052 { 053 accept = true; 054 } else if (isWindows()) 055 { 056 accept = 057 f.getName().toLowerCase().endsWith("." + extension.toLowerCase()); 058 } else 059 { 060 accept = f.getName().endsWith("." + extension); 061 } 062 } 063 return accept; 064 } 065 066 @Override 067 public String getDescription() 068 { 069 return description; 070 } 071}