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