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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
public class View extends ViewPart {
public static final String ID = "simpleExplorer.view";
private TableViewer viewer;
private TblRowFilter filter;
private TableSorter tableSorter;
private DbBean myDbBean;
private ProcStock myProcStock;
/**
* Méthode permettant de définir le contenu de la vue
*/
public void createPartControl(Composite parent) {
ResultSet rs = null;
myProcStock = new ProcStock("rep_getAnnouncePendingAcknowledgement");
// ouverture de la connexion
try {
myDbBean = new DbBean();
myDbBean.connect();
} catch (Exception e) {
e.printStackTrace();
}
// on récupère le résulSset
try {
rs = myDbBean.executeProc(myProcStock.getName());
} catch (SQLException e) {
System.out.println("Erreur lors de l'éxécution de la requête");
} catch (Exception e) {
e.printStackTrace();
}
GridLayout layout = new GridLayout(2, false);
parent.setLayout(layout);
Label searchLabel = new Label(parent, SWT.NONE);
searchLabel.setText("Search: ");
final Text searchText = new Text(parent, SWT.BORDER | SWT.SEARCH);
searchText.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL
| GridData.HORIZONTAL_ALIGN_FILL));
searchText.addKeyListener(new KeyAdapter() {
public void keyReleased(KeyEvent ke) {
filter.setSearchText(searchText.getText());
viewer.refresh();
}
});
createTableViewer(parent, rs);
// on ferme les connexions
try {
rs.close();
myDbBean.disConnect();
} catch (SQLException e) {
System.out.println("Erreur lors de la fermeture de la connexion");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* cette méthode permet de créer un tableViewer
* @param parent
* @param resultset
*/
private void createTableViewer(final Composite parent, final ResultSet resultset){
viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL
| SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER);
createColumns(viewer, resultset);
viewer.setContentProvider(new TblRowContentProvider());
viewer.setLabelProvider(new TblRowLabelProvider());
viewer.setInput(ModelProvider.getInstance().getTblRows());
getSite().setSelectionProvider(viewer);
tableSorter = new TableSorter();
viewer.setSorter(tableSorter);
filter = new TblRowFilter();
viewer.addFilter(filter);
GridData gridData = new GridData();
gridData.verticalAlignment = GridData.FILL;
gridData.horizontalSpan = 2;
gridData.grabExcessHorizontalSpace = true;
gridData.grabExcessVerticalSpace = true;
gridData.horizontalAlignment = GridData.FILL;
viewer.getControl().setLayoutData(gridData);
}
/**
* les colonnes sont crées avec des noms vennant de la base de données
* @param viewer
*/
private void createColumns(final TableViewer viewer, ResultSet resultset) {
Table table = viewer.getTable();
String[] titles = null;
String[] types = null;
ResultSetMetaData metaData = null;
int colCount = 0;
// à gérer dynamiquement
int[] bounds = { 100, 100, 100, 100, 100, 100, 100, 100, 100 };
/*Récupération des info métadata sur la table. Penser à le faire plus dynamiquement*/
try {
metaData = resultset.getMetaData();
colCount = metaData.getColumnCount();
int j = 0;
titles = new String[colCount];
types = new String[colCount];
//Remplissage de titles
for (j = 0 ; j < colCount ; j++){
titles[j] = metaData.getColumnName(j+1);
types[j] = metaData.getColumnTypeName(j+1);
}
}
catch (Exception e) {
System.err.println(e);
}
for (int i = 0; i < titles.length; i++) {
final int index = i;
final TableViewerColumn viewerColumn = new TableViewerColumn(
viewer, SWT.NONE);
final TableColumn column = viewerColumn.getColumn();
column.setText(titles[i]);
column.setWidth(bounds[i]);
column.setResizable(true);
column.setMoveable(true);
column.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
tableSorter.setColumn(index);
int dir = viewer.getTable().getSortDirection();
if (viewer.getTable().getSortColumn() == column) {
dir = dir == SWT.UP ? SWT.DOWN : SWT.UP;
} else {
dir = SWT.DOWN;
}
viewer.getTable().setSortDirection(dir);
viewer.getTable().setSortColumn(column);
viewer.refresh();
}
});
}
table.setHeaderVisible(true);
table.setLinesVisible(true);
}
public void setFocus() {
viewer.getControl().setFocus();
}
} |
Partager