0N/A/*
3909N/A * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/Apackage sun.swing;
0N/A
0N/Aimport java.awt.*;
0N/Aimport java.awt.event.*;
0N/Aimport java.beans.PropertyChangeEvent;
0N/Aimport java.beans.PropertyChangeListener;
0N/Aimport java.io.*;
4089N/Aimport java.security.AccessController;
4089N/Aimport java.security.PrivilegedAction;
0N/A
0N/Aimport javax.swing.*;
0N/Aimport javax.swing.border.*;
0N/Aimport javax.swing.filechooser.*;
0N/A
0N/Aimport sun.awt.shell.*;
0N/Aimport sun.awt.OSInfo;
0N/A
0N/A/**
0N/A * <b>WARNING:</b> This class is an implementation detail and is only
0N/A * public so that it can be used by two packages. You should NOT consider
0N/A * this public API.
0N/A * <p>
0N/A *
0N/A * @author Leif Samuelsson
0N/A */
0N/Apublic class WindowsPlacesBar extends JToolBar
0N/A implements ActionListener, PropertyChangeListener {
0N/A JFileChooser fc;
0N/A JToggleButton[] buttons;
0N/A ButtonGroup buttonGroup;
0N/A File[] files;
0N/A final Dimension buttonSize;
0N/A
0N/A public WindowsPlacesBar(JFileChooser fc, boolean isXPStyle) {
0N/A super(JToolBar.VERTICAL);
0N/A this.fc = fc;
0N/A setFloatable(false);
0N/A putClientProperty("JToolBar.isRollover", Boolean.TRUE);
0N/A
0N/A boolean isXPPlatform = (OSInfo.getOSType() == OSInfo.OSType.WINDOWS &&
0N/A OSInfo.getWindowsVersion().compareTo(OSInfo.WINDOWS_XP) >= 0);
0N/A
0N/A if (isXPStyle) {
0N/A buttonSize = new Dimension(83, 69);
0N/A putClientProperty("XPStyle.subAppName", "placesbar");
0N/A setBorder(new EmptyBorder(1, 1, 1, 1));
0N/A } else {
0N/A // The button size almost matches the XP style when in Classic style on XP
0N/A buttonSize = new Dimension(83, isXPPlatform ? 65 : 54);
0N/A setBorder(new BevelBorder(BevelBorder.LOWERED,
0N/A UIManager.getColor("ToolBar.highlight"),
0N/A UIManager.getColor("ToolBar.background"),
0N/A UIManager.getColor("ToolBar.darkShadow"),
0N/A UIManager.getColor("ToolBar.shadow")));
0N/A }
0N/A Color bgColor = new Color(UIManager.getColor("ToolBar.shadow").getRGB());
0N/A setBackground(bgColor);
0N/A FileSystemView fsv = fc.getFileSystemView();
0N/A
4089N/A files = AccessController.doPrivileged(new PrivilegedAction<File[]>() {
4089N/A public File[] run() {
4089N/A return (File[]) ShellFolder.get("fileChooserShortcutPanelFolders");
4089N/A }
4089N/A });
4089N/A
0N/A buttons = new JToggleButton[files.length];
0N/A buttonGroup = new ButtonGroup();
0N/A for (int i = 0; i < files.length; i++) {
0N/A if (fsv.isFileSystemRoot(files[i])) {
0N/A // Create special File wrapper for drive path
0N/A files[i] = fsv.createFileObject(files[i].getAbsolutePath());
0N/A }
0N/A
0N/A String folderName = fsv.getSystemDisplayName(files[i]);
0N/A int index = folderName.lastIndexOf(File.separatorChar);
0N/A if (index >= 0 && index < folderName.length() - 1) {
0N/A folderName = folderName.substring(index + 1);
0N/A }
231N/A Icon icon;
0N/A if (files[i] instanceof ShellFolder) {
0N/A // We want a large icon, fsv only gives us a small.
0N/A ShellFolder sf = (ShellFolder)files[i];
231N/A Image image = sf.getIcon(true);
231N/A
231N/A if (image == null) {
231N/A // Get default image
231N/A image = (Image) ShellFolder.get("shell32LargeIcon 1");
231N/A }
231N/A
231N/A icon = image == null ? null : new ImageIcon(image, sf.getFolderType());
0N/A } else {
0N/A icon = fsv.getSystemIcon(files[i]);
0N/A }
0N/A buttons[i] = new JToggleButton(folderName, icon);
0N/A if (isXPPlatform) {
0N/A buttons[i].setText("<html><center>"+folderName+"</center></html>");
0N/A }
0N/A if (isXPStyle) {
0N/A buttons[i].putClientProperty("XPStyle.subAppName", "placesbar");
0N/A } else {
0N/A Color fgColor = new Color(UIManager.getColor("List.selectionForeground").getRGB());
0N/A buttons[i].setContentAreaFilled(false);
0N/A buttons[i].setForeground(fgColor);
0N/A }
0N/A buttons[i].setMargin(new Insets(3, 2, 1, 2));
3457N/A buttons[i].setFocusPainted(false);
0N/A buttons[i].setIconTextGap(0);
0N/A buttons[i].setHorizontalTextPosition(JToggleButton.CENTER);
0N/A buttons[i].setVerticalTextPosition(JToggleButton.BOTTOM);
0N/A buttons[i].setAlignmentX(JComponent.CENTER_ALIGNMENT);
0N/A buttons[i].setPreferredSize(buttonSize);
0N/A buttons[i].setMaximumSize(buttonSize);
0N/A buttons[i].addActionListener(this);
0N/A add(buttons[i]);
0N/A if (i < files.length-1 && isXPStyle) {
0N/A add(Box.createRigidArea(new Dimension(1, 1)));
0N/A }
0N/A buttonGroup.add(buttons[i]);
0N/A }
0N/A doDirectoryChanged(fc.getCurrentDirectory());
0N/A }
0N/A
0N/A protected void doDirectoryChanged(File f) {
0N/A for (int i=0; i<buttons.length; i++) {
0N/A JToggleButton b = buttons[i];
0N/A if (files[i].equals(f)) {
0N/A b.setSelected(true);
0N/A break;
0N/A } else if (b.isSelected()) {
0N/A // Remove temporarily from group because it doesn't
0N/A // allow for no button to be selected.
0N/A buttonGroup.remove(b);
0N/A b.setSelected(false);
0N/A buttonGroup.add(b);
0N/A }
0N/A }
0N/A }
0N/A
0N/A public void propertyChange(PropertyChangeEvent e) {
0N/A String prop = e.getPropertyName();
0N/A if (prop == JFileChooser.DIRECTORY_CHANGED_PROPERTY) {
0N/A doDirectoryChanged(fc.getCurrentDirectory());
0N/A }
0N/A }
0N/A
0N/A public void actionPerformed(ActionEvent e) {
0N/A JToggleButton b = (JToggleButton)e.getSource();
0N/A for (int i=0; i<buttons.length; i++) {
0N/A if (b == buttons[i]) {
0N/A fc.setCurrentDirectory(files[i]);
0N/A break;
0N/A }
0N/A }
0N/A }
0N/A
0N/A public Dimension getPreferredSize() {
0N/A Dimension min = super.getMinimumSize();
0N/A Dimension pref = super.getPreferredSize();
0N/A int h = min.height;
0N/A if (buttons != null && buttons.length > 0 && buttons.length < 5) {
0N/A JToggleButton b = buttons[0];
0N/A if (b != null) {
0N/A int bh = 5 * (b.getPreferredSize().height + 1);
0N/A if (bh > h) {
0N/A h = bh;
0N/A }
0N/A }
0N/A }
0N/A if (h > pref.height) {
0N/A pref = new Dimension(pref.width, h);
0N/A }
0N/A return pref;
0N/A }
0N/A}