updating SQL plugin with latest Quantum code
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.sql / src / net / sourceforge / phpdt / sql / wizards / PHPInsertRowPage.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.layout.GridData;
11 import org.eclipse.swt.layout.GridLayout;
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.PHPEclipseSQLPlugin;
17 import net.sourceforge.phpdt.sql.sql.TableRow;
18 import net.sourceforge.phpdt.sql.view.PHPSourceConsole;
19 import net.sourceforge.phpdt.sql.view.tableview.TableAdapter;
20
21 public class PHPInsertRowPage extends WizardPage implements SQLPage {
22         TableRow row;
23         String[] columnNames;
24         Text[] values;
25         Label query;
26         private IPreferenceStore fStore;
27
28         public PHPInsertRowPage(String pageName) {
29                 super(pageName);
30         }
31
32         public void init(TableRow row, TableAdapter adapter) {
33                 this.row = row;
34         }
35
36         public void createControl(Composite parent) {
37                 System.out.println("page create control");
38                 fStore = PHPEclipseSQLPlugin.getDefault().getPreferenceStore();
39                 Composite container = new Composite(parent, SWT.NULL);
40                 GridLayout layout = new GridLayout();
41                 container.setLayout(layout);
42                 int layoutColumns = 2;
43                 layout.numColumns = layoutColumns;
44
45                 if (row == null) {
46                         System.out.println("Row is null");
47                 }
48                 if (row.getColumnNames() == null) {
49                         System.out.println("Columns are null");
50                 }
51                 if (row.getTableData() == null) {
52                         System.out.println("Data is null");
53                 }
54                 columnNames = row.getColumnNames();
55                 String[] data = row.getTableData();
56                 for (int i = 0; i < row.getColumnCount(); i++) {
57                         System.out.println("data = " + i + "=" + data[i]);
58                         System.out.println("column = " + i + "=" + columnNames[i]);
59                 }
60                 values = new Text[row.getColumnCount()];
61                 Label temp = new Label(container, SWT.NULL);
62                 temp.setText("Column Name");
63                 temp = new Label(container, SWT.NULL);
64                 temp.setText("Value");
65                 for (int i = 0; i < row.getColumnCount(); i++) {
66                         Label label = new Label(container, SWT.NULL);
67                         label.setText(columnNames[i]);
68                         values[i] = new Text(container, SWT.BORDER | SWT.SINGLE);
69                         GridData fullHorizontal = new GridData();
70                         fullHorizontal.horizontalAlignment = GridData.FILL;
71                         values[i].setLayoutData(fullHorizontal);
72
73                         //values[i].setText(data[i]);
74                         values[i].addModifyListener(new ModifyListener() {
75                                 public void modifyText(ModifyEvent e) {
76                                         updateQuery();
77                                 }
78                         });
79                 }
80                 query = new Label(container, SWT.WRAP);
81                 GridData gridData = new GridData();
82                 gridData.horizontalSpan = layoutColumns;
83                 gridData.horizontalAlignment = GridData.FILL;
84                 gridData.verticalAlignment = GridData.FILL;
85                 gridData.grabExcessHorizontalSpace = true;
86                 gridData.grabExcessVerticalSpace = true;
87                 query.setLayoutData(gridData);
88
89                 setControl(container);
90                 updateQuery();
91
92                 setPageComplete(true);
93         }
94         public void updateQuery() {
95                 System.out.println("Updating insert query");
96                 StringBuffer fieldClause = new StringBuffer();
97
98                 StringBuffer valuesClause = new StringBuffer();
99                 String text;
100                 boolean first = false;
101                 for (int i = 0; i < columnNames.length; i++) {
102                         text = values[i].getText();
103                         if (!text.equals("")) {
104                                 if (first) {
105                                         valuesClause.append(", ");
106                                         fieldClause.append(", ");
107                                 }
108                                 valuesClause.append("'" + values[i].getText() + "'");
109                                 fieldClause.append(columnNames[i]);
110                                 first = true;
111                         }
112                 }
113                 //    if (valuesClause.length() > 1) {
114                 //      valuesClause.deleteCharAt(valuesClause.length() - 1);
115                 //      valuesClause.deleteCharAt(valuesClause.length() - 1);
116                 //    }
117                 String[] arguments =
118                         { row.getTable(), fieldClause.toString(), valuesClause.toString()};
119                 MessageFormat form =
120                         new MessageFormat(
121                                 fStore.getString("phpeclipse.sql.insert.template"));
122
123                 String query = form.format(arguments);
124
125                 //    String query = "$results = mysql_query(\"INSERT INTO " + row.getTable() + " (";
126                 //    query += fieldClause.toString() + ") ";
127                 //    query += " VALUES (" + valuesClause.toString();
128                 //    query += ")\");";
129                 this.query.setText(query);
130         }
131         public boolean performFinish() {
132                 PHPSourceConsole console = PHPSourceConsole.getInstance();
133                 console.clear();
134                 console.print(query.getText());
135                 return true;
136         }
137 }