1 package com.quantum.wizards;
3 import com.quantum.Messages;
4 import com.quantum.adapters.AdapterFactory;
5 import com.quantum.adapters.DatabaseAdapter;
6 import com.quantum.model.Bookmark;
7 import com.quantum.model.Entity;
9 import org.eclipse.swt.SWT;
10 import org.eclipse.swt.events.ModifyEvent;
11 import org.eclipse.swt.events.ModifyListener;
12 import org.eclipse.swt.events.SelectionEvent;
13 import org.eclipse.swt.events.SelectionListener;
14 import org.eclipse.swt.layout.GridData;
15 import org.eclipse.swt.layout.GridLayout;
16 import org.eclipse.swt.widgets.Button;
17 import org.eclipse.swt.widgets.Composite;
18 import org.eclipse.swt.widgets.Label;
19 import org.eclipse.swt.widgets.Text;
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 if (entity != null && entity.getColumn(columnNames[i]).isPrimaryKey()) {
78 whereValues[i].setSelection(true);
80 whereValues[i].setSelection(false);
82 whereValues[i].addSelectionListener(new SelectionListener() {
83 public void widgetDefaultSelected(SelectionEvent e) {
85 public void widgetSelected(SelectionEvent e) {
90 query = new Text(container, SWT.MULTI | SWT.READ_ONLY | SWT.WRAP);
91 GridData gridData = new GridData();
92 gridData.horizontalSpan = layout.numColumns;
93 gridData.verticalSpan = 3;
94 gridData.horizontalAlignment = GridData.FILL;
95 gridData.verticalAlignment = GridData.FILL;
96 gridData.grabExcessHorizontalSpace = true;
97 gridData.grabExcessVerticalSpace = true;
98 query.setLayoutData(gridData);
100 setControl(container);
103 setPageComplete(true);
105 public void updateQuery() {
106 System.out.println(Messages.getString("DeleteRowPage.UpdatingQuery")); //$NON-NLS-1$
107 StringBuffer whereClause = new StringBuffer();
108 Bookmark bookmark = row.getBookmark();
109 Entity entity = row.getEntity();
110 DatabaseAdapter adapter = AdapterFactory.getInstance().getAdapter(bookmark.getType());
113 for (int i = 0; i < columnNames.length; i++) {
114 if (whereValues[i].getSelection()) {
115 if (numSelected > 0) whereClause.append(" AND "); //$NON-NLS-1$
117 whereClause.append("("); //$NON-NLS-1$
118 whereClause.append(columnNames[i]);
119 whereClause.append(" = "); //$NON-NLS-1$
120 appendColumn(whereClause, entity, columnNames[i], adapter, values[i].getText());
121 whereClause.append(")"); //$NON-NLS-1$
124 String query = "DELETE FROM " + row.getTable(); //$NON-NLS-1$
125 if (numSelected > 0) {
126 query += " WHERE " + whereClause.toString(); //$NON-NLS-1$
128 if (numSelected > 0) {
129 setMessage(""); //$NON-NLS-1$
131 setMessage(Messages.getString("DeleteRowPage.WarningNoWhere")); //$NON-NLS-1$
133 this.query.setText(query);
136 protected String getQueryText() {
137 return query.getText();