1) Added missing strings for italic, underline and strike through.
[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 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.layout.GridData;
11 import org.eclipse.swt.layout.GridLayout;
12 import org.eclipse.swt.widgets.Composite;
13 import org.eclipse.swt.widgets.Label;
14 import org.eclipse.swt.widgets.Text;
15
16 import net.sourceforge.phpdt.sql.PHPEclipseSQLPlugin;
17 import net.sourceforge.phpdt.sql.sql.TableRow;
18 import net.sourceforge.phpdt.sql.view.PHPSourceConsole;
19 import net.sourceforge.phpdt.sql.view.tableview.TableAdapter;
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
59     columnNames = row.getColumnNames();
60     String[] data = row.getTableData();
61     if (DEBUG) {
62       for (int i = 0; i < row.getColumnCount(); i++) {
63         System.out.println("data = " + i + "=" + data[i]);
64         System.out.println("column = " + i + "=" + columnNames[i]);
65       }
66     }
67     values = new Text[row.getColumnCount()];
68     Label temp = new Label(container, SWT.NULL);
69     temp.setText("Column Name");
70     temp = new Label(container, SWT.NULL);
71     temp.setText("Value");
72     for (int i = 0; i < row.getColumnCount(); i++) {
73       Label label = new Label(container, SWT.NULL);
74       label.setText(columnNames[i]);
75       values[i] = new Text(container, SWT.BORDER | SWT.SINGLE);
76       GridData fullHorizontal = new GridData();
77       fullHorizontal.horizontalAlignment = GridData.FILL;
78       values[i].setLayoutData(fullHorizontal);
79
80       //values[i].setText(data[i]);
81       values[i].addModifyListener(new ModifyListener() {
82         public void modifyText(ModifyEvent e) {
83           updateQuery();
84         }
85       });
86     }
87     query = new Label(container, SWT.WRAP);
88     GridData gridData = new GridData();
89     gridData.horizontalSpan = layoutColumns;
90     gridData.horizontalAlignment = GridData.FILL;
91     gridData.verticalAlignment = GridData.FILL;
92     gridData.grabExcessHorizontalSpace = true;
93     gridData.grabExcessVerticalSpace = true;
94     query.setLayoutData(gridData);
95
96     setControl(container);
97     updateQuery();
98
99     setPageComplete(true);
100   }
101   public void updateQuery() {
102     if (DEBUG) {
103       System.out.println("Updating insert query");
104     }
105     StringBuffer fieldClause = new StringBuffer();
106
107     StringBuffer valuesClause = new StringBuffer();
108     String text;
109     boolean first = false;
110     for (int i = 0; i < columnNames.length; i++) {
111       text = values[i].getText();
112       if (!text.equals("")) {
113         if (first) {
114           valuesClause.append(", ");
115           fieldClause.append(", ");
116         }
117         valuesClause.append("'" + values[i].getText() + "'");
118         fieldClause.append(columnNames[i]);
119         first = true;
120       }
121     }
122     //    if (valuesClause.length() > 1) {
123     //      valuesClause.deleteCharAt(valuesClause.length() - 1);
124     //      valuesClause.deleteCharAt(valuesClause.length() - 1);
125     //    }
126     String[] arguments = { row.getTable(), fieldClause.toString(), valuesClause.toString()};
127     MessageFormat form = new MessageFormat(fStore.getString("phpeclipse.sql.insert.template"));
128
129     String query = form.format(arguments);
130
131     //    String query = "$results = mysql_query(\"INSERT INTO " + row.getTable() + " (";
132     //    query += fieldClause.toString() + ") ";
133     //    query += " VALUES (" + valuesClause.toString();
134     //    query += ")\");";
135     this.query.setText(query);
136   }
137   public boolean performFinish() {
138     PHPSourceConsole console = PHPSourceConsole.getInstance();
139     console.clear();
140     console.print(query.getText());
141     return true;
142   }
143 }