< prev index next >

src/java.desktop/share/classes/javax/swing/filechooser/FileSystemView.java

Print this page




  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
 275      * @param height height of the icon in pixels to be scaled
 276      * @return an icon as it would be displayed by a native file chooser
 277      * @exception RuntimeException if the icon failes to be scaled or
 278      * @exception IllegalArgumentException if the icon scaling is out of range
 279      * @see JFileChooser#getIcon
 280      * @since 12
 281      */
 282     public Icon getSystemIcon(File f, int width, int height) {
 283         if (f == null) {
 284             return null;
 285         }
 286 
 287         if((width > 256 || width < 1) || (height > 256 || height < 1)) {
 288             System.err.println("Warning: Icon scaling may be distorted");
 289             throw new IllegalArgumentException("unexpected icon scaling size");
 290         }
 291 
 292         ShellFolder sf;
 293         try {
 294             sf = getShellFolder(f);
 295         } catch (FileNotFoundException e) {
 296             return null;
 297         }
 298 
 299         int size;
 300         if(width > height) {
 301             size = width;
 302         } else {
 303             size = height;
 304         }
 305 
 306         Image img = sf.getIcon(size);
 307 
 308         // scale the icon in case the width/height does not match the requested
 309         if (img != null) {
 310             if((img.getWidth(null) != width) || (img.getHeight(null) != height)) {
 311                 Image scaledImg = scaleIconImage(img, width, height);
 312 
 313                 // set the scaled icon
 314                 if(scaledImg != null) {
 315                     img = scaledImg;
 316                 } else {
 317                     throw new RuntimeException("unable to scale the icon");
 318                 }
 319             }
 320         } else {
 321             Icon icon = UIManager.getIcon(f.isDirectory() ? "FileView.directoryIcon" : "FileView.fileIcon");
 322 
 323             if((icon != null) && ((icon.getIconWidth() != width) || (icon.getIconHeight() != height))) {
 324                 Image scaledImg = scaleIcon(icon, width, height);
 325 
 326                 // set the scaled icon
 327                 if(scaledImg != null) {
 328                     img = scaledImg;
 329                 } else {
 330                     throw new RuntimeException("unable to scale the icon");
 331                 }
 332             }
 333         }
 334 
 335         if (img != null) {
 336             return new ImageIcon(img, sf.getFolderType());
 337         }
 338 
 339         return null;
 340     }
 341 
 342     private static Image scaleIconImage(Image icon, int scaledW, int scaledH) {
 343         if (icon == null) {
 344             return null;
 345         }
 346 
 347         int w = icon.getWidth(null);
 348         int h = icon.getHeight(null);
 349 
 350         GraphicsEnvironment ge =
 351                 GraphicsEnvironment.getLocalGraphicsEnvironment();
 352         GraphicsDevice gd = ge.getDefaultScreenDevice();
 353         GraphicsConfiguration gc = gd.getDefaultConfiguration();
 354 
 355         // convert icon into image
 356         BufferedImage iconImage = gc.createCompatibleImage(w, h,
 357                 Transparency.TRANSLUCENT);
 358         Graphics2D g = iconImage.createGraphics();
 359         g.drawImage(icon, 0, 0, w, h, null);
 360         g.dispose();
 361 
 362         // and scale it nicely
 363         BufferedImage scaledImage = gc.createCompatibleImage(scaledW, scaledH,
 364                 Transparency.TRANSLUCENT);
 365         g = scaledImage.createGraphics();
 366         g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
 367                 RenderingHints.VALUE_INTERPOLATION_BILINEAR);
 368         g.drawImage(iconImage, 0, 0, scaledW, scaledH, null);
 369         g.dispose();
 370 
 371         return (Image)scaledImage;
 372     }
 373 
 374     private static Image scaleIcon(Icon icon, int scaledW, int scaledH) {
 375         if (icon == null) {
 376             return null;
 377         }
 378 
 379         int w = icon.getIconWidth();
 380         int h = icon.getIconHeight();
 381 
 382         GraphicsEnvironment ge =
 383                 GraphicsEnvironment.getLocalGraphicsEnvironment();
 384         GraphicsDevice gd = ge.getDefaultScreenDevice();
 385         GraphicsConfiguration gc = gd.getDefaultConfiguration();
 386 
 387         // convert icon into image
 388         BufferedImage iconImage = gc.createCompatibleImage(w, h,
 389                 Transparency.TRANSLUCENT);
 390         Graphics2D g = iconImage.createGraphics();
 391         icon.paintIcon(null, g, 0, 0);
 392         g.dispose();
 393 
 394         // and scale it nicely
 395         BufferedImage scaledImage = gc.createCompatibleImage(scaledW, scaledH,
 396                 Transparency.TRANSLUCENT);
 397         g = scaledImage.createGraphics();
 398         g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
 399                 RenderingHints.VALUE_INTERPOLATION_BILINEAR);
 400         g.drawImage(iconImage, 0, 0, scaledW, scaledH, null);
 401         g.dispose();
 402 
 403         return (Image)scaledImage;
 404     }
 405 
 406     /**
 407      * On Windows, a file can appear in multiple folders, other than its
 408      * parent directory in the filesystem. Folder could for example be the
 409      * "Desktop" folder which is not the same as file.getParentFile().
 410      *
 411      * @param folder a <code>File</code> object representing a directory or special folder
 412      * @param file a <code>File</code> object
 413      * @return <code>true</code> if <code>folder</code> is a directory or special folder and contains <code>file</code>.
 414      * @since 1.4
 415      */
 416     public boolean isParent(File folder, File file) {
 417         if (folder == null || file == null) {
 418             return false;
 419         } else if (folder instanceof ShellFolder) {
 420                 File parent = file.getParentFile();
 421                 if (parent != null && parent.equals(folder)) {
 422                     return true;
 423                 }


< prev index next >