< prev index next >

src/java.desktop/share/classes/javax/swing/plaf/basic/BasicFileChooserUI.java

Print this page




  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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javax.swing.plaf.basic;
  27 
  28 import javax.swing.*;
  29 import javax.swing.filechooser.*;
  30 import javax.swing.filechooser.FileFilter;
  31 import javax.swing.event.*;
  32 import javax.swing.plaf.*;
  33 import java.awt.*;
  34 import java.awt.event.*;
  35 import java.awt.datatransfer.*;


  36 import java.beans.*;
  37 import java.io.*;


  38 import java.util.*;
  39 import java.util.List;
  40 import java.util.regex.*;


  41 import sun.awt.shell.ShellFolder;
  42 import sun.swing.*;
  43 import sun.swing.SwingUtilities2;
  44 
  45 /**
  46  * Basic L&amp;F implementation of a FileChooser.
  47  *
  48  * @author Jeff Dinkins
  49  */
  50 public class BasicFileChooserUI extends FileChooserUI {
  51 
  52     /* FileView icons */
  53     /** Directory icon */
  54     protected Icon directoryIcon = null;
  55     /** File icon */
  56     protected Icon fileIcon = null;
  57     /** Computer icon */
  58     protected Icon computerIcon = null;
  59     /** Hard drive icon */
  60     protected Icon hardDriveIcon = null;


1329             getFileChooser().cancelSelection();
1330         }
1331     }
1332 
1333     /**
1334      * Rescans the files in the current directory
1335      */
1336     @SuppressWarnings("serial") // Superclass is not serializable across versions
1337     protected class UpdateAction extends AbstractAction {
1338         /** {@inheritDoc} */
1339         public void actionPerformed(ActionEvent e) {
1340             JFileChooser fc = getFileChooser();
1341             fc.setCurrentDirectory(fc.getFileSystemView().createFileObject(getDirectoryName()));
1342             fc.rescanCurrentDirectory();
1343         }
1344     }
1345 
1346 
1347     private void changeDirectory(File dir) {
1348         JFileChooser fc = getFileChooser();




1349         // Traverse shortcuts on Windows
1350         if (dir != null && FilePane.usesShellFolder(fc)) {
1351             try {
1352                 ShellFolder shellFolder = ShellFolder.getShellFolder(dir);
1353 
1354                 if (shellFolder.isLink()) {
1355                     File linkedTo = shellFolder.getLinkLocation();
1356 
1357                     // If linkedTo is null we try to use dir
1358                     if (linkedTo != null) {
1359                         if (fc.isTraversable(linkedTo)) {
1360                             dir = linkedTo;
1361                         } else {
1362                             return;
1363                         }
1364                     } else {
1365                         dir = shellFolder;
1366                     }
1367                 }
1368             } catch (FileNotFoundException ex) {
1369                 return;
1370             }
1371         }
1372         fc.setCurrentDirectory(dir);
1373         if (fc.getFileSelectionMode() == JFileChooser.FILES_AND_DIRECTORIES &&
1374             fc.getFileSystemView().isFileSystem(dir)) {
1375 
1376             setFileName(dir.getAbsolutePath());
1377         }


1378     }
1379 
1380 
1381     // *****************************************
1382     // ***** default AcceptAll file filter *****
1383     // *****************************************
1384     /**
1385      * Accept all file filter.
1386      */
1387     protected class AcceptAllFileFilter extends FileFilter {
1388 
1389         /** Constructs an {@code AcceptAllFileFilter}. */
1390         public AcceptAllFileFilter() {
1391         }
1392 
1393         /**
1394          * Returns true.
1395          * @param f the file
1396          * @return true
1397          */


1538          */
1539         @SuppressWarnings("deprecation")
1540         protected Transferable createTransferable(JComponent c) {
1541             Object[] values = null;
1542             if (c instanceof JList) {
1543                 values = ((JList)c).getSelectedValues();
1544             } else if (c instanceof JTable) {
1545                 JTable table = (JTable)c;
1546                 int[] rows = table.getSelectedRows();
1547                 if (rows != null) {
1548                     values = new Object[rows.length];
1549                     for (int i=0; i<rows.length; i++) {
1550                         values[i] = table.getValueAt(rows[i], 0);
1551                     }
1552                 }
1553             }
1554             if (values == null || values.length == 0) {
1555                 return null;
1556             }
1557 


































1558             StringBuilder plainBuf = new StringBuilder();
1559             StringBuilder htmlBuf = new StringBuilder();
1560 
1561             htmlBuf.append("<html>\n<body>\n<ul>\n");
1562 
1563             for (Object obj : values) {
1564                 String val = ((obj == null) ? "" : obj.toString());
1565                 plainBuf.append(val).append('\n');
1566                 htmlBuf.append("  <li>").append(val).append('\n');
1567             }
1568 
1569             // remove the last newline
1570             plainBuf.deleteCharAt(plainBuf.length() - 1);
1571             htmlBuf.append("</ul>\n</body>\n</html>");
1572 
1573             return new FileTransferable(plainBuf.toString(), htmlBuf.toString(), values);
1574         }
1575 
1576         public int getSourceActions(JComponent c) {
1577             return COPY;
1578         }
1579 
1580         static class FileTransferable extends BasicTransferable {
1581 
1582             Object[] fileData;
1583 
1584             FileTransferable(String plainData, String htmlData, Object[] fileData) {
1585                 super(plainData, htmlData);
1586                 this.fileData = fileData;
1587             }
1588 
1589             /**
1590              * Best format of the file chooser is DataFlavor.javaFileListFlavor.
1591              */
1592             protected DataFlavor[] getRicherFlavors() {
1593                 DataFlavor[] flavors = new DataFlavor[1];
1594                 flavors[0] = DataFlavor.javaFileListFlavor;
1595                 return flavors;
1596             }
1597 


  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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javax.swing.plaf.basic;
  27 
  28 import javax.swing.*;
  29 import javax.swing.filechooser.*;
  30 import javax.swing.filechooser.FileFilter;
  31 import javax.swing.event.*;
  32 import javax.swing.plaf.*;
  33 import java.awt.*;
  34 import java.awt.event.*;
  35 import java.awt.datatransfer.*;
  36 import java.awt.image.BufferedImage;
  37 import static java.awt.image.BufferedImage.TYPE_INT_ARGB;
  38 import java.beans.*;
  39 import java.io.*;
  40 import java.nio.file.Files;
  41 import java.nio.file.Path;
  42 import java.util.*;
  43 import java.util.List;
  44 import java.util.regex.*;
  45 import static java.util.stream.Collectors.toList;
  46 import javax.swing.tree.TreePath;
  47 import sun.awt.shell.ShellFolder;
  48 import sun.swing.*;
  49 import sun.swing.SwingUtilities2;
  50 
  51 /**
  52  * Basic L&amp;F implementation of a FileChooser.
  53  *
  54  * @author Jeff Dinkins
  55  */
  56 public class BasicFileChooserUI extends FileChooserUI {
  57 
  58     /* FileView icons */
  59     /** Directory icon */
  60     protected Icon directoryIcon = null;
  61     /** File icon */
  62     protected Icon fileIcon = null;
  63     /** Computer icon */
  64     protected Icon computerIcon = null;
  65     /** Hard drive icon */
  66     protected Icon hardDriveIcon = null;


1335             getFileChooser().cancelSelection();
1336         }
1337     }
1338 
1339     /**
1340      * Rescans the files in the current directory
1341      */
1342     @SuppressWarnings("serial") // Superclass is not serializable across versions
1343     protected class UpdateAction extends AbstractAction {
1344         /** {@inheritDoc} */
1345         public void actionPerformed(ActionEvent e) {
1346             JFileChooser fc = getFileChooser();
1347             fc.setCurrentDirectory(fc.getFileSystemView().createFileObject(getDirectoryName()));
1348             fc.rescanCurrentDirectory();
1349         }
1350     }
1351 
1352 
1353     private void changeDirectory(File dir) {
1354         JFileChooser fc = getFileChooser();
1355         
1356         Cursor prevCursor = fc.getCursor();
1357         fc.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
1358         
1359         // Traverse shortcuts on Windows
1360         if (dir != null && FilePane.usesShellFolder(fc)) {
1361             try {
1362                 ShellFolder shellFolder = ShellFolder.getShellFolder(dir);
1363 
1364                 if (shellFolder.isLink()) {
1365                     File linkedTo = shellFolder.getLinkLocation();
1366 
1367                     // If linkedTo is null we try to use dir
1368                     if (linkedTo != null) {
1369                         if (fc.isTraversable(linkedTo)) {
1370                             dir = linkedTo;
1371                         } else {
1372                             return;
1373                         }
1374                     } else {
1375                         dir = shellFolder;
1376                     }
1377                 }
1378             } catch (FileNotFoundException ex) {
1379                 return;
1380             }
1381         }
1382         fc.setCurrentDirectory(dir);
1383         if (fc.getFileSelectionMode() == JFileChooser.FILES_AND_DIRECTORIES &&
1384             fc.getFileSystemView().isFileSystem(dir)) {
1385 
1386             setFileName(dir.getAbsolutePath());
1387         }
1388         
1389         fc.setCursor(prevCursor);
1390     }
1391 
1392 
1393     // *****************************************
1394     // ***** default AcceptAll file filter *****
1395     // *****************************************
1396     /**
1397      * Accept all file filter.
1398      */
1399     protected class AcceptAllFileFilter extends FileFilter {
1400 
1401         /** Constructs an {@code AcceptAllFileFilter}. */
1402         public AcceptAllFileFilter() {
1403         }
1404 
1405         /**
1406          * Returns true.
1407          * @param f the file
1408          * @return true
1409          */


1550          */
1551         @SuppressWarnings("deprecation")
1552         protected Transferable createTransferable(JComponent c) {
1553             Object[] values = null;
1554             if (c instanceof JList) {
1555                 values = ((JList)c).getSelectedValues();
1556             } else if (c instanceof JTable) {
1557                 JTable table = (JTable)c;
1558                 int[] rows = table.getSelectedRows();
1559                 if (rows != null) {
1560                     values = new Object[rows.length];
1561                     for (int i=0; i<rows.length; i++) {
1562                         values[i] = table.getValueAt(rows[i], 0);
1563                     }
1564                 }
1565             }
1566             if (values == null || values.length == 0) {
1567                 return null;
1568             }
1569             
1570             try {
1571                 BufferedImage img = null;
1572                 Graphics2D g = null;
1573                 for (Object value : values) {
1574                     File f=(File) value;
1575                     Image icon = ShellFolder.getShellFolder(f).getIcon(true);
1576                     if(img == null) {
1577                         img = new BufferedImage(icon.getWidth(null)+20, 
1578                             icon.getHeight(null)+20, TYPE_INT_ARGB);
1579                         g = img.createGraphics();
1580                         
1581                         int w = img.getWidth()-2;
1582                         int h = img.getHeight()-2;
1583                         g.setPaint(new GradientPaint(
1584                                 0, 0, new Color(0, 149, 234, 12), 
1585                                 0, h, new Color(0, 155, 237, 28)
1586                         ));
1587                         g.fillRoundRect(0, 0, w, h, 4, 4);
1588                         
1589                         g.setColor(new Color(0, 170, 248, 36));
1590                         g.drawRoundRect(0, 0, w, h, 4, 4);
1591                     }
1592                     g.drawImage(icon, 
1593                             img.getWidth()/2-icon.getWidth(null)/2, 
1594                             img.getHeight() / 2-icon.getHeight(null)/2, null);
1595                 }
1596                 setDragImage(img);
1597                 setDragImageOffset(new Point(
1598                         img.getWidth()/2, img.getHeight()-5));
1599             } catch (FileNotFoundException ex) {
1600                 ex.printStackTrace();
1601                 setDragImage(null);
1602             }
1603 
1604             StringBuilder plainBuf = new StringBuilder();
1605             StringBuilder htmlBuf = new StringBuilder();
1606 
1607             htmlBuf.append("<html>\n<body>\n<ul>\n");
1608 
1609             for (Object obj : values) {
1610                 String val = ((obj == null) ? "" : obj.toString());
1611                 plainBuf.append(val).append('\n');
1612                 htmlBuf.append("  <li>").append(val).append('\n');
1613             }
1614 
1615             // remove the last newline
1616             plainBuf.deleteCharAt(plainBuf.length() - 1);
1617             htmlBuf.append("</ul>\n</body>\n</html>");
1618 
1619             return new FileTransferable(plainBuf.toString(), htmlBuf.toString(), values);
1620         }
1621 
1622         public int getSourceActions(JComponent c) {
1623             return COPY|MOVE;
1624         }
1625 
1626         static class FileTransferable extends BasicTransferable {
1627 
1628             Object[] fileData;
1629 
1630             FileTransferable(String plainData, String htmlData, Object[] fileData) {
1631                 super(plainData, htmlData);
1632                 this.fileData = fileData;
1633             }
1634 
1635             /**
1636              * Best format of the file chooser is DataFlavor.javaFileListFlavor.
1637              */
1638             protected DataFlavor[] getRicherFlavors() {
1639                 DataFlavor[] flavors = new DataFlavor[1];
1640                 flavors[0] = DataFlavor.javaFileListFlavor;
1641                 return flavors;
1642             }
1643 
< prev index next >