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