Java by API/org.eclipse.jface.viewers/TableViewer

Материал из Java эксперт
Перейти к: навигация, поиск

TableViewer: setCellEditors(CellEditor[] arg0)

import java.util.ArrayList;
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.ruboBoxCellEditor;
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.ruposite;
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;
public class MainClass extends ApplicationWindow {
  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};
  private java.util.List studentList= new ArrayList();
  public MainClass() {
    super(null);
    setBlockOnOpen(true);
    open();
    Display.getCurrent().dispose();
  }
  protected void configureShell(Shell shell) {
    super.configureShell(shell);
    shell.setText("Person Editor");
    shell.setSize(400, 400);
  }
  protected Control createContents(Composite parent) {
    Composite composite = new Composite(parent, SWT.NONE);
    composite.setLayout(new GridLayout(1, false));
    Button newPerson = new Button(composite, SWT.PUSH);
    newPerson.setText("Create New Person");
    final TableViewer tv = new TableViewer(composite, SWT.FULL_SELECTION);
    tv.setContentProvider(new PersonContentProvider());
    tv.setLabelProvider(new StudentLabelProvider());
    tv.setInput(studentList);
    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);
    newPerson.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent event) {
        Student p = new Student();
        p.setName("Name");
        p.setMale(true);
        p.setAgeRange(Integer.valueOf("0"));
        p.setShirtColor(new RGB(255, 0, 0));
        studentList.add(p);
        tv.refresh();
      }
    });
    CellEditor[] editors = new CellEditor[4];
    editors[0] = new TextCellEditor(table);
    editors[1] = new CheckboxCellEditor(table);
    editors[2] = new ComboBoxCellEditor(table, AgeRange.INSTANCES, SWT.READ_ONLY);
    editors[3] = new ColorCellEditor(table);
    tv.setColumnProperties(PROPS);
    tv.setCellModifier(new StudentCellModifier(tv));
    tv.setCellEditors(editors);
    return composite;
  }
  public static void main(String[] args) {
    new MainClass();
  }
}
class StudentCellModifier implements ICellModifier {
  private Viewer viewer;
  public StudentCellModifier(Viewer viewer) {
    this.viewer = viewer;
  }
  public boolean canModify(Object element, String property) {
    return true;
  }
  public Object getValue(Object element, String property) {
    Student p = (Student) element;
    if (MainClass.NAME.equals(property))
      return p.getName();
    else if (MainClass.MALE.equals(property))
      return Boolean.valueOf(p.isMale());
    else if (MainClass.AGE.equals(property))
      return p.getAgeRange();
    else if (MainClass.SHIRT_COLOR.equals(property))
      return p.getShirtColor();
    else
      return null;
  }
  public void modify(Object element, String property, Object value) {
    if (element instanceof Item) element = ((Item) element).getData();
    Student p = (Student) element;
    if (MainClass.NAME.equals(property))
      p.setName((String) value);
    else if (MainClass.MALE.equals(property))
      p.setMale(((Boolean) value).booleanValue());
    else if (MainClass.AGE.equals(property))
      p.setAgeRange((Integer) value);
    else if (MainClass.SHIRT_COLOR.equals(property))
        p.setShirtColor((RGB) value);
    viewer.refresh();
  }
}
class PersonContentProvider implements IStructuredContentProvider {
  public Object[] getElements(Object inputElement) {
    return ((List) inputElement).toArray();
  }
  public void dispose() {
  }
  public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
  }
}
class Student {
  private String name;
  private boolean male;
  private Integer ageRange;
  private RGB shirtColor;
  public Integer getAgeRange() {
    return ageRange;
  }
  public void setAgeRange(Integer ageRange) {
    this.ageRange = ageRange;
  }
  public boolean isMale() {
    return male;
  }
  public void setMale(boolean male) {
    this.male = male;
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public RGB getShirtColor() {
    return shirtColor;
  }
  public void setShirtColor(RGB shirtColor) {
    this.shirtColor = shirtColor;
  }
}
class StudentLabelProvider implements ITableLabelProvider {
  public Image getColumnImage(Object element, int columnIndex) {
    return null;
  }
  public String getColumnText(Object element, int columnIndex) {
    Student person = (Student) element;
    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;
  }
  public void addListener(ILabelProviderListener listener) {
  }
  public void dispose() {
  }
  public boolean isLabelProperty(Object element, String property) {
    return false;
  }
  public void removeListener(ILabelProviderListener listener) {
  }
}
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};
}





