1 package com.quantum.wizards;
3 import org.eclipse.swt.SWT;
4 import org.eclipse.swt.events.ModifyEvent;
5 import org.eclipse.swt.events.ModifyListener;
6 import org.eclipse.swt.events.SelectionEvent;
7 import org.eclipse.swt.events.SelectionListener;
8 import org.eclipse.swt.layout.GridData;
9 import org.eclipse.swt.layout.GridLayout;
10 import org.eclipse.swt.widgets.Button;
11 import org.eclipse.swt.widgets.Composite;
12 import org.eclipse.swt.widgets.Label;
13 import org.eclipse.swt.widgets.Text;
15 import com.quantum.Messages;
16 import com.quantum.adapters.DatabaseAdapter;
17 import com.quantum.model.Bookmark;
18 import com.quantum.model.Column;
19 import com.quantum.model.Entity;
21 public class DeleteRowPage extends BaseSQLPage implements SQLPage {
27 public DeleteRowPage(String pageName) {
31 public void createControl(Composite parent) {
32 System.out.println("page create control"); //$NON-NLS-1$
33 Composite container = new Composite(parent, SWT.NULL);
34 GridLayout layout = new GridLayout();
35 container.setLayout(layout);
36 layout.numColumns = 3;
39 System.out.println("Row is null"); //$NON-NLS-1$
41 if (row.getColumnNames() == null) {
42 System.out.println("Columns are null"); //$NON-NLS-1$
44 if (row.getTableData() == null) {
45 System.out.println("Data is null"); //$NON-NLS-1$
47 Entity entity = row.getEntity();
48 columnNames = row.getColumnNames();
49 String[] data = row.getTableData();
50 for (int i = 0; i < row.getColumnCount(); i++) {
51 System.out.println("data = " + i + "=" + data[i]); //$NON-NLS-1$ //$NON-NLS-2$
52 System.out.println("column = " + i + "=" + columnNames[i]); //$NON-NLS-1$ //$NON-NLS-2$
54 values = new Text[row.getColumnCount()];
55 whereValues = new Button[row.getColumnCount()];
56 new Label(container, SWT.NULL).setText(Messages.getString("DeleteRowPage.ColumnName")); //$NON-NLS-1$
57 new Label(container, SWT.NULL).setText(Messages.getString("DeleteRowPage.Value")); //$NON-NLS-1$
58 new Label(container, SWT.NULL).setText(Messages.getString("DeleteRowPage.IncludeIn")); //$NON-NLS-1$
59 for (int i = 0; i < row.getColumnCount(); i++) {
60 Label label = new Label(container, SWT.NULL);
61 label.setText(columnNames[i]);
62 values[i] = new Text(container, SWT.BORDER | SWT.SINGLE);
63 GridData fullHorizontal = new GridData();
64 fullHorizontal.horizontalAlignment = GridData.FILL;
65 values[i].setLayoutData(fullHorizontal);
66 values[i].setText(data[i]);
68 values[i].addModifyListener(new ModifyListener() {
69 public void modifyText(ModifyEvent e) {
74 whereValues[i] = new Button(container, SWT.CHECK);
75 whereValues[i].setText(Messages.getString("DeleteRowPage.WhereClause")); //$NON-NLS-1$
76 // we check if it's a primary key to select it in the WHERE clause
77 Column column = getColumn(entity, columnNames[i]);
78 whereValues[i].setSelection(column == null ? false : column.isPrimaryKey());
79 whereValues[i].addSelectionListener(new SelectionListener() {
80 public void widgetDefaultSelected(SelectionEvent e) {
82 public void widgetSelected(SelectionEvent e) {
87 query = new Text(container, SWT.MULTI | SWT.READ_ONLY | SWT.WRAP);
88 GridData gridData = new GridData();
89 gridData.horizontalSpan = layout.numColumns;
90 gridData.verticalSpan = 3;
91 gridData.horizontalAlignment = GridData.FILL;
92 gridData.verticalAlignment = GridData.FILL;
93 gridData.grabExcessHorizontalSpace = true;
94 gridData.grabExcessVerticalSpace = true;
95 query.setLayoutData(gridData);
97 setControl(container);
100 setPageComplete(true);
102 public void updateQuery() {
103 System.out.println(Messages.getString("DeleteRowPage.UpdatingQuery")); //$NON-NLS-1$
104 StringBuffer whereClause = new StringBuffer();
105 Bookmark bookmark = row.getBookmark();
106 Entity entity = row.getEntity();
107 DatabaseAdapter adapter = bookmark.getAdapter();
110 for (int i = 0; i < columnNames.length; i++) {
111 if (whereValues[i].getSelection()) {
112 if (numSelected > 0) whereClause.append(" AND "); //$NON-NLS-1$
114 whereClause.append("("); //$NON-NLS-1$
115 whereClause.append(columnNames[i]);
116 whereClause.append(" = "); //$NON-NLS-1$
117 appendColumn(whereClause, entity, columnNames[i], adapter, values[i].getText());
118 whereClause.append(")"); //$NON-NLS-1$
121 String query = "DELETE FROM " + row.getTable(); //$NON-NLS-1$
122 if (numSelected > 0) {
123 query += " WHERE " + whereClause.toString(); //$NON-NLS-1$
125 if (numSelected > 0) {
126 setMessage(""); //$NON-NLS-1$
128 setMessage(Messages.getString("DeleteRowPage.WarningNoWhere")); //$NON-NLS-1$
130 this.query.setText(query);
133 protected String getQueryText() {
134 return query.getText();