Current php5 doc from www.php.net (english);
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / wizards / InsertRowPage.java
index 91c55d0..f25054d 100644 (file)
 package com.quantum.wizards;
 
+import java.util.Arrays;
+
+import org.eclipse.jface.viewers.CellEditor;
+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.graphics.Image;
 import org.eclipse.swt.layout.GridData;
 import org.eclipse.swt.layout.GridLayout;
 import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Label;
+import org.eclipse.swt.widgets.Table;
+import org.eclipse.swt.widgets.TableColumn;
+import org.eclipse.swt.widgets.TableItem;
 import org.eclipse.swt.widgets.Text;
 
 import com.quantum.Messages;
 import com.quantum.adapters.DatabaseAdapter;
 import com.quantum.model.Bookmark;
 import com.quantum.model.Entity;
+import com.quantum.sql.SQLResultSetResults;
 
 public class InsertRowPage extends BaseSQLPage implements SQLPage {
+    
+    class InsertRowTableValues {
+        private String colNames = null;
+        private String values  = null;
+        
+        public InsertRowTableValues() {            
+        }
+        /**
+         * @return Returns the colNames.
+         */
+        public String getColNames() {
+            return colNames;
+        }
+        /**
+         * @param colNames The colNames to set.
+         */
+        public void setColNames(String colNames) {
+            this.colNames = colNames;
+        }
+        /**
+         * @return Returns the values.
+         */
+        public String getValues() {
+            return values;
+        }
+        /**
+         * @param values The values to set.
+         */
+        public void setValues(String values) {
+            this.values = values;
+        }
+    }
+    
+    class LabelProviderImpl implements ITableLabelProvider {
+               public Image getColumnImage(Object element, int columnIndex) {
+                       return null;
+               }
+               public String getColumnText(Object element, int columnIndex) {
+                       String sReturn = "";                    
+                       InsertRowTableValues insertRow = (InsertRowTableValues)element;
+                       switch (columnIndex) {
+                               case 0:
+                                   sReturn = insertRow.getColNames();
+                                   break;
+                               case 1:
+                                   sReturn = insertRow.getValues();
+                                   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 insertTable;
+               }
+
+               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 = getColumnNamesAsList(colNames).indexOf(property);
+               System.out.println("colIndx : " + colIndx);             
+                
+               Object rResult = null;
+               InsertRowTableValues insertVal = (InsertRowTableValues)element;
+                               
+               switch (colIndx) {
+                       case 0:
+                           rResult = insertVal.getColNames();
+                                   break;
+                               case 1:
+                                   rResult = insertVal.getValues();
+                                   break;
+                       default:
+                               rResult = "";
+                               break;
+               }
+               
+               return rResult;
+       }
+       
+       public void modify(Object element, String property, Object value) {
+               int colIndx = getColumnNamesAsList(colNames).indexOf(property);
+               
+               TableItem item = (TableItem) element;
+               InsertRowTableValues insertVal = (InsertRowTableValues)item.getData();
+               
+               switch (colIndx) {
+                       case 0: // field names
+                           break;
+                       case 1: // field values
+                           insertVal.setValues(value.toString());
+                           updateView();                           
+                           updateQuery();
+                           break;
+                       default:
+                           break;
+               }
+       }
+    }
+    
        String[] columnNames;
+       String[] colNames;
        Text[] values;
        Label query;
+       InsertRowTableValues[] insertTable = null;
+       TableViewer tableViewer = null; 
        int numColumns = 0;
        
        public InsertRowPage(String pageName) {
-               super(pageName);
+               super(pageName);                
        }
 
        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 = 2;
+               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$
-               }
-               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()];
-               new Label(container, SWT.NULL).setText(Messages.getString("InsertRowPage.ColumnName")); //$NON-NLS-1$
-               new Label(container, SWT.NULL).setText(Messages.getString("InsertRowPage.Value")); //$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();
-                               }                               
-                       });
-               }
+               // init values to be displayed on the table
+               columnNames = this.results.getColumnNames();
+               int nLen = columnNames.length;
+               insertTable = new InsertRowTableValues[nLen];
+               
+               for (int nCtr=0; nCtr<nLen; nCtr++) {
+                   insertTable[nCtr] = new InsertRowTableValues();
+                   insertTable[nCtr].setColNames(columnNames[nCtr]);
+                   insertTable[nCtr].setValues("");
+               }               
+               
+               createTable(container);                         
+               
                query = new Label(container, SWT.WRAP);
                GridData gridData = new GridData();
