Fixed: malfunctioned "Remove trailing spaces on editor save"
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / wizards / InsertRowPage.java
1 package com.quantum.wizards;
2
3 import org.eclipse.jface.viewers.CellEditor;
4 import org.eclipse.jface.viewers.ICellModifier;
5 import org.eclipse.jface.viewers.ILabelProviderListener;
6 import org.eclipse.jface.viewers.IStructuredContentProvider;
7 import org.eclipse.jface.viewers.ITableLabelProvider;
8 import org.eclipse.jface.viewers.TableViewer;
9 import org.eclipse.jface.viewers.TextCellEditor;
10 import org.eclipse.jface.viewers.Viewer;
11 import org.eclipse.swt.SWT;
12 import org.eclipse.swt.graphics.Image;
13 import org.eclipse.swt.layout.GridData;
14 import org.eclipse.swt.layout.GridLayout;
15 import org.eclipse.swt.widgets.Composite;
16 import org.eclipse.swt.widgets.Label;
17 import org.eclipse.swt.widgets.Table;
18 import org.eclipse.swt.widgets.TableItem;
19
20 import com.quantum.Messages;
21 import com.quantum.util.StringMatrix;
22 import com.quantum.util.sql.SQLInstructionBuilder;
23
24 /**
25  * @author BC Holmes
26  * @author Elvin E. Ebora
27  */
28 public class InsertRowPage extends BaseSQLPage implements SQLPage {
29     
30     class InsertRowTableValues {
31         private String colNames = null;
32         private String values   = null;
33         
34         public InsertRowTableValues() {            
35         }
36         /**
37          * @return Returns the colNames.
38          */
39         public String getColNames() {
40             return colNames;
41         }
42         /**
43          * @param colNames The colNames to set.
44          */
45         public void setColNames(String colNames) {
46             this.colNames = colNames;
47         }
48         /**
49          * @return Returns the values.
50          */
51         public String getValues() {
52             return values;
53         }
54         /**
55          * @param values The values to set.
56          */
57         public void setValues(String values) {
58             this.values = values;
59         }
60     }
61     
62     class LabelProviderImpl implements ITableLabelProvider {
63                 public Image getColumnImage(Object element, int columnIndex) {
64                         return null;
65                 }
66                 public String getColumnText(Object element, int columnIndex) {
67                         String sReturn = "";                    
68                         InsertRowTableValues insertRow = (InsertRowTableValues)element;
69                         switch (columnIndex) {
70                                 case 0:
71                                     sReturn = insertRow.getColNames();
72                                     break;
73                                 case 1:
74                                     sReturn = insertRow.getValues();
75                                     break;
76                                 default:
77                                     break;
78                         }
79                         return sReturn;
80                 }
81                 public void addListener(ILabelProviderListener listener) {}
82                 public void dispose() {}
83                 public boolean isLabelProperty(Object element, String property) {
84                         return false;
85                 }
86                 public void removeListener(ILabelProviderListener listener) {}
87         }
88         
89     class ContentProviderImpl implements IStructuredContentProvider {
90                 public Object[] getElements(Object inputElement) {
91                         return insertTable;
92                 }
93
94                 public void dispose() {}
95                 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {}            
96         }
97         
98     class CellModifierImpl implements ICellModifier {
99
100         public boolean canModify(Object element, String property) {
101                 return true;
102         }
103         
104         public Object getValue(Object element, String property) {               
105                 System.out.println("getValue called");
106         
107                 // Find the index of the column
108                 int colIndx = comUI.getColumnNamesAsList(colNames).indexOf(property);
109                 System.out.println("colIndx : " + colIndx);             
110                  
111                 Object rResult = null;
112                 InsertRowTableValues insertVal = (InsertRowTableValues)element;
113                                 
114                 switch (colIndx) {
115                         case 0:
116                             rResult = insertVal.getColNames();
117                                     break;
118                                 case 1:
119                                     rResult = insertVal.getValues();
120                                     break;
121                         default:
122                                 rResult = "";
123                                 break;
124                 }
125                 
126                 return rResult;
127         }
128         
129         public void modify(Object element, String property, Object value) {
130                 int colIndx = comUI.getColumnNamesAsList(colNames).indexOf(property);
131                 
132                 TableItem item = (TableItem) element;
133                 InsertRowTableValues insertVal = (InsertRowTableValues)item.getData();
134                 
135                 switch (colIndx) {
136                         case 0: // field names
137                             break;
138                         case 1: // field values
139                             insertVal.setValues(value.toString());
140                             updateView();                           
141                             updateQuery();
142                             break;
143                         default:
144                             break;
145                 }
146         }
147     }
148     
149         String[] columnNames;
150         String[] colNames;
151         Label query;
152         InsertRowTableValues[] insertTable = null;
153         CommonWizardUI comUI;
154         TableViewer tableViewer = null; 
155         int numColumns = 0;
156         
157         public InsertRowPage(String pageName) {
158                 super(pageName);                
159         }
160
161         public void createControl(Composite parent) {
162                 System.out.println("page create control"); //$NON-NLS-1$
163                 Composite container = new Composite(parent, SWT.NULL);
164                 container.setLayout(new GridLayout());
165                 container.setLayoutData(new GridData(GridData.FILL_BOTH | GridData.VERTICAL_ALIGN_BEGINNING));
166                 
167                 comUI = new CommonWizardUI();
168
169                 // init values to be displayed on the table
170                 columnNames = this.results.getColumnNames();
171                 int nLen = columnNames.length;
172                 insertTable = new InsertRowTableValues[nLen];
173                 
174                 for (int nCtr=0; nCtr<nLen; nCtr++) {
175                     insertTable[nCtr] = new InsertRowTableValues();
176                     insertTable[nCtr].setColNames(columnNames[nCtr]);
177                     insertTable[nCtr].setValues("");
178                 }               
179                 
180                 createTable(container);                         
181                 
182                 query = new Label(container, SWT.WRAP);
183                 query.setLayoutData(comUI.createGridData(1, GridData.FILL));
184
185                 setControl(container);
186         updateQuery();
187        
188                 setPageComplete(true);
189         }
190         public void updateQuery() {
191                 System.out.println("Updating query"); //$NON-NLS-1$
192                 
193                 StringMatrix columns = new StringMatrix();
194                 for (int i = 0; i < columnNames.length; i++) {
195                     columns.addHeader( insertTable[i].getColNames() );
196                     columns.add( insertTable[i].getValues() , 0 );
197                 }
198                 this.query.setText(SQLInstructionBuilder.buildInsert(this.results.getEntity(), columns));
199         }
200     /* (non-Javadoc)
201      * @see com.quantum.wizards.BaseSQLPage#getQueryText()
202      */
203     protected String getQueryText() {
204         return this.query.getText();
205     }
206     
207     private void createTable(Composite composite) {
208         System.out.println("Creating table...");
209         Table table = comUI.createTablePage(composite);
210         colNames = new String[] { Messages.getString("InsertRowPage.ColumnName"), Messages.getString("InsertRowPage.Value") };
211
212         comUI.createTableColumn(table, colNames[0], SWT.LEFT, 0, 200);
213         comUI.createTableColumn(table, colNames[1], SWT.LEFT, 1, 500);
214                 this.tableViewer = new TableViewer(table);
215                 this.tableViewer.setColumnProperties(colNames);
216
217                 CellEditor[] editor = new CellEditor[colNames.length];
218                 TextCellEditor txtEditorField = new TextCellEditor(table);
219                 txtEditorField.getControl().setEnabled(false);
220                 editor[0] = txtEditorField;
221                 
222                 TextCellEditor txtEditorValues = new TextCellEditor(table);
223                 editor[1] = txtEditorValues;
224                 
225                 this.tableViewer.setCellEditors(editor);
226                 this.tableViewer.setLabelProvider(new LabelProviderImpl());
227                 this.tableViewer.setContentProvider(new ContentProviderImpl());
228                 this.tableViewer.setCellModifier(new CellModifierImpl());
229                 this.tableViewer.setInput(insertTable);         
230     }        
231         
232     private void updateView() {
233         this.tableViewer.update(insertTable, null);
234     }
235 }