First submit for debug plugin
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.sql / src / net / sourceforge / phpdt / sql / wizards / PHPUpdateRowPage.java
1 package net.sourceforge.phpdt.sql.wizards;
2
3 import java.text.MessageFormat;
4
5 import org.eclipse.jface.preference.IPreferenceStore;
6 import org.eclipse.jface.wizard.WizardPage;
7 import org.eclipse.swt.SWT;
8 import org.eclipse.swt.events.ModifyEvent;
9 import org.eclipse.swt.events.ModifyListener;
10 import org.eclipse.swt.events.SelectionEvent;
11 import org.eclipse.swt.events.SelectionListener;
12 import org.eclipse.swt.layout.GridData;
13 import org.eclipse.swt.layout.GridLayout;
14 import org.eclipse.swt.widgets.Button;
15 import org.eclipse.swt.widgets.Composite;
16 import org.eclipse.swt.widgets.Label;
17 import org.eclipse.swt.widgets.Text;
18
19 import net.sourceforge.phpdt.sql.PHPEclipseSQLPlugin;
20 import net.sourceforge.phpdt.sql.sql.TableRow;
21 import net.sourceforge.phpdt.sql.view.PHPSourceConsole;
22 import net.sourceforge.phpdt.sql.view.tableview.TableAdapter;
23
24 public class PHPUpdateRowPage extends WizardPage implements SQLPage {
25         TableRow row;
26         String[] columnNames;
27         Text[] oldValues;
28         Text[] newValues;
29         Button[] primaryKeys;
30         Button[] setValues;
31         Label query;
32         IPreferenceStore fStore;
33
34         public PHPUpdateRowPage(String pageName) {
35                 super(pageName);
36         }
37
38         public void init(TableRow row, TableAdapter adapter) {
39                 this.row = row;
40         }
41
42         public void createControl(Composite parent) {
43                 System.out.println("page create control");
44                 fStore = PHPEclipseSQLPlugin.getDefault().getPreferenceStore();
45                 Composite container = new Composite(parent, SWT.NULL);
46                 GridLayout layout = new GridLayout();
47                 container.setLayout(layout);
48                 int layoutColumns = 5;
49                 layout.numColumns = layoutColumns;
50
51                 if (row == null) {
52                         System.out.println("Row is null");
53                 }
54                 if (row.getColumnNames() == null) {
55                         System.out.println("Columns are null");
56                 }
57                 if (row.getTableData() == null) {
58                         System.out.println("Data is null");
59                 }
60
61                 columnNames = row.getColumnNames();
62                 String[] data = row.getTableData();
63                 for (int i = 0; i < row.getColumnCount(); i++) {
64                         System.out.println("data = " + i + "=" + data[i]);
65                         System.out.println("column = " + i + "=" + columnNames[i]);
66                 }
67                 oldValues = new Text[row.getColumnCount()];
68                 newValues = new Text[row.getColumnCount()];
69                 primaryKeys = new Button[row.getColumnCount()];
70                 setValues = new Button[row.getColumnCount()];
71                 Label temp = new Label(container, SWT.NULL);
72                 temp.setText("Column Name");
73                 temp = new Label(container, SWT.NULL);
74                 temp.setText("Where Value");
75                 temp = new Label(container, SWT.NULL);
76                 temp.setText("Where");
77                 temp = new Label(container, SWT.NULL);
78                 temp.setText("Set Value");
79                 temp = new Label(container, SWT.NULL);
80                 temp.setText("Set");
81                 for (int i = 0; i < row.getColumnCount(); i++) {
82                         Label label = new Label(container, SWT.NULL);
83                         label.setText(columnNames[i]);
84                         oldValues[i] = new Text(container, SWT.BORDER | SWT.SINGLE);
85                         if (data[i] == null || data[i].equals("")) {
86                                 oldValues[i].setText('$' + columnNames[i]);
87                         } else {
88                                 oldValues[i].setText(data[i]);
89                         }
90                         oldValues[i].addModifyListener(new ModifyListener() {
91                                 public void modifyText(ModifyEvent e) {
92                                         updateQuery();
93                                 }
94                         });
95                         primaryKeys[i] = new Button(container, SWT.CHECK);
96                         //   primaryKeys[i].setText("Where");
97                         primaryKeys[i].addSelectionListener(new SelectionListener() {
98                                 public void widgetDefaultSelected(SelectionEvent e) {
99                                 }
100                                 public void widgetSelected(SelectionEvent e) {
101                                         updateQuery();
102                                 }
103                         });
104                         newValues[i] = new Text(container, SWT.BORDER | SWT.SINGLE);
105
106                         if (data[i] == null || data[i].equals("")) {
107                                 newValues[i].setText('$' + columnNames[i]);
108                         } else {
109                                 newValues[i].setText(data[i]);
110                         }
111                         newValues[i].addModifyListener(new ModifyListener() {
112                                 public void modifyText(ModifyEvent e) {
113                                         updateQuery();
114                                 }
115                         });
116                         setValues[i] = new Button(container, SWT.CHECK);
117                         //  setValues[i].setText("Set Value");
118                         setValues[i].addSelectionListener(new SelectionListener() {
119                                 public void widgetDefaultSelected(SelectionEvent e) {
120                                 }
121                                 public void widgetSelected(SelectionEvent e) {
122                                         updateQuery();
123                                 }
124                         });
125                 }
126                 query = new Label(container, SWT.WRAP);
127                 GridData gridData = new GridData();
128                 gridData.horizontalSpan = layoutColumns;
129                 gridData.horizontalAlignment = GridData.FILL;
130                 gridData.verticalAlignment = GridData.FILL;
131                 gridData.grabExcessHorizontalSpace = true;
132                 gridData.grabExcessVerticalSpace = true;
133                 query.setLayoutData(gridData);
134
135                 setControl(container);
136                 updateQuery();
137
138                 setPageComplete(true);
139         }
140         public void updateQuery() {
141                 System.out.println("Updating update query");
142                 StringBuffer setClause = new StringBuffer();
143                 StringBuffer whereClause = new StringBuffer();
144                 for (int i = 0; i < columnNames.length; i++) {
145                         if (primaryKeys[i].getSelection()) {
146                                 whereClause.append(columnNames[i]);
147                                 whereClause.append(" = ");
148                                 whereClause.append("'" + oldValues[i].getText() + "'");
149                                 whereClause.append(", ");
150                         }
151                         if (setValues[i].getSelection()) {
152                                 setClause.append(columnNames[i]);
153                                 setClause.append(" = ");
154                                 setClause.append("'" + newValues[i].getText() + "'");
155                                 setClause.append(", ");
156                         }
157                 }
158                 if (whereClause.length() > 1) {
159                         whereClause.deleteCharAt(whereClause.length() - 1);
160                         whereClause.deleteCharAt(whereClause.length() - 1);
161                 }
162                 if (setClause.length() > 1) {
163                         setClause.deleteCharAt(setClause.length() - 1);
164                         setClause.deleteCharAt(setClause.length() - 1);
165                 }
166                 String[] arguments =
167                         { setClause.toString(), row.getTable(), whereClause.toString()};
168                 MessageFormat form =
169                         new MessageFormat(
170                                 fStore.getString("phpeclipse.sql.update.template"));
171
172                 String query = form.format(arguments);
173                 //
174                 //    String query = "$results = mysql_query(\"UPDATE " + row.getTable();
175                 //    query += " SET " + setClause.toString();
176                 //    query += " WHERE " + whereClause.toString() + "\");";
177                 this.query.setText(query);
178         }
179         public boolean performFinish() {
180                 PHPSourceConsole console = PHPSourceConsole.getInstance();
181                 console.clear();
182                 console.print(query.getText());
183                 return true;
184         }
185 }