misc changes
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.sql / src / net / sourceforge / phpdt / sql / wizards / PHPInsertRowPage.java
1 package net.sourceforge.phpdt.sql.wizards;
2
3 import java.text.MessageFormat;
4
5 import net.sourceforge.phpdt.sql.PHPEclipseSQLPlugin;
6 import net.sourceforge.phpdt.sql.sql.TableRow;
7 import net.sourceforge.phpdt.sql.view.PHPSourceConsole;
8 import net.sourceforge.phpdt.sql.view.tableview.TableAdapter;
9
10 import org.eclipse.jface.preference.IPreferenceStore;
11 import org.eclipse.jface.wizard.WizardPage;
12 import org.eclipse.swt.SWT;
13 import org.eclipse.swt.events.ModifyEvent;
14 import org.eclipse.swt.events.ModifyListener;
15 import org.eclipse.swt.layout.GridData;
16 import org.eclipse.swt.layout.GridLayout;
17 import org.eclipse.swt.widgets.Composite;
18 import org.eclipse.swt.widgets.Label;
19 import org.eclipse.swt.widgets.Text;
20
21 public class PHPInsertRowPage extends WizardPage implements SQLPage {
22   TableRow row;
23   String[] columnNames;
24   Text[] values;
25   Label query;
26   private IPreferenceStore fStore;
27   
28   public PHPInsertRowPage(String pageName) {
29     super(pageName);
30   }
31
32   public void init(TableRow row, TableAdapter adapter) {
33     this.row = row;
34   }
35
36   public void createControl(Composite parent) {
37     if (DEBUG) {
38       System.out.println("page create control");
39     }
40     fStore = PHPEclipseSQLPlugin.getDefault().getPreferenceStore();
41     Composite container = new Composite(parent, SWT.NULL);
42     GridLayout layout = new GridLayout();
43     container.setLayout(layout);
44     int layoutColumns = 2;
45     layout.numColumns = layoutColumns;
46
47     if (DEBUG) {
48       if (row == null) {
49         System.out.println("Row is null");
50       }
51       if (row.getColumnNames() == null) {
52         System.out.println("Columns are null");
53       }
54       if (row.getTableData() == null) {
55         System.out.println("Data is null");
56       }
57     }
58     columnNames = row.getColumnNames();
59     String[] data = row.getTableData();
60     if (DEBUG) {
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     }
66     values = new Text[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     for (int i = 0; i < row.getColumnCount(); i++) {
72       Label label = new Label(container, SWT.NULL);
73       label.setText(columnNames[i]);
74       values[i] = new Text(container, SWT.BORDER | SWT.SINGLE);
75       GridData fullHorizontal = new GridData();
76       fullHorizontal.horizontalAlignment = GridData.FILL;
77       values[i].setLayoutData(fullHorizontal);
78
79       //values[i].setText(data[i]);
80       values[i].addModifyListener(new ModifyListener() {
81         public void modifyText(ModifyEvent e) {
82           updateQuery();
83         }
84       });
85     }
86     query = new Label(container, SWT.WRAP);
87     GridData gridData = new GridData();
88     gridData.horizontalSpan = layoutColumns;
89     gridData.horizontalAlignment = GridData.FILL;
90     gridData.verticalAlignment = GridData.FILL;
91     gridData.grabExcessHorizontalSpace = true;
92     gridData.grabExcessVerticalSpace = true;
93     query.setLayoutData(gridData);
94
95     setControl(container);
96     updateQuery();
97
98     setPageComplete(true);
99   }
100   public void updateQuery() {
101     if (DEBUG) {
102       System.out.println("Updating insert query");
103     }
104     StringBuffer fieldClause = new StringBuffer();
105
106     StringBuffer valuesClause = new StringBuffer();
107     String text;
108     boolean first = false;
109     for (int i = 0; i < columnNames.length; i++) {
110       text = values[i].getText();
111       if (! text.equals("")) {
112         if (first) {
113           valuesClause.append(", ");
114           fieldClause.append(", ");
115         }
116         valuesClause.append("'"+values[i].getText()+"'");
117         fieldClause.append(columnNames[i]);
118         first = true;
119       }
120     }
121 //    if (valuesClause.length() > 1) {
122 //      valuesClause.deleteCharAt(valuesClause.length() - 1);
123 //      valuesClause.deleteCharAt(valuesClause.length() - 1);
124 //    }
125     String[] arguments = { row.getTable(), fieldClause.toString(), valuesClause.toString() };
126     MessageFormat form = new MessageFormat(fStore.getString("phpeclipse.sql.insert.template"));
127
128     String query = form.format(arguments);
129
130 //    String query = "$results = mysql_query(\"INSERT INTO " + row.getTable() + " (";
131 //    query += fieldClause.toString() + ") ";
132 //    query += " VALUES (" + valuesClause.toString();
133 //    query += ")\");";
134     this.query.setText(query);
135   }
136   public boolean performFinish() {
137     PHPSourceConsole console = PHPSourceConsole.getInstance();
138     console.clear();
139     console.print(query.getText());
140     return true;
141   }
142 }