1 package com.quantum.wizards;
3 import java.util.Arrays;
5 import org.eclipse.jface.viewers.CellEditor;
6 import org.eclipse.jface.viewers.CheckboxCellEditor;
7 import org.eclipse.jface.viewers.ICellModifier;
8 import org.eclipse.jface.viewers.ILabelProviderListener;
9 import org.eclipse.jface.viewers.IStructuredContentProvider;
10 import org.eclipse.jface.viewers.ITableLabelProvider;
11 import org.eclipse.jface.viewers.TableViewer;
12 import org.eclipse.jface.viewers.TextCellEditor;
13 import org.eclipse.jface.viewers.Viewer;
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.events.ModifyEvent;
16 import org.eclipse.swt.events.ModifyListener;
17 import org.eclipse.swt.events.SelectionEvent;
18 import org.eclipse.swt.events.SelectionListener;
19 import org.eclipse.swt.graphics.Image;
20 import org.eclipse.swt.layout.GridData;
21 import org.eclipse.swt.layout.GridLayout;
22 import org.eclipse.swt.widgets.Button;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.swt.widgets.Label;
25 import org.eclipse.swt.widgets.Table;
26 import org.eclipse.swt.widgets.TableColumn;
27 import org.eclipse.swt.widgets.TableItem;
28 import org.eclipse.swt.widgets.Text;
30 import com.quantum.ImageStore;
31 import com.quantum.Messages;
32 import com.quantum.adapters.DatabaseAdapter;
33 import com.quantum.model.Bookmark;
34 import com.quantum.model.Column;
35 import com.quantum.model.Entity;
36 import com.quantum.wizards.InsertRowPage.CellModifierImpl;
37 import com.quantum.wizards.InsertRowPage.ContentProviderImpl;
38 import com.quantum.wizards.InsertRowPage.InsertRowTableValues;
39 import com.quantum.wizards.InsertRowPage.LabelProviderImpl;
41 public class UpdateRowPage extends BaseSQLPage implements SQLPage {
43 class UpdateRowTableValues {
44 private String sColNames = null;
45 private String sOldValue = null;
46 private boolean bPrimary = false;
47 private String sNewValue = null;
48 private boolean bSetValue = false;
50 public UpdateRowTableValues() {
53 * @return Returns the bSetValue.
55 public boolean isBSetValue() {
59 * @param setValue The bSetValue to set.
61 public void setBSetValue(boolean setValue) {
65 * @return Returns the bPrimary.
67 public boolean isBPrimary() {
71 * @param where The bPrimary to set.
73 public void setBPrimary(boolean where) {
77 * @return Returns the sColNames.
79 public String getSColNames() {
83 * @param colNames The sColNames to set.
85 public void setSColNames(String colNames) {
89 * @return Returns the sNewValue.
91 public String getSNewValue() {
95 * @param newValue The sNewValue to set.
97 public void setSNewValue(String newValue) {
101 * @return Returns the sOldValue.
103 public String getSOldValue() {
107 * @param oldValue The sOldValue to set.
109 public void setSOldValue(String oldValue) {
110 sOldValue = oldValue;
114 class LabelProviderImpl implements ITableLabelProvider {
115 public Image getColumnImage(Object element, int columnIndex) {
116 if (columnIndex == 2) {
117 return ((UpdateRowTableValues)element).isBPrimary() ? imgCheck : imgUncheck;
118 } else if (columnIndex == 4) {
119 return ((UpdateRowTableValues)element).isBSetValue() ? imgCheck : imgUncheck;
124 public String getColumnText(Object element, int columnIndex) {
126 UpdateRowTableValues updateRow = (UpdateRowTableValues)element;
127 switch (columnIndex) {
128 case 0: // column names
129 sReturn = updateRow.getSColNames();
131 case 1: // old values
132 sReturn = updateRow.getSOldValue();
134 case 2: // set checkbox
137 sReturn = updateRow.getSNewValue();
139 case 4: // set value checkbox
146 public void addListener(ILabelProviderListener listener) {}
147 public void dispose() {}
148 public boolean isLabelProperty(Object element, String property) {
151 public void removeListener(ILabelProviderListener listener) {}
154 class ContentProviderImpl implements IStructuredContentProvider {
155 public Object[] getElements(Object inputElement) {
159 public void dispose() {}
160 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {}
163 class CellModifierImpl implements ICellModifier {
165 public boolean canModify(Object element, String property) {
169 public Object getValue(Object element, String property) {
170 System.out.println("getValue called");
172 // Find the index of the column
173 int colIndx = getColumnNamesAsList(colNames).indexOf(property);
174 System.out.println("colIndx : " + colIndx);
176 Object rResult = null;
177 UpdateRowTableValues updateVal = (UpdateRowTableValues)element;
180 case 0: // column names
181 rResult = updateVal.getSColNames();
183 case 1: // old values
184 rResult = updateVal.getSOldValue();
186 case 2: // set checkbox
187 rResult = new Boolean(updateVal.isBPrimary());
190 rResult = updateVal.getSNewValue();
192 case 4: // set value checkbox
193 rResult = new Boolean(updateVal.isBSetValue());
202 public void modify(Object element, String property, Object value) {
203 int colIndx = getColumnNamesAsList(colNames).indexOf(property);
205 TableItem item = (TableItem) element;
206 UpdateRowTableValues updateVal = (UpdateRowTableValues)item.getData();
209 case 0: // column names
211 case 1: // old values
213 case 2: // set checkbox
214 updateVal.setBPrimary(((Boolean)value).booleanValue());
217 updateVal.setSNewValue(value.toString());
219 case 4: // set value checkbox
220 updateVal.setBSetValue(((Boolean)value).booleanValue());
231 String[] columnNames;
235 Button[] primaryKeys;
238 UpdateRowTableValues[] updateTable = null;
239 TableViewer tableViewer = null;
240 static Image imgCheck = null;
241 static Image imgUncheck = null;
244 imgCheck = ImageStore.getImage(ImageStore.CHECKED);
245 imgUncheck = ImageStore.getImage(ImageStore.UNCHECKED);
248 public UpdateRowPage(String pageName) {
252 public void createControl(Composite parent) {
253 System.out.println("page create control"); //$NON-NLS-1$
254 Composite container = new Composite(parent, SWT.NULL);
255 container.setLayout(new GridLayout());
256 container.setLayoutData(new GridData(GridData.FILL_BOTH | GridData.VERTICAL_ALIGN_BEGINNING));
258 Entity entity = this.results.getEntity();
260 // init values to be displayed on the table
261 columnNames = this.results.getColumnNames();
262 int nLen = columnNames.length;
263 updateTable = new UpdateRowTableValues[nLen];
265 for(int nCtr=0; nCtr<nLen; nCtr++) {
266 updateTable[nCtr] = new UpdateRowTableValues();
267 updateTable[nCtr].setSColNames(columnNames[nCtr]);
268 Object data = this.row == null ? null : this.row.get(nCtr+1);
269 updateTable[nCtr].setSOldValue(data == null ? "" : data.toString());
270 Column column = (entity == null) ? null : getColumn(entity, columnNames[nCtr]);
271 if (column != null && column.isPrimaryKey()) {
272 updateTable[nCtr].setBPrimary(true);
274 updateTable[nCtr].setSNewValue(data == null ? "" : data.toString());
277 createTable(container);
279 query = new Label(container, SWT.WRAP);
280 GridData gridData = new GridData();
281 gridData.horizontalSpan = 1;
282 gridData.horizontalAlignment = GridData.FILL;
283 gridData.verticalAlignment = GridData.FILL;
284 query.setLayoutData(gridData);
286 setControl(container);
289 setPageComplete(true);
291 public void updateQuery() {
292 System.out.println("Updating query"); //$NON-NLS-1$
293 StringBuffer setClause = new StringBuffer();
294 StringBuffer whereClause = new StringBuffer();
295 Bookmark bookmark = this.results.getBookmark();
296 Entity entity = this.results.getEntity();
297 DatabaseAdapter adapter = bookmark.getAdapter();
299 int numValuesSet = 0;
300 int numValuesWhere = 0;
301 for (int nCtr = 0; nCtr < columnNames.length; nCtr++) {
302 if (updateTable[nCtr].isBPrimary()) {
303 String value = updateTable[nCtr].getSOldValue();
304 if (numValuesWhere > 0) whereClause.append(" AND "); //$NON-NLS-1$
305 whereClause.append("("); //$NON-NLS-1$
306 whereClause.append(updateTable[nCtr].getSColNames());
307 whereClause.append(" = "); //$NON-NLS-1$
308 appendColumn(whereClause, entity, updateTable[nCtr].getSColNames(), adapter, value);
309 whereClause.append(")"); //$NON-NLS-1$
312 if (updateTable[nCtr].isBSetValue()) {
313 String value = updateTable[nCtr].getSNewValue();
314 if (numValuesSet > 0) setClause.append(", "); //$NON-NLS-1$
315 setClause.append(updateTable[nCtr].getSColNames());
316 setClause.append(" = "); //$NON-NLS-1$
317 appendColumn(setClause, entity, updateTable[nCtr].getSColNames(), adapter, value);
322 String query = "UPDATE " + this.results.getEntity().getQuotedTableName(); //$NON-NLS-1$
323 query += " SET " + setClause.toString(); //$NON-NLS-1$
324 query += " WHERE " + whereClause.toString(); //$NON-NLS-1$
325 this.query.setText(query);
329 * @see com.quantum.wizards.BaseSQLPage#getQueryText()
331 protected String getQueryText() {
332 return query.getText();
335 private void updateView() {
336 this.tableViewer.update(updateTable, null);
339 private void createTable(Composite composite) {
340 System.out.println("Creating table...");
341 int style = SWT.SINGLE | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.HIDE_SELECTION;
342 Table table = new Table(composite, style);
343 table.setHeaderVisible(true);
344 table.setLinesVisible(true);
345 table.setLayoutData(new GridData(GridData.FILL_BOTH | GridData.VERTICAL_ALIGN_BEGINNING));
347 colNames = new String[] { Messages.getString("UpdateRowPage.ColumnName"), Messages.getString("UpdateRowPage.OldValue"),
348 "Where", Messages.getString("UpdateRowPage.NewValue"), Messages.getString("UpdateRowPage.SetValue") };
350 createTableColumn(table, colNames[0], SWT.LEFT, 0, 150);
351 createTableColumn(table, colNames[1], SWT.LEFT, 1, 300);
352 createTableColumn(table, colNames[2], SWT.CENTER, 2, 60);
353 createTableColumn(table, colNames[3], SWT.LEFT, 3, 300);
354 createTableColumn(table, colNames[4], SWT.CENTER, 4, 70);
355 this.tableViewer = new TableViewer(table);
356 this.tableViewer.setColumnProperties(colNames);
358 CellEditor[] editor = new CellEditor[colNames.length];
359 TextCellEditor txtEditorField = new TextCellEditor(table);
360 txtEditorField.getControl().setEnabled(false);
361 editor[0] = txtEditorField;
363 TextCellEditor txtEditorFieldOld = new TextCellEditor(table);
364 txtEditorFieldOld.getControl().setEnabled(false);
365 editor[1] = txtEditorFieldOld;
367 editor[2] = new CheckboxCellEditor(table, SWT.NULL);
369 TextCellEditor txtEditorValues = new TextCellEditor(table);
370 editor[3] = txtEditorValues;
372 editor[4] = new CheckboxCellEditor(table, SWT.NULL);
374 this.tableViewer.setCellEditors(editor);
375 this.tableViewer.setLabelProvider(new LabelProviderImpl());
376 this.tableViewer.setContentProvider(new ContentProviderImpl());
377 this.tableViewer.setCellModifier(new CellModifierImpl());
378 this.tableViewer.setInput(updateTable);