TableViewer: setContentProvider(IContentProvider icp)

import java.util.LinkedList;
import java.util.List;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.jface.resource.ImageDescriptor;
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.Viewer;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.ruposite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Text;
public class MainClass extends ApplicationWindow {
  public static MainClass mainWindow;
  AddEntryAction addEntryAction;
  List entries;
  private TableViewer viewer;
  public MainClass() {
    super(null);
    mainWindow = this;
    addEntryAction = new AddEntryAction();
    entries = new LinkedList();
    addToolBar(SWT.NONE);
  }
  public void run() {
    setBlockOnOpen(true);
    open();
    Display.getCurrent().dispose();
  }
  public void add(AddressEntry entry) {
    entries.add(entry);
    refresh();
  }
  protected void configureShell(Shell shell) {
    super.configureShell(shell);
    shell.setSize(600, 400);
  }
  protected Control createContents(Composite parent) {
    viewer = new TableViewer(parent);
    viewer.setContentProvider(new AddressBookContentProvider());
    viewer.setLabelProvider(new AddressBookLabelProvider());
    viewer.setInput(entries);
    Table table = viewer.getTable();
    new TableColumn(table, SWT.LEFT).setText("First Name");
    new TableColumn(table, SWT.LEFT).setText("Last Name");
    new TableColumn(table, SWT.LEFT).setText("E-mail Address");
    table.setHeaderVisible(true);
    table.setLinesVisible(true);
    refresh();
    return table;
  }
  protected ToolBarManager createToolBarManager(int style) {
    ToolBarManager tbm = new ToolBarManager(style);
    tbm.add(addEntryAction);
    return tbm;
  }
  private void refresh() {
    viewer.refresh();
    Table table = viewer.getTable();
    for (int i = 0, n = table.getColumnCount(); i < n; i++) {
      table.getColumn(i).pack();
    }
  }
  public static void main(String[] args) {
    new MainClass().run();
  }
}
class AddEntryAction extends Action {
  public AddEntryAction() {
    super("Add Entry");
    setToolTipText("Add Entry");
  }
  public void run() {
    WizardDialog dlg = new WizardDialog(MainClass.mainWindow.getShell(),
        new AddEntryWizard());
    dlg.open();
  }
}
class AddEntryWizard extends Wizard {
  private WelcomePage welcomePage= new WelcomePage();
  private NamePage namePage= new NamePage();
  private EmailPage emailPage= new EmailPage();
  public AddEntryWizard() {
    addPage(welcomePage);
    addPage(namePage);
    addPage(emailPage);
    setWindowTitle("Address Book Entry Wizard");
  }
  public boolean performFinish() {
    AddressEntry entry = new AddressEntry();
    entry.setFirstName(namePage.getFirstName());
    entry.setLastName(namePage.getLastName());
    entry.setEmail(emailPage.getEmail());
    MainClass.mainWindow.add(entry);
    return true;
  }
}
class EmailPage extends WizardPage {
  private String email = "";
  public EmailPage() {
    super("E-mail", "E-mail Address", ImageDescriptor.createFromFile(EmailPage.class,
        "email.gif"));
    setDescription("Enter the e-mail address");
    setPageComplete(false);
  }
  public void createControl(Composite parent) {
    Composite composite = new Composite(parent, SWT.NONE);
    composite.setLayout(new GridLayout(2, false));
    new Label(composite, SWT.LEFT).setText("E-mail Address:");
    final Text ea = new Text(composite, SWT.BORDER);
    ea.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    ea.addModifyListener(new ModifyListener() {
      public void modifyText(ModifyEvent event) {
        email = ea.getText();
        setPageComplete(email.length() > 0);
      }
    });
    setControl(composite);
  }
  public String getEmail() {
    return email;
  }
}
class NamePage extends WizardPage {
  private String firstName = "";
  private String lastName = "";
  public NamePage() {
    super("Name", "Name", ImageDescriptor.createFromFile(NamePage.class, "name.gif"));
    setDescription("Enter the first and last names");
    setPageComplete(false);
  }
  public void createControl(Composite parent) {
    Composite composite = new Composite(parent, SWT.NONE);
    composite.setLayout(new GridLayout(2, false));
    new Label(composite, SWT.LEFT).setText("First Name:");
    final Text first = new Text(composite, SWT.BORDER);
    first.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    new Label(composite, SWT.LEFT).setText("Last Name:");
    final Text last = new Text(composite, SWT.BORDER);
    last.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    first.addModifyListener(new ModifyListener() {
      public void modifyText(ModifyEvent event) {
        firstName = first.getText();
        setPageComplete(firstName.length() > 0 && lastName.length() > 0);
      }
    });
    last.addModifyListener(new ModifyListener() {
      public void modifyText(ModifyEvent event) {
        lastName = last.getText();
        setPageComplete(firstName.length() > 0 && lastName.length() > 0);
      }
    });
    setControl(composite);
  }
  public String getFirstName() {
    return firstName;
  }
  public String getLastName() {
    return lastName;
  }
}
class WelcomePage extends WizardPage {
  protected WelcomePage() {
    super("Welcome", "Welcome", ImageDescriptor.createFromFile(WelcomePage.class,
        "welcome.gif"));
    setDescription("Welcome to the Address Book Entry Wizard");
  }
  public void createControl(Composite parent) {
    Composite composite = new Composite(parent, SWT.NONE);
    composite.setLayout(new FillLayout(SWT.VERTICAL));
    new Label(composite, SWT.CENTER).setText("Welcome to the Address Book Entry Wizard!");
    new Label(composite, SWT.LEFT)
        .setText("This wizard guides you through creating an Address Book entry.");
    new Label(composite, SWT.LEFT).setText("Click Next to continue.");
    setControl(composite);
  }
}
class AddressEntry {
  private String lastName;
  private String firstName;
  private String email;
  public String getEmail() {
    return email;
  }
  public void setEmail(String email) {
    this.email = email;
  }
  public String getFirstName() {
    return firstName;
  }
  public void setFirstName(String firstName) {
    this.firstName = firstName;
  }
  public String getLastName() {
    return lastName;
  }
  public void setLastName(String lastName) {
    this.lastName = lastName;
  }
}
class AddressBookLabelProvider implements ITableLabelProvider {
  public Image getColumnImage(Object element, int columnIndex) {
    return null;
  }
  public String getColumnText(Object element, int columnIndex) {
    AddressEntry ae = (AddressEntry) element;
    switch (columnIndex) {
    case 0:
      return ae.getFirstName();
    case 1:
      return ae.getLastName();
    case 2:
      return ae.getEmail();
    }
    return "";
  }
  public void addListener(ILabelProviderListener listener) {
  }
  public void dispose() {
  }
  public boolean isLabelProperty(Object element, String property) {
    return false;
  }
  public void removeListener(ILabelProviderListener listener) {
  }
}
class AddressBookContentProvider implements IStructuredContentProvider {
  public Object[] getElements(Object inputElement) {
    return ((List) inputElement).toArray();
  }
  public void dispose() {
  }
  public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
  }
}





