1 package com.quantum.php.wizards;
3 import java.text.MessageFormat;
5 import org.eclipse.jface.preference.IPreferenceStore;
6 import org.eclipse.swt.SWT;
7 import org.eclipse.swt.dnd.TextTransfer;
8 import org.eclipse.swt.dnd.Transfer;
9 import org.eclipse.swt.events.ModifyEvent;
10 import org.eclipse.swt.events.ModifyListener;
11 import org.eclipse.swt.events.SelectionEvent;
12 import org.eclipse.swt.events.SelectionListener;
13 import org.eclipse.swt.layout.GridData;
14 import org.eclipse.swt.layout.GridLayout;
15 import org.eclipse.swt.widgets.Button;
16 import org.eclipse.swt.widgets.Composite;
17 import org.eclipse.swt.widgets.Label;
18 import org.eclipse.swt.widgets.Text;
20 import com.quantum.QuantumPlugin;
21 import com.quantum.adapters.DatabaseAdapter;
22 import com.quantum.model.Bookmark;
23 import com.quantum.model.Column;
24 import com.quantum.model.Entity;
25 import com.quantum.php.PHPMessages;
26 import com.quantum.wizards.BaseSQLPage;
27 import com.quantum.wizards.SQLPage;
29 public class PHPDeleteRowPage extends BaseSQLPage implements SQLPage {
38 IPreferenceStore fStore;
40 public PHPDeleteRowPage(String pageName) {
44 public void createControl(Composite parent) {
45 System.out.println("page create control"); //$NON-NLS-1$
46 Composite container = new Composite(parent, SWT.V_SCROLL);
47 fStore = QuantumPlugin.getDefault().getPreferenceStore();
48 GridLayout layout = new GridLayout();
49 container.setLayout(layout);
50 layout.numColumns = 3;
52 Entity entity = this.results.getEntity();
53 columnNames = this.results.getColumnNames();
55 values = new Text[columnNames.length];
56 whereValues = new Button[columnNames.length];
57 new Label(container, SWT.NULL).setText(PHPMessages
58 .getString("PHPDeleteRowPage.ColumnName")); //$NON-NLS-1$
59 new Label(container, SWT.NULL).setText(PHPMessages
60 .getString("PHPDeleteRowPage.Value")); //$NON-NLS-1$
61 new Label(container, SWT.NULL).setText(PHPMessages
62 .getString("PHPDeleteRowPage.IncludeIn")); //$NON-NLS-1$
63 for (int i = 0; i < columnNames.length; i++) {
64 Label label = new Label(container, SWT.NULL);
65 label.setText(columnNames[i]);
66 values[i] = new Text(container, SWT.BORDER | SWT.SINGLE);
67 // Object object = this.row == null ? null : this.row.get(i + 1);
68 // values[i].setText(object == null ? "" : '$'+columnNames[i]); //object.toString());
69 values[i].setText('$'+columnNames[i]); //object.toString());
70 GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
71 gridData.widthHint = 150;
72 values[i].setLayoutData(gridData);
73 values[i].addModifyListener(new ModifyListener() {
74 public void modifyText(ModifyEvent event) {
79 whereValues[i] = new Button(container, SWT.CHECK);
80 whereValues[i].setText(PHPMessages
81 .getString("PHPDeleteRowPage.WhereClause")); //$NON-NLS-1$
82 // we check if it's a primary key to select it in the WHERE clause
83 Column column = getColumn(entity, columnNames[i]);
84 whereValues[i].setSelection(column == null ? false : column
86 whereValues[i].addSelectionListener(new SelectionListener() {
87 public void widgetDefaultSelected(SelectionEvent e) {
90 public void widgetSelected(SelectionEvent e) {
95 query = new Text(container, SWT.MULTI | SWT.READ_ONLY | SWT.WRAP);
96 GridData gridData = new GridData();
97 gridData.horizontalSpan = layout.numColumns;
98 gridData.verticalSpan = 3;
99 gridData.horizontalAlignment = GridData.FILL;
100 gridData.verticalAlignment = GridData.FILL;
101 gridData.grabExcessHorizontalSpace = true;
102 gridData.grabExcessVerticalSpace = true;
103 query.setLayoutData(gridData);
105 setControl(container);
108 setPageComplete(true);
111 public void updateQuery() {
112 System.out.println(PHPMessages.getString("PHPDeleteRowPage.UpdatingQuery")); //$NON-NLS-1$
113 StringBuffer whereClause = new StringBuffer();
114 Bookmark bookmark = this.results.getBookmark();
115 Entity entity = this.results.getEntity();
116 DatabaseAdapter adapter = bookmark.getAdapter();
119 for (int i = 0; i < columnNames.length; i++) {
120 if (whereValues[i].getSelection()) {
122 whereClause.append(" AND "); //$NON-NLS-1$
124 whereClause.append("("); //$NON-NLS-1$
125 whereClause.append(columnNames[i]);
126 whereClause.append(" = "); //$NON-NLS-1$
127 appendColumn(whereClause, entity, columnNames[i], adapter, values[i]
129 whereClause.append(")"); //$NON-NLS-1$
132 // String query = "DELETE FROM " +
133 // this.results.getEntity().getQuotedTableName(); //$NON-NLS-1$
134 // if (numSelected > 0) {
135 // query += " WHERE " + whereClause.toString(); //$NON-NLS-1$
137 // if (numSelected > 0) {
138 // setMessage(""); //$NON-NLS-1$
140 // setMessage(PHPMessages.getString("PHPDeleteRowPage.WarningNoWhere"));
143 String[] arguments = { this.results.getEntity().getQuotedTableName(),
144 whereClause.toString() };
145 MessageFormat form = new MessageFormat(fStore
146 .getString("phpeclipse.sql.delete.template"));
148 String query = form.format(arguments);
150 if (numSelected > 0) {
151 setMessage(""); //$NON-NLS-1$
153 setMessage(PHPMessages.getString("PHPDeleteRowPage.WarningNoWhere"));
156 this.query.setText(query);
159 protected String getQueryText() {
160 return query.getText();
166 * @see com.quantum.wizards.SQLPage#performFinish()
168 public boolean performFinish() {
169 QuantumPlugin.getDefault().getSysClip().setContents(
170 new Object[] { query.getText() },
171 new Transfer[] { TextTransfer.getInstance() });
175 //import java.text.MessageFormat;
177 //import org.eclipse.jface.preference.IPreferenceStore;
178 //import org.eclipse.jface.wizard.WizardPage;
179 //import org.eclipse.swt.SWT;
180 //import org.eclipse.swt.dnd.TextTransfer;
181 //import org.eclipse.swt.dnd.Transfer;
182 //import org.eclipse.swt.events.ModifyEvent;
183 //import org.eclipse.swt.events.ModifyListener;
184 //import org.eclipse.swt.events.SelectionEvent;
185 //import org.eclipse.swt.events.SelectionListener;
186 //import org.eclipse.swt.layout.GridData;
187 //import org.eclipse.swt.layout.GridLayout;
188 //import org.eclipse.swt.widgets.Button;
189 //import org.eclipse.swt.widgets.Composite;
190 //import org.eclipse.swt.widgets.Label;
191 //import org.eclipse.swt.widgets.Text;
193 //import com.quantum.QuantumPlugin;
194 //import com.quantum.sql.TableRow;
195 //import com.quantum.wizards.BaseSQLPage;
196 //import com.quantum.wizards.SQLPage;
197 ////import com.quantum.view.PHPSourceConsole;
198 ////import com.quantum.view.tableview.TableAdapter;
200 //public class PHPDeleteRowPage extends BaseSQLPage implements SQLPage {
202 // String[] columnNames;
204 // Button[] whereValues;
206 // IPreferenceStore fStore;
207 // private final static boolean DEBUG = false;
209 // public PHPDeleteRowPage(String pageName) {
213 //// public void init(TableRow row, TableAdapter adapter) {
217 // protected String getQueryText() {
218 // return query.getText();
221 // public void createControl(Composite parent) {
222 // System.out.println("page create control");
223 // fStore = QuantumPlugin.getDefault().getPreferenceStore();
224 // Composite container = new Composite(parent, SWT.NULL);
225 // GridLayout layout = new GridLayout();
226 // container.setLayout(layout);
227 // int layoutColumns = 3;
228 // layout.numColumns = layoutColumns;
231 // if (row == null) {
232 // System.out.println("Row is null");
234 // if (row.getColumnNames() == null) {
235 // System.out.println("Columns are null");
237 // if (row.getTableData() == null) {
238 // System.out.println("Data is null");
241 // columnNames = row.getColumnNames();
242 // String[] data = row.getTableData();
244 // for (int i = 0; i < row.getColumnCount(); i++) {
245 // System.out.println("data = " + i + "=" + data[i]);
246 // System.out.println("column = " + i + "=" + columnNames[i]);
249 // values = new Text[row.getColumnCount()];
250 // whereValues = new Button[row.getColumnCount()];
251 // Label temp = new Label(container, SWT.NULL);
252 // temp.setText("Column Name");
253 // temp = new Label(container, SWT.NULL);
254 // temp.setText("Value");
255 // temp = new Label(container, SWT.NULL);
256 // temp.setText("Include in?");
257 // for (int i = 0; i < row.getColumnCount(); i++) {
258 // Label label = new Label(container, SWT.NULL);
259 // label.setText(columnNames[i]);
260 // values[i] = new Text(container, SWT.BORDER | SWT.SINGLE);
261 // GridData fullHorizontal = new GridData();
262 // fullHorizontal.horizontalAlignment = GridData.FILL;
263 // values[i].setLayoutData(fullHorizontal);
265 // if (data[i] == null || data[i].equals("")) {
266 // values[i].setText('$' + columnNames[i]);
268 // values[i].setText(data[i]);
271 // values[i].addModifyListener(new ModifyListener() {
272 // public void modifyText(ModifyEvent e) {
277 // whereValues[i] = new Button(container, SWT.CHECK);
278 // whereValues[i].setText("Where clause");
279 // whereValues[i].addSelectionListener(new SelectionListener() {
280 // public void widgetDefaultSelected(SelectionEvent e) {
282 // public void widgetSelected(SelectionEvent e) {
287 // query = new Label(container, SWT.WRAP);
288 // GridData gridData = new GridData();
289 // gridData.horizontalSpan = layoutColumns;
290 // gridData.horizontalAlignment = GridData.FILL;
291 // gridData.verticalAlignment = GridData.FILL;
292 // gridData.grabExcessHorizontalSpace = true;
293 // gridData.grabExcessVerticalSpace = true;
294 // query.setLayoutData(gridData);
296 // setControl(container);
299 // setPageComplete(true);
301 // public void updateQuery() {
303 // System.out.println("Updating delete query");
305 // StringBuffer whereClause = new StringBuffer();
306 // int numSelected = 0;
307 // boolean first = false;
308 // for (int i = 0; i < columnNames.length; i++) {
309 // if (whereValues[i].getSelection()) {
312 // whereClause.append(", ");
315 // whereClause.append(columnNames[i]);
316 // whereClause.append(" = ");
317 // whereClause.append("'" + values[i].getText() + "'");
322 // // if (whereClause.length() > 1) {
323 // // whereClause.deleteCharAt(whereClause.length() - 1);
324 // // whereClause.deleteCharAt(whereClause.length() - 1);
327 // String[] arguments = { row.getTable(), whereClause.toString()};
328 // MessageFormat form = new
329 // MessageFormat(fStore.getString("phpeclipse.sql.delete.template"));
331 // String query = form.format(arguments);
333 // // String query = "$results = mysql_query(\"DELETE FROM " + row.getTable();
334 // // if (numSelected > 0) {
335 // // query += " WHERE " + whereClause.toString() + "\");";
337 // // query += "\");";
340 // if (numSelected > 0) {
343 // setMessage("Warning: no \"where clause\" columns selected, all rows will be
347 // this.getControl().pack();
348 // this.query.setText(query);
350 // public boolean performFinish() {
351 //// PHPSourceConsole console = PHPSourceConsole.getInstance();
352 //// console.clear();
353 //// console.print(query.getText());
354 // QuantumPlugin.getDefault().getSysClip().setContents(
355 // new Object[] { query.getText() },
356 // new Transfer[] { TextTransfer.getInstance()});