Advertisement
Guest User

Untitled

a guest
May 24th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 26.38 KB | None | 0 0
  1. import java.awt.EventQueue;
  2.  
  3. import javax.swing.JFrame;
  4.  
  5. import java.awt.BorderLayout;
  6. import java.awt.FlowLayout;
  7. import java.awt.GridLayout;
  8. import java.awt.Desktop;
  9. import java.awt.Dimension;
  10. import java.awt.Container;
  11. import java.awt.Component;
  12. import java.awt.Graphics;
  13. import java.awt.Image;
  14. import java.awt.event.*;
  15. import java.awt.image.*;
  16.  
  17. import javax.swing.*;
  18. import javax.swing.border.*;
  19. import javax.swing.event.*;
  20. import javax.swing.tree.*;
  21. import javax.swing.table.*;
  22. import javax.swing.filechooser.FileSystemView;
  23.  
  24. import javax.imageio.ImageIO;
  25.  
  26. import java.util.Date;
  27. import java.util.List;
  28. import java.util.ArrayList;
  29.  
  30. import java.io.*;
  31. import java.nio.channels.FileChannel;
  32.  
  33. import java.net.URL;
  34.  
  35.  
  36. public class fileexplorer {
  37.  
  38. /** Title of the application */
  39. public static final String APP_TITLE = "FileMan";
  40. /** Used to open/edit/print files. */
  41. private Desktop desktop;
  42. /** Provides nice icons and names for files. */
  43. private FileSystemView fileSystemView;
  44.  
  45. /** currently selected File. */
  46. private File currentFile;
  47.  
  48. /** Main GUI container */
  49. private JPanel gui;
  50.  
  51. /** File-system tree. Built Lazily */
  52. private JTree tree;
  53. private DefaultTreeModel treeModel;
  54.  
  55. /** Directory listing */
  56. private JTable table;
  57. private JProgressBar progressBar;
  58. /** Table model for File[]. */
  59. private FileTableModel fileTableModel;
  60. private ListSelectionListener listSelectionListener;
  61. private boolean cellSizesSet = false;
  62. private int rowIconPadding = 6;
  63.  
  64. /* File controls. */
  65. private JButton openFile;
  66. private JButton printFile;
  67. private JButton editFile;
  68. private JButton deleteFile;
  69. private JButton newFile;
  70. private JButton copyFile;
  71. /* File details. */
  72. private JLabel fileName;
  73. private JTextField path;
  74. private JLabel date;
  75. private JLabel size;
  76. private JCheckBox readable;
  77. private JCheckBox writable;
  78. private JCheckBox executable;
  79. private JRadioButton isDirectory;
  80. private JRadioButton isFile;
  81.  
  82. /* GUI options/containers for new File/Directory creation. Created lazily. */
  83. private JPanel newFilePanel;
  84. private JRadioButton newTypeFile;
  85. private JTextField name;
  86.  
  87. public Container getGui() {
  88. if (gui==null) {
  89. gui = new JPanel(new BorderLayout(3,3));
  90. gui.setBorder(new EmptyBorder(5,5,5,5));
  91.  
  92. fileSystemView = FileSystemView.getFileSystemView();
  93. desktop = Desktop.getDesktop();
  94.  
  95. JPanel detailView = new JPanel(new BorderLayout(3,3));
  96. //fileTableModel = new FileTableModel();
  97.  
  98. table = new JTable();
  99. table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  100. table.setAutoCreateRowSorter(true);
  101. table.setShowVerticalLines(false);
  102.  
  103. listSelectionListener = new ListSelectionListener() {
  104. @Override
  105. public void valueChanged(ListSelectionEvent lse) {
  106. int row = table.getSelectionModel().getLeadSelectionIndex();
  107. setFileDetails( ((FileTableModel)table.getModel()).getFile(row) );
  108. }
  109. };
  110. table.getSelectionModel().addListSelectionListener(listSelectionListener);
  111. JScrollPane tableScroll = new JScrollPane(table);
  112. Dimension d = tableScroll.getPreferredSize();
  113. tableScroll.setPreferredSize(new Dimension((int)d.getWidth(), (int)d.getHeight()/2));
  114. detailView.add(tableScroll, BorderLayout.CENTER);
  115.  
  116. // the File tree
  117. DefaultMutableTreeNode root = new DefaultMutableTreeNode();
  118. treeModel = new DefaultTreeModel(root);
  119.  
  120. TreeSelectionListener treeSelectionListener = new TreeSelectionListener() {
  121. public void valueChanged(TreeSelectionEvent tse){
  122. DefaultMutableTreeNode node =
  123. (DefaultMutableTreeNode)tse.getPath().getLastPathComponent();
  124. showChildren(node);
  125. setFileDetails((File)node.getUserObject());
  126. }
  127. };
  128.  
  129. // show the file system roots.
  130. File[] roots = fileSystemView.getRoots();
  131. for (File fileSystemRoot : roots) {
  132. DefaultMutableTreeNode node = new DefaultMutableTreeNode(fileSystemRoot);
  133. root.add( node );
  134. //showChildren(node);
  135. //
  136. File[] files = fileSystemView.getFiles(fileSystemRoot, true);
  137. for (File file : files) {
  138. if (file.isDirectory()) {
  139. node.add(new DefaultMutableTreeNode(file));
  140. }
  141. }
  142. //
  143. }
  144.  
  145. tree = new JTree(treeModel);
  146. tree.setRootVisible(false);
  147. tree.addTreeSelectionListener(treeSelectionListener);
  148. tree.setCellRenderer(new FileTreeCellRenderer());
  149. tree.expandRow(0);
  150. JScrollPane treeScroll = new JScrollPane(tree);
  151.  
  152. // as per trashgod tip
  153. tree.setVisibleRowCount(15);
  154.  
  155. Dimension preferredSize = treeScroll.getPreferredSize();
  156. Dimension widePreferred = new Dimension(
  157. 200,
  158. (int)preferredSize.getHeight());
  159. treeScroll.setPreferredSize( widePreferred );
  160.  
  161. // details for a File
  162. JPanel fileMainDetails = new JPanel(new BorderLayout(4,2));
  163. fileMainDetails.setBorder(new EmptyBorder(0,6,0,6));
  164.  
  165. JPanel fileDetailsLabels = new JPanel(new GridLayout(0,1,2,2));
  166. fileMainDetails.add(fileDetailsLabels, BorderLayout.WEST);
  167.  
  168. JPanel fileDetailsValues = new JPanel(new GridLayout(0,1,2,2));
  169. fileMainDetails.add(fileDetailsValues, BorderLayout.CENTER);
  170.  
  171. fileDetailsLabels.add(new JLabel("File", JLabel.TRAILING));
  172. fileName = new JLabel();
  173. fileDetailsValues.add(fileName);
  174. fileDetailsLabels.add(new JLabel("Path/name", JLabel.TRAILING));
  175. path = new JTextField(5);
  176. path.setEditable(false);
  177. fileDetailsValues.add(path);
  178. fileDetailsLabels.add(new JLabel("Last Modified", JLabel.TRAILING));
  179. date = new JLabel();
  180. fileDetailsValues.add(date);
  181. fileDetailsLabels.add(new JLabel("File size", JLabel.TRAILING));
  182. size = new JLabel();
  183. fileDetailsValues.add(size);
  184. fileDetailsLabels.add(new JLabel("Type", JLabel.TRAILING));
  185.  
  186. JPanel flags = new JPanel(new FlowLayout(FlowLayout.LEADING,4,0));
  187. isDirectory = new JRadioButton("Directory");
  188. isDirectory.setEnabled(false);
  189. flags.add(isDirectory);
  190.  
  191. isFile = new JRadioButton("File");
  192. isFile.setEnabled(false);
  193. flags.add(isFile);
  194. fileDetailsValues.add(flags);
  195.  
  196. int count = fileDetailsLabels.getComponentCount();
  197. for (int ii=0; ii<count; ii++) {
  198. fileDetailsLabels.getComponent(ii).setEnabled(false);
  199. }
  200.  
  201. JToolBar toolBar = new JToolBar();
  202. // mnemonics stop working in a floated toolbar
  203. toolBar.setFloatable(false);
  204.  
  205.  
  206. toolBar.add(deleteFile);
  207.  
  208. toolBar.addSeparator();
  209.  
  210. readable = new JCheckBox("Read ");
  211. readable.setMnemonic('a');
  212. //readable.setEnabled(false);
  213. toolBar.add(readable);
  214.  
  215. writable = new JCheckBox("Write ");
  216. writable.setMnemonic('w');
  217. //writable.setEnabled(false);
  218. toolBar.add(writable);
  219.  
  220. executable = new JCheckBox("Execute");
  221. executable.setMnemonic('x');
  222. //executable.setEnabled(false);
  223. toolBar.add(executable);
  224.  
  225. JPanel fileView = new JPanel(new BorderLayout(3,3));
  226.  
  227. fileView.add(toolBar,BorderLayout.NORTH);
  228. fileView.add(fileMainDetails,BorderLayout.CENTER);
  229.  
  230. detailView.add(fileView, BorderLayout.SOUTH);
  231.  
  232. JSplitPane splitPane = new JSplitPane(
  233. JSplitPane.HORIZONTAL_SPLIT,
  234. treeScroll,
  235. detailView);
  236. gui.add(splitPane, BorderLayout.CENTER);
  237.  
  238. JPanel simpleOutput = new JPanel(new BorderLayout(3,3));
  239. progressBar = new JProgressBar();
  240. simpleOutput.add(progressBar, BorderLayout.EAST);
  241. progressBar.setVisible(false);
  242.  
  243. gui.add(simpleOutput, BorderLayout.SOUTH);
  244.  
  245. }
  246. return gui;
  247. }
  248.  
  249. public void showRootFile() {
  250. // ensure the main files are displayed
  251. tree.setSelectionInterval(0,0);
  252. }
  253.  
  254. private TreePath findTreePath(File find) {
  255. for (int ii=0; ii<tree.getRowCount(); ii++) {
  256. TreePath treePath = tree.getPathForRow(ii);
  257. Object object = treePath.getLastPathComponent();
  258. DefaultMutableTreeNode node = (DefaultMutableTreeNode)object;
  259. File nodeFile = (File)node.getUserObject();
  260.  
  261. if (nodeFile==find) {
  262. return treePath;
  263. }
  264. }
  265. // not found!
  266. return null;
  267. }
  268.  
  269. private void renameFile() {
  270. if (currentFile==null) {
  271. showErrorMessage("No file selected to rename.","Select File");
  272. return;
  273. }
  274.  
  275. String renameTo = JOptionPane.showInputDialog(gui, "New Name");
  276. if (renameTo!=null) {
  277. try {
  278. boolean directory = currentFile.isDirectory();
  279. TreePath parentPath = findTreePath(currentFile.getParentFile());
  280. DefaultMutableTreeNode parentNode =
  281. (DefaultMutableTreeNode)parentPath.getLastPathComponent();
  282.  
  283. boolean renamed = currentFile.renameTo(new File(
  284. currentFile.getParentFile(), renameTo));
  285. if (renamed) {
  286. if (directory) {
  287. // rename the node..
  288.  
  289. // delete the current node..
  290. TreePath currentPath = findTreePath(currentFile);
  291. System.out.println(currentPath);
  292. DefaultMutableTreeNode currentNode =
  293. (DefaultMutableTreeNode)currentPath.getLastPathComponent();
  294.  
  295. treeModel.removeNodeFromParent(currentNode);
  296.  
  297. // add a new node..
  298. }
  299.  
  300. showChildren(parentNode);
  301. } else {
  302. String msg = "The file '" +
  303. currentFile +
  304. "' could not be renamed.";
  305. showErrorMessage(msg,"Rename Failed");
  306. }
  307. } catch(Throwable t) {
  308. showThrowable(t);
  309. }
  310. }
  311. gui.repaint();
  312. }
  313.  
  314. private void deleteFile() {
  315. if (currentFile==null) {
  316. showErrorMessage("No file selected for deletion.","Select File");
  317. return;
  318. }
  319.  
  320. int result = JOptionPane.showConfirmDialog(
  321. gui,
  322. "Are you sure you want to delete this file?",
  323. "Delete File",
  324. JOptionPane.ERROR_MESSAGE
  325. );
  326. if (result==JOptionPane.OK_OPTION) {
  327. try {
  328. System.out.println("currentFile: " + currentFile);
  329. TreePath parentPath = findTreePath(currentFile.getParentFile());
  330. System.out.println("parentPath: " + parentPath);
  331. DefaultMutableTreeNode parentNode =
  332. (DefaultMutableTreeNode)parentPath.getLastPathComponent();
  333. System.out.println("parentNode: " + parentNode);
  334.  
  335. boolean directory = currentFile.isDirectory();
  336. boolean deleted = currentFile.delete();
  337. if (deleted) {
  338. if (directory) {
  339. // delete the node..
  340. TreePath currentPath = findTreePath(currentFile);
  341. System.out.println(currentPath);
  342. DefaultMutableTreeNode currentNode =
  343. (DefaultMutableTreeNode)currentPath.getLastPathComponent();
  344.  
  345. treeModel.removeNodeFromParent(currentNode);
  346. }
  347.  
  348. showChildren(parentNode);
  349. } else {
  350. String msg = "The file '" +
  351. currentFile +
  352. "' could not be deleted.";
  353. showErrorMessage(msg,"Delete Failed");
  354. }
  355. } catch(Throwable t) {
  356. showThrowable(t);
  357. }
  358. }
  359. gui.repaint();
  360. }
  361.  
  362. private void newFile() {
  363. if (currentFile==null) {
  364. showErrorMessage("No location selected for new file.","Select Location");
  365. return;
  366. }
  367.  
  368. if (newFilePanel==null) {
  369. newFilePanel = new JPanel(new BorderLayout(3,3));
  370.  
  371. JPanel southRadio = new JPanel(new GridLayout(1,0,2,2));
  372. newTypeFile = new JRadioButton("File", true);
  373. JRadioButton newTypeDirectory = new JRadioButton("Directory");
  374. ButtonGroup bg = new ButtonGroup();
  375. bg.add(newTypeFile);
  376. bg.add(newTypeDirectory);
  377. southRadio.add( newTypeFile );
  378. southRadio.add( newTypeDirectory );
  379.  
  380. name = new JTextField(15);
  381.  
  382. newFilePanel.add( new JLabel("Name"), BorderLayout.WEST );
  383. newFilePanel.add( name );
  384. newFilePanel.add( southRadio, BorderLayout.SOUTH );
  385. }
  386.  
  387. int result = JOptionPane.showConfirmDialog(
  388. gui,
  389. newFilePanel,
  390. "Create File",
  391. JOptionPane.OK_CANCEL_OPTION);
  392. if (result==JOptionPane.OK_OPTION) {
  393. try {
  394. boolean created;
  395. File parentFile = currentFile;
  396. if (!parentFile.isDirectory()) {
  397. parentFile = parentFile.getParentFile();
  398. }
  399. File file = new File( parentFile, name.getText() );
  400. if (newTypeFile.isSelected()) {
  401. created = file.createNewFile();
  402. } else {
  403. created = file.mkdir();
  404. }
  405. if (created) {
  406.  
  407. TreePath parentPath = findTreePath(parentFile);
  408. DefaultMutableTreeNode parentNode =
  409. (DefaultMutableTreeNode)parentPath.getLastPathComponent();
  410.  
  411. if (file.isDirectory()) {
  412. // add the new node..
  413. DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(file);
  414.  
  415. TreePath currentPath = findTreePath(currentFile);
  416. DefaultMutableTreeNode currentNode =
  417. (DefaultMutableTreeNode)currentPath.getLastPathComponent();
  418.  
  419. treeModel.insertNodeInto(newNode, parentNode, parentNode.getChildCount());
  420. }
  421.  
  422. showChildren(parentNode);
  423. } else {
  424. String msg = "The file '" +
  425. file +
  426. "' could not be created.";
  427. showErrorMessage(msg, "Create Failed");
  428. }
  429. } catch(Throwable t) {
  430. showThrowable(t);
  431. }
  432. }
  433. gui.repaint();
  434. }
  435.  
  436. private void showErrorMessage(String errorMessage, String errorTitle) {
  437. JOptionPane.showMessageDialog(
  438. gui,
  439. errorMessage,
  440. errorTitle,
  441. JOptionPane.ERROR_MESSAGE
  442. );
  443. }
  444.  
  445. private void showThrowable(Throwable t) {
  446. t.printStackTrace();
  447. JOptionPane.showMessageDialog(
  448. gui,
  449. t.toString(),
  450. t.getMessage(),
  451. JOptionPane.ERROR_MESSAGE
  452. );
  453. gui.repaint();
  454. }
  455.  
  456. /** Update the table on the EDT */
  457. private void setTableData(final File[] files) {
  458. SwingUtilities.invokeLater(new Runnable() {
  459. public void run() {
  460. if (fileTableModel==null) {
  461. fileTableModel = new FileTableModel();
  462. table.setModel(fileTableModel);
  463. }
  464. table.getSelectionModel().removeListSelectionListener(listSelectionListener);
  465. fileTableModel.setFiles(files);
  466. table.getSelectionModel().addListSelectionListener(listSelectionListener);
  467. if (!cellSizesSet) {
  468. Icon icon = fileSystemView.getSystemIcon(files[0]);
  469.  
  470. // size adjustment to better account for icons
  471. table.setRowHeight( icon.getIconHeight()+rowIconPadding );
  472.  
  473. setColumnWidth(0,-1);
  474. setColumnWidth(3,60);
  475. table.getColumnModel().getColumn(3).setMaxWidth(120);
  476. setColumnWidth(4,-1);
  477. setColumnWidth(5,-1);
  478. setColumnWidth(6,-1);
  479. setColumnWidth(7,-1);
  480. setColumnWidth(8,-1);
  481. setColumnWidth(9,-1);
  482.  
  483. cellSizesSet = true;
  484. }
  485. }
  486. });
  487. }
  488.  
  489. private void setColumnWidth(int column, int width) {
  490. TableColumn tableColumn = table.getColumnModel().getColumn(column);
  491. if (width<0) {
  492. // use the preferred width of the header..
  493. JLabel label = new JLabel( (String)tableColumn.getHeaderValue() );
  494. Dimension preferred = label.getPreferredSize();
  495. // altered 10->14 as per camickr comment.
  496. width = (int)preferred.getWidth()+14;
  497. }
  498. tableColumn.setPreferredWidth(width);
  499. tableColumn.setMaxWidth(width);
  500. tableColumn.setMinWidth(width);
  501. }
  502.  
  503. /** Add the files that are contained within the directory of this node.
  504. Thanks to Hovercraft Full Of Eels. */
  505. private void showChildren(final DefaultMutableTreeNode node) {
  506. tree.setEnabled(false);
  507. progressBar.setVisible(true);
  508. progressBar.setIndeterminate(true);
  509.  
  510. SwingWorker<Void, File> worker = new SwingWorker<Void, File>() {
  511. @Override
  512. public Void doInBackground() {
  513. File file = (File) node.getUserObject();
  514. if (file.isDirectory()) {
  515. File[] files = fileSystemView.getFiles(file, true); //!!
  516. if (node.isLeaf()) {
  517. for (File child : files) {
  518. if (child.isDirectory()) {
  519. publish(child);
  520. }
  521. }
  522. }
  523. setTableData(files);
  524. }
  525. return null;
  526. }
  527.  
  528. @Override
  529. protected void process(List<File> chunks) {
  530. for (File child : chunks) {
  531. node.add(new DefaultMutableTreeNode(child));
  532. }
  533. }
  534.  
  535. @Override
  536. protected void done() {
  537. progressBar.setIndeterminate(false);
  538. progressBar.setVisible(false);
  539. tree.setEnabled(true);
  540. }
  541. };
  542. worker.execute();
  543. }
  544.  
  545. /** Update the File details view with the details of this File. */
  546. private void setFileDetails(File file) {
  547. currentFile = file;
  548. Icon icon = fileSystemView.getSystemIcon(file);
  549. fileName.setIcon(icon);
  550. fileName.setText(fileSystemView.getSystemDisplayName(file));
  551. path.setText(file.getPath());
  552. date.setText(new Date(file.lastModified()).toString());
  553. size.setText(file.length() + " bytes");
  554. readable.setSelected(file.canRead());
  555. writable.setSelected(file.canWrite());
  556. executable.setSelected(file.canExecute());
  557. isDirectory.setSelected(file.isDirectory());
  558.  
  559. isFile.setSelected(file.isFile());
  560.  
  561. JFrame f = (JFrame)gui.getTopLevelAncestor();
  562. if (f!=null) {
  563. f.setTitle(
  564. APP_TITLE +
  565. " :: " +
  566. fileSystemView.getSystemDisplayName(file) );
  567. }
  568.  
  569. gui.repaint();
  570. }
  571.  
  572. public static boolean copyFile(File from, File to) throws IOException {
  573.  
  574. boolean created = to.createNewFile();
  575.  
  576. if (created) {
  577. FileChannel fromChannel = null;
  578. FileChannel toChannel = null;
  579. try {
  580. fromChannel = new FileInputStream(from).getChannel();
  581. toChannel = new FileOutputStream(to).getChannel();
  582.  
  583. toChannel.transferFrom(fromChannel, 0, fromChannel.size());
  584.  
  585. // set the flags of the to the same as the from
  586. to.setReadable(from.canRead());
  587. to.setWritable(from.canWrite());
  588. to.setExecutable(from.canExecute());
  589. } finally {
  590. if (fromChannel != null) {
  591. fromChannel.close();
  592. }
  593. if (toChannel != null) {
  594. toChannel.close();
  595. }
  596. return false;
  597. }
  598. }
  599. return created;
  600. }
  601.  
  602. public static void main(String[] args) {
  603. SwingUtilities.invokeLater(new Runnable() {
  604. public void run() {
  605. try {
  606. // Significantly improves the look of the output in
  607. // terms of the file names returned by FileSystemView!
  608. UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  609. } catch(Exception weTried) {
  610. }
  611. JFrame f = new JFrame(APP_TITLE);
  612. f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  613.  
  614. fileexplorer fileManager = new fileexplorer();
  615. f.setContentPane(fileManager.getGui());
  616.  
  617. try {
  618. URL urlBig = fileManager.getClass().getResource("fm-icon-32x32.png");
  619. URL urlSmall = fileManager.getClass().getResource("fm-icon-16x16.png");
  620. ArrayList<Image> images = new ArrayList<Image>();
  621. images.add( ImageIO.read(urlBig) );
  622. images.add( ImageIO.read(urlSmall) );
  623. f.setIconImages(images);
  624. } catch(Exception weTried) {}
  625.  
  626. f.pack();
  627. f.setLocationByPlatform(true);
  628. f.setMinimumSize(f.getSize());
  629. f.setVisible(true);
  630.  
  631. fileManager.showRootFile();
  632. }
  633. });
  634. }
  635. }
  636.  
  637. /** A TableModel to hold File[]. */
  638. class FileTableModel extends AbstractTableModel {
  639.  
  640. private File[] files;
  641. private FileSystemView fileSystemView = FileSystemView.getFileSystemView();
  642. private String[] columns = {
  643. "Icon",
  644. "File",
  645. "Path/name",
  646. "Size",
  647. "Last Modified",
  648. "R",
  649. "W",
  650. "E",
  651. "D",
  652. "F",
  653. };
  654.  
  655. FileTableModel() {
  656. this(new File[0]);
  657. }
  658.  
  659. FileTableModel(File[] files) {
  660. this.files = files;
  661. }
  662.  
  663. public Object getValueAt(int row, int column) {
  664. File file = files[row];
  665. switch (column) {
  666. case 0:
  667. return fileSystemView.getSystemIcon(file);
  668. case 1:
  669. return fileSystemView.getSystemDisplayName(file);
  670. case 2:
  671. return file.getPath();
  672. case 3:
  673. return file.length();
  674. case 4:
  675. return file.lastModified();
  676. case 5:
  677. return file.canRead();
  678. case 6:
  679. return file.canWrite();
  680. case 7:
  681. return file.canExecute();
  682. case 8:
  683. return file.isDirectory();
  684. case 9:
  685. return file.isFile();
  686. default:
  687. System.err.println("Logic Error");
  688. }
  689. return "";
  690. }
  691.  
  692. public int getColumnCount() {
  693. return columns.length;
  694. }
  695.  
  696. public Class<?> getColumnClass(int column) {
  697. switch (column) {
  698. case 0:
  699. return ImageIcon.class;
  700. case 3:
  701. return Long.class;
  702. case 4:
  703. return Date.class;
  704. case 5:
  705. case 6:
  706. case 7:
  707. case 8:
  708. case 9:
  709. return Boolean.class;
  710. }
  711. return String.class;
  712. }
  713.  
  714. public String getColumnName(int column) {
  715. return columns[column];
  716. }
  717.  
  718. public int getRowCount() {
  719. return files.length;
  720. }
  721.  
  722. public File getFile(int row) {
  723. return files[row];
  724. }
  725.  
  726. public void setFiles(File[] files) {
  727. this.files = files;
  728. fireTableDataChanged();
  729. }
  730. }
  731.  
  732. /** A TreeCellRenderer for a File. */
  733. class FileTreeCellRenderer extends DefaultTreeCellRenderer {
  734.  
  735. private FileSystemView fileSystemView;
  736.  
  737. private JLabel label;
  738.  
  739. FileTreeCellRenderer() {
  740. label = new JLabel();
  741. label.setOpaque(true);
  742. fileSystemView = FileSystemView.getFileSystemView();
  743. }
  744.  
  745. @Override
  746. public Component getTreeCellRendererComponent(
  747. JTree tree,
  748. Object value,
  749. boolean selected,
  750. boolean expanded,
  751. boolean leaf,
  752. int row,
  753. boolean hasFocus) {
  754.  
  755. DefaultMutableTreeNode node = (DefaultMutableTreeNode)value;
  756. File file = (File)node.getUserObject();
  757. label.setIcon(fileSystemView.getSystemIcon(file));
  758. label.setText(fileSystemView.getSystemDisplayName(file));
  759. label.setToolTipText(file.getPath());
  760.  
  761. if (selected) {
  762. label.setBackground(backgroundSelectionColor);
  763. label.setForeground(textSelectionColor);
  764. } else {
  765. label.setBackground(backgroundNonSelectionColor);
  766. label.setForeground(textNonSelectionColor);
  767. }
  768.  
  769. return label;
  770. }
  771. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement