Added error log to the debug perspective
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / wizards / InsertRowPage.java
1 package com.quantum.wizards;
2
3 import java.util.Arrays;
4
5 import org.eclipse.jface.viewers.CellEditor;
6 import org.eclipse.jface.viewers.ICellModifier;
7 import org.eclipse.jface.viewers.ILabelProviderListener;
8 import org.eclipse.jface.viewers.IStructuredContentProvider;
9 import org.eclipse.jface.viewers.ITableLabelProvider;
10 import org.eclipse.jface.viewers.TableViewer;
11 import org.eclipse.jface.viewers.TextCellEditor;
12 import org.eclipse.jface.viewers.Viewer;
13 import org.eclipse.swt.SWT;
14 import org.eclipse.swt.events.ModifyEvent;
15 import org.eclipse.swt.events.ModifyListener;
16 import org.eclipse.swt.graphics.Image;
17 import org.eclipse.swt.layout.GridData;
18 import org.eclipse.swt.layout.GridLayout;
19 import org.eclipse.swt.widgets.Composite;
20 import org.eclipse.swt.widgets.Label;
21 import org.eclipse.swt.widgets.Table;
22 import org.eclipse.swt.widgets.TableColumn;
23 import org.eclipse.swt.widgets.TableItem;
24 import org.eclipse.swt.widgets.Text;
25
26 import com.quantum.Messages;
27 import com.quantum.adapters.DatabaseAdapter;
28 import com.quantum.model.Bookmark;
29 import com.quantum.model.Entity;
30 import com.quantum.sql.SQLResultSetResults;
31
32 public class InsertRowPage extends BaseSQLPage implements SQLPage {
33     
34     class InsertRowTableValues {
35         private String colNames = null;
36         private String values   = null;
37         
38         public InsertRowTableValues() {            
39         }
40         /**
41          * @return Returns the colNames.
42          */
43         public String getColNames() {
44             return colNames;
45         }
46         /**
47          * @param colNames The colNames to set.
48          */
49         public void setColNames(String colNames) {
50             this.colNames = colNames;
51         }
52         /**
53          * @return Returns the values.
54          */
55         public String getValues() {
56             return values;
57         }
58         /**
59          * @param values The values to set.
60          */
61         public void setValues(String values) {
62             this.values = values;
63         }
64     }
65     
66     class LabelProviderImpl implements ITableLabelProvider {
67                 public Image getColumnImage(Object element, int columnIndex) {
68                         return null;
69                 }
70                 public String getColumnText(Object element, int columnIndex) {
71                         String sReturn = "";                    
72                         InsertRowTableValues insertRow = (InsertRowTableValues)element;
73                         switch (columnIndex) {
74                                 case 0:
75                                     sReturn = insertRow.getColNames();
76                                     break;
77                                 case 1:
78                                     sReturn = insertRow.getValues();
79                                     break;
80                                 default:
81                                     break;
82                         }
83                         return sReturn;
84                 }
85                 public void addListener(ILabelProviderListener listener) {}
86                 public void dispose() {}
87                 public boolean isLabelProperty(Object element, String property) {
88                         return false;
89                 }
90                 public void removeListener(ILabelProviderListener listener) {}
91         }
92         
93     class ContentProviderImpl implements IStructuredContentProvider {
94                 public Object[] getElements(Object inputElement) {
95                         return insertTable;
96                 }
97
98                 public void dispose() {}
99                 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {}            
100         }
101         
102     class CellModifierImpl implements ICellModifier {
103
104         public boolean canModify(Object element, String property) {
105                 return true;
106         }
107         
108         public Object getValue(Object element, String property) {               
109                 System.out.println("getValue called");
110         
111                 // Find the index of the column
112                 int colIndx = getColumnNamesAsList(colNames).indexOf(property);
113                 System.out.println("colIndx : " + colIndx);             
114                  
115                 Object rResult = null;
116                 InsertRowTableValues insertVal = (InsertRowTableValues)element;
117                                 
118                 switch (colIndx) {
119                         case 0:
120                             rResult = insertVal.getColNames();
121                                     break;
122                                 case 1:
123                                     rResult = insertVal.getValues();
124                                     break;
125                         default:
126                                 rResult = "";
127                                 break;
128                 }
129                 
130                 return rResult;
131         }
132         
133         public void modify(Object element, String property, Object value) {
134                 int colIndx = getColumnNamesAsList(colNames).indexOf(property);
135                 
136                 TableItem item = (TableItem) element;
137                 InsertRowTableValues insertVal = (InsertRowTableValues)item.getData();
138                 
139                 switch (colIndx) {
140                         case 0: // field names
141                             break;
142                         case 1: // field values
143                             insertVal.setValues(value.toString());
144                             updateView();                           
145                             updateQuery();
146                             break;
147                         default:
148                             break;
149                 }
150         }
151     }
152     
153         String[] columnNames;
154         String[] colNames;
155         Text[] values;
156         Label query;
157         InsertRowTableValues[] insertTable = null;
158         TableViewer tableViewer = null; 
159         int numColumns = 0;
160         
161         public InsertRowPage(String pageName) {
162                 super(pageName);                
163         }
164
165         public void createControl(Composite parent) {
166                 System.out.println("page create control"); //$NON-NLS-1$
167                 Composite container = new Composite(parent, SWT.NULL);
168                 container.setLayout(new GridLayout());
169                 container.setLayoutData(new GridData(GridData.FILL_BOTH | GridData.VERTICAL_ALIGN_BEGINNING));
170
171                 // init values to be displayed on the table
172                 columnNames = this.results.getColumnNames();
173                 int nLen = columnNames.length;
174                 insertTable = new InsertRowTableValues[nLen];
175                 
176                 for (int nCtr=0; nCtr<nLen; nCtr++) {
177                     insertTable[nCtr] = new InsertRowTableValues();
178                     insertTable[nCtr].setColNames(columnNames[nCtr]);
179                     insertTable[nCtr].setValues("");
180                 }               
181                 
182                 createTable(container);                         
183                 
184                 query = new Label(container, SWT.WRAP);
185                 GridData gridData = new GridData();
186                 gridData.horizontalSpan = 1;            
187                 gridData.horizontalAlignment = GridData.FILL;
188                 gridData.verticalAlignment = GridData.FILL;             
189                 query.setLayoutData(gridData);
190
191                 setControl(container);
192         updateQuery();
193        
194                 setPageComplete(true);
195         }
196         public void updateQuery() {
197                 System.out.println("Updating query"); //$NON-NLS-1$
198                 StringBuffer valuesClause = new StringBuffer();
199                 StringBuffer namesClause = new StringBuffer();
200                 Bookmark bookmark = this.results.getBookmark();
201                 Entity entity = this.results.getEntity();
202                 DatabaseAdapter adapter = bookmark.getAdapter();
203                 
204                 numColumns = 0;
205                 for (int i = 0; i < columnNames.length; i++) {
206                     String name = insertTable[i].getColNames();
207                     String value = insertTable[i].getValues();
208                         if (value != null && value.length() > 0) {
209                                 if (numColumns > 0) {
210                                         valuesClause.append(", "); //$NON-NLS-1$
211                                         namesClause.append(", ");
212                                 }
213                 appendColumn(valuesClause, entity, name, adapter, value);
214                                 namesClause.append(name);       
215                                 numColumns++;
216                         }
217                 }
218                 
219                 String query = "INSERT INTO " + this.results.getEntity().getQuotedTableName(); //$NON-NLS-1$
220                 if (numColumns > 0) {
221                         query += " (" + namesClause + ")";
222                         query += " VALUES " + "(" + valuesClause; //$NON-NLS-1$
223                         query += " )"; //$NON-NLS-1$
224                 }
225                 this.query.setText(query);
226         }
227     /* (non-Javadoc)
228      * @see com.quantum.wizards.BaseSQLPage#getQueryText()
229      */
230     protected String getQueryText() {
231         return this.query.getText();
232     }
233     
234     private void createTable(Composite composite) {
235         System.out.println("Creating table...");
236         int style = SWT.SINGLE | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.HIDE_SELECTION;
237                 Table table = new Table(composite, style);
238         table.setHeaderVisible(true);
239         table.setLinesVisible(true);
240         table.setLayoutData(new GridData(GridData.FILL_BOTH | GridData.VERTICAL_ALIGN_BEGINNING));
241         
242         colNames = new String[] { Messages.getString("InsertRowPage.ColumnName"), Messages.getString("InsertRowPage.Value") };
243
244                 createTableColumn(table, colNames[0], SWT.LEFT, 0, 200);
245                 createTableColumn(table, colNames[1], SWT.LEFT, 1, 500);
246                 this.tableViewer = new TableViewer(table);
247                 this.tableViewer.setColumnProperties(colNames);
248
249                 CellEditor[] editor = new CellEditor[colNames.length];
250                 TextCellEditor txtEditorField = new TextCellEditor(table);
251                 txtEditorField.getControl().setEnabled(false);
252                 editor[0] = txtEditorField;
253                 
254                 TextCellEditor txtEditorValues = new TextCellEditor(table);
255                 editor[1] = txtEditorValues;
256                 
257                 this.tableViewer.setCellEditors(editor);
258                 this.tableViewer.setLabelProvider(new LabelProviderImpl());
259                 this.tableViewer.setContentProvider(new ContentProviderImpl());
260                 this.tableViewer.setCellModifier(new CellModifierImpl());
261                 this.tableViewer.setInput(insertTable);         
262     }        
263         
264     private void updateView() {
265         this.tableViewer.update(insertTable, null);
266     }
267 }