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