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