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