added button to start tests.
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / wizards / InsertRowPage.java
1 package com.quantum.wizards;
2
3 import com.quantum.Messages;
4 import com.quantum.adapters.AdapterFactory;
5 import com.quantum.adapters.DatabaseAdapter;
6 import com.quantum.model.Bookmark;
7 import com.quantum.model.Entity;
8
9 import org.eclipse.swt.SWT;
10 import org.eclipse.swt.events.ModifyEvent;
11 import org.eclipse.swt.events.ModifyListener;
12 import org.eclipse.swt.layout.GridData;
13 import org.eclipse.swt.layout.GridLayout;
14 import org.eclipse.swt.widgets.Composite;
15 import org.eclipse.swt.widgets.Label;
16 import org.eclipse.swt.widgets.Text;
17
18 public class InsertRowPage extends BaseSQLPage implements SQLPage {
19         String[] columnNames;
20         Text[] values;
21         Label query;
22         int numColumns = 0;
23         
24         public InsertRowPage(String pageName) {
25                 super(pageName);
26         }
27
28         public void createControl(Composite parent) {
29                 System.out.println("page create control"); //$NON-NLS-1$
30                 Composite container = new Composite(parent, SWT.NULL);
31                 GridLayout layout = new GridLayout();
32                 container.setLayout(layout);
33                 layout.numColumns = 2;
34
35                 if (row == null) {
36                         System.out.println("Row is null"); //$NON-NLS-1$
37                 }
38                 if (row.getColumnNames() == null) {
39                         System.out.println("Columns are null"); //$NON-NLS-1$
40                 }
41                 if (row.getTableData() == null) {
42                         System.out.println("Data is null"); //$NON-NLS-1$
43                 }
44                 columnNames = row.getColumnNames();
45                 String[] data = row.getTableData();
46                 for (int i = 0; i < row.getColumnCount(); i++) {
47                         System.out.println("data = " + i + "=" + data[i]); //$NON-NLS-1$ //$NON-NLS-2$
48                         System.out.println("column = " + i + "=" + columnNames[i]); //$NON-NLS-1$ //$NON-NLS-2$
49                 }
50                 values = new Text[row.getColumnCount()];
51                 new Label(container, SWT.NULL).setText(Messages.getString("InsertRowPage.ColumnName")); //$NON-NLS-1$
52                 new Label(container, SWT.NULL).setText(Messages.getString("InsertRowPage.Value")); //$NON-NLS-1$
53                 for (int i = 0; i < row.getColumnCount(); i++) {
54                         Label label = new Label(container, SWT.NULL);
55                         label.setText(columnNames[i]);
56                         values[i] = new Text(container, SWT.BORDER | SWT.SINGLE);
57                         GridData fullHorizontal = new GridData();
58                         fullHorizontal.horizontalAlignment = GridData.FILL;
59                         values[i].setLayoutData(fullHorizontal);
60
61                         //values[i].setText(data[i]);
62                         values[i].addModifyListener(new ModifyListener() {
63                                 public void modifyText(ModifyEvent e) {
64                                         updateQuery();
65                                 }                               
66                         });
67                 }
68                 query = new Label(container, SWT.WRAP);
69                 GridData gridData = new GridData();
70                 gridData.horizontalSpan = layout.numColumns;
71                 gridData.horizontalAlignment = GridData.FILL;
72                 gridData.verticalAlignment = GridData.FILL;
73                 gridData.grabExcessHorizontalSpace = true;
74                 gridData.grabExcessVerticalSpace = true;
75                 query.setLayoutData(gridData);
76
77                 setControl(container);
78         updateQuery();
79        
80                 setPageComplete(true);
81         }
82         public void updateQuery() {
83                 System.out.println("Updating query"); //$NON-NLS-1$
84                 StringBuffer valuesClause = new StringBuffer();
85                 StringBuffer namesClause = new StringBuffer();
86                 Bookmark bookmark = row.getBookmark();
87                 Entity entity = row.getEntity();
88                 DatabaseAdapter adapter = AdapterFactory.getInstance().getAdapter(bookmark.getType());
89                 
90                 numColumns = 0;
91                 for (int i = 0; i < columnNames.length; i++) {
92                         String name = columnNames[i];
93                         String value = values[i].getText();
94                         if (value.length() > 0) {
95                                 if (numColumns > 0) {
96                                         valuesClause.append(", "); //$NON-NLS-1$
97                                         namesClause.append(", ");
98                                 }
99                 appendColumn(valuesClause, entity, name, adapter, value);
100                                 namesClause.append(name);       
101                                 numColumns++;
102                         }
103                 }
104                 
105                 String query = "INSERT INTO " + row.getTable(); //$NON-NLS-1$
106                 if (numColumns > 0) {
107                         query += " (" + namesClause + ")";
108                         query += " VALUES " + "(" + valuesClause; //$NON-NLS-1$
109                         query += " )"; //$NON-NLS-1$
110                 }
111                 this.query.setText(query);
112         }
113     /* (non-Javadoc)
114      * @see com.quantum.wizards.BaseSQLPage#getQueryText()
115      */
116     protected String getQueryText() {
117         return this.query.getText();
118     }
119
120 }