Demonstrates CellEditors : Table « SWT JFace Eclipse « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » SWT JFace Eclipse » TableScreenshots 
Demonstrates CellEditors
Demonstrates CellEditors

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.CheckboxCellEditor;
import org.eclipse.jface.viewers.ColorCellEditor;
import org.eclipse.jface.viewers.ComboBoxCellEditor;
import org.eclipse.jface.viewers.ICellModifier;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.RGB;
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.Display;
import org.eclipse.swt.widgets.Item;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;

/**
 * This class demonstrates CellEditors. It allows you to create and edit Person
 * objects
 */
public class PersonEditor extends ApplicationWindow {
  // Table column names/properties
  public static final String NAME = "Name";

  public static final String MALE = "Male?";

  public static final String AGE = "Age Range";

  public static final String SHIRT_COLOR = "Shirt Color";

  public static final String[] PROPS = NAME, MALE, AGE, SHIRT_COLOR };

  // The data model
  private java.util.List people;

  /**
   * Constructs a PersonEditor
   */
  public PersonEditor() {
    super(null);
    people = new ArrayList();
  }

  /**
   * 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);
    shell.setText("Person Editor");
    shell.setSize(400400);
  }

  /**
   * 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(1false));

    // Add a button to create the new person
    Button newPerson = new Button(composite, SWT.PUSH);
    newPerson.setText("Create New Person");

    // Add the TableViewer
    final TableViewer tv = new TableViewer(composite, SWT.FULL_SELECTION);
    tv.setContentProvider(new PersonContentProvider());
    tv.setLabelProvider(new PersonLabelProvider());
    tv.setInput(people);

    // Set up the table
    Table table = tv.getTable();
    table.setLayoutData(new GridData(GridData.FILL_BOTH));

    new TableColumn(table, SWT.CENTER).setText(NAME);
    new TableColumn(table, SWT.CENTER).setText(MALE);
    new TableColumn(table, SWT.CENTER).setText(AGE);
    new TableColumn(table, SWT.CENTER).setText(SHIRT_COLOR);

    for (int i = 0, n = table.getColumnCount(); i < n; i++) {
      table.getColumn(i).pack();
    }

    table.setHeaderVisible(true);
    table.setLinesVisible(true);

    // Add a new person when the user clicks button
    newPerson.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent event) {
        Person p = new Person();
        p.setName("Name");
        p.setMale(true);
        p.setAgeRange(Integer.valueOf("0"));
        p.setShirtColor(new RGB(25500));
        people.add(p);
        tv.refresh();
      }
    });

    // Create the cell editors
    CellEditor[] editors = new CellEditor[4];
    editors[0new TextCellEditor(table);
    editors[1new CheckboxCellEditor(table);
    editors[2new ComboBoxCellEditor(table, AgeRange.INSTANCES,
        SWT.READ_ONLY);
    editors[3new ColorCellEditor(table);

    // Set the editors, cell modifier, and column properties
    tv.setColumnProperties(PROPS);
    tv.setCellModifier(new PersonCellModifier(tv));
    tv.setCellEditors(editors);

    return composite;
  }

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

/**
 * This class provides the content for the person table
 */

class PersonContentProvider implements IStructuredContentProvider {
  /**
   * Returns the Person objects
   */
  public Object[] getElements(Object inputElement) {
    return ((ListinputElement).toArray();
  }

  /**
   * Disposes any created resources
   */
  public void dispose() {
    // Do nothing
  }

  /**
   * Called when the input changes
   */
  public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
    // Ignore
  }
}

/**
 * This class provides the content for the FoodList application
 */

class FoodContentProvider implements IStructuredContentProvider {
  /**
   * Gets the food items for the list
   
   @param arg0
   *            the data model
   @return Object[]
   */
  public Object[] getElements(Object arg0) {
    return ((GroceryListarg0).getFoods().toArray();
  }

  /**
   * Disposes any created resources
   */
  public void dispose() {
    // Do nothing
  }

  /**
   * Called when the input changes
   
   @param arg0
   *            the viewer
   @param arg1
   *            the old input
   @param arg2
   *            the new input
   */
  public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
    // Do nothing
  }
}

/**
 * This class represents the cell modifier for the PersonEditor program
 */

class PersonCellModifier implements ICellModifier {
  private Viewer viewer;

  public PersonCellModifier(Viewer viewer) {
    this.viewer = viewer;
  }

  /**
   * Returns whether the property can be modified
   
   @param element
   *            the element
   @param property
   *            the property
   @return boolean
   */
  public boolean canModify(Object element, String property) {
    // Allow editing of all values
    return true;
  }

