10d1fda8e157ee314240fc1841501aaa513c15de
[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.V_SCROLL);
34                 GridLayout layout = new GridLayout();
35                 container.setLayout(layout);
36                 layout.numColumns = 3;
37
38                 Entity entity = this.results.getEntity();
39                 columnNames = this.results.getColumnNames();
40
41                 values = new Text[columnNames.length];
42                 whereValues = new Button[columnNames.length];
43                 new Label(container, SWT.NULL).setText(Messages.getString("DeleteRowPage.ColumnName")); //$NON-NLS-1$
44                 new Label(container, SWT.NULL).setText(Messages.getString("DeleteRowPage.Value")); //$NON-NLS-1$
45                 new Label(container, SWT.NULL).setText(Messages.getString("DeleteRowPage.IncludeIn")); //$NON-NLS-1$
46                 for (int i = 0; i < columnNames.length; i++) {
47                         Label label = new Label(container, SWT.NULL);
48                         label.setText(columnNames[i]);
49                         values[i] = new Text(container, SWT.BORDER | SWT.SINGLE);
50                         Object object = this.row == null ? null : this.row.get(i+1);
51                         values[i].setText(object == null ? "" : object.toString());
52                         GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
53                         gridData.widthHint = 150;
54                         values[i].setLayoutData(gridData);
55                         values[i].addModifyListener(new ModifyListener() {
56                                 public void modifyText(ModifyEvent event) {
57                                         updateQuery();
58                                 }
59                         });
60                         
61                         whereValues[i] = new Button(container, SWT.CHECK);
62                         whereValues[i].setText(Messages.getString("DeleteRowPage.WhereClause")); //$NON-NLS-1$
63                         // we check if it's a primary key to select it in the WHERE clause
64                         Column column = getColumn(entity, columnNames[i]);
65             whereValues[i].setSelection(column == null ? false : column.isPrimaryKey());
66                         whereValues[i].addSelectionListener(new SelectionListener() {
67                                 public void widgetDefaultSelected(SelectionEvent e) {
68                                 }
69                                 public void widgetSelected(SelectionEvent e) {
70                                         updateQuery();
71                                 }
72                         });
73                 }
74                 query = new Text(container, SWT.MULTI | SWT.READ_ONLY | SWT.WRAP);
75                 GridData gridData = new GridData();
76                 gridData.horizontalSpan = layout.numColumns;
77                 gridData.verticalSpan = 3;
78                 gridData.horizontalAlignment = GridData.FILL;
79                 gridData.verticalAlignment = GridData.FILL;
80                 gridData.grabExcessHorizontalSpace = true;
81                 gridData.grabExcessVerticalSpace = true;
82                 query.setLayoutData(gridData);
83
84                 setControl(container);
85         updateQuery();
86        
87                 setPageComplete(true);
88         }
89         public void updateQuery() {
90                 System.out.println(Messages.getString("DeleteRowPage.UpdatingQuery")); //$NON-NLS-1$
91                 StringBuffer whereClause = new StringBuffer();
92                 Bookmark bookmark = this.results.getBookmark();
93                 Entity entity = this.results.getEntity();
94                 DatabaseAdapter adapter = bookmark.getAdapter();
95                 
96                 int numSelected = 0;
97                 for (int i = 0; i < columnNames.length; i++) {
98                         if (whereValues[i].getSelection()) {
99                                 if (numSelected > 0) whereClause.append(" AND "); //$NON-NLS-1$
100                                 numSelected++;
101                                 whereClause.append("("); //$NON-NLS-1$
102                                 whereClause.append(columnNames[i]);
103                                 whereClause.append(" = "); //$NON-NLS-1$
104                 appendColumn(whereClause, entity, columnNames[i], adapter, values[i].getText());
105                                 whereClause.append(")"); //$NON-NLS-1$
106                         }
107                 }
108                 String query = "DELETE FROM " + this.results.getEntity().getQuotedTableName(); //$NON-NLS-1$
109                 if (numSelected > 0) {
110                         query += " WHERE " + whereClause.toString(); //$NON-NLS-1$
111                 }
112                 if (numSelected > 0) {
113                         setMessage(""); //$NON-NLS-1$
114                 } else {
115                         setMessage(Messages.getString("DeleteRowPage.WarningNoWhere")); //$NON-NLS-1$
116                 }
117                 this.query.setText(query);
118         }
119         
120     protected String getQueryText() {
121         return query.getText();
122     }
123 }