1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
|
public class TableView extends ViewPart {
public static final String ID = "RCPDemo.tableView";
private static final SimpleDateFormat SDF = new SimpleDateFormat("dd/MM/yyyy", Locale.FRANCE);
private static final String[] colonnes = { "N°", "Nom"};
private List<Personne> personnes = new ArrayList<Personne>();
private TableViewer tViewer;
class ViewContentProvider implements IStructuredContentProvider {
public Object[] getElements(Object input) {
return personnes.toArray();
}
public void dispose() {}
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {}
}
class ViewLabelProvider extends LabelProvider implements ITableLabelProvider {
public String getColumnText(Object obj, int index) {
Personne p = (Personne) obj;
switch (index) {
case 0:
return p.getNumero().toString();
case 1:
return p.getNom();
}
return null;
}
public Image getColumnImage(Object obj, int index) {
return null;
}
public boolean isLabelProperty(Object element, String property) {
return false;
}
}
public void createPartControl(Composite parent) {
// RowLayout avec fill pour avoir cTable et cButtons de la même largeur
RowLayout loParent = new RowLayout(SWT.VERTICAL);
loParent.fill = true;
parent.setLayout(loParent);
// Conteneur de la table
Composite cTable = new Composite(parent, SWT.NONE);
// GridLayout obligatoire pour GridData
cTable.setLayout(new GridLayout(1, false));
// Création du viewer RAS
tViewer = new TableViewer(cTable, SWT.FULL_SELECTION | SWT.SINGLE | SWT.V_SCROLL | SWT.BORDER);
tViewer.setContentProvider(new ViewContentProvider());
tViewer.setLabelProvider(new ViewLabelProvider());
tViewer.setInput(personnes);
// Gestion de la table : FILL, GRAB, CENTER, noms des colonnes, affichage
Table table = tViewer.getTable();
table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
new TableColumn(table, SWT.CENTER).setText(colonnes[0]);
new TableColumn(table, SWT.CENTER).setText(colonnes[1]);
table.setHeaderVisible(true);
table.setLinesVisible(true);
// Données contenues dans la table
try {
personnes.add(new Personne(new Integer(1), "Martin", "Jacques", SDF.parse("10/10/1950"), new Boolean(false)));
} catch (ParseException e) {
e.printStackTrace();
}
tViewer.refresh();
// Conteneur des boutons : justify pour disposition des boutons plus mieux
Composite cButtons = new Composite(parent, SWT.NONE);
RowLayout loButtons = new RowLayout(SWT.HORIZONTAL);
loButtons.justify = true;
cButtons.setLayout(loButtons);
// bouton fiche RAS
Button fiche = new Button(cButtons, SWT.PUSH | SWT.CENTER);
fiche.setText("Fiche");
// Bouton quitter RAS
Button quitter = new Button(cButtons, SWT.PUSH);
quitter.setText("Quitter");
// Affichage : Packing des tous les objets
for (int i = 0, n = table.getColumnCount(); i < n; i++) {
table.getColumn(i).pack();
}
table.pack();
cButtons.pack();
parent.pack();
parent.getShell().pack(); // Et là c'est le drame : fenêtre rikiki ...
}
public void setFocus() {
tViewer.getControl().setFocus();
}
} |
Partager