1 package com.quantum.wizards;
3 import java.text.MessageFormat;
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.dnd.TextTransfer;
9 import org.eclipse.swt.dnd.Transfer;
10 import org.eclipse.swt.events.ModifyEvent;
11 import org.eclipse.swt.events.ModifyListener;
12 import org.eclipse.swt.events.SelectionEvent;
13 import org.eclipse.swt.events.SelectionListener;
14 import org.eclipse.swt.layout.GridData;
15 import org.eclipse.swt.layout.GridLayout;
16 import org.eclipse.swt.widgets.Button;
17 import org.eclipse.swt.widgets.Composite;
18 import org.eclipse.swt.widgets.Label;
19 import org.eclipse.swt.widgets.Text;
21 import com.quantum.QuantumPlugin;
22 import com.quantum.sql.TableRow;
23 //import com.quantum.view.PHPSourceConsole;
24 import com.quantum.view.tableview.TableAdapter;
26 public class PHPUpdateRowPage extends WizardPage implements SQLPage {
34 IPreferenceStore fStore;
35 private final static boolean DEBUG = false;
37 public PHPUpdateRowPage(String pageName) {
41 public void init(TableRow row, TableAdapter adapter) {
45 public void createControl(Composite parent) {
47 System.out.println("page create control");
49 fStore = QuantumPlugin.getDefault().getPreferenceStore();
50 Composite container = new Composite(parent, SWT.NULL);
51 GridLayout layout = new GridLayout();
52 container.setLayout(layout);
53 int layoutColumns = 5;
54 layout.numColumns = layoutColumns;
58 System.out.println("Row is null");
60 if (row.getColumnNames() == null) {
61 System.out.println("Columns are null");
63 if (row.getTableData() == null) {
64 System.out.println("Data is null");
68 columnNames = row.getColumnNames();
69 String[] data = row.getTableData();
72 for (int i = 0; i < row.getColumnCount(); i++) {
73 System.out.println("data = " + i + "=" + data[i]);
74 System.out.println("column = " + i + "=" + columnNames[i]);
78 oldValues = new Text[row.getColumnCount()];
79 newValues = new Text[row.getColumnCount()];
80 primaryKeys = new Button[row.getColumnCount()];
81 setValues = new Button[row.getColumnCount()];
82 Label temp = new Label(container, SWT.NULL);
83 temp.setText("Column Name");
84 temp = new Label(container, SWT.NULL);
85 temp.setText("Where Value");
86 temp = new Label(container, SWT.NULL);
87 temp.setText("Where");
88 temp = new Label(container, SWT.NULL);
89 temp.setText("Set Value");
90 temp = new Label(container, SWT.NULL);
92 for (int i = 0; i < row.getColumnCount(); i++) {
93 Label label = new Label(container, SWT.NULL);
94 label.setText(columnNames[i]);
95 oldValues[i] = new Text(container, SWT.BORDER | SWT.SINGLE);
96 if (data[i] == null || data[i].equals("")) {
97 oldValues[i].setText('$' + columnNames[i]);
99 oldValues[i].setText(data[i]);
101 oldValues[i].addModifyListener(new ModifyListener() {
102 public void modifyText(ModifyEvent e) {
106 primaryKeys[i] = new Button(container, SWT.CHECK);
107 // primaryKeys[i].setText("Where");
108 primaryKeys[i].addSelectionListener(new SelectionListener() {
109 public void widgetDefaultSelected(SelectionEvent e) {
111 public void widgetSelected(SelectionEvent e) {
115 newValues[i] = new Text(container, SWT.BORDER | SWT.SINGLE);
117 if (data[i] == null || data[i].equals("")) {
118 newValues[i].setText('$' + columnNames[i]);
120 newValues[i].setText(data[i]);
122 newValues[i].addModifyListener(new ModifyListener() {
123 public void modifyText(ModifyEvent e) {
127 setValues[i] = new Button(container, SWT.CHECK);
128 // setValues[i].setText("Set Value");
129 setValues[i].addSelectionListener(new SelectionListener() {
130 public void widgetDefaultSelected(SelectionEvent e) {
132 public void widgetSelected(SelectionEvent e) {
137 query = new Label(container, SWT.WRAP);
138 GridData gridData = new GridData();
139 gridData.horizontalSpan = layoutColumns;
140 gridData.horizontalAlignment = GridData.FILL;
141 gridData.verticalAlignment = GridData.FILL;
142 gridData.grabExcessHorizontalSpace = true;
143 gridData.grabExcessVerticalSpace = true;
144 query.setLayoutData(gridData);
146 setControl(container);
149 setPageComplete(true);
152 public void updateQuery() {
154 System.out.println("Updating update query");
157 StringBuffer setClause = new StringBuffer();
158 StringBuffer whereClause = new StringBuffer();
159 for (int i = 0; i < columnNames.length; i++) {
160 if (primaryKeys[i].getSelection()) {
161 whereClause.append(columnNames[i]);
162 whereClause.append(" = ");
163 whereClause.append("'" + oldValues[i].getText() + "'");
164 whereClause.append(", ");
166 if (setValues[i].getSelection()) {
167 setClause.append(columnNames[i]);
168 setClause.append(" = ");
169 setClause.append("'" + newValues[i].getText() + "'");
170 setClause.append(", ");
173 if (whereClause.length() > 1) {
174 whereClause.deleteCharAt(whereClause.length() - 1);
175 whereClause.deleteCharAt(whereClause.length() - 1);
177 if (setClause.length() > 1) {
178 setClause.deleteCharAt(setClause.length() - 1);
179 setClause.deleteCharAt(setClause.length() - 1);
181 String[] arguments = { row.getTable(), setClause.toString(), whereClause.toString()};
182 MessageFormat form = new MessageFormat(fStore.getString("phpeclipse.sql.update.template"));
184 String query = form.format(arguments);
186 // String query = "$results = mysql_query(\"UPDATE " + row.getTable();
187 // query += " SET " + setClause.toString();
188 // query += " WHERE " + whereClause.toString() + "\");";
189 this.query.setText(query);
191 public boolean performFinish() {
192 // PHPSourceConsole console = PHPSourceConsole.getInstance();
194 // console.print(query.getText());
195 QuantumPlugin.getDefault().getSysClip().setContents(
196 new Object[] { query.getText() },
197 new Transfer[] { TextTransfer.getInstance()});