1 /* 2 * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package sun.swing; 26 27 import javax.swing.AbstractListModel; 28 import javax.swing.ComboBoxModel; 29 import javax.swing.JFileChooser; 30 import javax.swing.filechooser.FileFilter; 31 import java.beans.PropertyChangeEvent; 32 import java.beans.PropertyChangeListener; 33 34 /** 35 * Data model for a type-face selection combo-box. 36 */ 37 @SuppressWarnings("serial") // JDK-implementation class 38 public abstract class AbstractFilterComboBoxModel 39 extends AbstractListModel<FileFilter> 40 implements ComboBoxModel<FileFilter>, PropertyChangeListener { 41 42 protected FileFilter[] filters; 43 44 protected AbstractFilterComboBoxModel() { 45 this.filters = getFileChooser().getChoosableFileFilters(); 46 } 47 48 protected abstract JFileChooser getFileChooser(); 49 50 @Override 51 public void propertyChange(PropertyChangeEvent event) { 52 String property = event.getPropertyName(); 53 if (property == JFileChooser.CHOOSABLE_FILE_FILTER_CHANGED_PROPERTY) { 54 this.filters = (FileFilter[]) event.getNewValue(); 55 fireContentsChanged(this, -1, -1); 56 } else if (property == JFileChooser.FILE_FILTER_CHANGED_PROPERTY) { 57 fireContentsChanged(this, -1, -1); 58 } 59 } 60 61 @Override 62 public void setSelectedItem(Object filter) { 63 if (filter != null) { 64 getFileChooser().setFileFilter((FileFilter) filter); 65 fireContentsChanged(this, -1, -1); 66 } 67 } 68 69 @Override 70 public Object getSelectedItem() { 71 // Ensure that the current filter is in the list. 72 // NOTE: we should not have to do this, since JFileChooser adds 73 // the filter to the choosable filters list when the filter 74 // is set. Lets be paranoid just in case someone overrides 75 // setFileFilter in JFileChooser. 76 FileFilter currentFilter = getFileChooser().getFileFilter(); 77 if (currentFilter != null) { 78 for (FileFilter filter : this.filters) { 79 if (filter == currentFilter) { 80 return currentFilter; 81 } 82 } 83 getFileChooser().addChoosableFileFilter(currentFilter); 84 } 85 return currentFilter; 86 } 87 88 @Override 89 public int getSize() { 90 return (this.filters != null) 91 ? filters.length 92 : 0; 93 } 94 95 @Override 96 public FileFilter getElementAt(int index) { 97 if (index >= getSize()) { 98 // This shouldn't happen. Try to recover gracefully. 99 return getFileChooser().getFileFilter(); 100 } 101 return (this.filters != null) 102 ? filters[index] 103 : null; 104 } 105 }