1 package com.quantum.wizards;
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;
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;
30 * @author Elvin E. Ebora
32 public class UpdateRowPage extends BaseSQLPage implements SQLPage {
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;
41 public UpdateRowTableValues() {
44 * @return Returns the bSetValue.
46 public boolean isBSetValue() {
50 * @param setValue The bSetValue to set.
52 public void setBSetValue(boolean setValue) {
56 * @return Returns the bPrimary.
58 public boolean isBPrimary() {
62 * @param where The bPrimary to set.
64 public void setBPrimary(boolean where) {
68 * @return Returns the sColNames.
70 public String getSColNames() {
74 * @param colNames The sColNames to set.
76 public void setSColNames(String colNames) {
80 * @return Returns the sNewValue.
82 public String getSNewValue() {
86 * @param newValue The sNewValue to set.
88 public void setSNewValue(String newValue) {
92 * @return Returns the sOldValue.
94 public String getSOldValue() {
98 * @param oldValue The sOldValue to set.
100 public void setSOldValue(String oldValue) {
101 sOldValue = oldValue;
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;
115 public String getColumnText(Object element, int columnIndex) {
117 UpdateRowTableValues updateRow = (UpdateRowTableValues)element;
118 switch (columnIndex) {
119 case 0: // column names
120 sReturn = updateRow.getSColNames();
122 case 1: // old values
123 sReturn = updateRow.getSOldValue();
125 case 2: // set checkbox
128 sReturn = updateRow.getSNewValue();
130 case 4: // set value checkbox
137 public void addListener(ILabelProviderListener listener) {}
138 public void dispose() {}
139 public boolean isLabelProperty(Object element, String property) {
142 public void removeListener(ILabelProviderListener listener) {}
145 class ContentProviderImpl implements IStructuredContentProvider {
146 public Object[] getElements(Object inputElement) {
150 public void dispose() {}
151 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {}
154 class CellModifierImpl implements ICellModifier {
156 public boolean canModify(Object element, String property) {
160 public Object getValue(Object element, String property) {
161 System.out.println("getValue called");
163 // Find the index of the column
164 int colIndx = comUI.getColumnNamesAsList(colNames).indexOf(property);
165 System.out.println("colIndx : " + colIndx);
167 Object rResult = null;
168 UpdateRowTableValues updateVal = (UpdateRowTableValues)element;
171 case 0: // column names
172 rResult = updateVal.getSColNames();
174 case 1: // old values
175 rResult = updateVal.getSOldValue();
177 case 2: // set checkbox
178 rResult = new Boolean(updateVal.isBPrimary());
181 rResult = updateVal.getSNewValue();
183 case 4: // set value checkbox
184 rResult = new Boolean(updateVal.isBSetValue());
193 public void modify(Object element, String property, Object value) {
194 int colIndx = comUI.getColumnNamesAsList(colNames).indexOf(property);
196 TableItem item = (TableItem) element;
197 UpdateRowTableValues updateVal = (UpdateRowTableValues)item.getData();
200 case 0: // column names
202 case 1: // old values
204 case 2: // set checkbox
205 updateVal.setBPrimary(((Boolean)value).booleanValue());
208 updateVal.setSNewValue(value.toString());
210 case 4: // set value checkbox
211 updateVal.setBSetValue(((Boolean)value).booleanValue());
222 String[] columnNames;
225 UpdateRowTableValues[] updateTable = null;
226 CommonWizardUI comUI;
227 TableViewer tableViewer = null;
228 static Image imgCheck = null;
229 static Image imgUncheck = null;
232 imgCheck = ImageStore.getImage(ImageStore.CHECKED);
233 imgUncheck = ImageStore.getImage(ImageStore.UNCHECKED);
236 public UpdateRowPage(String pageName) {
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));
246 Entity entity = this.results.getEntity();
248 comUI = new CommonWizardUI();
250 // init values to be displayed on the table
251 columnNames = this.results.getColumnNames();
252 int nLen = columnNames.length;
253 updateTable = new UpdateRowTableValues[nLen];
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);
264 updateTable[nCtr].setSNewValue(data == null ? "" : data.toString());
267 createTable(container);
269 query = new Label(container, SWT.WRAP);
270 query.setLayoutData(comUI.createGridData(1, GridData.FILL));
272 setControl(container);
275 setPageComplete(true);
277 public void updateQuery() {
278 System.out.println("Updating query"); //$NON-NLS-1$
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 );
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 );
295 this.query.setText(SQLInstructionBuilder.buildUpdate(this.results.getEntity(), columns, key));
299 * @see com.quantum.wizards.BaseSQLPage#getQueryText()
301 protected String getQueryText() {
302 return query.getText();
305 private void updateView() {
306 this.tableViewer.update(updateTable, null);
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") };
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);
323 CellEditor[] editor = new CellEditor[colNames.length];
324 TextCellEditor txtEditorField = new TextCellEditor(table);
325 txtEditorField.getControl().setEnabled(false);
326 editor[0] = txtEditorField;
328 TextCellEditor txtEditorFieldOld = new TextCellEditor(table);
329 txtEditorFieldOld.getControl().setEnabled(false);
330 editor[1] = txtEditorFieldOld;
332 editor[2] = new CheckboxCellEditor(table, SWT.NULL);
334 TextCellEditor txtEditorValues = new TextCellEditor(table);
335 editor[3] = txtEditorValues;
337 editor[4] = new CheckboxCellEditor(table, SWT.NULL);
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);