1 package com.quantum.wizards;
3 import java.sql.SQLException;
5 import com.quantum.ImageStore;
6 import com.quantum.model.Column;
7 import com.quantum.model.Entity;
8 import com.quantum.model.NotConnectedException;
9 import com.quantum.sql.FilterSort;
10 import com.quantum.sql.SQLResultSetResults;
11 import com.quantum.ui.dialog.SQLExceptionDialog;
12 import com.quantum.util.connection.ConnectionUtil;
14 import org.eclipse.jface.viewers.CellEditor;
15 import org.eclipse.jface.viewers.CheckboxCellEditor;
16 import org.eclipse.jface.viewers.ComboBoxCellEditor;
17 import org.eclipse.jface.viewers.ICellModifier;
18 import org.eclipse.jface.viewers.ILabelProviderListener;
19 import org.eclipse.jface.viewers.IStructuredContentProvider;
20 import org.eclipse.jface.viewers.ITableLabelProvider;
21 import org.eclipse.jface.viewers.TableViewer;
22 import org.eclipse.jface.viewers.TextCellEditor;
23 import org.eclipse.jface.viewers.Viewer;
24 import org.eclipse.jface.wizard.WizardPage;
25 import org.eclipse.swt.SWT;
26 import org.eclipse.swt.graphics.Image;
27 import org.eclipse.swt.layout.GridData;
28 import org.eclipse.swt.layout.GridLayout;
29 import org.eclipse.swt.widgets.Composite;
30 import org.eclipse.swt.widgets.Label;
31 import org.eclipse.swt.widgets.Table;
32 import org.eclipse.swt.widgets.TableItem;
34 public class SortFilterPage extends WizardPage implements SQLPage {
36 class SortFilterTableValues {
37 private String colNames = null;
38 private boolean isWhere = false;
39 private String operators = "";
40 private String values = null;
41 private boolean isString = false;
42 private boolean isOrderBy = false;
43 private String sorter = "";
46 * @return Returns the isOrderBy.
48 public boolean isOrderBy() {
52 * @param isOrderBy The isOrderBy to set.
54 public void setOrderBy(boolean isOrderBy) {
55 this.isOrderBy = isOrderBy;
58 * @return Returns the isString.
60 public boolean isString() {
64 * @param isString The isString to set.
66 public void setString(boolean isString) {
67 this.isString = isString;
70 * @return Returns the isWhere.
72 public boolean isWhere() {
76 * @param isWhere The isWhere to set.
78 public void setWhere(boolean isWhere) {
79 this.isWhere = isWhere;
82 * @return Returns the operators.
84 public String getOperators() {
88 * @param operators The operators to set.
90 public void setOperators(String operators) {
91 this.operators = operators;
94 * @return Returns the sorter.
96 public String getSorter() {
100 * @param sorter The sorter to set.
102 public void setSorter(String sorter) {
103 this.sorter = sorter;
106 * @return Returns the values.
108 public String getValues() {
112 * @param values The values to set.
114 public void setValues(String values) {
115 this.values = values;
118 * @return Returns the colNames.
120 public String getColNames() {
124 * @param colNames The colNames to set.
126 public void setColNames(String colNames) {
127 this.colNames = colNames;
131 class LabelProviderImpl implements ITableLabelProvider {
132 public Image getColumnImage(Object element, int columnIndex) {
133 if (columnIndex == 0) {
134 return ((SortFilterTableValues)element).isWhere() ? imgCheck : imgUncheck;
135 } else if (columnIndex == 3) {
136 return ((SortFilterTableValues)element).isString() ? imgCheck : imgUncheck;
137 } else if (columnIndex == 4) {
138 return ((SortFilterTableValues)element).isOrderBy() ? imgCheck : imgUncheck;
143 public String getColumnText(Object element, int columnIndex) {
145 SortFilterTableValues sortFilterRow = (SortFilterTableValues)element;
147 switch (columnIndex) {
149 sReturn = sortFilterRow.getColNames();
152 sReturn = sortFilterRow.getOperators();
155 sReturn = sortFilterRow.getValues();
160 sReturn = sortFilterRow.getColNames();
163 sReturn = sortFilterRow.getSorter();
170 public void addListener(ILabelProviderListener listener) {}
171 public void dispose() {}
172 public boolean isLabelProperty(Object element, String property) {
175 public void removeListener(ILabelProviderListener listener) {}
178 class ContentProviderImpl implements IStructuredContentProvider {
179 public Object[] getElements(Object inputElement) {
180 return sortFilterValues;
183 public void dispose() {}
184 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {}
187 class CellModifierImpl implements ICellModifier {
189 public boolean canModify(Object element, String property) {
193 public Object getValue(Object element, String property) {
194 System.out.println("getValue called");
196 // Find the index of the column
197 int colIndx = comUI.getColumnNamesAsList(colNames).indexOf(property);
198 System.out.println("colIndx : " + colIndx);
200 Object rResult = null;
201 SortFilterTableValues sortFilterVal = (SortFilterTableValues)element;
205 rResult = new Boolean(sortFilterVal.isWhere());
208 String sTemp = sortFilterVal.getOperators();
209 int nIndex = comUI.getColumnNamesAsList(oper).indexOf(sTemp);
210 rResult = new Integer(nIndex);
213 rResult = sortFilterVal.getValues();
216 rResult = new Boolean(sortFilterVal.isString());
219 rResult = new Boolean(sortFilterVal.isOrderBy());
222 String sTemp2 = sortFilterVal.getSorter();
223 int nIndx = comUI.getColumnNamesAsList(order).indexOf(sTemp2);
224 rResult = new Integer(nIndx);
234 public void modify(Object element, String property, Object value) {
235 int colIndx = comUI.getColumnNamesAsList(colNames).indexOf(property);
237 TableItem item = (TableItem) element;
238 SortFilterTableValues sortFilterVal = (SortFilterTableValues)item.getData();
242 sortFilterVal.setWhere(((Boolean)value).booleanValue());
245 int nIdx = ((Integer)value).intValue();
247 sortFilterVal.setOperators(oper[nIdx]);
249 sortFilterVal.setOperators("");
253 sortFilterVal.setValues(value.toString());
256 sortFilterVal.setString(((Boolean)value).booleanValue());
259 sortFilterVal.setOrderBy(((Boolean)value).booleanValue());
262 int nId = ((Integer)value).intValue();
264 sortFilterVal.setSorter(order[nId]);
266 sortFilterVal.setSorter("");
278 private SQLResultSetResults.Row row;
279 private ConnectionUtil connectionUtil = new ConnectionUtil();
280 CommonWizardUI comUI;
282 SortFilterTableValues[] sortFilterValues;
283 TableViewer tableViewer = null;
284 static Image imgCheck = null;
285 static Image imgUncheck = null;
286 String columnNames[];
288 FilterSort filterSort = new FilterSort();
289 private SQLResultSetResults results;
292 imgCheck = ImageStore.getImage(ImageStore.CHECKED);
293 imgUncheck = ImageStore.getImage(ImageStore.UNCHECKED);
296 // constant declarations
297 String[] colNames = new String[] { "Where", "Operator", "Values", "Is String", "Order By", "Sorter" };
298 String[] oper = new String[] { "=", "<>", "<", ">" };
299 String[] order = new String[] { "", "ASC", "DESC" };
301 public SortFilterPage(String pageName) {
305 public void init(SQLResultSetResults results, SQLResultSetResults.Row row) {
306 this.results = results;
310 public void createControl(Composite parent) {
311 System.out.println("page create control"); //$NON-NLS-1$
313 Composite container = new Composite(parent, SWT.NULL);
314 container.setLayout(new GridLayout());
315 container.setLayoutData(new GridData(GridData.FILL_BOTH | GridData.VERTICAL_ALIGN_BEGINNING));
317 comUI = new CommonWizardUI();
319 columnNames = this.results.getColumnNames();
320 int nLen = columnNames.length;
321 sortFilterValues = new SortFilterTableValues[nLen];
323 for (int nCtr = 0; nCtr < nLen; nCtr++) {
324 sortFilterValues[nCtr] = new SortFilterTableValues();
325 sortFilterValues[nCtr].setColNames(columnNames[nCtr]);
327 Object data = this.row == null ? null : this.row.get(nCtr+1);
328 sortFilterValues[nCtr].setValues(data == null ? "" : data.toString());
331 createTable(container);
333 query = new Label(container, SWT.WRAP);
334 query.setLayoutData(comUI.createGridData(1, GridData.FILL));
336 setControl(container);
337 setPageComplete(true);
340 public void updateQuery() {
341 filterSort.clearFilters();
343 int nLen = columnNames.length;
344 for (int nCtr = 0; nCtr < nLen; nCtr++) {
345 if (sortFilterValues[nCtr].isWhere()) {
346 filterSort.addFilter(columnNames[nCtr], sortFilterValues[nCtr].getOperators(), sortFilterValues[nCtr].getValues(), sortFilterValues[nCtr].isString());
348 if (sortFilterValues[nCtr].isOrderBy()) {
349 filterSort.addSort(columnNames[nCtr], sortFilterValues[nCtr].getSorter());
351 filterSort.removeSort(columnNames[nCtr]);
355 query.setText(filterSort.toString());
358 public boolean performFinish() {
359 this.results.setFilterSort(filterSort);
361 this.results.refresh(this.connectionUtil.connect(
362 this.results.getBookmark(), getShell()));
364 } catch (SQLException e) {
365 SQLExceptionDialog.openException(getShell(), this.results.getBookmark(), e);
370 private void updateView() {
371 this.tableViewer.update(sortFilterValues, null);
378 * @throws NotConnectedException
379 * @throws SQLException
381 protected Column getColumn(Entity entity, String columnName) {
383 return entity == null ? null : entity.getColumn(columnName);
384 } catch (NotConnectedException e) {
386 } catch (SQLException e) {
391 private void createTable(Composite composite) {
392 System.out.println("Creating table...");
393 Table table = comUI.createTablePage(composite);
395 comUI.createTableColumn(table, colNames[0], SWT.LEFT, 0, 150); // isWhere
396 comUI.createTableColumn(table, colNames[1], SWT.CENTER, 1, 70); // operator
397 comUI.createTableColumn(table, colNames[2], SWT.LEFT, 2, 150); // values
398 comUI.createTableColumn(table, colNames[3], SWT.LEFT, 3, 70); // isString
399 comUI.createTableColumn(table, colNames[4], SWT.LEFT, 4, 150); // isOrderBy
400 comUI.createTableColumn(table, colNames[5], SWT.CENTER, 5, 70); // sorter
401 this.tableViewer = new TableViewer(table);
402 this.tableViewer.setColumnProperties(colNames);
404 CellEditor[] editor = new CellEditor[colNames.length];
406 editor[0] = new CheckboxCellEditor(table, SWT.NULL);
408 editor[1] = new ComboBoxCellEditor(table, oper, SWT.READ_ONLY);
410 editor[2] = new TextCellEditor(table);
412 editor[3] = new CheckboxCellEditor(table, SWT.NULL);
414 editor[4] = new CheckboxCellEditor(table, SWT.NULL);
416 editor[5] = new ComboBoxCellEditor(table, order, SWT.READ_ONLY);
418 this.tableViewer.setCellEditors(editor);
419 this.tableViewer.setLabelProvider(new LabelProviderImpl());
420 this.tableViewer.setContentProvider(new ContentProviderImpl());
421 this.tableViewer.setCellModifier(new CellModifierImpl());
422 this.tableViewer.setInput(sortFilterValues);