jdk/src/share/classes/sun/swing/WindowsPlacesBar.java
author rupashka
Tue, 19 Apr 2011 10:11:58 +0400
changeset 9486 20a70b3d09c1
parent 9035 1255eb81cc2f
child 15330 1d23649a5422
permissions -rw-r--r--
7036025: java.security.AccessControlException when creating JFileChooser in signed applet Reviewed-by: malenkov
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.*;
9486
20a70b3d09c1 7036025: java.security.AccessControlException when creating JFileChooser in signed applet
rupashka
parents: 9035
diff changeset
    32
import java.security.AccessController;
20a70b3d09c1 7036025: java.security.AccessControlException when creating JFileChooser in signed applet
rupashka
parents: 9035
diff changeset
    33
import java.security.PrivilegedAction;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.swing.border.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.swing.filechooser.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import sun.awt.shell.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import sun.awt.OSInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <b>WARNING:</b> This class is an implementation detail and is only
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * public so that it can be used by two packages. You should NOT consider
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * this public API.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @author Leif Samuelsson
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
public class WindowsPlacesBar extends JToolBar
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
                              implements ActionListener, PropertyChangeListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    JFileChooser fc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    JToggleButton[] buttons;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    ButtonGroup buttonGroup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    File[] files;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    final Dimension buttonSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    public WindowsPlacesBar(JFileChooser fc, boolean isXPStyle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        super(JToolBar.VERTICAL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        this.fc = fc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        setFloatable(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        putClientProperty("JToolBar.isRollover", Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        boolean isXPPlatform = (OSInfo.getOSType() == OSInfo.OSType.WINDOWS &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                OSInfo.getWindowsVersion().compareTo(OSInfo.WINDOWS_XP) >= 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        if (isXPStyle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            buttonSize = new Dimension(83, 69);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            putClientProperty("XPStyle.subAppName", "placesbar");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            setBorder(new EmptyBorder(1, 1, 1, 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            // The button size almost matches the XP style when in Classic style on XP
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            buttonSize = new Dimension(83, isXPPlatform ? 65 : 54);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            setBorder(new BevelBorder(BevelBorder.LOWERED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                                      UIManager.getColor("ToolBar.highlight"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                                      UIManager.getColor("ToolBar.background"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                                      UIManager.getColor("ToolBar.darkShadow"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                                      UIManager.getColor("ToolBar.shadow")));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        Color bgColor = new Color(UIManager.getColor("ToolBar.shadow").getRGB());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        setBackground(bgColor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        FileSystemView fsv = fc.getFileSystemView();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
9486
20a70b3d09c1 7036025: java.security.AccessControlException when creating JFileChooser in signed applet
rupashka
parents: 9035
diff changeset
    84
        files = AccessController.doPrivileged(new PrivilegedAction<File[]>() {
20a70b3d09c1 7036025: java.security.AccessControlException when creating JFileChooser in signed applet
rupashka
parents: 9035
diff changeset
    85
            public File[] run() {
20a70b3d09c1 7036025: java.security.AccessControlException when creating JFileChooser in signed applet
rupashka
parents: 9035
diff changeset
    86
                return (File[]) ShellFolder.get("fileChooserShortcutPanelFolders");
20a70b3d09c1 7036025: java.security.AccessControlException when creating JFileChooser in signed applet
rupashka
parents: 9035
diff changeset
    87
            }
20a70b3d09c1 7036025: java.security.AccessControlException when creating JFileChooser in signed applet
rupashka
parents: 9035
diff changeset
    88
        });
20a70b3d09c1 7036025: java.security.AccessControlException when creating JFileChooser in signed applet
rupashka
parents: 9035
diff changeset
    89
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        buttons = new JToggleButton[files.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        buttonGroup = new ButtonGroup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        for (int i = 0; i < files.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            if (fsv.isFileSystemRoot(files[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                // Create special File wrapper for drive path
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                files[i] = fsv.createFileObject(files[i].getAbsolutePath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            String folderName = fsv.getSystemDisplayName(files[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            int index = folderName.lastIndexOf(File.separatorChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            if (index >= 0 && index < folderName.length() - 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                folderName = folderName.substring(index + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            }
454
2f4f4fef8880 6210674: FileChooser fails to load custom harddrive icon and gets NullPointerException
rupashka
parents: 2
diff changeset
   103
            Icon icon;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            if (files[i] instanceof ShellFolder) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                // We want a large icon, fsv only gives us a small.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                ShellFolder sf = (ShellFolder)files[i];
454
2f4f4fef8880 6210674: FileChooser fails to load custom harddrive icon and gets NullPointerException
rupashka
parents: 2
diff changeset
   107
                Image image = sf.getIcon(true);
2f4f4fef8880 6210674: FileChooser fails to load custom harddrive icon and gets NullPointerException
rupashka
parents: 2
diff changeset
   108
2f4f4fef8880 6210674: FileChooser fails to load custom harddrive icon and gets NullPointerException
rupashka
parents: 2
diff changeset
   109
                if (image == null) {
2f4f4fef8880 6210674: FileChooser fails to load custom harddrive icon and gets NullPointerException
rupashka
parents: 2
diff changeset
   110
                    // Get default image
2f4f4fef8880 6210674: FileChooser fails to load custom harddrive icon and gets NullPointerException
rupashka
parents: 2
diff changeset
   111
                    image = (Image) ShellFolder.get("shell32LargeIcon 1");
2f4f4fef8880 6210674: FileChooser fails to load custom harddrive icon and gets NullPointerException
rupashka
parents: 2
diff changeset
   112
                }
2f4f4fef8880 6210674: FileChooser fails to load custom harddrive icon and gets NullPointerException
rupashka
parents: 2
diff changeset
   113
2f4f4fef8880 6210674: FileChooser fails to load custom harddrive icon and gets NullPointerException
rupashka
parents: 2
diff changeset
   114
                icon = image == null ? null : new ImageIcon(image, sf.getFolderType());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                icon = fsv.getSystemIcon(files[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            buttons[i] = new JToggleButton(folderName, icon);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            if (isXPPlatform) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                buttons[i].setText("<html><center>"+folderName+"</center></html>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            if (isXPStyle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                buttons[i].putClientProperty("XPStyle.subAppName", "placesbar");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                Color fgColor = new Color(UIManager.getColor("List.selectionForeground").getRGB());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                buttons[i].setContentAreaFilled(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                buttons[i].setForeground(fgColor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            buttons[i].setMargin(new Insets(3, 2, 1, 2));
8144
e70d57808ef0 6988176: There is focus painted inside the button.
rupashka
parents: 5506
diff changeset
   130
            buttons[i].setFocusPainted(false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            buttons[i].setIconTextGap(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            buttons[i].setHorizontalTextPosition(JToggleButton.CENTER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            buttons[i].setVerticalTextPosition(JToggleButton.BOTTOM);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            buttons[i].setAlignmentX(JComponent.CENTER_ALIGNMENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            buttons[i].setPreferredSize(buttonSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            buttons[i].setMaximumSize(buttonSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            buttons[i].addActionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            add(buttons[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            if (i < files.length-1 && isXPStyle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                add(Box.createRigidArea(new Dimension(1, 1)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            buttonGroup.add(buttons[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        doDirectoryChanged(fc.getCurrentDirectory());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    protected void doDirectoryChanged(File f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        for (int i=0; i<buttons.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            JToggleButton b = buttons[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            if (files[i].equals(f)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                b.setSelected(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            } else if (b.isSelected()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                // Remove temporarily from group because it doesn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                // allow for no button to be selected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                buttonGroup.remove(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                b.setSelected(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                buttonGroup.add(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            }
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 propertyChange(PropertyChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        String prop = e.getPropertyName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        if (prop == JFileChooser.DIRECTORY_CHANGED_PROPERTY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            doDirectoryChanged(fc.getCurrentDirectory());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        JToggleButton b = (JToggleButton)e.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        for (int i=0; i<buttons.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            if (b == buttons[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                fc.setCurrentDirectory(files[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    public Dimension getPreferredSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        Dimension min  = super.getMinimumSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        Dimension pref = super.getPreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        int h = min.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        if (buttons != null && buttons.length > 0 && buttons.length < 5) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            JToggleButton b = buttons[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            if (b != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                int bh = 5 * (b.getPreferredSize().height + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                if (bh > h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                    h = bh;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        if (h > pref.height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            pref = new Dimension(pref.width, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        return pref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
}