  /**
   * Returns the value for the property
   
   @param element
   *            the element
   @param property
   *            the property
   @return Object
   */
  public Object getValue(Object element, String property) {
    Person p = (Personelement;
    if (PersonEditor.NAME.equals(property))
      return p.getName();
    else if (PersonEditor.MALE.equals(property))
      return Boolean.valueOf(p.isMale());
    else if (PersonEditor.AGE.equals(property))
      return p.getAgeRange();
    else if (PersonEditor.SHIRT_COLOR.equals(property))
      return p.getShirtColor();
    else
      return null;
  }

  /**
   * Modifies the element
   
   @param element
   *            the element
   @param property
   *            the property
   @param value
   *            the value
   */
  public void modify(Object element, String property, Object value) {
    if (element instanceof Item)
      element = ((Itemelement).getData();

    Person p = (Personelement;
    if (PersonEditor.NAME.equals(property))
      p.setName((Stringvalue);
    else if (PersonEditor.MALE.equals(property))
      p.setMale(((Booleanvalue).booleanValue());
    else if (PersonEditor.AGE.equals(property))
      p.setAgeRange((Integervalue);
    else if (PersonEditor.SHIRT_COLOR.equals(property))
      p.setShirtColor((RGBvalue);

    // Force the viewer to refresh
    viewer.refresh();
  }
}

/**
 * This class represents a person
 */

class Person {
  private String name;

  private boolean male;

  private Integer ageRange;

  private RGB shirtColor;

  /**
   @return Returns the ageRange.
   */
  public Integer getAgeRange() {
    return ageRange;
  }

  /**
   @param ageRange
   *            The ageRange to set.
   */
  public void setAgeRange(Integer ageRange) {
    this.ageRange = ageRange;
  }

  /**
   @return Returns the male.
   */
  public boolean isMale() {
    return male;
  }

  /**
   @param male
   *            The male to set.
   */
  public void setMale(boolean male) {
    this.male = male;
  }

  /**
   @return Returns the name.
   */
  public String getName() {
    return name;
  }

  /**
   @param name
   *            The name to set.
   */
  public void setName(String name) {
    this.name = name;
  }

  /**
   @return Returns the shirtColor.
   */
  public RGB getShirtColor() {
    return shirtColor;
  }

  /**
   @param shirtColor
   *            The shirtColor to set.
   */
  public void setShirtColor(RGB shirtColor) {
    this.shirtColor = shirtColor;
  }
}

/**
 * This class contains all the foods on the "grocery list"
 */

class GroceryList {
  // Holds the foods
  private List foods;

  /**
   * Constructs a grocery list
   */
  public GroceryList() {
    foods = new ArrayList();

    // Add some foods
    foods.add(new Food("Broccoli"true));
    foods.add(new Food("Bundt Cake"false));
    foods.add(new Food("Cabbage"true));
    foods.add(new Food("Candy Canes"false));
    foods.add(new Food("Eggs"true));
    foods.add(new Food("Potato Chips"false));
    foods.add(new Food("Milk"true));
    foods.add(new Food("Soda"false));
    foods.add(new Food("Chicken"true));
    foods.add(new Food("Cinnamon Rolls"false));
  }

  /**
   * Returns the foods in this grocery list
   
   @return List
   */
  public List getFoods() {
    return Collections.unmodifiableList(foods);
  }
}

/**
 * This class represents a type of food
 */

class Food {
  // The name of the food
  private String name;

  // Is it healthy?
  private boolean healthy;

  /**
   * Food constructor
   
   @param name
   *            the name
   @param healthy
   *            whether or not it's healthy
   */
  public Food(String name, boolean healthy) {
    this.name = name;
    this.healthy = healthy;
  }

  /**
   * Gets whether this is healthy
   
   @return boolean
   */
  public boolean isHealthy() {
    return healthy;
  }

  /**
   * Gets the name
   
   @return String
   */
  public String getName() {
    return name;
  }
}

/**
 * This class provides the labels for the person table
 */

class PersonLabelProvider implements ITableLabelProvider {
  /**
   * Returns the image
   
   @param element
   *            the element
   @param columnIndex
   *            the column index
   @return Image
   */
  public Image getColumnImage(Object element, int columnIndex) {
    return null;
  }

  /**
   * Returns the column text
   
   @param element
   *            the element
   @param columnIndex
   *            the column index
   @return String
   */
  public String getColumnText(Object element, int columnIndex) {
    Person person = (Personelement;
    switch (columnIndex) {
    case 0:
      return person.getName();
    case 1:
      return Boolean.toString(person.isMale());
    case 2:
      return AgeRange.INSTANCES[person.getAgeRange().intValue()];
    case 3:
      return person.getShirtColor().toString();
    }
    return null;
  }

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

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

  /**
   * Returns whether altering this property on this element will affect the
   * label
   
   @param element
   *            the element
   @param property
   *            the property
   @return boolean
   */
  public boolean isLabelProperty(Object element, String property) {
    return false;
  }

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


/**
 * This class encapsulates age ranges
 */

class AgeRange {
  public static final String NONE = "";

  public static final String BABY = "0 - 3";

  public static final String TODDLER = "4 - 7";

  public static final String CHILD = "8 - 12";

  public static final String TEENAGER = "13 - 19";

  public static final String ADULT = "20 - ?";

  public static final String[] INSTANCES = NONE, BABY, TODDLER, CHILD,
      TEENAGER, ADULT };
}

           
       
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 CheckboxTableViewerDemonstrates CheckboxTableViewer
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)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.