Demonstrates CheckboxTableViewer : Table « SWT JFace Eclipse « Java






Demonstrates CheckboxTableViewer

Demonstrates CheckboxTableViewer
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.eclipse.jface.viewers.CheckboxTableViewer;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

/**
 * This class demonstrates CheckboxTableViewer. It allows you to check files to
 * copy, and copy them to a backup directory.
 */
public class BackupFiles extends ApplicationWindow {
  // Used to show status
  private Label status;

  /**
   * BackupFiles constructor
   */
  public BackupFiles() {
    super(null);
  }

  /**
   * Runs the application
   */
  public void run() {
    // Don't return from open() until window closes
    setBlockOnOpen(true);

    // Open the main window
    open();

    // Dispose the display
    Display.getCurrent().dispose();
  }

  /**
   * Configures the shell
   * 
   * @param shell
   *            the shell
   */
  protected void configureShell(Shell shell) {
    super.configureShell(shell);

    // Set the title bar text and the size
    shell.setText("Backup Files");
    shell.setSize(400, 400);
  }

  /**
   * Creates the main window's contents
   * 
   * @param parent
   *            the main window
   * @return Control
   */
  protected Control createContents(Composite parent) {
    Composite composite = new Composite(parent, SWT.NONE);
    composite.setLayout(new GridLayout(1, false));

    // Create the source directory panel and its controls
    final Text sourceDir = createFilePanelHelper(composite, "Source Dir:");

    // Create the CheckboxTableViewer to display the files in the source dir
    final CheckboxTableViewer ctv = CheckboxTableViewer.newCheckList(
        composite, SWT.BORDER);
    ctv.getTable().setLayoutData(new GridData(GridData.FILL_BOTH));
    ctv.setContentProvider(new BackupFilesContentProvider());
    ctv.setLabelProvider(new BackupFilesLabelProvider());

    // Create the destination directory panel and its controls
    final Text destDir = createFilePanelHelper(composite, "Dest Dir:");

    // Create the Copy button
    Button copy = new Button(composite, SWT.PUSH);
    copy.setText("Copy");

    // Create the status field
    status = new Label(composite, SWT.NONE);
    status.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

    // When the source directory changes, change the input for the viewer
    sourceDir.addModifyListener(new ModifyListener() {
      public void modifyText(ModifyEvent event) {
        ctv.setInput(((Text) event.widget).getText());
      }
    });

    // When copy is pressed, copy the files
    copy.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent event) {
        // Get the checked elements
        Object[] files = ctv.getCheckedElements();
        if (files.length > 0) {
          // Some files are checked; make sure we have a valid
          // destination
          File dest = new File(destDir.getText());
          if (dest.isDirectory()) {
            // Go through each file
            for (int i = 0, n = files.length; i < n; i++) {
              copyFile((File) files[i], dest);
            }
          } else
            showMessage("You must select a valid destination directory");
        } else
          showMessage("You must select some files to copy");
      }
    });

    return composite;
  }

  /**
   * Helper method to create the label/text/button for a directory
   * 
   * @param composite
   *            the parent composite
   * @param label
   *            the text for the label
   * @return Text
   */
  private Text createFilePanelHelper(Composite composite, String label) {
    // Create the composite to hold the controls
    Composite panel = new Composite(composite, SWT.BORDER);
    panel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    panel.setLayout(new GridLayout(3, false));

    // Create the controls
    new Label(panel, SWT.LEFT).setText(label);
    Text text = new Text(panel, SWT.BORDER);
    text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    Button browse = new Button(panel, SWT.PUSH);

    // Add browsing
    browse.setText("...");
    browse.addSelectionListener(new DirectoryBrowser(text));

    // Return the Text that holds the directory
    return text;
  }

  /**
   * Copies a file
   * 
   * @param file
   *            the file to copy
   * @param targetDir
   *            the directory to copy to
   */
  private void copyFile(File file, File targetDir) {
    BufferedInputStream in = null;
    BufferedOutputStream out = null;
    File destFile = new File(targetDir.getAbsolutePath() + File.separator
        + file.getName());
    try {
      showMessage("Copying " + file.getName());
      in = new BufferedInputStream(new FileInputStream(file));
      out = new BufferedOutputStream(new FileOutputStream(destFile));
      int n;
      while ((n = in.read()) != -1) {
        out.write(n);
      }
      showMessage("Copied " + file.getName());
    } catch (Exception e) {
      showMessage("Cannot copy file " + file.getAbsolutePath());
    } finally {
      if (in != null)
        try {
          in.close();
        } catch (Exception e) {
        }
      if (out != null)
        try {
          out.close();
        } catch (Exception e) {
        }
    }
  }

  /**
   * Shows a message
   * 
   * @param message
   *            the message
   */
  private void showMessage(String message) {
    status.setText(message);
  }

  /**
   * The application entry point
   * 
   * @param args
   *            the command line arguments
   */
  public static void main(String[] args) {
    new BackupFiles().run();
  }
}

