031ed9ffe2bf8b467b98b4c320e0a76383723449
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / wizards / UpdateRowPage.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.CheckboxCellEditor;
7 import org.eclipse.jface.viewers.ICellModifier;
8 import org.eclipse.jface.viewers.ILabelProviderListener;
9 import org.eclipse.jface.viewers.IStructuredContentProvider;
10 import org.eclipse.jface.viewers.ITableLabelProvider;
11 import org.eclipse.jface.viewers.TableViewer;
12 import org.eclipse.jface.viewers.TextCellEditor;
13 import org.eclipse.jface.viewers.Viewer;
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.events.ModifyEvent;
16 import org.eclipse.swt.events.ModifyListener;
17 import org.eclipse.swt.events.SelectionEvent;
18 import org.eclipse.swt.events.SelectionListener;
19 import org.eclipse.swt.graphics.Image;
20 import org.eclipse.swt.layout.GridData;
21 import org.eclipse.swt.layout.GridLayout;
22 import org.eclipse.swt.widgets.Button;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.swt.widgets.Label;
25 import org.eclipse.swt.widgets.Table;
26 import org.eclipse.swt.widgets.TableColumn;
27 import org.eclipse.swt.widgets.TableItem;
28 import org.eclipse.swt.widgets.Text;
29
30 import com.quantum.ImageStore;
31 import com.quantum.Messages;
32 import com.quantum.adapters.DatabaseAdapter;
33 import com.quantum.model.Bookmark;
34 import com.quantum.model.Column;
35 import com.quantum.model.Entity;
36 import com.quantum.wizards.InsertRowPage.CellModifierImpl;
37 import com.quantum.wizards.InsertRowPage.ContentProviderImpl;
38 import com.quantum.wizards.InsertRowPage.InsertRowTableValues;
39 import com.quantum.wizards.InsertRowPage.LabelProviderImpl;
40
41 public class UpdateRowPage extends BaseSQLPage implements SQLPage {
42     
43     class UpdateRowTableValues {
44         private String sColNames = null;
45         private String sOldValue = null;
46         private boolean bPrimary = false;
47         private String sNewValue = null;
48         private boolean bSetValue = false;
49         
50         public UpdateRowTableValues() {
51         }
52         /**
53          * @return Returns the bSetValue.
54          */
55         public boolean isBSetValue() {
56             return bSetValue;
57         }
58         /**
59          * @param setValue The bSetValue to set.
60          */
61         public void setBSetValue(boolean setValue) {
62             bSetValue = setValue;
63         }
64         /**
65          * @return Returns the bPrimary.
66          */
67         public boolean isBPrimary() {
68             return bPrimary;
69         }
70         /**
71          * @param where The bPrimary to set.
72          */
73         public void setBPrimary(boolean where) {
74             bPrimary = where;
75         }
76         /**
77          * @return Returns the sColNames.
78          */
79         public String getSColNames() {
80             return sColNames;
81         }
82         /**
83          * @param colNames The sColNames to set.
84          */
85         public void setSColNames(String colNames) {
86             sColNames = colNames;
87         }
88         /**
89          * @return Returns the sNewValue.
90          */
91         public String getSNewValue() {
92             return sNewValue;
93         }
94         /**
95          * @param newValue The sNewValue to set.
96          */
97         public void setSNewValue(String newValue) {
98             sNewValue = newValue;
99         }
100         /**
101          * @return Returns the sOldValue.
102          */
103         public String getSOldValue() {
104             return sOldValue;
105         }
106         /**
107          * @param oldValue The sOldValue to set.
108          */
109         public void setSOldValue(String oldValue) {
110             sOldValue = oldValue;
111         }
112     }
113     
114     class LabelProviderImpl implements ITableLabelProvider {                
115                 public Image getColumnImage(Object element, int columnIndex) {
116                     if (columnIndex == 2) {
117                         return ((UpdateRowTableValues)element).isBPrimary() ? imgCheck : imgUncheck;
118                     } else if (columnIndex == 4) {
119                         return ((UpdateRowTableValues)element).isBSetValue() ? imgCheck : imgUncheck;
120                     } else {
121                         return null;
122                     }
123                 }
124                 public String getColumnText(Object element, int columnIndex) {
125                         String sReturn = "";                    
126                         UpdateRowTableValues updateRow = (UpdateRowTableValues)element;
127                         switch (columnIndex) {
128                                 case 0: // column names
129                                     sReturn = updateRow.getSColNames();
130                                     break;
131                                 case 1: // old values
132                                     sReturn = updateRow.getSOldValue();
133                                     break;
134                                 case 2: // set checkbox
135                                     break;
136                                 case 3: // new value
137                                     sReturn = updateRow.getSNewValue();
138                                     break;
139                                 case 4: // set value checkbox
140                                     break;                                  
141                                 default:
142                                     break;
143                         }
144                         return sReturn;
145                 }
146                 public void addListener(ILabelProviderListener listener) {}
147                 public void dispose() {}
148                 public boolean isLabelProperty(Object element, String property) {
149                         return false;
150                 }
151                 public void removeListener(ILabelProviderListener listener) {}
152         }
153         
154     class ContentProviderImpl implements IStructuredContentProvider {
155                 public Object[] getElements(Object inputElement) {
156                         return updateTable;
157                 }
158
159                 public void dispose() {}
160                 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {}            
161         }
162         
163     class CellModifierImpl implements ICellModifier {
164
165         public boolean canModify(Object element, String property) {
166                 return true;
167         }
168         
169         public Object getValue(Object element, String property) {               
170                 System.out.println("getValue called");
171         
172                 // Find the index of the column
173                 int colIndx = getColumnNamesAsList(colNames).indexOf(property);
174                 System.out.println("colIndx : " + colIndx);             
175                  
176                 Object rResult = null;
177                 UpdateRowTableValues updateVal = (UpdateRowTableValues)element;
178                                 
179                 switch (colIndx) {
180                         case 0: // column names
181                             rResult = updateVal.getSColNames();
182                                     break;
183                                 case 1: // old values
184                                     rResult = updateVal.getSOldValue();
185                                     break;
186                                 case 2: // set checkbox
187                                     rResult = new Boolean(updateVal.isBPrimary());
188                                     break;
189                                 case 3: // new value
190                                     rResult = updateVal.getSNewValue();
191                                     break;
192                                 case 4: // set value checkbox
193                                     rResult = new Boolean(updateVal.isBSetValue());
194                                     break;                                  
195                                 default:
196                                     break;
197                 }
198                 
199                 return rResult;
200         }
201         
202         public void modify(Object element, String property, Object value) {
203                 int colIndx = getColumnNamesAsList(colNames).indexOf(property);
204                 
205                 TableItem item = (TableItem) element;
206                 UpdateRowTableValues updateVal = (UpdateRowTableValues)item.getData();
207                 
208                 switch (colIndx) {
209                         case 0: // column names                     
210                                     break;
211                                 case 1: // old values                                               
212                                     break;
213                                 case 2: // set checkbox
214                                     updateVal.setBPrimary(((Boolean)value).booleanValue());
215                                     break;
216                                 case 3: // new value
217                                     updateVal.setSNewValue(value.toString());                               
218                                     break;
219                                 case 4: // set value checkbox
220                                     updateVal.setBSetValue(((Boolean)value).booleanValue());
221                                     break;                                  
222                                 default:
223                                     break;
224                 }
225                 
226                 updateView();                               
227                     updateQuery();
228         }
229     }
230     
231         String[] columnNames;
232         String[] colNames;
233         Text[] oldValues;
234         Text[] newValues;
235         Button[] primaryKeys;
236         Button[] setValues;
237         Label query;
238         UpdateRowTableValues[] updateTable = null;
239         TableViewer tableViewer = null;
240         static Image imgCheck = null;
241         static Image imgUncheck = null;
242         
243         static {
244             imgCheck = ImageStore.getImage(ImageStore.CHECKED);
245             imgUncheck = ImageStore.getImage(ImageStore.UNCHECKED);
246         }
247         
248         public UpdateRowPage(String pageName) {
249                 super(pageName);
250         }
251
252         public void createControl(Composite parent) {
253                 System.out.println("page create control"); //$NON-NLS-1$
254                 Composite container = new Composite(parent, SWT.NULL);
255                 container.setLayout(new GridLayout());
256                 container.setLayoutData(new GridData(GridData.FILL_BOTH | GridData.VERTICAL_ALIGN_BEGINNING));
257                 
258                 Entity entity = this.results.getEntity();
259                 
260                 // init values to be displayed on the table
261                 columnNames = this.results.getColumnNames();
262                 int nLen = columnNames.length;
263                 updateTable = new UpdateRowTableValues[nLen];
264                 
265                 for(int nCtr=0; nCtr<nLen; nCtr++) {
266                     updateTable[nCtr] = new UpdateRowTableValues();
267                     updateTable[nCtr].setSColNames(columnNames[nCtr]);
268                     Object data = this.row == null ? null : this.row.get(nCtr+1);
269                     updateTable[nCtr].setSOldValue(data == null ? "" : data.toString());
270                     Column column = (entity == null) ? null : getColumn(entity, columnNames[nCtr]);
271                         if (column != null && column.isPrimaryKey()) {
272                             updateTable[nCtr].setBPrimary(true);
273                         }
274                     updateTable[nCtr].setSNewValue(data == null ? "" : data.toString());
275                 }
276                 
277                 createTable(container);
278                 
279                 query = new Label(container, SWT.WRAP);
280                 GridData gridData = new GridData();
281                 gridData.horizontalSpan = 1;            
282                 gridData.horizontalAlignment = GridData.FILL;
283                 gridData.verticalAlignment = GridData.FILL;             
284                 query.setLayoutData(gridData);
285
286                 setControl(container);
287         updateQuery();
288        
289                 setPageComplete(true);
290         }
291         public void updateQuery() {
292                 System.out.println("Updating query"); //$NON-NLS-1$
293                 StringBuffer setClause = new StringBuffer();
294                 StringBuffer whereClause = new StringBuffer();
295                 Bookmark bookmark = this.results.getBookmark();
296                 Entity entity = this.results.getEntity();
297                 DatabaseAdapter adapter = bookmark.getAdapter();
298                 
299                 int numValuesSet = 0;
300                 int numValuesWhere = 0;         
301                 for (int nCtr = 0; nCtr < columnNames.length; nCtr++) {
302                         if (updateTable[nCtr].isBPrimary()) {
303                                 String value = updateTable[nCtr].getSOldValue();
304                                 if (numValuesWhere > 0) whereClause.append(" AND "); //$NON-NLS-1$
305                                 whereClause.append("("); //$NON-NLS-1$
306                                 whereClause.append(updateTable[nCtr].getSColNames());
307                                 whereClause.append(" = "); //$NON-NLS-1$
308                 appendColumn(whereClause, entity, updateTable[nCtr].getSColNames(), adapter, value);
309                                 whereClause.append(")"); //$NON-NLS-1$
310                                 numValuesWhere++;
311                         } 
312                         if (updateTable[nCtr].isBSetValue()) {
313                                 String value = updateTable[nCtr].getSNewValue();
314                                 if (numValuesSet > 0) setClause.append(", "); //$NON-NLS-1$
315                                 setClause.append(updateTable[nCtr].getSColNames());
316                                 setClause.append(" = "); //$NON-NLS-1$
317                 appendColumn(setClause, entity, updateTable[nCtr].getSColNames(), adapter, value);
318                                 numValuesSet++;
319                         }
320                 }
321                 
322                 String query = "UPDATE " + this.results.getEntity().getQuotedTableName(); //$NON-NLS-1$
323                 query += " SET " + setClause.toString(); //$NON-NLS-1$
324                 query += " WHERE " + whereClause.toString(); //$NON-NLS-1$
325                 this.query.setText(query);
326         }
327
328     /* (non-Javadoc)
329      * @see com.quantum.wizards.BaseSQLPage#getQueryText()
330      */
331     protected String getQueryText() {
332         return query.getText();
333     }
334         
335     private void updateView() {
336         this.tableViewer.update(updateTable, null);
337     }
338
339     private void createTable(Composite composite) {
340         System.out.println("Creating table...");
341         int style = SWT.SINGLE | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.HIDE_SELECTION;
342                 Table table = new Table(composite, style);
343         table.setHeaderVisible(true);
344         table.setLinesVisible(true);
345         table.setLayoutData(new GridData(GridData.FILL_BOTH | GridData.VERTICAL_ALIGN_BEGINNING));
346         
347         colNames = new String[] { Messages.getString("UpdateRowPage.ColumnName"), Messages.getString("UpdateRowPage.OldValue"),
348                         "Where", Messages.getString("UpdateRowPage.NewValue"), Messages.getString("UpdateRowPage.SetValue")     };
349
350                 createTableColumn(table, colNames[0], SWT.LEFT, 0, 150);
351                 createTableColumn(table, colNames[1], SWT.LEFT, 1, 300);
352                 createTableColumn(table, colNames[2], SWT.CENTER, 2, 60);
353                 createTableColumn(table, colNames[3], SWT.LEFT, 3, 300);
354                 createTableColumn(table, colNames[4], SWT.CENTER, 4, 70);
355                 this.tableViewer = new TableViewer(table);
356                 this.tableViewer.setColumnProperties(colNames);
357
358                 CellEditor[] editor = new CellEditor[colNames.length];
359                 TextCellEditor txtEditorField = new TextCellEditor(table);
360                 txtEditorField.getControl().setEnabled(false);
361                 editor[0] = txtEditorField;
362                 
363                 TextCellEditor txtEditorFieldOld = new TextCellEditor(table);
364                 txtEditorFieldOld.getControl().setEnabled(false);
365                 editor[1] = txtEditorFieldOld;
366                 
367                 editor[2] = new CheckboxCellEditor(table, SWT.NULL);
368                 
369                 TextCellEditor txtEditorValues = new TextCellEditor(table);
370                 editor[3] = txtEditorValues;
371                 
372                 editor[4] = new CheckboxCellEditor(table, SWT.NULL);
373                 
374                 this.tableViewer.setCellEditors(editor);
375                 this.tableViewer.setLabelProvider(new LabelProviderImpl());
376                 this.tableViewer.setContentProvider(new ContentProviderImpl());
377                 this.tableViewer.setCellModifier(new CellModifierImpl());
378                 this.tableViewer.setInput(updateTable);         
379     }
380     
381     
382 }