Home » openjdk-7 » sun » swing » [javadoc | source]

    1   /*
    2    * Copyright (c) 2003, 2011, 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 java.awt;
   28   import java.awt.event;
   29   import java.beans.PropertyChangeEvent;
   30   import java.beans.PropertyChangeListener;
   31   import java.io;
   32   import java.security.AccessController;
   33   import java.security.PrivilegedAction;
   34   
   35   import javax.swing;
   36   import javax.swing.border;
   37   import javax.swing.filechooser;
   38   
   39   import sun.awt.shell;
   40   import sun.awt.OSInfo;
   41   
   42   /**
   43    * <b>WARNING:</b> This class is an implementation detail and is only
   44    * public so that it can be used by two packages. You should NOT consider
   45    * this public API.
   46    * <p>
   47    *
   48    * @author Leif Samuelsson
   49    */
   50   public class WindowsPlacesBar extends JToolBar
   51                                 implements ActionListener, PropertyChangeListener {
   52       JFileChooser fc;
   53       JToggleButton[] buttons;
   54       ButtonGroup buttonGroup;
   55       File[] files;
   56       final Dimension buttonSize;
   57   
   58       public WindowsPlacesBar(JFileChooser fc, boolean isXPStyle) {
   59           super(JToolBar.VERTICAL);
   60           this.fc = fc;
   61           setFloatable(false);
   62           putClientProperty("JToolBar.isRollover", Boolean.TRUE);
   63   
   64           boolean isXPPlatform = (OSInfo.getOSType() == OSInfo.OSType.WINDOWS &&
   65                   OSInfo.getWindowsVersion().compareTo(OSInfo.WINDOWS_XP) >= 0);
   66   
   67           if (isXPStyle) {
   68               buttonSize = new Dimension(83, 69);
   69               putClientProperty("XPStyle.subAppName", "placesbar");
   70               setBorder(new EmptyBorder(1, 1, 1, 1));
   71           } else {
   72               // The button size almost matches the XP style when in Classic style on XP
   73               buttonSize = new Dimension(83, isXPPlatform ? 65 : 54);
   74               setBorder(new BevelBorder(BevelBorder.LOWERED,
   75                                         UIManager.getColor("ToolBar.highlight"),
   76                                         UIManager.getColor("ToolBar.background"),
   77                                         UIManager.getColor("ToolBar.darkShadow"),
   78                                         UIManager.getColor("ToolBar.shadow")));
   79           }
   80           Color bgColor = new Color(UIManager.getColor("ToolBar.shadow").getRGB());
   81           setBackground(bgColor);
   82           FileSystemView fsv = fc.getFileSystemView();
   83   
   84           files = AccessController.doPrivileged(new PrivilegedAction<File[]>() {
   85               public File[] run() {
   86                   return (File[]) ShellFolder.get("fileChooserShortcutPanelFolders");
   87               }
   88           });
   89   
   90           buttons = new JToggleButton[files.length];
   91           buttonGroup = new ButtonGroup();
   92           for (int i = 0; i < files.length; i++) {
   93               if (fsv.isFileSystemRoot(files[i])) {
   94                   // Create special File wrapper for drive path
   95                   files[i] = fsv.createFileObject(files[i].getAbsolutePath());
   96               }
   97   
   98               String folderName = fsv.getSystemDisplayName(files[i]);
   99               int index = folderName.lastIndexOf(File.separatorChar);
  100               if (index >= 0 && index < folderName.length() - 1) {
  101                   folderName = folderName.substring(index + 1);
  102               }
  103               Icon icon;
  104               if (files[i] instanceof ShellFolder) {
  105                   // We want a large icon, fsv only gives us a small.
  106                   ShellFolder sf = (ShellFolder)files[i];
  107                   Image image = sf.getIcon(true);
  108   
  109                   if (image == null) {
  110                       // Get default image
  111                       image = (Image) ShellFolder.get("shell32LargeIcon 1");
  112                   }
  113   
  114                   icon = image == null ? null : new ImageIcon(image, sf.getFolderType());
  115               } else {
  116                   icon = fsv.getSystemIcon(files[i]);
  117               }
  118               buttons[i] = new JToggleButton(folderName, icon);
  119               if (isXPPlatform) {
  120                   buttons[i].setText("<html><center>"+folderName+"</center></html>");
  121               }
  122               if (isXPStyle) {
  123                   buttons[i].putClientProperty("XPStyle.subAppName", "placesbar");
  124               } else {
  125                   Color fgColor = new Color(UIManager.getColor("List.selectionForeground").getRGB());
  126                   buttons[i].setContentAreaFilled(false);
  127                   buttons[i].setForeground(fgColor);
  128               }
  129               buttons[i].setMargin(new Insets(3, 2, 1, 2));
  130               buttons[i].setFocusPainted(false);
  131               buttons[i].setIconTextGap(0);
  132               buttons[i].setHorizontalTextPosition(JToggleButton.CENTER);
  133               buttons[i].setVerticalTextPosition(JToggleButton.BOTTOM);
  134               buttons[i].setAlignmentX(JComponent.CENTER_ALIGNMENT);
  135               buttons[i].setPreferredSize(buttonSize);
  136               buttons[i].setMaximumSize(buttonSize);
  137               buttons[i].addActionListener(this);
  138               add(buttons[i]);
  139               if (i < files.length-1 && isXPStyle) {
  140                   add(Box.createRigidArea(new Dimension(1, 1)));
  141               }
  142               buttonGroup.add(buttons[i]);
  143           }
  144           doDirectoryChanged(fc.getCurrentDirectory());
  145       }
  146   
  147       protected void doDirectoryChanged(File f) {
  148           for (int i=0; i<buttons.length; i++) {
  149               JToggleButton b = buttons[i];
  150               if (files[i].equals(f)) {
  151                   b.setSelected(true);
  152                   break;
  153               } else if (b.isSelected()) {
  154                   // Remove temporarily from group because it doesn't
  155                   // allow for no button to be selected.
  156                   buttonGroup.remove(b);
  157                   b.setSelected(false);
  158                   buttonGroup.add(b);
  159               }
  160           }
  161       }
  162   
  163       public void propertyChange(PropertyChangeEvent e) {
  164           String prop = e.getPropertyName();
  165           if (prop == JFileChooser.DIRECTORY_CHANGED_PROPERTY) {
  166               doDirectoryChanged(fc.getCurrentDirectory());
  167           }
  168       }
  169   
  170       public void actionPerformed(ActionEvent e) {
  171           JToggleButton b = (JToggleButton)e.getSource();
  172           for (int i=0; i<buttons.length; i++) {
  173               if (b == buttons[i]) {
  174                   fc.setCurrentDirectory(files[i]);
  175                   break;
  176               }
  177           }
  178       }
  179   
  180       public Dimension getPreferredSize() {
  181           Dimension min  = super.getMinimumSize();
  182           Dimension pref = super.getPreferredSize();
  183           int h = min.height;
  184           if (buttons != null && buttons.length > 0 && buttons.length < 5) {
  185               JToggleButton b = buttons[0];
  186               if (b != null) {
  187                   int bh = 5 * (b.getPreferredSize().height + 1);
  188                   if (bh > h) {
  189                       h = bh;
  190                   }
  191               }
  192           }
  193           if (h > pref.height) {
  194               pref = new Dimension(pref.width, h);
  195           }
  196           return pref;
  197       }
  198   }

Home » openjdk-7 » sun » swing » [javadoc | source]