SQL Plugin copied from Quantum plugin and refactored for PHPEclipse
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.sql / src / net / sourceforge / phpdt / sql / wizards / InsertRowPage.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.layout.GridData;
8 import org.eclipse.swt.layout.GridLayout;
9 import org.eclipse.swt.widgets.Composite;
10 import org.eclipse.swt.widgets.Label;
11 import org.eclipse.swt.widgets.Text;
12
13 import net.sourceforge.phpdt.sql.sql.MultiSQLServer;
14 import net.sourceforge.phpdt.sql.sql.TableRow;
15 import net.sourceforge.phpdt.sql.view.tableview.TableAdapter;
16
17 public class InsertRowPage extends WizardPage implements SQLPage {
18         TableRow row;
19         String[] columnNames;
20         Text[] values;
21         Label query;
22         public InsertRowPage(String pageName) {
23                 super(pageName);
24         }
25
26         public void init(TableRow row, TableAdapter adapter) {
27                 this.row = row;
28         }
29
30         public void createControl(Composite parent) {
31                 System.out.println("page create control");
32                 Composite container = new Composite(parent, SWT.NULL);
33                 GridLayout layout = new GridLayout();
34                 container.setLayout(layout);
35                 int layoutColumns = 2;
36                 layout.numColumns = layoutColumns;
37
38                 if (row == null) {
39                         System.out.println("Row is null");
40                 }
41                 if (row.getColumnNames() == null) {
42                         System.out.println("Columns are null");
43                 }
44                 if (row.getTableData() == null) {
45                         System.out.println("Data is null");
46                 }
47                 columnNames = row.getColumnNames();
48                 String[] data = row.getTableData();
49                 for (int i = 0; i < row.getColumnCount(); i++) {
50                         System.out.println("data = " + i + "=" + data[i]);
51                         System.out.println("column = " + i + "=" + columnNames[i]);
52                 }
53                 values = new Text[row.getColumnCount()];
54                 Label temp = new Label(container, SWT.NULL);
55                 temp.setText("Column Name");
56                 temp = new Label(container, SWT.NULL);
57                 temp.setText("Value");
58                 for (int i = 0; i < row.getColumnCount(); i++) {
59                         Label label = new Label(container, SWT.NULL);
60                         label.setText(columnNames[i]);
61                         values[i] = new Text(container, SWT.BORDER | SWT.SINGLE);
62                         GridData fullHorizontal = new GridData();
63                         fullHorizontal.horizontalAlignment = GridData.FILL;
64                         values[i].setLayoutData(fullHorizontal);
65
66                         //values[i].setText(data[i]);
67                         values[i].addModifyListener(new ModifyListener() {
68                                 public void modifyText(ModifyEvent e) {
69                                         updateQuery();
70                                 }                               
71                         });
72                 }
73                 query = new Label(container, SWT.WRAP);
74                 GridData gridData = new GridData();
75                 gridData.horizontalSpan = layoutColumns;
76                 gridData.horizontalAlignment = GridData.FILL;
77                 gridData.verticalAlignment = GridData.FILL;
78                 gridData.grabExcessHorizontalSpace = true;
79                 gridData.grabExcessVerticalSpace = true;
80                 query.setLayoutData(gridData);
81
82                 setControl(container);
83         updateQuery();
84        
85                 setPageComplete(true);
86         }
87         public void updateQuery() {
88                 System.out.println("Updating query");
89                 StringBuffer valuesClause = new StringBuffer();
90                 for (int i = 0; i < columnNames.length; i++) {
91                         valuesClause.append(values[i].getText());
92                         valuesClause.append(", ");
93                 }
94                 if (valuesClause.length() > 1) {
95                         valuesClause.deleteCharAt(valuesClause.length() - 1);
96                         valuesClause.deleteCharAt(valuesClause.length() - 1);
97                 }
98                 String query = "INSERT INTO " + row.getTable();
99                 query += " VALUES (" + valuesClause.toString();
100                 query += " )";
101                 this.query.setText(query);
102         }
103         public boolean performFinish() {
104            MultiSQLServer server = MultiSQLServer.getInstance();
105            server.execute(query.getText());
106            return true;
107         }
108 }