jdk/src/share/classes/sun/swing/WindowsPlacesBar.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 8144 e70d57808ef0
child 9486 20a70b3d09c1
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 8144
diff changeset
     2
 * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package sun.swing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.beans.PropertyChangeEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.beans.PropertyChangeListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.swing.border.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.swing.filechooser.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.awt.shell.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import sun.awt.OSInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <b>WARNING:</b> This class is an implementation detail and is only
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * public so that it can be used by two packages. You should NOT consider
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * this public API.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @author Leif Samuelsson
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
public class WindowsPlacesBar extends JToolBar
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
                              implements ActionListener, PropertyChangeListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    JFileChooser fc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    JToggleButton[] buttons;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    ButtonGroup buttonGroup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    File[] files;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    final Dimension buttonSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    public WindowsPlacesBar(JFileChooser fc, boolean isXPStyle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        super(JToolBar.VERTICAL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        this.fc = fc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        setFloatable(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        putClientProperty("JToolBar.isRollover", Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        boolean isXPPlatform = (OSInfo.getOSType() == OSInfo.OSType.WINDOWS &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                OSInfo.getWindowsVersion().compareTo(OSInfo.WINDOWS_XP) >= 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        if (isXPStyle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            buttonSize = new Dimension(83, 69);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            putClientProperty("XPStyle.subAppName", "placesbar");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            setBorder(new EmptyBorder(1, 1, 1, 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            // The button size almost matches the XP style when in Classic style on XP
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            buttonSize = new Dimension(83, isXPPlatform ? 65 : 54);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            setBorder(new BevelBorder(BevelBorder.LOWERED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                                      UIManager.getColor("ToolBar.highlight"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                                      UIManager.getColor("ToolBar.background"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                                      UIManager.getColor("ToolBar.darkShadow"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                                      UIManager.getColor("ToolBar.shadow")));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        Color bgColor = new Color(UIManager.getColor("ToolBar.shadow").getRGB());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        setBackground(bgColor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        FileSystemView fsv = fc.getFileSystemView();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        files = (File[])ShellFolder.get("fileChooserShortcutPanelFolders");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        buttons = new JToggleButton[files.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        buttonGroup = new ButtonGroup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        for (int i = 0; i < files.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            if (fsv.isFileSystemRoot(files[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                // Create special File wrapper for drive path
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                files[i] = fsv.createFileObject(files[i].getAbsolutePath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            String folderName = fsv.getSystemDisplayName(files[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            int index = folderName.lastIndexOf(File.separatorChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            if (index >= 0 && index < folderName.length() - 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                folderName = folderName.substring(index + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            }
454
2f4f4fef8880 6210674: FileChooser fails to load custom harddrive icon and gets NullPointerException
rupashka
parents: 2
diff changeset
    96
            Icon icon;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            if (files[i] instanceof ShellFolder) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                // We want a large icon, fsv only gives us a small.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                ShellFolder sf = (ShellFolder)files[i];
454
2f4f4fef8880 6210674: FileChooser fails to load custom harddrive icon and gets NullPointerException
rupashka
parents: 2
diff changeset
   100
                Image image = sf.getIcon(true);
2f4f4fef8880 6210674: FileChooser fails to load custom harddrive icon and gets NullPointerException
rupashka
parents: 2
diff changeset
   101
2f4f4fef8880 6210674: FileChooser fails to load custom harddrive icon and gets NullPointerException
rupashka
parents: 2
diff changeset
   102
                if (image == null) {
2f4f4fef8880 6210674: FileChooser fails to load custom harddrive icon and gets NullPointerException
rupashka
parents: 2
diff changeset
   103
                    // Get default image
2f4f4fef8880 6210674: FileChooser fails to load custom harddrive icon and gets NullPointerException
rupashka
parents: 2
diff changeset
   104
                    image = (Image) ShellFolder.get("shell32LargeIcon 1");
2f4f4fef8880 6210674: FileChooser fails to load custom harddrive icon and gets NullPointerException
rupashka
parents: 2
diff changeset
   105
                }
2f4f4fef8880 6210674: FileChooser fails to load custom harddrive icon and gets NullPointerException
rupashka
parents: 2
diff changeset
   106
2f4f4fef8880 6210674: FileChooser fails to load custom harddrive icon and gets NullPointerException
rupashka
parents: 2
diff changeset
   107
                icon = image == null ? null : new ImageIcon(image, sf.getFolderType());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                icon = fsv.getSystemIcon(files[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            buttons[i] = new JToggleButton(folderName, icon);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            if (isXPPlatform) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                buttons[i].setText("<html><center>"+folderName+"</center></html>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            if (isXPStyle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                buttons[i].putClientProperty("XPStyle.subAppName", "placesbar");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                Color fgColor = new Color(UIManager.getColor("List.selectionForeground").getRGB());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                buttons[i].setContentAreaFilled(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                buttons[i].setForeground(fgColor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            buttons[i].setMargin(new Insets(3, 2, 1, 2));
8144
e70d57808ef0 6988176: There is focus painted inside the button.
rupashka
parents: 5506
diff changeset
   123
            buttons[i].setFocusPainted(false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            buttons[i].setIconTextGap(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            buttons[i].setHorizontalTextPosition(JToggleButton.CENTER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            buttons[i].setVerticalTextPosition(JToggleButton.BOTTOM);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            buttons[i].setAlignmentX(JComponent.CENTER_ALIGNMENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            buttons[i].setPreferredSize(buttonSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            buttons[i].setMaximumSize(buttonSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            buttons[i].addActionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            add(buttons[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            if (i < files.length-1 && isXPStyle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                add(Box.createRigidArea(new Dimension(1, 1)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            buttonGroup.add(buttons[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        doDirectoryChanged(fc.getCurrentDirectory());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    protected void doDirectoryChanged(File f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        for (int i=0; i<buttons.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            JToggleButton b = buttons[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            if (files[i].equals(f)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                b.setSelected(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            } else if (b.isSelected()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                // Remove temporarily from group because it doesn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                // allow for no button to be selected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                buttonGroup.remove(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                b.setSelected(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                buttonGroup.add(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    public void propertyChange(PropertyChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        String prop = e.getPropertyName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        if (prop == JFileChooser.DIRECTORY_CHANGED_PROPERTY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            doDirectoryChanged(fc.getCurrentDirectory());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        JToggleButton b = (JToggleButton)e.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        for (int i=0; i<buttons.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            if (b == buttons[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                fc.setCurrentDirectory(files[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    public Dimension getPreferredSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        Dimension min  = super.getMinimumSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        Dimension pref = super.getPreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        int h = min.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        if (buttons != null && buttons.length > 0 && buttons.length < 5) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            JToggleButton b = buttons[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            if (b != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                int bh = 5 * (b.getPreferredSize().height + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                if (bh > h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                    h = bh;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        if (h > pref.height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            pref = new Dimension(pref.width, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        return pref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
}