Path: blob/master/src/java.desktop/share/classes/sun/swing/WindowsPlacesBar.java
41153 views
/*1* Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24package sun.swing;2526import java.awt.Dimension;27import java.awt.Insets;28import java.awt.Color;29import java.awt.Image;3031import java.awt.event.ActionEvent;32import java.awt.event.ActionListener;3334import java.beans.PropertyChangeEvent;35import java.beans.PropertyChangeListener;3637import java.io.File;38import java.security.AccessController;39import java.security.PrivilegedAction;4041import javax.swing.JToolBar;42import javax.swing.JFileChooser;43import javax.swing.JToggleButton;44import javax.swing.ButtonGroup;45import javax.swing.UIManager;46import javax.swing.Icon;47import javax.swing.ImageIcon;48import javax.swing.JComponent;49import javax.swing.Box;5051import javax.swing.border.EmptyBorder;52import javax.swing.border.BevelBorder;53import javax.swing.filechooser.FileSystemView;5455import sun.awt.shell.ShellFolder;56import sun.awt.OSInfo;5758/**59* <b>WARNING:</b> This class is an implementation detail and is only60* public so that it can be used by two packages. You should NOT consider61* this public API.62*63* @author Leif Samuelsson64*/65@SuppressWarnings("serial") // JDK-implementation class66public class WindowsPlacesBar extends JToolBar67implements ActionListener, PropertyChangeListener {68JFileChooser fc;69JToggleButton[] buttons;70ButtonGroup buttonGroup;71File[] files;72final Dimension buttonSize;7374public WindowsPlacesBar(JFileChooser fc, boolean isXPStyle) {75super(JToolBar.VERTICAL);76this.fc = fc;77setFloatable(false);78putClientProperty("JToolBar.isRollover", Boolean.TRUE);7980boolean isXPPlatform = (OSInfo.getOSType() == OSInfo.OSType.WINDOWS &&81OSInfo.getWindowsVersion().compareTo(OSInfo.WINDOWS_XP) >= 0);8283if (isXPStyle) {84buttonSize = new Dimension(83, 69);85putClientProperty("XPStyle.subAppName", "placesbar");86setBorder(new EmptyBorder(1, 1, 1, 1));87} else {88// The button size almost matches the XP style when in Classic style on XP89buttonSize = new Dimension(83, isXPPlatform ? 65 : 54);90setBorder(new BevelBorder(BevelBorder.LOWERED,91UIManager.getColor("ToolBar.highlight"),92UIManager.getColor("ToolBar.background"),93UIManager.getColor("ToolBar.darkShadow"),94UIManager.getColor("ToolBar.shadow")));95}96Color bgColor = new Color(UIManager.getColor("ToolBar.shadow").getRGB());97setBackground(bgColor);98FileSystemView fsv = fc.getFileSystemView();99100files = fsv.getChooserShortcutPanelFiles();101102buttons = new JToggleButton[files.length];103buttonGroup = new ButtonGroup();104for (int i = 0; i < files.length; i++) {105if (fsv.isFileSystemRoot(files[i])) {106// Create special File wrapper for drive path107files[i] = fsv.createFileObject(files[i].getAbsolutePath());108}109110String folderName = fsv.getSystemDisplayName(files[i]);111int index = folderName.lastIndexOf(File.separatorChar);112if (index >= 0 && index < folderName.length() - 1) {113folderName = folderName.substring(index + 1);114}115Icon icon;116if (files[i] instanceof ShellFolder) {117// We want a large icon, fsv only gives us a small.118ShellFolder sf = (ShellFolder)files[i];119Image image = sf.getIcon(true);120121if (image == null) {122// Get default image123image = (Image) ShellFolder.get("shell32LargeIcon 1");124}125126icon = image == null ? null : new ImageIcon(image, sf.getFolderType());127} else {128icon = fsv.getSystemIcon(files[i]);129}130buttons[i] = new JToggleButton(folderName, icon);131if (isXPStyle) {132buttons[i].putClientProperty("XPStyle.subAppName", "placesbar");133} else {134Color fgColor = new Color(UIManager.getColor("List.selectionForeground").getRGB());135buttons[i].setContentAreaFilled(false);136buttons[i].setForeground(fgColor);137}138buttons[i].setMargin(new Insets(3, 2, 1, 2));139buttons[i].setFocusPainted(false);140buttons[i].setIconTextGap(0);141buttons[i].setHorizontalTextPosition(JToggleButton.CENTER);142buttons[i].setVerticalTextPosition(JToggleButton.BOTTOM);143buttons[i].setAlignmentX(JComponent.CENTER_ALIGNMENT);144buttons[i].setPreferredSize(buttonSize);145buttons[i].setMaximumSize(buttonSize);146buttons[i].addActionListener(this);147add(buttons[i]);148if (i < files.length-1 && isXPStyle) {149add(Box.createRigidArea(new Dimension(1, 1)));150}151buttonGroup.add(buttons[i]);152}153doDirectoryChanged(fc.getCurrentDirectory());154}155156protected void doDirectoryChanged(File f) {157for (int i=0; i<buttons.length; i++) {158JToggleButton b = buttons[i];159if (files[i].equals(f)) {160b.setSelected(true);161break;162} else if (b.isSelected()) {163// Remove temporarily from group because it doesn't164// allow for no button to be selected.165buttonGroup.remove(b);166b.setSelected(false);167buttonGroup.add(b);168}169}170}171172public void propertyChange(PropertyChangeEvent e) {173String prop = e.getPropertyName();174if (prop == JFileChooser.DIRECTORY_CHANGED_PROPERTY) {175doDirectoryChanged(fc.getCurrentDirectory());176}177}178179public void actionPerformed(ActionEvent e) {180JToggleButton b = (JToggleButton)e.getSource();181for (int i=0; i<buttons.length; i++) {182if (b == buttons[i]) {183fc.setCurrentDirectory(files[i]);184break;185}186}187}188189public Dimension getPreferredSize() {190Dimension min = super.getMinimumSize();191Dimension pref = super.getPreferredSize();192int h = min.height;193if (buttons != null && buttons.length > 0 && buttons.length < 5) {194JToggleButton b = buttons[0];195if (b != null) {196int bh = 5 * (b.getPreferredSize().height + 1);197if (bh > h) {198h = bh;199}200}201}202if (h > pref.height) {203pref = new Dimension(pref.width, h);204}205return pref;206}207}208209210