The image is now on a transparent background
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.sql / src / net / sourceforge / phpdt / sql / wizards / PHPDeleteRowPage.java
1 package net.sourceforge.phpdt.sql.wizards;
2
3 import java.text.MessageFormat;
4
5 import org.eclipse.jface.preference.IPreferenceStore;
6 import org.eclipse.jface.wizard.WizardPage;
7 import org.eclipse.swt.SWT;
8 import org.eclipse.swt.events.ModifyEvent;
9 import org.eclipse.swt.events.ModifyListener;
10 import org.eclipse.swt.events.SelectionEvent;
11 import org.eclipse.swt.events.SelectionListener;
12 import org.eclipse.swt.layout.GridData;
13 import org.eclipse.swt.layout.GridLayout;
14 import org.eclipse.swt.widgets.Button;
15 import org.eclipse.swt.widgets.Composite;
16 import org.eclipse.swt.widgets.Label;
17 import org.eclipse.swt.widgets.Text;
18
19 import net.sourceforge.phpdt.sql.PHPEclipseSQLPlugin;
20 import net.sourceforge.phpdt.sql.sql.TableRow;
21 import net.sourceforge.phpdt.sql.view.PHPSourceConsole;
22 import net.sourceforge.phpdt.sql.view.tableview.TableAdapter;
23
24 public class PHPDeleteRowPage extends WizardPage implements SQLPage {
25         TableRow row;
26         String[] columnNames;
27         Text[] values;
28         Button[] whereValues;
29         Label query;
30         IPreferenceStore fStore;
31
32         public PHPDeleteRowPage(String pageName) {
33                 super(pageName);
34         }
35
36         public void init(TableRow row, TableAdapter adapter) {
37                 this.row = row;
38         }
39
40         public void createControl(Composite parent) {
41                 System.out.println("page create control");
42                 fStore = PHPEclipseSQLPlugin.getDefault().getPreferenceStore();
43                 Composite container = new Composite(parent, SWT.NULL);
44                 GridLayout layout = new GridLayout();
45                 container.setLayout(layout);
46                 int layoutColumns = 3;
47                 layout.numColumns = layoutColumns;
48
49                 if (row == null) {
50                         System.out.println("Row is null");
51                 }
52                 if (row.getColumnNames() == null) {
53                         System.out.println("Columns are null");
54                 }
55                 if (row.getTableData() == null) {
56                         System.out.println("Data is null");
57                 }
58
59                 columnNames = row.getColumnNames();
60                 String[] data = row.getTableData();
61                 for (int i = 0; i < row.getColumnCount(); i++) {
62                         System.out.println("data = " + i + "=" + data[i]);
63                         System.out.println("column = " + i + "=" + columnNames[i]);
64                 }
65                 values = new Text[row.getColumnCount()];
66                 whereValues = new Button[row.getColumnCount()];
67                 Label temp = new Label(container, SWT.NULL);
68                 temp.setText("Column Name");
69                 temp = new Label(container, SWT.NULL);
70                 temp.setText("Value");
71                 temp = new Label(container, SWT.NULL);
72                 temp.setText("Include in?");
73                 for (int i = 0; i < row.getColumnCount(); i++) {
74                         Label label = new Label(container, SWT.NULL);
75                         label.setText(columnNames[i]);
76                         values[i] = new Text(container, SWT.BORDER | SWT.SINGLE);
77                         GridData fullHorizontal = new GridData();
78                         fullHorizontal.horizontalAlignment = GridData.FILL;
79                         values[i].setLayoutData(fullHorizontal);
80
81                         if (data[i] == null || data[i].equals("")) {
82                                 values[i].setText('$' + columnNames[i]);
83                         } else {
84                                 values[i].setText(data[i]);
85                         }
86
87                         values[i].addModifyListener(new ModifyListener() {
88                                 public void modifyText(ModifyEvent e) {
89                                         updateQuery();
90                                 }
91                         });
92
93                         whereValues[i] = new Button(container, SWT.CHECK);
94                         whereValues[i].setText("Where clause");
95                         whereValues[i].addSelectionListener(new SelectionListener() {
96                                 public void widgetDefaultSelected(SelectionEvent e) {
97                                 }
98                                 public void widgetSelected(SelectionEvent e) {
99                                         updateQuery();
100                                 }
101                         });
102                 }
103                 query = new Label(container, SWT.WRAP);
104                 GridData gridData = new GridData();
105                 gridData.horizontalSpan = layoutColumns;
106                 gridData.horizontalAlignment = GridData.FILL;
107                 gridData.verticalAlignment = GridData.FILL;
108                 gridData.grabExcessHorizontalSpace = true;
109                 gridData.grabExcessVerticalSpace = true;
110                 query.setLayoutData(gridData);
111
112                 setControl(container);
113                 updateQuery();
114
115                 setPageComplete(true);
116         }
117         public void updateQuery() {
118                 System.out.println("Updating delete query");
119                 StringBuffer whereClause = new StringBuffer();
120                 int numSelected = 0;
121                 boolean first = false;
122                 for (int i = 0; i < columnNames.length; i++) {
123                         if (whereValues[i].getSelection()) {
124                                 numSelected++;
125                                 if (first) {
126                                         whereClause.append(", ");
127                                 }
128
129                                 whereClause.append(columnNames[i]);
130                                 whereClause.append(" = ");
131                                 whereClause.append("'" + values[i].getText() + "'");
132
133                                 first = true;
134                         }
135                 }
136                 //    if (whereClause.length() > 1) {
137                 //      whereClause.deleteCharAt(whereClause.length() - 1);
138                 //      whereClause.deleteCharAt(whereClause.length() - 1);
139                 //    }
140
141                 String[] arguments = { row.getTable(), whereClause.toString()};
142                 MessageFormat form =
143                         new MessageFormat(
144                                 fStore.getString("phpeclipse.sql.delete.template"));
145
146                 String query = form.format(arguments);
147
148                 //        String query = "$results = mysql_query(\"DELETE FROM " + row.getTable();
149                 //        if (numSelected > 0) {
150                 //              query += " WHERE " + whereClause.toString() + "\");";
151                 //        } else {
152                 //              query += "\");";
153                 //        }
154
155                 if (numSelected > 0) {
156                         setMessage("");
157                 } else {
158                         setMessage("Warning: no \"where clause\" columns selected, all rows will be deleted");
159                 }
160
161                 this.getControl().pack();
162                 this.query.setText(query);
163         }
164         public boolean performFinish() {
165                 PHPSourceConsole console = PHPSourceConsole.getInstance();
166                 console.clear();
167                 console.print(query.getText());
168                 return true;
169         }
170 }