added file extension php5
[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.NULL);
30                 GridLayout layout = new GridLayout();
31                 container.setLayout(layout);
32                 layout.numColumns = 2;
33
34                 if (row == null) {
35                         System.out.println("Row is null"); //$NON-NLS-1$
36                 }
37                 if (row.getColumnNames() == null) {
38                         System.out.println("Columns are null"); //$NON-NLS-1$
39                 }
40                 if (row.getTableData() == null) {
41                         System.out.println("Data is null"); //$NON-NLS-1$
42                 }
43                 columnNames = row.getColumnNames();
44                 String[] data = row.getTableData();
45                 for (int i = 0; i < row.getColumnCount(); i++) {
46                         System.out.println("data = " + i + "=" + data[i]); //$NON-NLS-1$ //$NON-NLS-2$
47                         System.out.println("column = " + i + "=" + columnNames[i]); //$NON-NLS-1$ //$NON-NLS-2$
48                 }
49                 values = new Text[row.getColumnCount()];
50                 new Label(container, SWT.NULL).setText(Messages.getString("InsertRowPage.ColumnName")); //$NON-NLS-1$
51                 new Label(container, SWT.NULL).setText(Messages.getString("InsertRowPage.Value")); //$NON-NLS-1$
52                 for (int i = 0; i < row.getColumnCount(); i++) {
53                         Label label = new Label(container, SWT.NULL);
54                         label.setText(columnNames[i]);
55                         values[i] = new Text(container, SWT.BORDER | SWT.SINGLE);
56                         GridData fullHorizontal = new GridData();
57                         fullHorizontal.horizontalAlignment = GridData.FILL;
58                         values[i].setLayoutData(fullHorizontal);
59
60                         //values[i].setText(data[i]);
61                         values[i].addModifyListener(new ModifyListener() {
62                                 public void modifyText(ModifyEvent e) {
63                                         updateQuery();
64                                 }                               
65                         });
66                 }
67                 query = new Label(container, SWT.WRAP);
68                 GridData gridData = new GridData();
69                 gridData.horizontalSpan = layout.numColumns;
70                 gridData.horizontalAlignment = GridData.FILL;
71                 gridData.verticalAlignment = GridData.FILL;
72                 gridData.grabExcessHorizontalSpace = true;
73                 gridData.grabExcessVerticalSpace = true;
74                 query.setLayoutData(gridData);
75
76                 setControl(container);
77         updateQuery();
78        
79                 setPageComplete(true);
80         }
81         public void updateQuery() {
82                 System.out.println("Updating query"); //$NON-NLS-1$
83                 StringBuffer valuesClause = new StringBuffer();
84                 StringBuffer namesClause = new StringBuffer();
85                 Bookmark bookmark = row.getBookmark();
86                 Entity entity = row.getEntity();
87                 DatabaseAdapter adapter = bookmark.getAdapter();
88                 
89                 numColumns = 0;
90                 for (int i = 0; i < columnNames.length; i++) {
91                         String name = columnNames[i];
92                         String value = values[i].getText();
93                         if (value.length() > 0) {
94                                 if (numColumns > 0) {
95                                         valuesClause.append(", "); //$NON-NLS-1$
96                                         namesClause.append(", ");
97                                 }
98                 appendColumn(valuesClause, entity, name, adapter, value);
99                                 namesClause.append(name);       
100                                 numColumns++;
101                         }
102                 }
103                 
104                 String query = "INSERT INTO " + row.getTable(); //$NON-NLS-1$
105                 if (numColumns > 0) {
106                         query += " (" + namesClause + ")";
107                         query += " VALUES " + "(" + valuesClause; //$NON-NLS-1$
108                         query += " )"; //$NON-NLS-1$
109                 }
110                 this.query.setText(query);
111         }
112     /* (non-Javadoc)
113      * @see com.quantum.wizards.BaseSQLPage#getQueryText()
114      */
115     protected String getQueryText() {
116         return this.query.getText();
117     }
118
119 }