package com.quantum.php.wizards; import java.text.MessageFormat; import org.eclipse.jface.preference.IPreferenceStore; import org.eclipse.swt.SWT; import org.eclipse.swt.dnd.TextTransfer; import org.eclipse.swt.dnd.Transfer; import org.eclipse.swt.events.ModifyEvent; import org.eclipse.swt.events.ModifyListener; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.events.SelectionListener; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Text; import com.quantum.QuantumPlugin; import com.quantum.adapters.DatabaseAdapter; import com.quantum.model.Bookmark; import com.quantum.model.Column; import com.quantum.model.Entity; import com.quantum.php.PHPMessages; import com.quantum.wizards.BaseSQLPage; import com.quantum.wizards.SQLPage; public class PHPDeleteRowPage extends BaseSQLPage implements SQLPage { String[] columnNames; Text[] values; Button[] whereValues; Text query; IPreferenceStore fStore; public PHPDeleteRowPage(String pageName) { super(pageName); } public void createControl(Composite parent) { System.out.println("page create control"); //$NON-NLS-1$ Composite container = new Composite(parent, SWT.V_SCROLL); fStore = QuantumPlugin.getDefault().getPreferenceStore(); GridLayout layout = new GridLayout(); container.setLayout(layout); layout.numColumns = 3; Entity entity = this.results.getEntity(); columnNames = this.results.getColumnNames(); values = new Text[columnNames.length]; whereValues = new Button[columnNames.length]; new Label(container, SWT.NULL).setText(PHPMessages .getString("PHPDeleteRowPage.ColumnName")); //$NON-NLS-1$ new Label(container, SWT.NULL).setText(PHPMessages .getString("PHPDeleteRowPage.Value")); //$NON-NLS-1$ new Label(container, SWT.NULL).setText(PHPMessages .getString("PHPDeleteRowPage.IncludeIn")); //$NON-NLS-1$ for (int i = 0; i < columnNames.length; i++) { Label label = new Label(container, SWT.NULL); label.setText(columnNames[i]); values[i] = new Text(container, SWT.BORDER | SWT.SINGLE); // Object object = this.row == null ? null : this.row.get(i + 1); // values[i].setText(object == null ? "" : '$'+columnNames[i]); //object.toString()); values[i].setText('$'+columnNames[i]); //object.toString()); GridData gridData = new GridData(GridData.FILL_HORIZONTAL); gridData.widthHint = 150; values[i].setLayoutData(gridData); values[i].addModifyListener(new ModifyListener() { public void modifyText(ModifyEvent event) { updateQuery(); } }); whereValues[i] = new Button(container, SWT.CHECK); whereValues[i].setText(PHPMessages .getString("PHPDeleteRowPage.WhereClause")); //$NON-NLS-1$ // we check if it's a primary key to select it in the WHERE clause Column column = getColumn(entity, columnNames[i]); whereValues[i].setSelection(column == null ? false : column .isPrimaryKey()); whereValues[i].addSelectionListener(new SelectionListener() { public void widgetDefaultSelected(SelectionEvent e) { } public void widgetSelected(SelectionEvent e) { updateQuery(); } }); } query = new Text(container, SWT.MULTI | SWT.READ_ONLY | SWT.WRAP); GridData gridData = new GridData(); gridData.horizontalSpan = layout.numColumns; gridData.verticalSpan = 3; gridData.horizontalAlignment = GridData.FILL; gridData.verticalAlignment = GridData.FILL; gridData.grabExcessHorizontalSpace = true; gridData.grabExcessVerticalSpace = true; query.setLayoutData(gridData); setControl(container); updateQuery(); setPageComplete(true); } public void updateQuery() { System.out.println(PHPMessages.getString("PHPDeleteRowPage.UpdatingQuery")); //$NON-NLS-1$ StringBuffer whereClause = new StringBuffer(); Bookmark bookmark = this.results.getBookmark(); Entity entity = this.results.getEntity(); DatabaseAdapter adapter = bookmark.getAdapter(); int numSelected = 0; for (int i = 0; i < columnNames.length; i++) { if (whereValues[i].getSelection()) { if (numSelected > 0) whereClause.append(" AND "); //$NON-NLS-1$ numSelected++; whereClause.append("("); //$NON-NLS-1$ whereClause.append(columnNames[i]); whereClause.append(" = "); //$NON-NLS-1$ appendColumn(whereClause, entity, columnNames[i], adapter, values[i] .getText()); whereClause.append(")"); //$NON-NLS-1$ } } // String query = "DELETE FROM " + // this.results.getEntity().getQuotedTableName(); //$NON-NLS-1$ // if (numSelected > 0) { // query += " WHERE " + whereClause.toString(); //$NON-NLS-1$ // } // if (numSelected > 0) { // setMessage(""); //$NON-NLS-1$ // } else { // setMessage(PHPMessages.getString("PHPDeleteRowPage.WarningNoWhere")); // //$NON-NLS-1$ // } String[] arguments = { this.results.getEntity().getQuotedTableName(), whereClause.toString() }; MessageFormat form = new MessageFormat(fStore .getString("phpeclipse.sql.delete.template")); String query = form.format(arguments); if (numSelected > 0) { setMessage(""); //$NON-NLS-1$ } else { setMessage(PHPMessages.getString("PHPDeleteRowPage.WarningNoWhere")); } this.query.setText(query); } protected String getQueryText() { return query.getText(); } /* * (non-Javadoc) * * @see com.quantum.wizards.SQLPage#performFinish() */ public boolean performFinish() { QuantumPlugin.getDefault().getSysClip().setContents( new Object[] { query.getText() }, new Transfer[] { TextTransfer.getInstance() }); return true; } } //import java.text.MessageFormat; // //import org.eclipse.jface.preference.IPreferenceStore; //import org.eclipse.jface.wizard.WizardPage; //import org.eclipse.swt.SWT; //import org.eclipse.swt.dnd.TextTransfer; //import org.eclipse.swt.dnd.Transfer; //import org.eclipse.swt.events.ModifyEvent; //import org.eclipse.swt.events.ModifyListener; //import org.eclipse.swt.events.SelectionEvent; //import org.eclipse.swt.events.SelectionListener; //import org.eclipse.swt.layout.GridData; //import org.eclipse.swt.layout.GridLayout; //import org.eclipse.swt.widgets.Button; //import org.eclipse.swt.widgets.Composite; //import org.eclipse.swt.widgets.Label; //import org.eclipse.swt.widgets.Text; // //import com.quantum.QuantumPlugin; //import com.quantum.sql.TableRow; //import com.quantum.wizards.BaseSQLPage; //import com.quantum.wizards.SQLPage; ////import com.quantum.view.PHPSourceConsole; ////import com.quantum.view.tableview.TableAdapter; // //public class PHPDeleteRowPage extends BaseSQLPage implements SQLPage { // TableRow row; // String[] columnNames; // Text[] values; // Button[] whereValues; // Label query; // IPreferenceStore fStore; // private final static boolean DEBUG = false; // // public PHPDeleteRowPage(String pageName) { // super(pageName); // } // //// public void init(TableRow row, TableAdapter adapter) { //// this.row = row; //// } // // protected String getQueryText() { // return query.getText(); // } // // public void createControl(Composite parent) { // System.out.println("page create control"); // fStore = QuantumPlugin.getDefault().getPreferenceStore(); // Composite container = new Composite(parent, SWT.NULL); // GridLayout layout = new GridLayout(); // container.setLayout(layout); // int layoutColumns = 3; // layout.numColumns = layoutColumns; // // if (DEBUG) { // if (row == null) { // System.out.println("Row is null"); // } // if (row.getColumnNames() == null) { // System.out.println("Columns are null"); // } // if (row.getTableData() == null) { // System.out.println("Data is null"); // } // } // columnNames = row.getColumnNames(); // String[] data = row.getTableData(); // if (DEBUG) { // for (int i = 0; i < row.getColumnCount(); i++) { // System.out.println("data = " + i + "=" + data[i]); // System.out.println("column = " + i + "=" + columnNames[i]); // } // } // values = new Text[row.getColumnCount()]; // whereValues = new Button[row.getColumnCount()]; // Label temp = new Label(container, SWT.NULL); // temp.setText("Column Name"); // temp = new Label(container, SWT.NULL); // temp.setText("Value"); // temp = new Label(container, SWT.NULL); // temp.setText("Include in?"); // for (int i = 0; i < row.getColumnCount(); i++) { // Label label = new Label(container, SWT.NULL); // label.setText(columnNames[i]); // values[i] = new Text(container, SWT.BORDER | SWT.SINGLE); // GridData fullHorizontal = new GridData(); // fullHorizontal.horizontalAlignment = GridData.FILL; // values[i].setLayoutData(fullHorizontal); // // if (data[i] == null || data[i].equals("")) { // values[i].setText('$' + columnNames[i]); // } else { // values[i].setText(data[i]); // } // // values[i].addModifyListener(new ModifyListener() { // public void modifyText(ModifyEvent e) { // updateQuery(); // } // }); // // whereValues[i] = new Button(container, SWT.CHECK); // whereValues[i].setText("Where clause"); // whereValues[i].addSelectionListener(new SelectionListener() { // public void widgetDefaultSelected(SelectionEvent e) { // } // public void widgetSelected(SelectionEvent e) { // updateQuery(); // } // }); // } // query = new Label(container, SWT.WRAP); // GridData gridData = new GridData(); // gridData.horizontalSpan = layoutColumns; // gridData.horizontalAlignment = GridData.FILL; // gridData.verticalAlignment = GridData.FILL; // gridData.grabExcessHorizontalSpace = true; // gridData.grabExcessVerticalSpace = true; // query.setLayoutData(gridData); // // setControl(container); // updateQuery(); // // setPageComplete(true); // } // public void updateQuery() { // if (DEBUG) { // System.out.println("Updating delete query"); // } // StringBuffer whereClause = new StringBuffer(); // int numSelected = 0; // boolean first = false; // for (int i = 0; i < columnNames.length; i++) { // if (whereValues[i].getSelection()) { // numSelected++; // if (first) { // whereClause.append(", "); // } // // whereClause.append(columnNames[i]); // whereClause.append(" = "); // whereClause.append("'" + values[i].getText() + "'"); // // first = true; // } // } // // if (whereClause.length() > 1) { // // whereClause.deleteCharAt(whereClause.length() - 1); // // whereClause.deleteCharAt(whereClause.length() - 1); // // } // // String[] arguments = { row.getTable(), whereClause.toString()}; // MessageFormat form = new // MessageFormat(fStore.getString("phpeclipse.sql.delete.template")); // // String query = form.format(arguments); // // // String query = "$results = mysql_query(\"DELETE FROM " + row.getTable(); // // if (numSelected > 0) { // // query += " WHERE " + whereClause.toString() + "\");"; // // } else { // // query += "\");"; // // } // // if (numSelected > 0) { // setMessage(""); // } else { // setMessage("Warning: no \"where clause\" columns selected, all rows will be // deleted"); // } // // this.getControl().pack(); // this.query.setText(query); // } // public boolean performFinish() { //// PHPSourceConsole console = PHPSourceConsole.getInstance(); //// console.clear(); //// console.print(query.getText()); // QuantumPlugin.getDefault().getSysClip().setContents( // new Object[] { query.getText() }, // new Transfer[] { TextTransfer.getInstance()}); // return true; // } //}