Added the PHP wizards again
[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 (DEBUG) {
50       if (row == null) {
51         System.out.println("Row is null");
52       }
53       if (row.getColumnNames() == null) {
54         System.out.println("Columns are null");
55       }
56       if (row.getTableData() == null) {
57         System.out.println("Data is null");
58       }
59     }
60     columnNames = row.getColumnNames();
61     String[] data = row.getTableData();
62     if (DEBUG) {
63       for (int i = 0; i < row.getColumnCount(); i++) {
64         System.out.println("data = " + i + "=" + data[i]);
65         System.out.println("column = " + i + "=" + columnNames[i]);
66       }
67     }
68     values = new Text[row.getColumnCount()];
69     whereValues = new Button[row.getColumnCount()];
70     Label temp = new Label(container, SWT.NULL);
71     temp.setText("Column Name");
72     temp = new Label(container, SWT.NULL);
73     temp.setText("Value");
74     temp = new Label(container, SWT.NULL);
75     temp.setText("Include in?");
76     for (int i = 0; i < row.getColumnCount(); i++) {
77       Label label = new Label(container, SWT.NULL);
78       label.setText(columnNames[i]);
79       values[i] = new Text(container, SWT.BORDER | SWT.SINGLE);
80       GridData fullHorizontal = new GridData();
81       fullHorizontal.horizontalAlignment = GridData.FILL;
82       values[i].setLayoutData(fullHorizontal);
83
84       if (data[i] == null || data[i].equals("")) {
85         values[i].setText('$' + columnNames[i]);
86       } else {
87         values[i].setText(data[i]);
88       }
89
90       values[i].addModifyListener(new ModifyListener() {
91         public void modifyText(ModifyEvent e) {
92           updateQuery();
93         }
94       });
95
96       whereValues[i] = new Button(container, SWT.CHECK);
97       whereValues[i].setText("Where clause");
98       whereValues[i].addSelectionListener(new SelectionListener() {
99         public void widgetDefaultSelected(SelectionEvent e) {
100         }
101         public void widgetSelected(SelectionEvent e) {
102           updateQuery();
103         }
104       });
105     }
106     query = new Label(container, SWT.WRAP);
107     GridData gridData = new GridData();
108     gridData.horizontalSpan = layoutColumns;
109     gridData.horizontalAlignment = GridData.FILL;
110     gridData.verticalAlignment = GridData.FILL;
111     gridData.grabExcessHorizontalSpace = true;
112     gridData.grabExcessVerticalSpace = true;
113     query.setLayoutData(gridData);
114
115     setControl(container);
116     updateQuery();
117
118     setPageComplete(true);
119   }
120   public void updateQuery() {
121     if (DEBUG) {
122       System.out.println("Updating delete query");
123     }
124     StringBuffer whereClause = new StringBuffer();
125     int numSelected = 0;
126     boolean first = false;
127     for (int i = 0; i < columnNames.length; i++) {
128       if (whereValues[i].getSelection()) {
129         numSelected++;
130         if (first) {
131           whereClause.append(", ");
132         }
133
134         whereClause.append(columnNames[i]);
135         whereClause.append(" = ");
136         whereClause.append("'" + values[i].getText() + "'");
137
138         first = true;
139       }
140     }
141     //    if (whereClause.length() > 1) {
142     //      whereClause.deleteCharAt(whereClause.length() - 1);
143     //      whereClause.deleteCharAt(whereClause.length() - 1);
144     //    }
145
146     String[] arguments = { row.getTable(), whereClause.toString()};
147     MessageFormat form = new MessageFormat(fStore.getString("phpeclipse.sql.delete.template"));
148
149     String query = form.format(arguments);
150
151     //    String query = "$results = mysql_query(\"DELETE FROM " + row.getTable();
152     //    if (numSelected > 0) {
153     //          query += " WHERE " + whereClause.toString() + "\");";
154     //    } else {
155     //          query += "\");";
156     //    }
157
158     if (numSelected > 0) {
159       setMessage("");
160     } else {
161       setMessage("Warning: no \"where clause\" columns selected, all rows will be deleted");
162     }
163
164     this.getControl().pack();
165     this.query.setText(query);
166   }
167   public boolean performFinish() {
168     PHPSourceConsole console = PHPSourceConsole.getInstance();
169     console.clear();
170     console.print(query.getText());
171     return true;
172   }
173 }