File Browser Demo : Table « SWT JFace Eclipse « Java






File Browser Demo

File Browser Demo
/******************************************************************************
 * All Right Reserved. 
 * Copyright (c) 1998, 2004 Jackwind Li Guojie
 * 
 * Created on Mar 3, 2004 2:09:15 PM by JACK
 * $Id$
 * 
 *****************************************************************************/



import java.io.File;
import java.util.Date;

import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.program.Program;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

public class FileBrowserDemo {
  Display display = new Display();
  Shell shell = new Shell(display);

  Table table;
  
  File rootDir;
  
  Image iconFolder = new Image(shell.getDisplay(), "java2s.gif");
  Image iconFile = new Image(shell.getDisplay(), "java2s.gif");
  
  public FileBrowserDemo() {
    GridLayout gridLayout = new GridLayout();
    shell.setLayout(gridLayout);
    
    // Tool bar. 
    
    // Table. 
    table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);

    //table.setLinesVisible(true);
    table.setHeaderVisible(true);
    
    TableColumn tcFileName = new TableColumn(table, SWT.LEFT);
    tcFileName.setText("File name");
    //tcFileName.setImage(new Image(shell.getDisplay(), "java2s.gif"));
    
    TableColumn tcFileSize = new TableColumn(table, SWT.RIGHT);
    tcFileSize.setText("Size");
    
    TableColumn tcDateModified = new TableColumn(table, SWT.RIGHT);
    tcDateModified.setText("Date Modified");
    
    tcFileName.setWidth(200);
    tcFileSize.setWidth(80);
    tcDateModified.setWidth(180);
    
    TableItem item = new TableItem(table, SWT.NULL);
    item.setText(new String[]{"Name", "Size"});
    
    table.setLayoutData(new GridData(GridData.FILL_BOTH));

    table.pack();
    
    shell.pack();
    shell.open();
    
    setRootDirectory(new File("."));
    //textUser.forceFocus();

    // Set up the event loop.
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        // If no more entries in event queue
        display.sleep();
      }
    }

    display.dispose();
  }
  
  private void setRootDirectory(File dir) {
    this.rootDir = dir;
    if(! (dir.exists() && dir.isDirectory()))
      throw new IllegalArgumentException("Invalid directory. ");
    
    // Remove all existing items. 
    table.removeAll();
    
    File[] files = dir.listFiles();
    for(int i=0; files != null && i < files.length; i++) {
      File file = files[i];
      TableItem item = new TableItem(table, SWT.NULL);
      
      item.setBackground(i % 2 == 0 ? shell.getDisplay().getSystemColor(SWT.COLOR_WHITE) :
                      shell.getDisplay().getSystemColor(SWT.COLOR_GRAY));
      
      item.setText(0, file.getName());
      item.setText(2, new Date(file.lastModified()).toString());
      
      
      if(file.isDirectory()) {
        item.setImage(0, iconFolder);
      }else{
        int lastDot = file.getName().lastIndexOf('.');
        if(lastDot >= 0 && lastDot < file.getName().length()) {
          item.setImage(0, getIcon(file.getName().substring(lastDot+1)));
        }
        long kbytes = file.length() / 1024;
        if(kbytes == 0)
          kbytes = 1;
        item.setText(1, kbytes + " KB");
      }
    }
    
  }
  
  private ImageRegistry imageRegistry;
  
  /**
   * Returns the icon for the file type with the specified extension. 
   * @param extension
   * @return
   */
  public Image getIcon(String extension) {
    if(imageRegistry == null)
      imageRegistry = new ImageRegistry();
    Image image = imageRegistry.get(extension);
    if(image != null)
      return image;
    
    Program program = Program.findProgram(extension);
    if(program != null) {
      image = new Image(shell.getDisplay(), program.getImageData());
      imageRegistry.put(extension, image);
    }else{
      image = iconFile;
    }
    
    return image;
  }

  public static void main(String[] args) {
    new FileBrowserDemo();
  }
}



           
       








Related examples in the same category

