dc90a00d38df3eeb7d9d085195ee22ac28f552e2
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / wizards / DeleteRowPage.java
1 package com.quantum.wizards;
2
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;
8
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;
20
21 public class DeleteRowPage extends BaseSQLPage implements SQLPage {
22         String[] columnNames;
23         Text[] values;
24         Button[] whereValues;
25         Text query;
26
27         public DeleteRowPage(String pageName) {
28                 super(pageName);
29         }
30         
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;
37
38                 if (row == null) {
39                         System.out.println("Row is null"); //$NON-NLS-1$
40                 }
41                 if (row.getColumnNames() == null) {
42                         System.out.println("Columns are null"); //$NON-NLS-1$
43                 }
44                 if (row.getTableData() == null) {
45                         System.out.println("Data is null"); //$NON-NLS-1$
46                 }
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$
53                 }
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]);
67
68                         values[i].addModifyListener(new ModifyListener() {
69                                 public void modifyText(ModifyEvent e) {
70                                         updateQuery();
71                                 }                               
72                         });
73                         
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);
79                         } else {
80                                 whereValues[i].setSelection(false);
81             }
82                         whereValues[i].addSelectionListener(new SelectionListener() {
83                                 public void widgetDefaultSelected(SelectionEvent e) {
84                                 }
85                                 public void widgetSelected(SelectionEvent e) {
86                                         updateQuery();
87                                 }
88                         });
89                 }
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);
99
100                 setControl(container);
101         updateQuery();
102        
103                 setPageComplete(true);
104         }
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());
111                 
112                 int numSelected = 0;
113                 for (int i = 0; i < columnNames.length; i++) {
114                         if (whereValues[i].getSelection()) {
115                                 if (numSelected > 0) whereClause.append(" AND "); //$NON-NLS-1$
116                                 numSelected++;
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$
122                         }
123                 }
124                 String query = "DELETE FROM " + row.getTable(); //$NON-NLS-1$
125                 if (numSelected > 0) {
126                         query += " WHERE " + whereClause.toString(); //$NON-NLS-1$
127                 }
128                 if (numSelected > 0) {
129                         setMessage(""); //$NON-NLS-1$
130                 } else {
131                         setMessage(Messages.getString("DeleteRowPage.WarningNoWhere")); //$NON-NLS-1$
132                 }
133                 this.query.setText(query);
134         }
135         
136     protected String getQueryText() {
137         return query.getText();
138     }
139 }