TableViewer: setInput(Object inputs)

import java.util.LinkedList;
import java.util.List;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.jface.resource.ImageDescriptor;
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.Viewer;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.ruposite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Text;
public class MainClass extends ApplicationWindow {
  public static MainClass mainWindow;
  AddEntryAction addEntryAction;
  List entries;
  private TableViewer viewer;
  public MainClass() {
    super(null);
    mainWindow = this;
    addEntryAction = new AddEntryAction();
    entries = new LinkedList();
    addToolBar(SWT.NONE);
  }
  public void run() {
    setBlockOnOpen(true);
    open();
    Display.getCurrent().dispose();
  }
  public void add(AddressEntry entry) {
    entries.add(entry);
    refresh();
  }
  protected void configureShell(Shell shell) {
    super.configureShell(shell);
    shell.setSize(600, 400);
  }
  protected Control createContents(Composite parent) {
    viewer = new TableViewer(parent);
    viewer.setContentProvider(new AddressBookContentProvider());
    viewer.setLabelProvider(new AddressBookLabelProvider());
    viewer.setInput(entries);
    Table table = viewer.getTable();
    new TableColumn(table, SWT.LEFT).setText("First Name");
    new TableColumn(table, SWT.LEFT).setText("Last Name");
    new TableColumn(table, SWT.LEFT).setText("E-mail Address");
    table.setHeaderVisible(true);
    table.setLinesVisible(true);
    refresh();
    return table;
  }
  protected ToolBarManager createToolBarManager(int style) {
    ToolBarManager tbm = new ToolBarManager(style);
    tbm.add(addEntryAction);
    return tbm;
  }
  private void refresh() {
    viewer.refresh();
    Table table = viewer.getTable();
    for (int i = 0, n = table.getColumnCount(); i < n; i++) {
      table.getColumn(i).pack();
    }
  }
  public static void main(String[] args) {
    new MainClass().run();
  }
}
class AddEntryAction extends Action {
  public AddEntryAction() {
    super("Add Entry");
    setToolTipText("Add Entry");
  }
  public void run() {
    WizardDialog dlg = new WizardDialog(MainClass.mainWindow.getShell(),
        new AddEntryWizard());
    dlg.open();
  }
}
class AddEntryWizard extends Wizard {
  private WelcomePage welcomePage= new WelcomePage();
  private NamePage namePage= new NamePage();
  private EmailPage emailPage= new EmailPage();
  public AddEntryWizard() {
    addPage(welcomePage);
    addPage(namePage);
    addPage(emailPage);
    setWindowTitle("Address Book Entry Wizard");
  }
  public boolean performFinish() {
    AddressEntry entry = new AddressEntry();
    entry.setFirstName(namePage.getFirstName());
    entry.setLastName(namePage.getLastName());
    entry.setEmail(emailPage.getEmail());
    MainClass.mainWindow.add(entry);
    return true;
  }
}
class EmailPage extends WizardPage {
  private String email = "";
  public EmailPage() {
    super("E-mail", "E-mail Address", ImageDescriptor.createFromFile(EmailPage.class,
        "email.gif"));
    setDescription("Enter the e-mail address");
    setPageComplete(false);
  }
  public void createControl(Composite parent) {
    Composite composite = new Composite(parent, SWT.NONE);
    composite.setLayout(new GridLayout(2, false));
    new Label(composite, SWT.LEFT).setText("E-mail Address:");
    final Text ea = new Text(composite, SWT.BORDER);
    ea.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    ea.addModifyListener(new ModifyListener() {
      public void modifyText(ModifyEvent event) {
        email = ea.getText();
        setPageComplete(email.length() > 0);
      }
    });
    setControl(composite);
  }
  public String getEmail() {
    return email;
  }
}
class NamePage extends WizardPage {
  private String firstName = "";
  private String lastName = "";
  public NamePage() {
    super("Name", "Name", ImageDescriptor.createFromFile(NamePage.class, "name.gif"));
    setDescription("Enter the first and last names");
    setPageComplete(false);
  }
  public void createControl(Composite parent) {
    Composite composite = new Composite(parent, SWT.NONE);
    composite.setLayout(new GridLayout(2, false));
    new Label(composite, SWT.LEFT).setText("First Name:");
    final Text first = new Text(composite, SWT.BORDER);
    first.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    new Label(composite, SWT.LEFT).setText("Last Name:");
    final Text last = new Text(composite, SWT.BORDER);
    last.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    first.addModifyListener(new ModifyListener() {
      public void modifyText(ModifyEvent event) {
        firstName = first.getText();
        setPageComplete(firstName.length() > 0 && lastName.length() > 0);
      }
    });
    last.addModifyListener(new ModifyListener() {
      public void modifyText(ModifyEvent event) {
        lastName = last.getText();
        setPageComplete(firstName.length() > 0 && lastName.length() > 0);
      }
    });
    setControl(composite);
  }
  public String getFirstName() {
    return firstName;
  }
  public String getLastName() {
    return lastName;
  }
}
class WelcomePage extends WizardPage {
  protected WelcomePage() {
    super("Welcome", "Welcome", ImageDescriptor.createFromFile(WelcomePage.class,
        "welcome.gif"));
    setDescription("Welcome to the Address Book Entry Wizard");
  }
  public void createControl(Composite parent) {
    Composite composite = new Composite(parent, SWT.NONE);
    composite.setLayout(new FillLayout(SWT.VERTICAL));
    new Label(composite, SWT.CENTER).setText("Welcome to the Address Book Entry Wizard!");
    new Label(composite, SWT.LEFT)
        .setText("This wizard guides you through creating an Address Book entry.");
    new Label(composite, SWT.LEFT).setText("Click Next to continue.");
    setControl(composite);
  }
}
class AddressEntry {
  private String lastName;
  private String firstName;
  private String email;
  public String getEmail() {
    return email;
  }
  public void setEmail(String email) {
    this.email = email;
  }
  public String getFirstName() {
    return firstName;
  }
  public void setFirstName(String firstName) {
    this.firstName = firstName;
  }
  public String getLastName() {
    return lastName;
  }
  public void setLastName(String lastName) {
    this.lastName = lastName;
  }
}
class AddressBookLabelProvider implements ITableLabelProvider {
  public Image getColumnImage(Object element, int columnIndex) {
    return null;
  }
  public String getColumnText(Object element, int columnIndex) {
    AddressEntry ae = (AddressEntry) element;
    switch (columnIndex) {
    case 0:
      return ae.getFirstName();
    case 1:
      return ae.getLastName();
    case 2:
      return ae.getEmail();
    }
    return "";
  }
  public void addListener(ILabelProviderListener listener) {
  }
  public void dispose() {
  }
  public boolean isLabelProperty(Object element, String property) {
    return false;
  }
  public void removeListener(ILabelProviderListener listener) {
  }
}
class AddressBookContentProvider implements IStructuredContentProvider {
  public Object[] getElements(Object inputElement) {
    return ((List) inputElement).toArray();
  }
  public void dispose() {
  }
  public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
  }
}





