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.layout.GridData; import org.eclipse.swt.layout.GridLayout; 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.Entity; import com.quantum.php.PHPMessages; import com.quantum.wizards.BaseSQLPage; import com.quantum.wizards.SQLPage; public class PHPInsertRowPage extends BaseSQLPage implements SQLPage { String[] columnNames; Text[] values; Label query; int numColumns = 0; private IPreferenceStore fStore; public PHPInsertRowPage(String pageName) { super(pageName); } public void createControl(Composite parent) { System.out.println("page create control"); //$NON-NLS-1$ fStore = QuantumPlugin.getDefault().getPreferenceStore(); Composite container = new Composite(parent, SWT.V_SCROLL); GridLayout layout = new GridLayout(); container.setLayout(layout); layout.numColumns = 2; columnNames = this.results.getColumnNames(); values = new Text[columnNames.length]; new Label(container, SWT.NULL).setText(PHPMessages.getString("PHPInsertRowPage.ColumnName")); //$NON-NLS-1$ new Label(container, SWT.NULL).setText(PHPMessages.getString("PHPInsertRowPage.Value")); //$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); GridData gridData = new GridData(GridData.FILL_HORIZONTAL); gridData.widthHint = 150; values[i].setLayoutData(gridData); //values[i].setText(data[i]); values[i].addModifyListener(new ModifyListener() { public void modifyText(ModifyEvent e) { updateQuery(); } }); } query = new Label(container, SWT.WRAP); GridData gridData = new GridData(); gridData.horizontalSpan = layout.numColumns; 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("Updating query"); //$NON-NLS-1$ StringBuffer valuesClause = new StringBuffer(); StringBuffer namesClause = new StringBuffer(); Bookmark bookmark = this.results.getBookmark(); Entity entity = this.results.getEntity(); DatabaseAdapter adapter = bookmark.getAdapter(); numColumns = 0; for (int i = 0; i < columnNames.length; i++) { String name = columnNames[i]; String value = values[i].getText(); if (value.length() > 0) { if (numColumns > 0) { valuesClause.append(", "); //$NON-NLS-1$ namesClause.append(", "); } appendColumn(valuesClause, entity, name, adapter, value); namesClause.append(name); numColumns++; } } // String query = "INSERT INTO " + this.results.getEntity().getQuotedTableName(); //$NON-NLS-1$ // if (numColumns > 0) { // query += " (" + namesClause + ")"; // query += " VALUES " + "(" + valuesClause; //$NON-NLS-1$ // query += " )"; //$NON-NLS-1$ // } String[] arguments = { this.results.getEntity().getQuotedTableName(), namesClause.toString(), valuesClause.toString()}; MessageFormat form = new MessageFormat(fStore.getString("phpeclipse.sql.insert.template")); String query = form.format(arguments); // String query = "$results = mysql_query(\"INSERT INTO " + row.getTable() + " ("; // query += fieldClause.toString() + ") "; // query += " VALUES (" + valuesClause.toString(); // query += ")\");"; this.query.setText(query); } /* (non-Javadoc) * @see com.quantum.wizards.BaseSQLPage#getQueryText() */ protected String getQueryText() { return this.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; } } //package com.quantum.php.wizards; // //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.layout.GridData; //import org.eclipse.swt.layout.GridLayout; //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 PHPInsertRowPage extends BaseSQLPage implements SQLPage { // TableRow row; // String[] columnNames; // Text[] values; // Label query; // private final static boolean DEBUG = false; // private IPreferenceStore fStore; // // public PHPInsertRowPage(String pageName) { // super(pageName); // } // //// public void init(TableRow row, TableAdapter adapter) { //// this.row = row; //// } // // public void createControl(Composite parent) { // if (DEBUG) { // 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 = 2; // 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()]; // Label temp = new Label(container, SWT.NULL); // temp.setText("Column Name"); // temp = new Label(container, SWT.NULL); // temp.setText("Value"); // 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); // // //values[i].setText(data[i]); // values[i].addModifyListener(new ModifyListener() { // public void modifyText(ModifyEvent 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 insert query"); // } // StringBuffer fieldClause = new StringBuffer(); // // StringBuffer valuesClause = new StringBuffer(); // String text; // boolean first = false; // for (int i = 0; i < columnNames.length; i++) { // text = values[i].getText(); // if (!text.equals("")) { // if (first) { // valuesClause.append(", "); // fieldClause.append(", "); // } // valuesClause.append("'" + values[i].getText() + "'"); // fieldClause.append(columnNames[i]); // first = true; // } // } // // if (valuesClause.length() > 1) { // // valuesClause.deleteCharAt(valuesClause.length() - 1); // // valuesClause.deleteCharAt(valuesClause.length() - 1); // // } // String[] arguments = { row.getTable(), fieldClause.toString(), valuesClause.toString()}; // MessageFormat form = new MessageFormat(fStore.getString("phpeclipse.sql.insert.template")); // // String query = form.format(arguments); // // // String query = "$results = mysql_query(\"INSERT INTO " + row.getTable() + " ("; // // query += fieldClause.toString() + ") "; // // query += " VALUES (" + valuesClause.toString(); // // query += ")\");"; // 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; // } // /* (non-Javadoc) // * @see com.quantum.wizards.BaseSQLPage#getQueryText() // */ // protected String getQueryText() { // return this.query.getText(); // } //}