SQL Plugin copied from Quantum plugin and refactored for PHPEclipse
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.sql / src / net / sourceforge / phpdt / sql / wizards / DeleteRowPage.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.Composite;
13 import org.eclipse.swt.widgets.Label;
14 import org.eclipse.swt.widgets.Text;
15
16 import net.sourceforge.phpdt.sql.sql.MultiSQLServer;
17 import net.sourceforge.phpdt.sql.sql.TableRow;
18 import net.sourceforge.phpdt.sql.view.tableview.TableAdapter;
19
20 public class DeleteRowPage extends WizardPage implements SQLPage {
21         TableRow row;
22         String[] columnNames;
23         Text[] values;
24         Button[] whereValues;
25         Label query;
26
27         public DeleteRowPage(String pageName) {
28                 super(pageName);
29         }
30         
31         public void init(TableRow row, TableAdapter adapter) {
32                 this.row = row;
33         }
34
35         public void createControl(Composite parent) {
36                 System.out.println("page create control");
37                 Composite container = new Composite(parent, SWT.NULL);
38                 GridLayout layout = new GridLayout();
39                 container.setLayout(layout);
40                 int layoutColumns = 3;
41                 layout.numColumns = layoutColumns;
42
43                 if (row == null) {
44                         System.out.println("Row is null");
45                 }
46                 if (row.getColumnNames() == null) {
47                         System.out.println("Columns are null");
48                 }
49                 if (row.getTableData() == null) {
50                         System.out.println("Data is null");
51                 }
52                 columnNames = row.getColumnNames();
53                 String[] data = row.getTableData();
54                 for (int i = 0; i < row.getColumnCount(); i++) {
55                         System.out.println("data = " + i + "=" + data[i]);
56                         System.out.println("column = " + i + "=" + columnNames[i]);
57                 }
58                 values = new Text[row.getColumnCount()];
59                 whereValues = new Button[row.getColumnCount()];
60                 Label temp = new Label(container, SWT.NULL);
61                 temp.setText("Column Name");
62                 temp = new Label(container, SWT.NULL);
63                 temp.setText("Value");
64                 temp = new Label(container, SWT.NULL);
65                 temp.setText("Include in?");
66                 for (int i = 0; i < row.getColumnCount(); i++) {
67                         Label label = new Label(container, SWT.NULL);
68                         label.setText(columnNames[i]);
69                         values[i] = new Text(container, SWT.BORDER | SWT.SINGLE);
70                         GridData fullHorizontal = new GridData();
71                         fullHorizontal.horizontalAlignment = GridData.FILL;
72                         values[i].setLayoutData(fullHorizontal);
73                         values[i].setText(data[i]);
74
75                         values[i].addModifyListener(new ModifyListener() {
76                                 public void modifyText(ModifyEvent e) {
77                                         updateQuery();
78                                 }                               
79                         });
80                         
81                         whereValues[i] = new Button(container, SWT.CHECK);
82                         whereValues[i].setText("Where clause");
83                         whereValues[i].addSelectionListener(new SelectionListener() {
84                                 public void widgetDefaultSelected(SelectionEvent e) {
85                                 }
86                                 public void widgetSelected(SelectionEvent e) {
87                                         updateQuery();
88                                 }
89                         });
90                 }
91                 query = new Label(container, SWT.WRAP);
92                 GridData gridData = new GridData();
93                 gridData.horizontalSpan = layoutColumns;
94                 gridData.horizontalAlignment = GridData.FILL;
95                 gridData.verticalAlignment = GridData.FILL;
96                 gridData.grabExcessHorizontalSpace = true;
97                 gridData.grabExcessVerticalSpace = true;
98                 query.setLayoutData(gridData);
99
100                 setControl(container);
101         updateQuery();
102        
103                 setPageComplete(true);
104         }
105         public void updateQuery() {
106                 System.out.println("Updating query");
107                 StringBuffer whereClause = new StringBuffer();
108                 int numSelected = 0;
109                 for (int i = 0; i < columnNames.length; i++) {
110                         if (whereValues[i].getSelection()) {
111                                 numSelected++;
112                                 whereClause.append(columnNames[i]);
113                                 whereClause.append(" = ");
114                                 whereClause.append(values[i].getText());
115                                 whereClause.append(", ");
116                         }
117                 }
118                 if (whereClause.length() > 1) {
119                         whereClause.deleteCharAt(whereClause.length() - 1);
120                         whereClause.deleteCharAt(whereClause.length() - 1);
121                 }
122                 String query = "DELETE FROM " + row.getTable();
123                 if (numSelected > 0) {
124                         query += " WHERE " + whereClause.toString();
125                 }
126                 if (numSelected > 0) {
127                         setMessage("");
128                 } else {
129                         setMessage("Warning: no \"where clause\" columns selected, all rows will be deleted");
130                 }
131                 this.getControl().pack();
132                 this.query.setText(query);
133         }
134         public boolean performFinish() {
135            MultiSQLServer server = MultiSQLServer.getInstance();
136            server.execute(query.getText());
137            return true;
138         }
139 }