src/share/classes/sun/swing/plaf/synth/SynthFileChooserUIImpl.java

Print this page


   1 /*
   2  * Copyright (c) 2003, 2010, 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


 173 
 174         public Action getNewFolderAction() {
 175             return SynthFileChooserUIImpl.this.getNewFolderAction();
 176         }
 177 
 178         public MouseListener createDoubleClickListener(JList list) {
 179             return SynthFileChooserUIImpl.this.createDoubleClickListener(getFileChooser(),
 180                                                                      list);
 181         }
 182 
 183         public ListSelectionListener createListSelectionListener() {
 184             return SynthFileChooserUIImpl.this.createListSelectionListener(getFileChooser());
 185         }
 186     }
 187 
 188     protected void installDefaults(JFileChooser fc) {
 189         super.installDefaults(fc);
 190         readOnly = UIManager.getBoolean("FileChooser.readOnly");
 191     }
 192 

 193     public void installComponents(JFileChooser fc) {
 194         super.installComponents(fc);
 195 
 196         SynthContext context = getContext(fc, ENABLED);
 197 
 198         fc.setLayout(new BorderLayout(0, 11));
 199 
 200         // ********************************* //
 201         // **** Construct the top panel **** //
 202         // ********************************* //
 203 
 204         // Directory manipulation buttons
 205         JPanel topPanel = new JPanel(new BorderLayout(11, 0));
 206     JPanel topButtonPanel = new JPanel();
 207     topButtonPanel.setLayout(new BoxLayout(topButtonPanel, BoxLayout.LINE_AXIS));
 208     topPanel.add(topButtonPanel, BorderLayout.AFTER_LINE_ENDS);
 209 
 210         // Add the top panel to the fileChooser
 211         fc.add(topPanel, BorderLayout.NORTH);
 212 


 717         public int getIconWidth() {
 718             return ((icon != null) ? icon.getIconWidth() : 0) + depth*space;
 719         }
 720 
 721         public int getIconHeight() {
 722             return (icon != null) ? icon.getIconHeight() : 0;
 723         }
 724 
 725     }
 726 
 727     //
 728     // DataModel for DirectoryComboxbox
 729     //
 730     protected DirectoryComboBoxModel createDirectoryComboBoxModel(JFileChooser fc) {
 731         return new DirectoryComboBoxModel();
 732     }
 733 
 734     /**
 735      * Data model for a type-face selection combo-box.
 736      */

 737     protected class DirectoryComboBoxModel extends AbstractListModel<File> implements ComboBoxModel<File> {
 738         Vector<File> directories = new Vector<File>();
 739         int[] depths = null;
 740         File selectedDirectory = null;
 741         JFileChooser chooser = getFileChooser();
 742         FileSystemView fsv = chooser.getFileSystemView();
 743 
 744         public DirectoryComboBoxModel() {
 745             // Add the current directory to the model, and make it the
 746             // selectedDirectory
 747             File dir = getFileChooser().getCurrentDirectory();
 748             if (dir != null) {
 749                 addItem(dir);
 750             }
 751         }
 752 
 753         /**
 754          * Adds the directory to the model and sets it to be selected,
 755          * additionally clears out the previous selected directory and
 756          * the paths leading up to it, if any.


 846             this.selectedDirectory = (File)selectedDirectory;
 847             fireContentsChanged(this, -1, -1);
 848         }
 849 
 850         public Object getSelectedItem() {
 851             return selectedDirectory;
 852         }
 853 
 854         public int getSize() {
 855             return directories.size();
 856         }
 857 
 858         public File getElementAt(int index) {
 859             return directories.elementAt(index);
 860         }
 861     }
 862 
 863     /**
 864      * Acts when DirectoryComboBox has changed the selected item.
 865      */

 866     protected class DirectoryComboBoxAction extends AbstractAction {
 867         protected DirectoryComboBoxAction() {
 868             super("DirectoryComboBoxAction");
 869         }
 870 
 871         public void actionPerformed(ActionEvent e) {
 872             directoryComboBox.hidePopup();
 873             JComponent cb = getDirectoryComboBox();
 874             if (cb instanceof JComboBox) {
 875                 File f = (File)((JComboBox)cb).getSelectedItem();
 876                 getFileChooser().setCurrentDirectory(f);
 877             }
 878         }
 879     }
 880 
 881     //
 882     // Renderer for Types ComboBox
 883     //
 884     protected FilterComboBoxRenderer createFilterComboBoxRenderer() {
 885         return new FilterComboBoxRenderer(filterComboBox.getRenderer());


 906             //this should always be true, since SynthComboBoxUI's SynthComboBoxRenderer
 907             //extends JLabel
 908             assert c instanceof JLabel;
 909             if (text != null) {
 910                 ((JLabel)c).setText(text);
 911             }
 912             return c;
 913         }
 914     }
 915 
 916     //
 917     // DataModel for Types Comboxbox
 918     //
 919     protected FilterComboBoxModel createFilterComboBoxModel() {
 920         return new FilterComboBoxModel();
 921     }
 922 
 923     /**
 924      * Data model for a type-face selection combo-box.
 925      */

 926     protected class FilterComboBoxModel extends AbstractFilterComboBoxModel {
 927         protected JFileChooser getFileChooser() {
 928             return SynthFileChooserUIImpl.this.getFileChooser();
 929         }
 930     }
 931 
 932 
 933 
 934     /**
 935      * <code>ButtonAreaLayout</code> behaves in a similar manner to
 936      * <code>FlowLayout</code>. It lays out all components from left to
 937      * right, flushed right. The widths of all components will be set
 938      * to the largest preferred size width.
 939      */
 940     private static class ButtonAreaLayout implements LayoutManager {
 941         private int hGap = 5;
 942         private int topMargin = 17;
 943 
 944         public void addLayoutComponent(String string, Component comp) {
 945         }


 995                                          (numChildren - 1) * hGap,
 996                                          extraHeight + height);
 997                 }
 998             }
 999             return new Dimension(0, 0);