1.How to order 1000 elements in a swt column table with O(n log(n)) complexity! using Comparator and Array.sort() implemented in a TableColumn Listener FactoryHow to order 1000 elements in a swt column table with O(n log(n)) complexity! using Comparator and Array.sort() implemented in a TableColumn Listener Factory
2.Print KTable (SWT Table)Example Print KTable (SWT Table)Example
3.The source of a custom table class for Java SWT applicationsThe source of a custom table class for Java SWT applications
4.SWT Table Editor
5.Simple File Browser in SWT TableSimple File Browser in SWT Table
6.Bug Tracker JFaceBug Tracker JFace
7.Bug TrackerBug Tracker
8.TableEditor exampleTableEditor example
9.File Browser SampleFile Browser Sample
10.File Browser JFace
11.Bug ReportBug Report
12.Displays ASCII CodesDisplays ASCII Codes
13.Demonstrates the SWT.VIRTUAL styleDemonstrates the SWT.VIRTUAL style
14.Displays a tableDisplays a table
15.A table of baseball players and allows sortingA table of baseball players and allows sorting
16.Demonstrates TableCursorDemonstrates TableCursor
17.Demonstrates TableTreeDemonstrates TableTree
18.Demonstrates TableEditorDemonstrates TableEditor
19.Shows the extensions on the system and their associated programsShows the extensions on the system and their associated programs
20.Table Example 3Table Example 3
21.Table Example 2Table Example 2
22.Table ExampleTable Example
23.Demonstrates TableViewersDemonstrates TableViewers
24.Demonstrates CheckboxTableViewerDemonstrates CheckboxTableViewer
25.Demonstrates CellEditorsDemonstrates CellEditors
26.SWT Table Simple DemoSWT Table Simple Demo
27.Create fake tooltips for items in a SWT tableCreate fake tooltips for items in a SWT table
28.Place a progress bar in a SWT tablePlace a progress bar in a SWT table
29.edit a cell in a SWT table (in place, fancy)edit a cell in a SWT table (in place, fancy)
30.edit the text of a SWT table item (in place)edit the text of a SWT table item (in place)
31.navigate a SWT table cells with arrow keysnavigate a SWT table cells with arrow keys
32.Update SWT table item textUpdate SWT table item text
33.Sort a SWT table by columnSort a SWT table by column
34.Select an index (select and scroll) in a SWT tableSelect an index (select and scroll) in a SWT table
35.Scroll a SWT table (set the top index)Scroll a SWT table (set the top index)
36.Resize columns as SWT table resizesResize columns as SWT table resizes
37.Remove selected items in a SWT tableRemove selected items in a SWT table
38.Print selected items in a SWT tablePrint selected items in a SWT table
39.Place arbitrary controls in a SWT tablePlace arbitrary controls in a SWT table
40.Reorder columns and reorder columns programmaticallyReorder columns and reorder columns programmatically
41.Insert a SWT table column (at an index)Insert a SWT table column (at an index)
42.Insert a SWT table item (at an index)Insert a SWT table item (at an index)
43.Find a SWT table cell from mouse down (works for any table style)Find a SWT table cell from mouse down (works for any table style)
44.Find a table cell from mouse down (SWT.FULL_SELECTION)Find a table cell from mouse down (SWT.FULL_SELECTION)
45.Detect a selection or check event in a table (SWT.CHECK)Detect a selection or check event in a table (SWT.CHECK)
46.Create a SWT table with 1,000,000 itemsCreate a SWT table with 1,000,000 items
47.Create a SWT table (columns, headers, lines)Create a SWT table (columns, headers, lines)
48.Create a SWT table (no columns, no headers)Create a SWT table (no columns, no headers)
49.Color cells and rows in SWT tableColor cells and rows in SWT table
50.Create a virtual SWT table and add 1000 entries to it every 500 msCreate a virtual SWT table and add 1000 entries to it every 500 ms
51.Dropped data type depends on target item in tableDropped data type depends on target item in table
52.Create a table (lazy)Create a table (lazy)