/**
 * This class returns the files in the specified directory. If the specified
 * directory doesn't exist, it returns an empty array.
 */

class BackupFilesContentProvider implements IStructuredContentProvider {
  private static final Object[] EMPTY = new Object[] {};

  /**
   * Gets the files in the specified directory
   * 
   * @param arg0
   *            a String containing the directory
   */
  public Object[] getElements(Object arg0) {
    File file = new File((String) arg0);
    if (file.isDirectory()) {
      return file.listFiles(new FileFilter() {
        public boolean accept(File pathName) {
          // Ignore directories; return only files
          return pathName.isFile();
        }
      });
    }
    return EMPTY;
  }

  /**
   * Disposes any created resources
   */
  public void dispose() {
    // Nothing to dispose
  }

  /**
   * Called when the input changes
   */
  public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
    // Nothing to do
  }
}

/**
 * This class provides the labels for files
 */

class BackupFilesLabelProvider implements ILabelProvider {
  /**
   * Returns the image
   * 
   * @param arg0
   *            the file
   * @return Image
   */
  public Image getImage(Object arg0) {
    return null;
  }

  /**
   * Returns the name of the file
   * 
   * @param arg0
   *            the name of the file
   * @return String
   */
  public String getText(Object arg0) {
    return ((File) arg0).getName();
  }

  /**
   * Adds a listener
   * 
   * @param arg0
   *            the listener
   */
  public void addListener(ILabelProviderListener arg0) {
    // Throw it away
  }

  /**
   * Disposes any created resources
   */
  public void dispose() {
    // Nothing to dispose
  }

  /**
   * Returns whether changing this property for this element affects the label
   * 
   * @param arg0
   *            the element
   * @param arg1
   *            the property
   */
  public boolean isLabelProperty(Object arg0, String arg1) {
    return false;
  }

  /**
   * Removes a listener
   * 
   * @param arg0
   *            the listener
   */
  public void removeListener(ILabelProviderListener arg0) {
    // Ignore
  }
}

/**
 * This class allows users to browse for a directory
 */

class DirectoryBrowser extends SelectionAdapter {
  // The Text this browser is tied to
  private Text text;

  /**
   * DirectoryBrowser constructor
   * 
   * @param text
   */
  public DirectoryBrowser(Text text) {
    this.text = text;
  }

  /**
   * Called when the browse button is pushed
   * 
   * @param event
   *            the generated event
   */
  public void widgetSelected(SelectionEvent event) {
    DirectoryDialog dlg = new DirectoryDialog(Display.getCurrent()
        .getActiveShell());
    dlg.setFilterPath(text.getText());
    String dir = dlg.open();
    if (dir != null) {
      text.setText(dir);
    }
  }
}

           
       








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.File Browser DemoFile Browser Demo
9.TableEditor exampleTableEditor example
10.File Browser SampleFile Browser Sample
11.File Browser JFace
12.Bug ReportBug Report
13.Displays ASCII CodesDisplays ASCII Codes
14.Demonstrates the SWT.VIRTUAL styleDemonstrates the SWT.VIRTUAL style
15.Displays a tableDisplays a table
16.A table of baseball players and allows sortingA table of baseball players and allows sorting
17.Demonstrates TableCursorDemonstrates TableCursor
18.Demonstrates TableTreeDemonstrates TableTree
19.Demonstrates TableEditorDemonstrates TableEditor
20.Shows the extensions on the system and their associated programsShows the extensions on the system and their associated programs
21.Table Example 3Table Example 3
22.Table Example 2Table Example 2
23.Table ExampleTable Example
24.Demonstrates TableViewersDemonstrates TableViewers
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)