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