Added missing EE to Java 1.4
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / php / wizards / PHPSelectRowPage.java
1
2 package com.quantum.php.wizards;
3
4 import java.text.MessageFormat;
5
6 import org.eclipse.jface.preference.IPreferenceStore;
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.events.SelectionEvent;
13 import org.eclipse.swt.events.SelectionListener;
14 import org.eclipse.swt.layout.GridData;
15 import org.eclipse.swt.layout.GridLayout;
16 import org.eclipse.swt.widgets.Button;
17 import org.eclipse.swt.widgets.Composite;
18 import org.eclipse.swt.widgets.Label;
19 import org.eclipse.swt.widgets.Text;
20
21 import com.quantum.QuantumPlugin;
22 import com.quantum.adapters.DatabaseAdapter;
23 import com.quantum.model.Bookmark;
24 import com.quantum.model.Column;
25 import com.quantum.model.Entity;
26 import com.quantum.php.PHPMessages;
27 import com.quantum.wizards.BaseSQLPage;
28 import com.quantum.wizards.SQLPage;
29
30 public class PHPSelectRowPage extends BaseSQLPage implements SQLPage {
31   String[] columnNames;
32
33   Text[] variableNames;
34
35   //    Text[] newValues;
36   Button[] primaryKeys;
37
38   Button[] selectValues;
39
40   Label query;
41
42   IPreferenceStore fStore;
43
44   public PHPSelectRowPage(String pageName) {
45     super(pageName);
46   }
47
48   public void createControl(Composite parent) {
49     System.out.println("page create control"); //$NON-NLS-1$
50     Composite container = new Composite(parent, SWT.V_SCROLL);
51     fStore = QuantumPlugin.getDefault().getPreferenceStore();
52     GridLayout layout = new GridLayout();
53     container.setLayout(layout);
54     Entity entity = this.results.getEntity();
55
56     int layoutColumns = 4;
57     layout.numColumns = layoutColumns;
58
59     columnNames = this.results.getColumnNames();
60     variableNames = new Text[columnNames.length];
61     //          newValues = new Text[columnNames.length];
62     primaryKeys = new Button[columnNames.length];
63     selectValues = new Button[columnNames.length];
64     Label temp = new Label(container, SWT.NULL);
65     temp.setText(PHPMessages.getString("PHPSelectRowPage.ColumnName")); //$NON-NLS-1$
66     temp = new Label(container, SWT.NULL);
67     temp.setText(PHPMessages.getString("PHPSelectRowPage.VariableName")); //$NON-NLS-1$
68     temp = new Label(container, SWT.NULL);
69     temp.setText(""); //$NON-NLS-1$
70     //          temp = new Label(container, SWT.NULL);
71     //          temp.setText(PHPMessages.getString("PHPSelectRowPage.NewValue"));
72     // //$NON-NLS-1$
73     temp = new Label(container, SWT.NULL);
74     temp.setText(PHPMessages.getString("PHPSelectRowPage._13")); //$NON-NLS-1$
75     for (int i = 0; i < columnNames.length; i++) {
76       Label label = new Label(container, SWT.NULL);
77       label.setText(columnNames[i]);
78       variableNames[i] = new Text(container, SWT.BORDER | SWT.SINGLE);
79       GridData gridData = new GridData();
80       gridData.widthHint = 100;
81       variableNames[i].setLayoutData(gridData);
82       Object data = this.row == null ? null : this.row.get(i + 1);
83       variableNames[i].setText(data == null ? "" : '$' + columnNames[i]);//data.toString());
84       variableNames[i].addModifyListener(new ModifyListener() {
85         public void modifyText(ModifyEvent e) {
86           updateQuery();
87         }
88       });
89       primaryKeys[i] = new Button(container, SWT.CHECK);
90       primaryKeys[i].setText("Where"); //$NON-NLS-1$
91       Column column = (entity == null) ? null : getColumn(entity,
92           columnNames[i]);
93       if (column != null && column.isPrimaryKey())
94         primaryKeys[i].setSelection(true);
95       primaryKeys[i].addSelectionListener(new SelectionListener() {
96         public void widgetDefaultSelected(SelectionEvent e) {
97         }
98
99         public void widgetSelected(SelectionEvent e) {
100           updateQuery();
101         }
102       });
103       //                        newValues[i] = new Text(container, SWT.BORDER | SWT.SINGLE);
104       //                        newValues[i].setText(data == null ? "" : '$'+data.toString());
105       //                        gridData = new GridData();
106       //                        gridData.widthHint = 100;
107       //                        newValues[i].setLayoutData(gridData);
108       //                        newValues[i].addModifyListener(new ModifyListener() {
109       //                                public void modifyText(ModifyEvent e) {
110       //                                        updateQuery();
111       //                                }
112       //                        });
113       selectValues[i] = new Button(container, SWT.CHECK);
114       selectValues[i].setText(PHPMessages
115           .getString("PHPSelectRowPage.SelectValue")); //$NON-NLS-1$
116       selectValues[i].addSelectionListener(new SelectionListener() {
117         public void widgetDefaultSelected(SelectionEvent e) {
118         }
119
120         public void widgetSelected(SelectionEvent e) {
121           updateQuery();
122         }
123       });
124     }
125     query = new Label(container, SWT.WRAP | SWT.V_SCROLL);
126     GridData gridData = new GridData();
127     gridData.horizontalSpan = layoutColumns;
128     gridData.horizontalAlignment = GridData.FILL;
129     gridData.verticalAlignment = GridData.FILL;
130     gridData.grabExcessHorizontalSpace = true;
131     gridData.grabExcessVerticalSpace = true;
132     query.setLayoutData(gridData);
133
134     setControl(container);
135     updateQuery();
136
137     setPageComplete(true);
138   }
139
140   public void updateQuery() {
141     System.out.println("Selecting query"); //$NON-NLS-1$
142     StringBuffer setClause = new StringBuffer();
143     StringBuffer whereClause = new StringBuffer();
144     Bookmark bookmark = this.results.getBookmark();
145     Entity entity = this.results.getEntity();
146     DatabaseAdapter adapter = bookmark.getAdapter();
147
148     int numValuesSet = 0;
149     int numValuesWhere = 0;
150     for (int i = 0; i < columnNames.length; i++) {
151       if (primaryKeys[i].getSelection()) {
152         String value = variableNames[i].getText();
153         if (numValuesWhere > 0)
154           whereClause.append(" AND "); //$NON-NLS-1$
155         whereClause.append("("); //$NON-NLS-1$
156         whereClause.append(columnNames[i]);
157         whereClause.append(" = "); //$NON-NLS-1$
158         appendColumn(whereClause, entity, columnNames[i], adapter, value);
159         whereClause.append(")"); //$NON-NLS-1$
160         numValuesWhere++;
161       }
162       if (selectValues[i].getSelection()) {
163         //                              String value = newValues[i].getText();
164         if (numValuesSet > 0)
165           setClause.append(", "); //$NON-NLS-1$
166         setClause.append(columnNames[i]);
167         //                              setClause.append(" = "); //$NON-NLS-1$
168         //                appendColumn(setClause, entity, columnNames[i], adapter, value);
169         numValuesSet++;
170       }
171     }
172     //          String query = "UPDATE " + this.results.getEntity().getQuotedTableName();
173     // //$NON-NLS-1$
174     //          query += " SET " + setClause.toString(); //$NON-NLS-1$
175     //          query += " WHERE " + whereClause.toString(); //$NON-NLS-1$
176     //          
177     String[] arguments = { setClause.toString(),
178         this.results.getEntity().getQuotedTableName(), whereClause.toString() };
179     MessageFormat form = new MessageFormat(fStore
180         .getString("phpeclipse.sql.select.template"));
181
182     String query = form.format(arguments);
183     //    String query = "$results = mysql_query(\"SELECT " + setClause.toString();
184     //    query += " FROM " + row.getTable();
185     //    query += " WHERE " + whereClause.toString() + "\");";
186     this.query.setText(query);
187
188   }
189
190   /*
191    * (non-Javadoc)
192    * 
193    * @see com.quantum.wizards.BaseSQLPage#getQueryText()
194    */
195   protected String getQueryText() {
196     return query.getText();
197   }
198
199   /*
200    * (non-Javadoc)
201    * 
202    * @see com.quantum.wizards.SQLPage#performFinish()
203    */
204   public boolean performFinish() {
205     QuantumPlugin.getDefault().getSysClip().setContents(
206         new Object[] { query.getText() },
207         new Transfer[] { TextTransfer.getInstance() });
208     return true;
209   }
210 }