Testing the post-commit script the only way i know how.
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / wizards / DeleteRowPage.java
index dc90a00..59e8cf3 100644 (file)
 package com.quantum.wizards;
 
-import com.quantum.Messages;
-import com.quantum.adapters.AdapterFactory;
-import com.quantum.adapters.DatabaseAdapter;
-import com.quantum.model.Bookmark;
-import com.quantum.model.Entity;
-
+import org.eclipse.jface.viewers.CellEditor;
+import org.eclipse.jface.viewers.CheckboxCellEditor;
+import org.eclipse.jface.viewers.ICellModifier;
+import org.eclipse.jface.viewers.ILabelProviderListener;
+import org.eclipse.jface.viewers.IStructuredContentProvider;
+import org.eclipse.jface.viewers.ITableLabelProvider;
+import org.eclipse.jface.viewers.TableViewer;
+import org.eclipse.jface.viewers.TextCellEditor;
+import org.eclipse.jface.viewers.Viewer;
 import org.eclipse.swt.SWT;
-import org.eclipse.swt.events.ModifyEvent;
-import org.eclipse.swt.events.ModifyListener;
-import org.eclipse.swt.events.SelectionEvent;
-import org.eclipse.swt.events.SelectionListener;
+import org.eclipse.swt.graphics.Image;
 import org.eclipse.swt.layout.GridData;
 import org.eclipse.swt.layout.GridLayout;
-import org.eclipse.swt.widgets.Button;
 import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Label;
-import org.eclipse.swt.widgets.Text;
+import org.eclipse.swt.widgets.Table;
+import org.eclipse.swt.widgets.TableItem;
+
+import com.quantum.ImageStore;
+import com.quantum.Messages;
+import com.quantum.model.Column;
+import com.quantum.model.Entity;
+import com.quantum.util.StringMatrix;
+import com.quantum.util.sql.SQLInstructionBuilder;
 
