WindowsPlacesBar.java revision 3457
0N/A/*
3200N/A * Copyright (c) 2003, 2008, 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
0N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
0N/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,
1472N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1472N/A *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0N/A * or visit www.oracle.com if you need additional information or have any
0N/A * questions.
0N/A */
1879N/Apackage sun.swing;
1879N/A
1879N/Aimport java.awt.*;
4141N/Aimport java.awt.event.*;
4141N/Aimport java.beans.PropertyChangeEvent;
4141N/Aimport java.beans.PropertyChangeListener;
4141N/Aimport java.io.*;
1879N/A
1879N/Aimport javax.swing.*;
1879N/Aimport javax.swing.border.*;
1879N/Aimport javax.swing.filechooser.*;
1879N/A
1879N/Aimport sun.awt.shell.*;
1879N/Aimport sun.awt.OSInfo;
1879N/A
1879N/A/**
1879N/A * <b>WARNING:</b> This class is an implementation detail and is only
1879N/A * public so that it can be used by two packages. You should NOT consider
1879N/A * this public API.
1879N/A * <p>
1879N/A *
1879N/A * @author Leif Samuelsson
1879N/A */
1879N/Apublic class WindowsPlacesBar extends JToolBar
1879N/A implements ActionListener, PropertyChangeListener {
1879N/A JFileChooser fc;
1879N/A JToggleButton[] buttons;
1879N/A ButtonGroup buttonGroup;
1879N/A File[] files;
1879N/A final Dimension buttonSize;
2796N/A
2796N/A public WindowsPlacesBar(JFileChooser fc, boolean isXPStyle) {
2796N/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();
113N/A
113N/A files = (File[])ShellFolder.get("fileChooserShortcutPanelFolders");
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
113N/A String folderName = fsv.getSystemDisplayName(files[i]);
113N/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 }
0N/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];
0N/A Image image = sf.getIcon(true);
0N/A
0N/A if (image == null) {
0N/A // Get default image
0N/A image = (Image) ShellFolder.get("shell32LargeIcon 1");
0N/A }
0N/A
0N/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);
1756N/A }
0N/A buttons[i].setMargin(new Insets(3, 2, 1, 2));
0N/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);
113N/A add(buttons[i]);
113N/A if (i < files.length-1 && isXPStyle) {
113N/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) {
113N/A for (int i=0; i<buttons.length; i++) {
113N/A JToggleButton b = buttons[i];
113N/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);
113N/A buttonGroup.add(b);
113N/A }
113N/A }
113N/A }
113N/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}
0N/A