4447e1076c791e907891531a5f394b61e5524b98
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / wizards / SortFilterPage.java
1 package com.quantum.wizards;
2
3 import java.sql.SQLException;
4
5 import com.quantum.sql.FilterSort;
6 import com.quantum.sql.SQLResultSetResults;
7 import com.quantum.ui.dialog.ExceptionDisplayDialog;
8 import com.quantum.util.connection.ConnectionUtil;
9
10 import org.eclipse.jface.wizard.WizardPage;
11 import org.eclipse.swt.SWT;
12 import org.eclipse.swt.events.ModifyEvent;
13 import org.eclipse.swt.events.ModifyListener;
14 import org.eclipse.swt.events.SelectionEvent;
15 import org.eclipse.swt.events.SelectionListener;
16 import org.eclipse.swt.layout.GridData;
17 import org.eclipse.swt.layout.GridLayout;
18 import org.eclipse.swt.widgets.Button;
19 import org.eclipse.swt.widgets.Combo;
20 import org.eclipse.swt.widgets.Composite;
21 import org.eclipse.swt.widgets.Label;
22 import org.eclipse.swt.widgets.Text;
23
24 public class SortFilterPage extends WizardPage implements SQLPage {
25         Button[] filter;
26         Combo[] operator;
27         Text[] filterValues;
28         Button[] stringFlags;
29         Button[] sort;
30         Combo[] ascDesc;
31         private SQLResultSetResults.Row row;
32         
33         private ConnectionUtil connectionUtil = new ConnectionUtil();
34         
35         String columnNames[];
36         Label query;
37         FilterSort filterSort = new FilterSort();
38         private SQLResultSetResults results;
39         public SortFilterPage(String pageName) {
40                 super(pageName);
41         }
42
43         public void init(SQLResultSetResults results, SQLResultSetResults.Row row) {
44                 this.results = results;
45                 this.row = row;
46         }
47
48         public void createControl(Composite parent) {
49                 System.out.println("page create control"); //$NON-NLS-1$
50                 
51                 Composite container = new Composite(parent, SWT.NULL);
52                 GridLayout layout = new GridLayout();
53                 container.setLayout(layout);
54                 int layoutColumns = 6;
55                 layout.numColumns = layoutColumns;
56
57                 columnNames = this.results.getColumnNames();
58                 
59                 int size = this.results.getColumnCount();
60                 filter = new Button[size];
61                 operator = new Combo[size];
62                 filterValues = new Text[size];
63                 stringFlags = new Button[size];
64                 sort = new Button[size];
65                 ascDesc = new Combo[size];
66                 for (int i = 0; i < size; i++) {
67                         filter[i] = new Button(container, SWT.CHECK);
68                         filter[i].setText(columnNames[i]);
69                         filter[i].addSelectionListener(new SelectionListener() {
70                                 public void widgetDefaultSelected(SelectionEvent e) {
71                                 }
72                                 public void widgetSelected(SelectionEvent e) {
73                                         updateQuery();
74                                 }
75                         });
76
77                         operator[i] = new Combo(container, SWT.SINGLE | SWT.READ_ONLY);
78                         operator[i].add("="); //$NON-NLS-1$
79                         operator[i].add("<>"); //$NON-NLS-1$
80                         operator[i].add("<"); //$NON-NLS-1$
81                         operator[i].add(">"); //$NON-NLS-1$
82                         operator[i].addSelectionListener(new SelectionListener() {
83                                 public void widgetDefaultSelected(SelectionEvent e) {
84                                 }
85                                 public void widgetSelected(SelectionEvent e) {
86                                         updateQuery();
87                                 }
88                         });
89                         
90                         filterValues[i] = new Text(container, SWT.BORDER);
91                         Object data = this.row == null ? null : this.row.get(i+1);
92                         filterValues[i].setText(data == null ? "" : data.toString());
93                         filterValues[i].addModifyListener(new ModifyListener() {
94                                 public void modifyText(ModifyEvent e) {
95                                         updateQuery();
96                                 }                               
97                         });
98                         
99                         stringFlags[i] = new Button(container, SWT.CHECK);
100                         stringFlags[i].setText("String"); //$NON-NLS-1$
101                         stringFlags[i].addSelectionListener(new SelectionListener() {
102                                 public void widgetDefaultSelected(SelectionEvent e) {
103                                 }
104                                 public void widgetSelected(SelectionEvent e) {
105                                         updateQuery();
106                                 }
107                         });
108                         
109                         final int index = i;
110                         sort[i] = new Button(container, SWT.CHECK);
111                         sort[i].setText(columnNames[i]);
112                         sort[i].addSelectionListener(new SelectionListener() {
113                                 public void widgetDefaultSelected(SelectionEvent e) {
114                                 }
115                                 public void widgetSelected(SelectionEvent e) {
116                                         if (sort[index].getSelection()) {
117                                                 filterSort.addSort(columnNames[index], ascDesc[index].getText());
118                                         } else {
119                                                 filterSort.removeSort(columnNames[index]);
120                                         }
121                                         updateQuery();
122                                 }
123                         });
124
125                         ascDesc[i] = new Combo(container, SWT.SINGLE | SWT.READ_ONLY);
126                         ascDesc[i].add(""); //$NON-NLS-1$
127                         ascDesc[i].add("ASC"); //$NON-NLS-1$
128                         ascDesc[i].add("DESC"); //$NON-NLS-1$
129                         ascDesc[i].addSelectionListener(new SelectionListener() {
130                                 public void widgetDefaultSelected(SelectionEvent e) {
131                                 }
132                                 public void widgetSelected(SelectionEvent e) {
133                                         if (sort[index].getSelection()) {
134                                                 filterSort.addSort(columnNames[index], ascDesc[index].getText());
135                                         } else {
136                                                 filterSort.removeSort(columnNames[index]);
137                                         }
138                                         updateQuery();
139                                 }
140                         });
141                 }
142                 query = new Label(container, SWT.WRAP);
143                 GridData gridData = new GridData();
144                 gridData.horizontalSpan = layoutColumns;
145                 gridData.horizontalAlignment = GridData.FILL;
146                 gridData.verticalAlignment = GridData.FILL;
147                 gridData.grabExcessHorizontalSpace = true;
148                 gridData.grabExcessVerticalSpace = true;
149                 query.setLayoutData(gridData);
150
151                 setControl(container);
152        
153                 setPageComplete(true);
154         }
155
156         public void updateQuery() {
157                 filterSort.clearFilters();
158                 for (int i = 0; i < filter.length; i++) {
159                         if (filter[i].getSelection()) {
160                                 filterSort.addFilter(filter[i].getText(), operator[i].getText(), filterValues[i].getText(), stringFlags[i].getSelection());
161                         }
162                 }
163                 query.setText(filterSort.toString());
164         }
165
166         public boolean performFinish() {
167                 this.results.setFilterSort(filterSort);
168                 try {
169                         this.results.refresh(this.connectionUtil.connect(
170                                         this.results.getBookmark(), getShell()));
171                         return true;
172                 } catch (SQLException e) {
173                         ExceptionDisplayDialog.openError(getShell(), null, null, e);
174                         return false;
175                 }
176         }
177 }