+/**
+ * @author BC Holmes
+ * @author Elvin E. Ebora
+ */
 public class DeleteRowPage extends BaseSQLPage implements SQLPage {
+    
+    class DeleteRowPageValues {
+        private String sColNames = null;
+        private String sValues = null;
+        private boolean bPrimary = false;
+        
+        public DeleteRowPageValues() {            
+        }
+        /**
+         * @return Returns the bPrimary.
+         */
+        public boolean isBPrimary() {
+            return bPrimary;
+        }
+        /**
+         * @param primary The bPrimary to set.
+         */
+        public void setBPrimary(boolean primary) {
+            bPrimary = primary;
+        }
+        /**
+         * @return Returns the sColNames.
+         */
+        public String getSColNames() {
+            return sColNames;
+        }
+        /**
+         * @param colNames The sColNames to set.
+         */
+        public void setSColNames(String colNames) {
+            sColNames = colNames;
+        }
+        /**
+         * @return Returns the sValues.
+         */
+        public String getSValues() {
+            return sValues;
+        }
+        /**
+         * @param values The sValues to set.
+         */
+        public void setSValues(String values) {
+            sValues = values;
+        }
+    }
+    
+    class LabelProviderImpl implements ITableLabelProvider {                
+               public Image getColumnImage(Object element, int columnIndex) {
+                   if (columnIndex == 2) {
+                       return ((DeleteRowPageValues)element).isBPrimary() ? imgCheck : imgUncheck;
+                   } else {
+                       return null;
+                   }
+               }
+               public String getColumnText(Object element, int columnIndex) {
+                       String sReturn = "";                    
+                       DeleteRowPageValues deleteRow = (DeleteRowPageValues)element;
+                       switch (columnIndex) {
+                               case 0: // column names
+                                   sReturn = deleteRow.getSColNames();
+                                   break;
+                               case 1: // values
+                                   sReturn = deleteRow.getSValues();
+                                   break;
+                               case 2: // set checkbox
+                                   break;
+                               default:
+                                   break;
+                       }
+                       return sReturn;
+               }
+               public void addListener(ILabelProviderListener listener) {}
+               public void dispose() {}
+               public boolean isLabelProperty(Object element, String property) {
+                       return false;
+               }
+               public void removeListener(ILabelProviderListener listener) {}
+       }
+       
+    class ContentProviderImpl implements IStructuredContentProvider {
+               public Object[] getElements(Object inputElement) {
+                       return deleteTable;
+               }
+
+               public void dispose() {}
+               public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {}            
+       }
+        
+    class CellModifierImpl implements ICellModifier {
+
+       public boolean canModify(Object element, String property) {
+               return true;
+       }
+       
+       public Object getValue(Object element, String property) {               
+               System.out.println("getValue called");
+       
+               // Find the index of the column
+               int colIndx = comUI.getColumnNamesAsList(colNames).indexOf(property);
+               System.out.println("colIndx : " + colIndx);             
+                
+               Object rResult = null;
+               DeleteRowPageValues deleteVal = (DeleteRowPageValues)element;
+                               
+               switch (colIndx) {
+                       case 0: // column names
+                           rResult = deleteVal.getSColNames();
+                                   break;
+                               case 1: // values
+                                   rResult = deleteVal.getSValues();
+                                   break;
+                               case 2: // checkbox
+                                   rResult = new Boolean(deleteVal.isBPrimary());
+                                   break;
+                               default:
+                                   break;
+               }
+               
+               return rResult;
+       }
+       
+       public void modify(Object element, String property, Object value) {
+               int colIndx = comUI.getColumnNamesAsList(colNames).indexOf(property);
+               
+               TableItem item = (TableItem) element;
+               DeleteRowPageValues deleteVal = (DeleteRowPageValues)item.getData();
+               
+               switch (colIndx) {
+                       case 0: // column names                     
+                                   break;
+                               case 1: // values               
+                                   deleteVal.setSValues(value.toString());
+                                   break;
+                               case 2: // checkbox
+                                   deleteVal.setBPrimary(((Boolean)value).booleanValue());
+                                   break;
+                               default:
+                                   break;
+               }
+               
+               updateView();                               
+                   updateQuery();
+       }
+    }
+    
        String[] columnNames;
-       Text[] values;
-       Button[] whereValues;
-       Text query;
+       String[] colNames;
+       Label query;
+       DeleteRowPageValues[] deleteTable = null;
+       CommonWizardUI comUI;
+       TableViewer tableViewer = null;
+       static Image imgCheck = null;
+       static Image imgUncheck = null;
+       
+       static {
+           imgCheck = ImageStore.getImage(ImageStore.CHECKED);
+           imgUncheck = ImageStore.getImage(ImageStore.UNCHECKED);
+       }
 
        public DeleteRowPage(String pageName) {
                super(pageName);
@@ -31,71 +196,33 @@ public class DeleteRowPage extends BaseSQLPage implements SQLPage {
        public void createControl(Composite parent) {
                System.out.println("page create control"); //$NON-NLS-1$
                Composite container = new Composite(parent, SWT.NULL);
-               GridLayout layout = new GridLayout();
-               container.setLayout(layout);
-               layout.numColumns = 3;
+               container.setLayout(new GridLayout());
+               container.setLayoutData(new GridData(GridData.FILL_BOTH | GridData.VERTICAL_ALIGN_BEGINNING));
 
-               if (row == null) {
-                       System.out.println("Row is null"); //$NON-NLS-1$
-               }
-               if (row.getColumnNames() == null) {
-                       System.out.println("Columns are null"); //$NON-NLS-1$
-               }
-               if (row.getTableData() == null) {
-                       System.out.println("Data is null"); //$NON-NLS-1$
-               }
-               Entity entity = row.getEntity();
-               columnNames = row.getColumnNames();
-               String[] data = row.getTableData();
-               for (int i = 0; i < row.getColumnCount(); i++) {
-                       System.out.println("data = " + i + "=" + data[i]); //$NON-NLS-1$ //$NON-NLS-2$
-                       System.out.println("column = " + i + "=" + columnNames[i]); //$NON-NLS-1$ //$NON-NLS-2$
-               }
-               values = new Text[row.getColumnCount()];
-               whereValues = new Button[row.getColumnCount()];
-               new Label(container, SWT.NULL).setText(Messages.getString("DeleteRowPage.ColumnName")); //$NON-NLS-1$
-               new Label(container, SWT.NULL).setText(Messages.getString("DeleteRowPage.Value")); //$NON-NLS-1$
-               new Label(container, SWT.NULL).setText(Messages.getString("DeleteRowPage.IncludeIn")); //$NON-NLS-1$
-               for (int i = 0; i < row.getColumnCount(); i++) {
-                       Label label = new Label(container, SWT.NULL);
-                       label.setText(columnNames[i]);
-                       values[i] = new Text(container, SWT.BORDER | SWT.SINGLE);
-                       GridData fullHorizontal = new GridData();
-                       fullHorizontal.horizontalAlignment = GridData.FILL;
-                       values[i].setLayoutData(fullHorizontal);
-                       values[i].setText(data[i]);
-
-                       values[i].addModifyListener(new ModifyListener() {
-                               public void modifyText(ModifyEvent e) {
-                                       updateQuery();
-                               }                               
-                       });
-                       
-                       whereValues[i] = new Button(container, SWT.CHECK);
-                       whereValues[i].setText(Messages.getString("DeleteRowPage.WhereClause")); //$NON-NLS-1$
-                       // we check if it's a primary key to select it in the WHERE clause
-                       if (entity != null && entity.getColumn(columnNames[i]).isPrimaryKey()) {
-                whereValues[i].setSelection(true);
-                       } else {
-                               whereValues[i].setSelection(false);
-            }
-                       whereValues[i].addSelectionListener(new SelectionListener() {
-                               public void widgetDefaultSelected(SelectionEvent e) {
-                               }
-                               public void widgetSelected(SelectionEvent e) {
-                                       updateQuery();
-                               }
-                       });
+               Entity entity = this.results.getEntity();
+               
+               comUI = new CommonWizardUI();
+               
+               // init values to be displayed on the table
+               columnNames = this.results.getColumnNames();
+               int nLen = columnNames.length;
+               deleteTable = new DeleteRowPageValues[nLen];
+               
+               for(int nCtr=0; nCtr<nLen; nCtr++) {
+                   deleteTable[nCtr] = new DeleteRowPageValues();
+                   deleteTable[nCtr].setSColNames(columnNames[nCtr]);
+                   Object data = this.row == null ? null : this.row.get(nCtr+1);
+                   deleteTable[nCtr].setSValues(data == null ? "" : data.toString());
+                   Column column = (entity == null) ? null : getColumn(entity, columnNames[nCtr]);
+                       if (column != null && column.isPrimaryKey()) {
+                           deleteTable[nCtr].setBPrimary(true);
+                       }                   
                }
-               query = new Text(container, SWT.MULTI | SWT.READ_ONLY | SWT.WRAP);
-               GridData gridData = new GridData();
-               gridData.horizontalSpan = layout.numColumns;
-               gridData.verticalSpan = 3;
-               gridData.horizontalAlignment = GridData.FILL;
-               gridData.verticalAlignment = GridData.FILL;
-               gridData.grabExcessHorizontalSpace = true;
-               gridData.grabExcessVerticalSpace = true;
-               query.setLayoutData(gridData);
+               
+               createTable(container);
+               
+               query = new Label(container, SWT.WRAP);
+               query.setLayoutData(comUI.createGridData(1, GridData.FILL));
 
                setControl(container);
         updateQuery();
@@ -104,36 +231,49 @@ public class DeleteRowPage extends BaseSQLPage implements SQLPage {
        }
        public void updateQuery() {
                System.out.println(Messages.getString("DeleteRowPage.UpdatingQuery")); //$NON-NLS-1$
-               StringBuffer whereClause = new StringBuffer();
-               Bookmark bookmark = row.getBookmark();
-               Entity entity = row.getEntity();
-               DatabaseAdapter adapter = AdapterFactory.getInstance().getAdapter(bookmark.getType());
-               
-               int numSelected = 0;
+               StringMatrix key = new StringMatrix();
                for (int i = 0; i < columnNames.length; i++) {
-                       if (whereValues[i].getSelection()) {
-                               if (numSelected > 0) whereClause.append(" AND "); //$NON-NLS-1$
-                               numSelected++;
-                               whereClause.append("("); //$NON-NLS-1$
-                               whereClause.append(columnNames[i]);
-                               whereClause.append(" = "); //$NON-NLS-1$
-                appendColumn(whereClause, entity, columnNames[i], adapter, values[i].getText());
-                               whereClause.append(")"); //$NON-NLS-1$
+                       if (deleteTable[i].isBPrimary()) {
+                           key.addHeader( deleteTable[i].getSColNames() );
+                           key.add( deleteTable[i].getSValues() , 0 );
                        }
                }
-               String query = "DELETE FROM " + row.getTable(); //$NON-NLS-1$
-               if (numSelected > 0) {
-                       query += " WHERE " + whereClause.toString(); //$NON-NLS-1$
-               }
-               if (numSelected > 0) {
-                       setMessage(""); //$NON-NLS-1$
-               } else {
-                       setMessage(Messages.getString("DeleteRowPage.WarningNoWhere")); //$NON-NLS-1$
-               }
-               this.query.setText(query);
+               this.query.setText(SQLInstructionBuilder.buildDelete(this.results.getEntity(), key));
        }
        
     protected String getQueryText() {
         return query.getText();
     }
+    
+    private void updateView() {
+        this.tableViewer.update(deleteTable, null);
+    }
+
+    private void createTable(Composite composite) {
+        System.out.println("Creating table...");
+        Table table = comUI.createTablePage(composite);
+       colNames = new String[] { Messages.getString("DeleteRowPage.ColumnName"), Messages.getString("DeleteRowPage.Value"), "Where" };
+
+       comUI.createTableColumn(table, colNames[0], SWT.LEFT, 0, 150);
+       comUI.createTableColumn(table, colNames[1], SWT.LEFT, 1, 300);
+       comUI.createTableColumn(table, colNames[2], SWT.CENTER, 2, 60);
+               this.tableViewer = new TableViewer(table);
+               this.tableViewer.setColumnProperties(colNames);
+
+               CellEditor[] editor = new CellEditor[colNames.length];
+               TextCellEditor txtEditorField = new TextCellEditor(table);
+               txtEditorField.getControl().setEnabled(false);
+               editor[0] = txtEditorField;
+               
+               TextCellEditor txtEditorFieldValue = new TextCellEditor(table);         
+               editor[1] = txtEditorFieldValue;
+               
+               editor[2] = new CheckboxCellEditor(table, SWT.NULL);                            
+               
+               this.tableViewer.setCellEditors(editor);
+               this.tableViewer.setLabelProvider(new LabelProviderImpl());
+               this.tableViewer.setContentProvider(new ContentProviderImpl());
+               this.tableViewer.setCellModifier(new CellModifierImpl());
+               this.tableViewer.setInput(deleteTable);         
+    }
 }
\ No newline at end of file