implemented 1416503, automatic closing of braces
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / wizards / DeleteRowPage.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 DeleteRowPage extends BaseSQLPage implements SQLPage {
33     
34     class DeleteRowPageValues {
35         private String sColNames = null;
36         private String sValues = null;
37         private boolean bPrimary = false;
38         
39         public DeleteRowPageValues() {            
40         }
41         /**
42          * @return Returns the bPrimary.
43          */
44         public boolean isBPrimary() {
45             return bPrimary;
46         }
47         /**
48          * @param primary The bPrimary to set.
49          */
50         public void setBPrimary(boolean primary) {
51             bPrimary = primary;
52         }
53         /**
54          * @return Returns the sColNames.
55          */
56         public String getSColNames() {
57             return sColNames;
58         }
59         /**
60          * @param colNames The sColNames to set.
61          */
62         public void setSColNames(String colNames) {
63             sColNames = colNames;
64         }
65         /**
66          * @return Returns the sValues.
67          */
68         public String getSValues() {
69             return sValues;
70         }
71         /**
72          * @param values The sValues to set.
73          */
74         public void setSValues(String values) {
75             sValues = values;
76         }
77     }
78     
79     class LabelProviderImpl implements ITableLabelProvider {                
80                 public Image getColumnImage(Object element, int columnIndex) {
81                     if (columnIndex == 2) {
82                         return ((DeleteRowPageValues)element).isBPrimary() ? imgCheck : imgUncheck;
83                     } else {
84                         return null;
85                     }
86                 }
87                 public String getColumnText(Object element, int columnIndex) {
88                         String sReturn = "";                    
89                         DeleteRowPageValues deleteRow = (DeleteRowPageValues)element;
90                         switch (columnIndex) {
91                                 case 0: // column names
92                                     sReturn = deleteRow.getSColNames();
93                                     break;
94                                 case 1: // values
95                                     sReturn = deleteRow.getSValues();
96                                     break;
97                                 case 2: // set checkbox
98                                     break;
99                                 default:
100                                     break;
101                         }
102                         return sReturn;
103                 }
104                 public void addListener(ILabelProviderListener listener) {}
105                 public void dispose() {}
106                 public boolean isLabelProperty(Object element, String property) {
107                         return false;
108                 }
109                 public void removeListener(ILabelProviderListener listener) {}
110         }
111         
112     class ContentProviderImpl implements IStructuredContentProvider {
113                 public Object[] getElements(Object inputElement) {
114                         return deleteTable;
115                 }
116
117                 public void dispose() {}
118                 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {}            
119         }
120         
121     class CellModifierImpl implements ICellModifier {
122
123         public boolean canModify(Object element, String property) {
124                 return true;
125         }
126         
127         public Object getValue(Object element, String property) {               
128                 System.out.println("getValue called");
129         
130                 // Find the index of the column
131                 int colIndx = comUI.getColumnNamesAsList(colNames).indexOf(property);
132                 System.out.println("colIndx : " + colIndx);             
133                  
134                 Object rResult = null;
135                 DeleteRowPageValues deleteVal = (DeleteRowPageValues)element;
136                                 
137                 switch (colIndx) {
138                         case 0: // column names
139                             rResult = deleteVal.getSColNames();
140                                     break;
141                                 case 1: // values
142                                     rResult = deleteVal.getSValues();
143                                     break;
144                                 case 2: // checkbox
145                                     rResult = new Boolean(deleteVal.isBPrimary());
146                                     break;
147                                 default:
148                                     break;
149                 }
150                 
151                 return rResult;
152         }
153         
154         public void modify(Object element, String property, Object value) {
155                 int colIndx = comUI.getColumnNamesAsList(colNames).indexOf(property);
156                 
157                 TableItem item = (TableItem) element;
158                 DeleteRowPageValues deleteVal = (DeleteRowPageValues)item.getData();
159                 
160                 switch (colIndx) {
161                         case 0: // column names                     
162                                     break;
163                                 case 1: // values               
164                                     deleteVal.setSValues(value.toString());
165                                     break;
166                                 case 2: // checkbox
167                                     deleteVal.setBPrimary(((Boolean)value).booleanValue());
168                                     break;
169                                 default:
170                                     break;
171                 }
172                 
173                 updateView();                               
174                     updateQuery();
175         }
176     }
177     
178         String[] columnNames;
179         String[] colNames;
180         Label query;
181         DeleteRowPageValues[] deleteTable = null;
182         CommonWizardUI comUI;
183         TableViewer tableViewer = null;
184         static Image imgCheck = null;
185         static Image imgUncheck = null;
186         
187         static {
188             imgCheck = ImageStore.getImage(ImageStore.CHECKED);
189             imgUncheck = ImageStore.getImage(ImageStore.UNCHECKED);
190         }
191
192         public DeleteRowPage(String pageName) {
193                 super(pageName);
194         }
195         
196         public void createControl(Composite parent) {
197                 System.out.println("page create control"); //$NON-NLS-1$
198                 Composite container = new Composite(parent, SWT.NULL);
199                 container.setLayout(new GridLayout());
200                 container.setLayoutData(new GridData(GridData.FILL_BOTH | GridData.VERTICAL_ALIGN_BEGINNING));
201
202                 Entity entity = this.results.getEntity();
203                 
204                 comUI = new CommonWizardUI();
205                 
206                 // init values to be displayed on the table
207                 columnNames = this.results.getColumnNames();
208                 int nLen = columnNames.length;
209                 deleteTable = new DeleteRowPageValues[nLen];
210                 
211                 for(int nCtr=0; nCtr<nLen; nCtr++) {
212                     deleteTable[nCtr] = new DeleteRowPageValues();
213                     deleteTable[nCtr].setSColNames(columnNames[nCtr]);
214                     Object data = this.row == null ? null : this.row.get(nCtr+1);
215                     deleteTable[nCtr].setSValues(data == null ? "" : data.toString());
216                     Column column = (entity == null) ? null : getColumn(entity, columnNames[nCtr]);
217                         if (column != null && column.isPrimaryKey()) {
218                             deleteTable[nCtr].setBPrimary(true);
219                         }                   
220                 }
221                 
222                 createTable(container);
223                 
224                 query = new Label(container, SWT.WRAP);
225                 query.setLayoutData(comUI.createGridData(1, GridData.FILL));
226
227                 setControl(container);
228         updateQuery();
229        
230                 setPageComplete(true);
231         }
232         public void updateQuery() {
233                 System.out.println(Messages.getString("DeleteRowPage.UpdatingQuery")); //$NON-NLS-1$
234                 StringMatrix key = new StringMatrix();
235                 for (int i = 0; i < columnNames.length; i++) {
236                         if (deleteTable[i].isBPrimary()) {
237                             key.addHeader( deleteTable[i].getSColNames() );
238                             key.add( deleteTable[i].getSValues() , 0 );
239                         }
240                 }
241                 this.query.setText(SQLInstructionBuilder.buildDelete(this.results.getEntity(), key));
242         }
243         
244     protected String getQueryText() {
245         return query.getText();
246     }
247     
248     private void updateView() {
249         this.tableViewer.update(deleteTable, null);
250     }
251
252     private void createTable(Composite composite) {
253         System.out.println("Creating table...");
254         Table table = comUI.createTablePage(composite);
255         colNames = new String[] { Messages.getString("DeleteRowPage.ColumnName"), Messages.getString("DeleteRowPage.Value"), "Where" };
256
257         comUI.createTableColumn(table, colNames[0], SWT.LEFT, 0, 150);
258         comUI.createTableColumn(table, colNames[1], SWT.LEFT, 1, 300);
259         comUI.createTableColumn(table, colNames[2], SWT.CENTER, 2, 60);
260                 this.tableViewer = new TableViewer(table);
261                 this.tableViewer.setColumnProperties(colNames);
262
263                 CellEditor[] editor = new CellEditor[colNames.length];
264                 TextCellEditor txtEditorField = new TextCellEditor(table);
265                 txtEditorField.getControl().setEnabled(false);
266                 editor[0] = txtEditorField;
267                 
268                 TextCellEditor txtEditorFieldValue = new TextCellEditor(table);         
269                 editor[1] = txtEditorFieldValue;
270                 
271                 editor[2] = new CheckboxCellEditor(table, SWT.NULL);                            
272                 
273                 this.tableViewer.setCellEditors(editor);
274                 this.tableViewer.setLabelProvider(new LabelProviderImpl());
275                 this.tableViewer.setContentProvider(new ContentProviderImpl());
276                 this.tableViewer.setCellModifier(new CellModifierImpl());
277                 this.tableViewer.setInput(deleteTable);         
278     }
279 }