TableViewer: setLabelProvider(IBaseLabelProvider ibp)

import java.util.LinkedList;
import java.util.List;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.jface.resource.ImageDescriptor;
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.Viewer;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.ruposite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Text;
public class MainClass extends ApplicationWindow {
  public static MainClass mainWindow;
  AddEntryAction addEntryAction;
  List entries;
  private TableViewer viewer;
  public MainClass() {
    super(null);
    mainWindow = this;
    addEntryAction = new AddEntryAction();
    entries = new LinkedList();
    addToolBar(SWT.NONE);
  }
  public void run() {
    setBlockOnOpen(true);
    open();
    Display.getCurrent().dispose();
  }
  public void add(AddressEntry entry) {
    entries.add(entry);
    refresh();
  }
  protected void configureShell(Shell shell) {
    super.configureShell(shell);
    shell.setSize(600, 400);
  }
  protected Control createContents(Composite parent) {
    viewer = new TableViewer(parent);
    viewer.setContentProvider(new AddressBookContentProvider());
    viewer.setLabelProvider(new AddressBookLabelProvider());
    viewer.setInput(entries);
    Table table = viewer.getTable();
    new TableColumn(table, SWT.LEFT).setText("First Name");
    new TableColumn(table, SWT.LEFT).setText("Last Name");
    new TableColumn(table, SWT.LEFT).setText("E-mail Address");
    table.setHeaderVisible(true);
    table.setLinesVisible(true);
    refresh();
    return table;
  }
  protected ToolBarManager createToolBarManager(int style) {
    ToolBarManager tbm = new ToolBarManager(style);
    tbm.add(addEntryAction);
    return tbm;
  }
  private void refresh() {
    viewer.refresh();
    Table table = viewer.getTable();
    for (int i = 0, n = table.getColumnCount(); i < n; i++) {
      table.getColumn(i).pack();
    }
  }
  public static void main(String[] args) {
    new MainClass().run();
  }
}
class AddEntryAction extends Action {
  public AddEntryAction() {
    super("Add Entry");
    setToolTipText("Add Entry");
  }
  public void run() {
    WizardDialog dlg = new WizardDialog(MainClass.mainWindow.getShell(),
        new AddEntryWizard());
    dlg.open();
  }
}
class AddEntryWizard extends Wizard {
  private WelcomePage welcomePage= new WelcomePage();
  private NamePage namePage= new NamePage();
  private EmailPage emailPage= new EmailPage();
  public AddEntryWizard() {
    addPage(welcomePage);
    addPage(namePage);
    addPage(emailPage);
    setWindowTitle("Address Book Entry Wizard");
  }
  public boolean performFinish() {
    AddressEntry entry = new AddressEntry();
    entry.setFirstName(namePage.getFirstName());
    entry.setLastName(namePage.getLastName());
    entry.setEmail(emailPage.getEmail());
    MainClass.mainWindow.add(entry);
    return true;
  }
}
class EmailPage extends WizardPage {
  private String email = "";
  public EmailPage() {
    super("E-mail", "E-mail Address", ImageDescriptor.createFromFile(EmailPage.class,
        "email.gif"));
    setDescription("Enter the e-mail address");
    setPageComplete(false);
  }
  public void createControl(Composite parent) {
    Composite composite = new Composite(parent, SWT.NONE);
    composite.setLayout(new GridLayout(2, false));
    new Label(composite, SWT.LEFT).setText("E-mail Address:");
    final Text ea = new Text(composite, SWT.BORDER);
    ea.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    ea.addModifyListener(new ModifyListener() {
      public void modifyText(ModifyEvent event) {
        email = ea.getText();
        setPageComplete(email.length() > 0);
      }
    });
    setControl(composite);
  }
  public String getEmail() {
    return email;
  }
}
class NamePage extends WizardPage {
  private String firstName = "";
  private String lastName = "";
  public NamePage() {
    super("Name", "Name", ImageDescriptor.createFromFile(NamePage.class, "name.gif"));
    setDescription("Enter the first and last names");
    setPageComplete(false);
  }
  public void createControl(Composite parent) {
    Composite composite = new Composite(parent, SWT.NONE);
    composite.setLayout(new GridLayout(2, false));
    new Label(composite, SWT.LEFT).setText("First Name:");
    final Text first = new Text(composite, SWT.BORDER);
    first.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    new Label(composite, SWT.LEFT).setText("Last Name:");
    final Text last = new Text(composite, SWT.BORDER);
    last.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    first.addModifyListener(new ModifyListener() {
      public void modifyText(ModifyEvent event) {
        firstName = first.getText();
        setPageComplete(firstName.length() > 0 && lastName.length() > 0);
      }
    });
    last.addModifyListener(new ModifyListener() {
      public void modifyText(ModifyEvent event) {
        lastName = last.getText();
        setPageComplete(firstName.length() > 0 && lastName.length() > 0);
      }
    });
    setControl(composite);
  }
  public String getFirstName() {
    return firstName;
  }
  public String getLastName() {
    return lastName;
  }
}
class WelcomePage extends WizardPage {
  protected WelcomePage() {
    super("Welcome", "Welcome", ImageDescriptor.createFromFile(WelcomePage.class,
        "welcome.gif"));
    setDescription("Welcome to the Address Book Entry Wizard");
  }
  public void createControl(Composite parent) {
    Composite composite = new Composite(parent, SWT.NONE);
    composite.setLayout(new FillLayout(SWT.VERTICAL));
    new Label(composite, SWT.CENTER).setText("Welcome to the Address Book Entry Wizard!");
    new Label(composite, SWT.LEFT)
        .setText("This wizard guides you through creating an Address Book entry.");
    new Label(composite, SWT.LEFT).setText("Click Next to continue.");
    setControl(composite);
  }
}
class AddressEntry {
  private String lastName;
  private String firstName;
  private String email;
  public String getEmail() {
    return email;
  }
  public void setEmail(String email) {
    this.email = email;
  }
  public String getFirstName() {
    return firstName;
  }
  public void setFirstName(String firstName) {
    this.firstName = firstName;
  }
  public String getLastName() {
    return lastName;
  }
  public void setLastName(String lastName) {
    this.lastName = lastName;
  }
}
class AddressBookLabelProvider implements ITableLabelProvider {
  public Image getColumnImage(Object element, int columnIndex) {
    return null;
  }
  public String getColumnText(Object element, int columnIndex) {
    AddressEntry ae = (AddressEntry) element;
    switch (columnIndex) {
    case 0:
      return ae.getFirstName();
    case 1:
      return ae.getLastName();
    case 2:
      return ae.getEmail();
    }
    return "";
  }
  public void addListener(ILabelProviderListener listener) {
  }
  public void dispose() {
  }
  public boolean isLabelProperty(Object element, String property) {
    return false;
  }
  public void removeListener(ILabelProviderListener listener) {
  }
}
class AddressBookContentProvider implements IStructuredContentProvider {
  public Object[] getElements(Object inputElement) {
    return ((List) inputElement).toArray();
  }
  public void dispose() {
  }
  public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
  }
}