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