1 package net.sourceforge.phpeclipse.wizards.actions.metadata;
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.Table;
18 import org.eclipse.swt.widgets.TableItem;
19 import org.eclipse.swt.widgets.Text;
21 import com.quantum.ImageStore;
22 import com.quantum.Messages;
23 import com.quantum.adapters.DatabaseAdapter;
24 import com.quantum.model.Bookmark;
25 import com.quantum.model.Column;
26 import com.quantum.model.Entity;
27 import com.quantum.util.StringMatrix;
31 * @author Elvin E. Ebora
33 public class UpdateRowPage extends BaseSQLPage implements SQLPage {
35 class UpdateRowTableValues {
36 private String sColNames = null;
38 private String sOldValue = null;
40 private boolean bPrimary = false;
42 private String sNewValue = null;
44 private boolean bSetValue = false;
46 public UpdateRowTableValues() {
50 * @return Returns the bSetValue.
52 public boolean isBSetValue() {
58 * The bSetValue to set.
60 public void setBSetValue(boolean setValue) {
65 * @return Returns the bPrimary.
67 public boolean isBPrimary() {
73 * The bPrimary to set.
75 public void setBPrimary(boolean where) {
80 * @return Returns the sColNames.
82 public String getSColNames() {
88 * The sColNames to set.
90 public void setSColNames(String colNames) {
95 * @return Returns the sNewValue.
97 public String getSNewValue() {
103 * The sNewValue to set.
105 public void setSNewValue(String newValue) {
106 sNewValue = newValue;
110 * @return Returns the sOldValue.
112 public String getSOldValue() {
118 * The sOldValue to set.
120 public void setSOldValue(String oldValue) {
121 sOldValue = oldValue;
125 class LabelProviderImpl implements ITableLabelProvider {
126 public Image getColumnImage(Object element, int columnIndex) {
127 if (columnIndex == 2) {
128 return ((UpdateRowTableValues) element).isBPrimary() ? imgCheck : imgUncheck;
129 } else if (columnIndex == 4) {
130 return ((UpdateRowTableValues) element).isBSetValue() ? imgCheck : imgUncheck;
136 public String getColumnText(Object element, int columnIndex) {
138 UpdateRowTableValues updateRow = (UpdateRowTableValues) element;
139 switch (columnIndex) {
140 case 0: // column names
141 sReturn = updateRow.getSColNames();
143 case 1: // old values
144 sReturn = updateRow.getSOldValue();
146 case 2: // set checkbox
149 sReturn = updateRow.getSNewValue();
151 case 4: // set value checkbox
159 public void addListener(ILabelProviderListener listener) {
162 public void dispose() {
165 public boolean isLabelProperty(Object element, String property) {
169 public void removeListener(ILabelProviderListener listener) {
173 class ContentProviderImpl implements IStructuredContentProvider {
174 public Object[] getElements(Object inputElement) {
178 public void dispose() {
181 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
185 class CellModifierImpl implements ICellModifier {
187 public boolean canModify(Object element, String property) {
191 public Object getValue(Object element, String property) {
192 System.out.println("getValue called");
194 // Find the index of the column
195 int colIndx = comUI.getColumnNamesAsList(colNames).indexOf(property);
196 System.out.println("colIndx : " + colIndx);
198 Object rResult = null;
199 UpdateRowTableValues updateVal = (UpdateRowTableValues) element;
202 case 0: // column names
203 rResult = updateVal.getSColNames();
205 case 1: // old values
206 rResult = updateVal.getSOldValue();
208 case 2: // set checkbox
209 rResult = new Boolean(updateVal.isBPrimary());
212 rResult = updateVal.getSNewValue();
214 case 4: // set value checkbox
215 rResult = new Boolean(updateVal.isBSetValue());
224 public void modify(Object element, String property, Object value) {
225 int colIndx = comUI.getColumnNamesAsList(colNames).indexOf(property);
227 TableItem item = (TableItem) element;
228 UpdateRowTableValues updateVal = (UpdateRowTableValues) item.getData();
231 case 0: // column names
233 case 1: // old values
235 case 2: // set checkbox
236 updateVal.setBPrimary(((Boolean) value).booleanValue());
239 updateVal.setSNewValue(value.toString());
241 case 4: // set value checkbox
242 updateVal.setBSetValue(((Boolean) value).booleanValue());
253 String[] columnNames;
259 UpdateRowTableValues[] updateTable = null;
261 CommonWizardUI comUI;
263 TableViewer tableViewer = null;
265 static Image imgCheck = null;
267 static Image imgUncheck = null;
270 imgCheck = ImageStore.getImage(ImageStore.CHECKED);
271 imgUncheck = ImageStore.getImage(ImageStore.UNCHECKED);
274 public UpdateRowPage(String pageName) {
278 public void createControl(Composite parent) {
279 System.out.println("page create control"); //$NON-NLS-1$
280 Composite container = new Composite(parent, SWT.NULL);
281 container.setLayout(new GridLayout());
282 container.setLayoutData(new GridData(GridData.FILL_BOTH | GridData.VERTICAL_ALIGN_BEGINNING));
284 // Entity entity = this.results.getEntity();
286 comUI = new CommonWizardUI();
288 // init values to be displayed on the table
290 // columnNames = this.results.getColumnNames();
291 int nLen = this.columns.length;
292 columnNames = new String[nLen];
293 for (int i = 0; i < this.columns.length; i++) {
294 columnNames[i] = this.columns[i].getName();
296 updateTable = new UpdateRowTableValues[nLen];
298 for (int nCtr = 0; nCtr < nLen; nCtr++) {
299 updateTable[nCtr] = new UpdateRowTableValues();
300 updateTable[nCtr].setSColNames(columnNames[nCtr]);
301 Object data = this.row == null ? null : this.row.get(nCtr + 1);
302 updateTable[nCtr].setSOldValue(data == null ? "" : data.toString());
303 Column column = null; //(entity == null) ? null : getColumn(entity, columnNames[nCtr]);
304 if (column != null && column.isPrimaryKey()) {
305 updateTable[nCtr].setBPrimary(true);
307 updateTable[nCtr].setSNewValue(data == null ? "" : data.toString());
310 createTable(container);
312 //query = new Label(container, SWT.WRAP);
313 query = new Text(container, SWT.V_SCROLL | SWT.MULTI | SWT.WRAP | SWT.READ_ONLY);
314 query.setLayoutData(comUI.createGridData(1, 10, GridData.FILL));
315 query.setVisible(false);
317 setControl(container);
320 setPageComplete(true);
323 public void updateQuery() {
324 System.out.println("Updating query"); //$NON-NLS-1$
325 query.setVisible(true);
327 StringMatrix columns = new StringMatrix();
328 for (int i = 0; i < columnNames.length; i++) {
329 if (updateTable[i].isBSetValue()) {
330 columns.addHeader(updateTable[i].getSColNames());
331 columns.add(updateTable[i].getSNewValue(), 0);
334 StringMatrix key = new StringMatrix();
335 for (int i = 0; i < columnNames.length; i++) {
336 if (updateTable[i].isBPrimary()) {
337 key.addHeader(updateTable[i].getSColNames());
338 // It's an old value because it't the key.
339 key.add(updateTable[i].getSOldValue(), 0);
342 // Bookmark bookmark = (Bookmark) this.results.getConnectable();
343 // DatabaseAdapter adapter = bookmark.getAdapter();
344 // this.query.setText(adapter.buildUpdate(this.results.getEntity(), columns, key));
350 * @see com.quantum.wizards.BaseSQLPage#getQueryText()
352 protected String getQueryText() {
353 return query.getText();
356 private void updateView() {
357 this.tableViewer.update(updateTable, null);
360 private void createTable(Composite composite) {
361 System.out.println("Creating table...");
362 Table table = comUI.createTablePage(composite);
363 colNames = new String[] {
364 Messages.getString("UpdateRowPage.ColumnName"),
365 Messages.getString("UpdateRowPage.OldValue"),
367 Messages.getString("UpdateRowPage.NewValue"),
368 Messages.getString("UpdateRowPage.SetValue") };
370 comUI.createTableColumn(table, colNames[0], SWT.LEFT, 0, 150);
371 comUI.createTableColumn(table, colNames[1], SWT.LEFT, 1, 300);
372 comUI.createTableColumn(table, colNames[2], SWT.CENTER, 2, 60);
373 comUI.createTableColumn(table, colNames[3], SWT.LEFT, 3, 300);
374 comUI.createTableColumn(table, colNames[4], SWT.CENTER, 4, 70);
375 this.tableViewer = new TableViewer(table);
376 this.tableViewer.setColumnProperties(colNames);
378 CellEditor[] editor = new CellEditor[colNames.length];
379 TextCellEditor txtEditorField = new TextCellEditor(table);
380 txtEditorField.getControl().setEnabled(false);
381 editor[0] = txtEditorField;
383 TextCellEditor txtEditorFieldOld = new TextCellEditor(table);
384 txtEditorFieldOld.getControl().setEnabled(false);
385 editor[1] = txtEditorFieldOld;
387 editor[2] = new CheckboxCellEditor(table, SWT.NULL);
389 TextCellEditor txtEditorValues = new TextCellEditor(table);
390 editor[3] = txtEditorValues;
392 editor[4] = new CheckboxCellEditor(table, SWT.NULL);
394 this.tableViewer.setCellEditors(editor);
395 this.tableViewer.setLabelProvider(new LabelProviderImpl());
396 this.tableViewer.setContentProvider(new ContentProviderImpl());
397 this.tableViewer.setCellModifier(new CellModifierImpl());
398 this.tableViewer.setInput(updateTable);