1000         }
1001 
1002         public Dimension preferredLayoutSize(Container c) {
1003             return minimumLayoutSize(c);
1004         }
1005 
1006         public void removeLayoutComponent(Component c) { }
1007     }
1008 
1009     private static void groupLabels(AlignedLabel[] group) {
1010         for (int i = 0; i < group.length; i++) {
1011             group[i].group = group;
1012         }
1013     }
1014 

1015     private class AlignedLabel extends JLabel {
1016         private AlignedLabel[] group;
1017         private int maxWidth = 0;
1018 
1019         AlignedLabel() {
1020             super();
1021             setAlignmentX(JComponent.LEFT_ALIGNMENT);
1022         }
1023 
1024         AlignedLabel(String text) {
1025             super(text);
1026             setAlignmentX(JComponent.LEFT_ALIGNMENT);
1027         }
1028 
1029         public Dimension getPreferredSize() {
1030             Dimension d = super.getPreferredSize();
1031             // Align the width with all other labels in group.
1032             return new Dimension(getMaxWidth() + 11, d.height);
1033         }
1034 
   1 /*
   2  * Copyright (c) 2003, 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


 173 
 174         public Action getNewFolderAction() {
 175             return SynthFileChooserUIImpl.this.getNewFolderAction();
 176         }
 177 
 178         public MouseListener createDoubleClickListener(JList list) {
 179             return SynthFileChooserUIImpl.this.createDoubleClickListener(getFileChooser(),
 180                                                                      list);
 181         }
 182 
 183         public ListSelectionListener createListSelectionListener() {
 184             return SynthFileChooserUIImpl.this.createListSelectionListener(getFileChooser());
 185         }
 186     }
 187 
 188     protected void installDefaults(JFileChooser fc) {
 189         super.installDefaults(fc);
 190         readOnly = UIManager.getBoolean("FileChooser.readOnly");
 191     }
 192 
 193     @SuppressWarnings("serial") // anonymous classes inside
 194     public void installComponents(JFileChooser fc) {
 195         super.installComponents(fc);
 196 
 197         SynthContext context = getContext(fc, ENABLED);
 198 
 199         fc.setLayout(new BorderLayout(0, 11));
 200 
 201         // ********************************* //
 202         // **** Construct the top panel **** //
 203         // ********************************* //
 204 
 205         // Directory manipulation buttons
 206         JPanel topPanel = new JPanel(new BorderLayout(11, 0));
 207     JPanel topButtonPanel = new JPanel();
 208     topButtonPanel.setLayout(new BoxLayout(topButtonPanel, BoxLayout.LINE_AXIS));
 209     topPanel.add(topButtonPanel, BorderLayout.AFTER_LINE_ENDS);
 210 
 211         // Add the top panel to the fileChooser
 212         fc.add(topPanel, BorderLayout.NORTH);
 213 


 718         public int getIconWidth() {
 719             return ((icon != null) ? icon.getIconWidth() : 0) + depth*space;
 720         }
 721 
 722         public int getIconHeight() {
 723             return (icon != null) ? icon.getIconHeight() : 0;
 724         }
 725 
 726     }
 727 
 728     //
 729     // DataModel for DirectoryComboxbox
 730     //
 731     protected DirectoryComboBoxModel createDirectoryComboBoxModel(JFileChooser fc) {
 732         return new DirectoryComboBoxModel();
 733     }
 734 
 735     /**
 736      * Data model for a type-face selection combo-box.
 737      */
 738     @SuppressWarnings("serial") // JDK-implementation class
 739     protected class DirectoryComboBoxModel extends AbstractListModel<File> implements ComboBoxModel<File> {
 740         Vector<File> directories = new Vector<File>();
 741         int[] depths = null;
 742         File selectedDirectory = null;
 743         JFileChooser chooser = getFileChooser();
 744         FileSystemView fsv = chooser.getFileSystemView();
 745 
 746         public DirectoryComboBoxModel() {
 747             // Add the current directory to the model, and make it the
 748             // selectedDirectory
 749             File dir = getFileChooser().getCurrentDirectory();
 750             if (dir != null) {
 751                 addItem(dir);
 752             }
 753         }
 754 
 755         /**
 756          * Adds the directory to the model and sets it to be selected,
 757          * additionally clears out the previous selected directory and
 758          * the paths leading up to it, if any.


 848             this.selectedDirectory = (File)selectedDirectory;
 849             fireContentsChanged(this, -1, -1);
 850         }
 851 
 852         public Object getSelectedItem() {
 853             return selectedDirectory;
 854         }
 855 
 856         public int getSize() {
 857             return directories.size();
 858         }
 859 
 860         public File getElementAt(int index) {
 861             return directories.elementAt(index);
 862         }
 863     }
 864 
 865     /**
 866      * Acts when DirectoryComboBox has changed the selected item.
 867      */
 868     @SuppressWarnings("serial") // JDK-implementation class
 869     protected class DirectoryComboBoxAction extends AbstractAction {
 870         protected DirectoryComboBoxAction() {
 871             super("DirectoryComboBoxAction");
 872         }
 873 
 874         public void actionPerformed(ActionEvent e) {
 875             directoryComboBox.hidePopup();
 876             JComponent cb = getDirectoryComboBox();
 877             if (cb instanceof JComboBox) {
 878                 File f = (File)((JComboBox)cb).getSelectedItem();
 879                 getFileChooser().setCurrentDirectory(f);
 880             }
 881         }
 882     }
 883 
 884     //
 885     // Renderer for Types ComboBox
 886     //
 887     protected FilterComboBoxRenderer createFilterComboBoxRenderer() {
 888         return new FilterComboBoxRenderer(filterComboBox.getRenderer());


 909             //this should always be true, since SynthComboBoxUI's SynthComboBoxRenderer
 910             //extends JLabel
 911             assert c instanceof JLabel;
 912             if (text != null) {
 913                 ((JLabel)c).setText(text);
 914             }
 915             return c;
 916         }
 917     }
 918 
 919     //
 920     // DataModel for Types Comboxbox
 921     //
 922     protected FilterComboBoxModel createFilterComboBoxModel() {
 923         return new FilterComboBoxModel();
 924     }
 925 
 926     /**
 927      * Data model for a type-face selection combo-box.
 928      */
 929     @SuppressWarnings("serial") // JDK-implementation class
 930     protected class FilterComboBoxModel extends AbstractFilterComboBoxModel {
 931         protected JFileChooser getFileChooser() {
 932             return SynthFileChooserUIImpl.this.getFileChooser();
 933         }
 934     }
 935 
 936 
 937 
 938     /**
 939      * <code>ButtonAreaLayout</code> behaves in a similar manner to
 940      * <code>FlowLayout</code>. It lays out all components from left to
 941      * right, flushed right. The widths of all components will be set
 942      * to the largest preferred size width.
 943      */
 944     private static class ButtonAreaLayout implements LayoutManager {
 945         private int hGap = 5;
 946         private int topMargin = 17;
 947 
 948         public void addLayoutComponent(String string, Component comp) {
 949         }


 999                                          (numChildren - 1) * hGap,
1000                                          extraHeight + height);
1001                 }
1002             }
1003             return new Dimension(0, 0);
1004         }
1005 
1006         public Dimension preferredLayoutSize(Container c) {
1007             return minimumLayoutSize(c);
1008         }
1009 
1010         public void removeLayoutComponent(Component c) { }
1011     }
1012 
1013     private static void groupLabels(AlignedLabel[] group) {
1014         for (int i = 0; i < group.length; i++) {
1015             group[i].group = group;
1016         }
1017     }
1018 
1019     @SuppressWarnings("serial") // JDK-implementation class
1020     private class AlignedLabel extends JLabel {
1021         private AlignedLabel[] group;
1022         private int maxWidth = 0;
1023 
1024         AlignedLabel() {
1025             super();
1026             setAlignmentX(JComponent.LEFT_ALIGNMENT);
1027         }
1028 
1029         AlignedLabel(String text) {
1030             super(text);
1031             setAlignmentX(JComponent.LEFT_ALIGNMENT);
1032         }
1033 
1034         public Dimension getPreferredSize() {
1035             Dimension d = super.getPreferredSize();
1036             // Align the width with all other labels in group.
1037             return new Dimension(getMaxWidth() + 11, d.height);
1038         }
1039