e259f7616336957968aea40f63c9d3e9bb7d2dd1
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / wizards / DeleteRowPage.java
1 package com.quantum.wizards;
2
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;
14
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;
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                         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) {
81                                 }
82                                 public void widgetSelected(SelectionEvent e) {
83                                         updateQuery();
84                                 }
85                         });
86                 }
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);
96
97                 setControl(container);
98         updateQuery();
99        
100                 setPageComplete(true);
101         }
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();
108                 
109                 int numSelected = 0;
110                 for (int i = 0; i < columnNames.length; i++) {
111                         if (whereValues[i].getSelection()) {
112                                 if (numSelected > 0) whereClause.append(" AND "); //$NON-NLS-1$
113                                 numSelected++;
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$
119                         }
120                 }
121                 String query = "DELETE FROM " + row.getTable(); //$NON-NLS-1$
122                 if (numSelected > 0) {
123                         query += " WHERE " + whereClause.toString(); //$NON-NLS-1$
124                 }
125                 if (numSelected > 0) {
126                         setMessage(""); //$NON-NLS-1$
127                 } else {
128                         setMessage(Messages.getString("DeleteRowPage.WarningNoWhere")); //$NON-NLS-1$
129                 }
130                 this.query.setText(query);
131         }
132         
133     protected String getQueryText() {
134         return query.getText();
135     }
136 }