-               gridData.horizontalSpan = layout.numColumns;
-               gridData.horizontalAlignment = GridData.FILL;
-               gridData.verticalAlignment = GridData.FILL;
-               gridData.grabExcessHorizontalSpace = true;
-               gridData.grabExcessVerticalSpace = true;
+               gridData.horizontalSpan = 1;            
+               gridData.horizontalAlignment = GridData.FILL;
+               gridData.verticalAlignment = GridData.FILL;             
                query.setLayoutData(gridData);
 
                setControl(container);
@@ -82,15 +197,15 @@ public class InsertRowPage extends BaseSQLPage implements SQLPage {
                System.out.println("Updating query"); //$NON-NLS-1$
                StringBuffer valuesClause = new StringBuffer();
                StringBuffer namesClause = new StringBuffer();
-               Bookmark bookmark = row.getBookmark();
-               Entity entity = row.getEntity();
+               Bookmark bookmark = this.results.getBookmark();
+               Entity entity = this.results.getEntity();
                DatabaseAdapter adapter = bookmark.getAdapter();
                
                numColumns = 0;
                for (int i = 0; i < columnNames.length; i++) {
-                       String name = columnNames[i];
-                       String value = values[i].getText();
-                       if (value.length() > 0) {
+                   String name = insertTable[i].getColNames();
+                   String value = insertTable[i].getValues();
+                       if (value != null && value.length() > 0) {
                                if (numColumns > 0) {
                                        valuesClause.append(", "); //$NON-NLS-1$
                                        namesClause.append(", ");
@@ -101,7 +216,7 @@ public class InsertRowPage extends BaseSQLPage implements SQLPage {
                        }
                }
                
-               String query = "INSERT INTO " + row.getTable(); //$NON-NLS-1$
+               String query = "INSERT INTO " + this.results.getEntity().getQuotedTableName(); //$NON-NLS-1$
                if (numColumns > 0) {
                        query += " (" + namesClause + ")";
                        query += " VALUES " + "(" + valuesClause; //$NON-NLS-1$
@@ -115,5 +230,38 @@ public class InsertRowPage extends BaseSQLPage implements SQLPage {
     protected String getQueryText() {
         return this.query.getText();
     }
+    
+    private void createTable(Composite composite) {
+        System.out.println("Creating table...");
+        int style = SWT.SINGLE | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.HIDE_SELECTION;
+               Table table = new Table(composite, style);
+       table.setHeaderVisible(true);
+       table.setLinesVisible(true);
+       table.setLayoutData(new GridData(GridData.FILL_BOTH | GridData.VERTICAL_ALIGN_BEGINNING));
+       
+       colNames = new String[] { Messages.getString("InsertRowPage.ColumnName"), Messages.getString("InsertRowPage.Value") };
+
+               createTableColumn(table, colNames[0], SWT.LEFT, 0, 200);
+               createTableColumn(table, colNames[1], SWT.LEFT, 1, 500);
+               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 txtEditorValues = new TextCellEditor(table);
+               editor[1] = txtEditorValues;
+               
+               this.tableViewer.setCellEditors(editor);
+               this.tableViewer.setLabelProvider(new LabelProviderImpl());
+               this.tableViewer.setContentProvider(new ContentProviderImpl());
+               this.tableViewer.setCellModifier(new CellModifierImpl());
+               this.tableViewer.setInput(insertTable);         
+    }        
+        
+    private void updateView() {
+        this.tableViewer.update(insertTable, null);
+    }
 }
\ No newline at end of file