28 import java.awt.Image; 29 import java.beans.PropertyChangeListener; 30 import java.io.File; 31 import java.io.FileNotFoundException; 32 import java.io.IOException; 33 import java.lang.ref.WeakReference; 34 import java.security.AccessController; 35 import java.security.PrivilegedAction; 36 import java.text.MessageFormat; 37 import java.util.ArrayList; 38 import java.util.List; 39 40 import javax.swing.Icon; 41 import javax.swing.ImageIcon; 42 import javax.swing.JFileChooser; 43 import javax.swing.UIManager; 44 45 import jdk.internal.ref.CleanerFactory; 46 import sun.awt.shell.ShellFolder; 47 48 /** 49 * FileSystemView is JFileChooser's gateway to the 50 * file system. Since the JDK1.1 File API doesn't allow 51 * access to such information as root partitions, file type 52 * information, or hidden file bits, this class is designed 53 * to intuit as much OS-specific file system information as 54 * possible. 55 * 56 * <p> 57 * 58 * Java Licensees may want to provide a different implementation of 59 * FileSystemView to better handle a given operating system. 60 * 61 * @author Jeff Dinkins 62 */ 63 64 // PENDING(jeff) - need to provide a specification for 65 // how Mac/OS2/BeOS/etc file systems can modify FileSystemView 66 // to handle their particular type of file system. 67 236 public Icon getSystemIcon(File f) { 237 if (f == null) { 238 return null; 239 } 240 241 ShellFolder sf; 242 243 try { 244 sf = getShellFolder(f); 245 } catch (FileNotFoundException e) { 246 return null; 247 } 248 249 Image img = sf.getIcon(false); 250 251 if (img != null) { 252 return new ImageIcon(img, sf.getFolderType()); 253 } else { 254 return UIManager.getIcon(f.isDirectory() ? "FileView.directoryIcon" : "FileView.fileIcon"); 255 } 256 } 257 258 /** 259 * On Windows, a file can appear in multiple folders, other than its 260 * parent directory in the filesystem. Folder could for example be the 261 * "Desktop" folder which is not the same as file.getParentFile(). 262 * 263 * @param folder a <code>File</code> object representing a directory or special folder 264 * @param file a <code>File</code> object 265 * @return <code>true</code> if <code>folder</code> is a directory or special folder and contains <code>file</code>. 266 * @since 1.4 267 */ 268 public boolean isParent(File folder, File file) { 269 if (folder == null || file == null) { 270 return false; 271 } else if (folder instanceof ShellFolder) { 272 File parent = file.getParentFile(); 273 if (parent != null && parent.equals(folder)) { 274 return true; 275 } | 28 import java.awt.Image; 29 import java.beans.PropertyChangeListener; 30 import java.io.File; 31 import java.io.FileNotFoundException; 32 import java.io.IOException; 33 import java.lang.ref.WeakReference; 34 import java.security.AccessController; 35 import java.security.PrivilegedAction; 36 import java.text.MessageFormat; 37 import java.util.ArrayList; 38 import java.util.List; 39 40 import javax.swing.Icon; 41 import javax.swing.ImageIcon; 42 import javax.swing.JFileChooser; 43 import javax.swing.UIManager; 44 45 import jdk.internal.ref.CleanerFactory; 46 import sun.awt.shell.ShellFolder; 47 48 import java.awt.image.BufferedImage; 49 import java.awt.Graphics2D; 50 import java.awt.GraphicsConfiguration; 51 import java.awt.GraphicsDevice; 52 import java.awt.GraphicsEnvironment; 53 import java.awt.RenderingHints; 54 import java.awt.Transparency; 55 56 /** 57 * FileSystemView is JFileChooser's gateway to the 58 * file system. Since the JDK1.1 File API doesn't allow 59 * access to such information as root partitions, file type 60 * information, or hidden file bits, this class is designed 61 * to intuit as much OS-specific file system information as 62 * possible. 63 * 64 * <p> 65 * 66 * Java Licensees may want to provide a different implementation of 67 * FileSystemView to better handle a given operating system. 68 * 69 * @author Jeff Dinkins 70 */ 71 72 // PENDING(jeff) - need to provide a specification for 73 // how Mac/OS2/BeOS/etc file systems can modify FileSystemView 74 // to handle their particular type of file system. 75 244 public Icon getSystemIcon(File f) { 245 if (f == null) { 246 return null; 247 } 248 249 ShellFolder sf; 250 251 try { 252 sf = getShellFolder(f); 253 } catch (FileNotFoundException e) { 254 return null; 255 } 256 257 Image img = sf.getIcon(false); 258 259 if (img != null) { 260 return new ImageIcon(img, sf.getFolderType()); 261 } else { 262 return UIManager.getIcon(f.isDirectory() ? "FileView.directoryIcon" : "FileView.fileIcon"); 263 } 264 } 265 266 /** 267 * Scaled icon for a file, directory, or folder as it would be displayed in 268 * a system file browser. Example from Windows: the "M:\" directory 269 * displays a CD-ROM icon. 270 * 271 * The default implementation gets information from the ShellFolder class. 272 * 273 * @param f a <code>File</code> object 274 * @param width width of the icon in pixels to be scaled(valid range: 1 to 256) 275 * @param height height of the icon in pixels to be scaled(valid range: 1 to 256) 276 * @return an icon as it would be displayed by a native file chooser 277 * @see JFileChooser#getIcon 278 * @since 12 279 */ 280 public Icon getSystemIcon(File f, int width, int height) { 281 if (f == null) { 282 return null; 283 } 284 285 if((width > 256 || width < 1) || (height > 256 || height < 1)) { 286 return null; 287 } 288 289 ShellFolder sf; 290 try { 291 sf = getShellFolder(f); 292 } catch (FileNotFoundException e) { 293 return null; 294 } 295 296 int size; 297 if(width > height) { 298 size = width; 299 } else { 300 size = height; 301 } 302 303 Image img = sf.getIcon(size); 304 305 // scale the icon in case the width/height does not match the requested 306 if (img != null) { 307 if((img.getWidth(null) != width) || (img.getHeight(null) != height)) { 308 Image scaledImg = scaleIconImage(img, null, width, height); 309 310 // set the scaled icon 311 if(scaledImg != null) { 312 img = scaledImg; 313 } else { 314 return null; 315 } 316 } 317 } else { 318 Icon icon = UIManager.getIcon(f.isDirectory() ? "FileView.directoryIcon" : "FileView.fileIcon"); 319 320 if((icon != null) && ((icon.getIconWidth() != width) || (icon.getIconHeight() != height))) { 321 Image scaledImg = scaleIconImage(null, icon, width, height); 322 323 // set the scaled icon 324 if(scaledImg != null) { 325 img = scaledImg; 326 } else { 327 return null; 328 } 329 } 330 } 331 332 if (img != null) { 333 return new ImageIcon(img, sf.getFolderType()); 334 } 335 336 return null; 337 } 338 339 private static Image scaleIconImage(Image srcImage, Icon srcIcon, int scaledW, int scaledH) { 340 if (srcImage == null && srcIcon == null) { 341 return null; 342 } 343 344 int w; 345 int h; 346 347 if(srcImage != null) { 348 w = srcImage.getWidth(null); 349 h = srcImage.getHeight(null); 350 } else { 351 w = srcIcon.getIconWidth(); 352 h = srcIcon.getIconHeight(); 353 } 354 355 GraphicsEnvironment ge = 356 GraphicsEnvironment.getLocalGraphicsEnvironment(); 357 GraphicsDevice gd = ge.getDefaultScreenDevice(); 358 GraphicsConfiguration gc = gd.getDefaultConfiguration(); 359 360 // convert to image 361 BufferedImage iconImage = gc.createCompatibleImage(w, h, 362 Transparency.TRANSLUCENT); 363 Graphics2D g = iconImage.createGraphics(); 364 if(srcImage != null) { 365 g.drawImage(srcImage, 0, 0, w, h, null); 366 } else { 367 srcIcon.paintIcon(null, g, 0, 0); 368 } 369 g.dispose(); 370 371 // and scale it nicely 372 BufferedImage scaledImage = gc.createCompatibleImage(scaledW, scaledH, 373 Transparency.TRANSLUCENT); 374 g = scaledImage.createGraphics(); 375 g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, 376 RenderingHints.VALUE_INTERPOLATION_BILINEAR); 377 g.drawImage(iconImage, 0, 0, scaledW, scaledH, null); 378 g.dispose(); 379 380 return (Image)scaledImage; 381 } 382 383 /** 384 * On Windows, a file can appear in multiple folders, other than its 385 * parent directory in the filesystem. Folder could for example be the 386 * "Desktop" folder which is not the same as file.getParentFile(). 387 * 388 * @param folder a <code>File</code> object representing a directory or special folder 389 * @param file a <code>File</code> object 390 * @return <code>true</code> if <code>folder</code> is a directory or special folder and contains <code>file</code>. 391 * @since 1.4 392 */ 393 public boolean isParent(File folder, File file) { 394 if (folder == null || file == null) { 395 return false; 396 } else if (folder instanceof ShellFolder) { 397 File parent = file.getParentFile(); 398 if (parent != null && parent.equals(folder)) { 399 return true; 400 } |