fixed "bracket matching highlighting" bug
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / php / wizards / PHPDeleteRowPage.java
1 package com.quantum.php.wizards;
2
3 import java.text.MessageFormat;
4
5 import org.eclipse.jface.preference.IPreferenceStore;
6 import org.eclipse.swt.SWT;
7 import org.eclipse.swt.dnd.TextTransfer;
8 import org.eclipse.swt.dnd.Transfer;
9 import org.eclipse.swt.events.ModifyEvent;
10 import org.eclipse.swt.events.ModifyListener;
11 import org.eclipse.swt.events.SelectionEvent;
12 import org.eclipse.swt.events.SelectionListener;
13 import org.eclipse.swt.layout.GridData;
14 import org.eclipse.swt.layout.GridLayout;
15 import org.eclipse.swt.widgets.Button;
16 import org.eclipse.swt.widgets.Composite;
17 import org.eclipse.swt.widgets.Label;
18 import org.eclipse.swt.widgets.Text;
19
20 import com.quantum.QuantumPlugin;
21 import com.quantum.adapters.DatabaseAdapter;
22 import com.quantum.model.Bookmark;
23 import com.quantum.model.Column;
24 import com.quantum.model.Entity;
25 import com.quantum.php.PHPMessages;
26 import com.quantum.wizards.BaseSQLPage;
27 import com.quantum.wizards.SQLPage;
28
29 public class PHPDeleteRowPage extends BaseSQLPage implements SQLPage {
30   String[] columnNames;
31
32   Text[] values;
33
34   Button[] whereValues;
35
36   Text query;
37
38   IPreferenceStore fStore;
39
40   public PHPDeleteRowPage(String pageName) {
41     super(pageName);
42   }
43
44   public void createControl(Composite parent) {
45     System.out.println("page create control"); //$NON-NLS-1$
46     Composite container = new Composite(parent, SWT.V_SCROLL);
47     fStore = QuantumPlugin.getDefault().getPreferenceStore();
48     GridLayout layout = new GridLayout();
49     container.setLayout(layout);
50     layout.numColumns = 3;
51
52     Entity entity = this.results.getEntity();
53     columnNames = this.results.getColumnNames();
54
55     values = new Text[columnNames.length];
56     whereValues = new Button[columnNames.length];
57     new Label(container, SWT.NULL).setText(PHPMessages
58         .getString("PHPDeleteRowPage.ColumnName")); //$NON-NLS-1$
59     new Label(container, SWT.NULL).setText(PHPMessages
60         .getString("PHPDeleteRowPage.Value")); //$NON-NLS-1$
61     new Label(container, SWT.NULL).setText(PHPMessages
62         .getString("PHPDeleteRowPage.IncludeIn")); //$NON-NLS-1$
63     for (int i = 0; i < columnNames.length; i++) {
64       Label label = new Label(container, SWT.NULL);
65       label.setText(columnNames[i]);
66       values[i] = new Text(container, SWT.BORDER | SWT.SINGLE);
67 //      Object object = this.row == null ? null : this.row.get(i + 1);
68 //      values[i].setText(object == null ? "" : '$'+columnNames[i]); //object.toString());
69       values[i].setText('$'+columnNames[i]); //object.toString());
70       GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
71       gridData.widthHint = 150;
72       values[i].setLayoutData(gridData);
73       values[i].addModifyListener(new ModifyListener() {
74         public void modifyText(ModifyEvent event) {
75           updateQuery();
76         }
77       });
78
79       whereValues[i] = new Button(container, SWT.CHECK);
80       whereValues[i].setText(PHPMessages
81           .getString("PHPDeleteRowPage.WhereClause")); //$NON-NLS-1$
82       // we check if it's a primary key to select it in the WHERE clause
83       Column column = getColumn(entity, columnNames[i]);
84       whereValues[i].setSelection(column == null ? false : column
85           .isPrimaryKey());
86       whereValues[i].addSelectionListener(new SelectionListener() {
87         public void widgetDefaultSelected(SelectionEvent e) {
88         }
89
90         public void widgetSelected(SelectionEvent e) {
91           updateQuery();
92         }
93       });
94     }
95     query = new Text(container, SWT.MULTI | SWT.READ_ONLY | SWT.WRAP);
96     GridData gridData = new GridData();
97     gridData.horizontalSpan = layout.numColumns;
98     gridData.verticalSpan = 3;
99     gridData.horizontalAlignment = GridData.FILL;
100     gridData.verticalAlignment = GridData.FILL;
101     gridData.grabExcessHorizontalSpace = true;
102     gridData.grabExcessVerticalSpace = true;
103     query.setLayoutData(gridData);
104
105     setControl(container);
106     updateQuery();
107
108     setPageComplete(true);
109   }
110
111   public void updateQuery() {
112     System.out.println(PHPMessages.getString("PHPDeleteRowPage.UpdatingQuery")); //$NON-NLS-1$
113     StringBuffer whereClause = new StringBuffer();
114     Bookmark bookmark = this.results.getBookmark();
115     Entity entity = this.results.getEntity();
116     DatabaseAdapter adapter = bookmark.getAdapter();
117
118     int numSelected = 0;
119     for (int i = 0; i < columnNames.length; i++) {
120       if (whereValues[i].getSelection()) {
121         if (numSelected > 0)
122           whereClause.append(" AND "); //$NON-NLS-1$
123         numSelected++;
124         whereClause.append("("); //$NON-NLS-1$
125         whereClause.append(columnNames[i]);
126         whereClause.append(" = "); //$NON-NLS-1$
127         appendColumn(whereClause, entity, columnNames[i], adapter, values[i]
128             .getText());
129         whereClause.append(")"); //$NON-NLS-1$
130       }
131     }
132     //          String query = "DELETE FROM " +
133     // this.results.getEntity().getQuotedTableName(); //$NON-NLS-1$
134     //          if (numSelected > 0) {
135     //                  query += " WHERE " + whereClause.toString(); //$NON-NLS-1$
136     //          }
137     //          if (numSelected > 0) {
138     //                  setMessage(""); //$NON-NLS-1$
139     //          } else {
140     //                  setMessage(PHPMessages.getString("PHPDeleteRowPage.WarningNoWhere"));
141     // //$NON-NLS-1$
142     //          }
143     String[] arguments = { this.results.getEntity().getQuotedTableName(),
144         whereClause.toString() };
145     MessageFormat form = new MessageFormat(fStore
146         .getString("phpeclipse.sql.delete.template"));
147
148     String query = form.format(arguments);
149
150     if (numSelected > 0) {
151       setMessage(""); //$NON-NLS-1$
152     } else {
153       setMessage(PHPMessages.getString("PHPDeleteRowPage.WarningNoWhere"));
154     }
155
156     this.query.setText(query);
157   }
158
159   protected String getQueryText() {
160     return query.getText();
161   }
162
163   /*
164    * (non-Javadoc)
165    * 
166    * @see com.quantum.wizards.SQLPage#performFinish()
167    */
168   public boolean performFinish() {
169     QuantumPlugin.getDefault().getSysClip().setContents(
170         new Object[] { query.getText() },
171         new Transfer[] { TextTransfer.getInstance() });
172     return true;
173   }
174 }
175 //import java.text.MessageFormat;
176 //
177 //import org.eclipse.jface.preference.IPreferenceStore;
178 //import org.eclipse.jface.wizard.WizardPage;
179 //import org.eclipse.swt.SWT;
180 //import org.eclipse.swt.dnd.TextTransfer;
181 //import org.eclipse.swt.dnd.Transfer;
182 //import org.eclipse.swt.events.ModifyEvent;
183 //import org.eclipse.swt.events.ModifyListener;
184 //import org.eclipse.swt.events.SelectionEvent;
185 //import org.eclipse.swt.events.SelectionListener;
186 //import org.eclipse.swt.layout.GridData;
187 //import org.eclipse.swt.layout.GridLayout;
188 //import org.eclipse.swt.widgets.Button;
189 //import org.eclipse.swt.widgets.Composite;
190 //import org.eclipse.swt.widgets.Label;
191 //import org.eclipse.swt.widgets.Text;
192 //
193 //import com.quantum.QuantumPlugin;
194 //import com.quantum.sql.TableRow;
195 //import com.quantum.wizards.BaseSQLPage;
196 //import com.quantum.wizards.SQLPage;
197 ////import com.quantum.view.PHPSourceConsole;
198 ////import com.quantum.view.tableview.TableAdapter;
199 //
200 //public class PHPDeleteRowPage extends BaseSQLPage implements SQLPage {
201 //  TableRow row;
202 //  String[] columnNames;
203 //  Text[] values;
204 //  Button[] whereValues;
205 //  Label query;
206 //  IPreferenceStore fStore;
207 //  private final static boolean DEBUG = false;
208 //
209 //  public PHPDeleteRowPage(String pageName) {
210 //    super(pageName);
211 //  }
212 //
213 //// public void init(TableRow row, TableAdapter adapter) {
214 //// this.row = row;
215 //// }
216 //
217 //  protected String getQueryText() {
218 //    return query.getText();
219 //  }
220 //  
221 //  public void createControl(Composite parent) {
222 //    System.out.println("page create control");
223 //    fStore = QuantumPlugin.getDefault().getPreferenceStore();
224 //    Composite container = new Composite(parent, SWT.NULL);
225 //    GridLayout layout = new GridLayout();
226 //    container.setLayout(layout);
227 //    int layoutColumns = 3;
228 //    layout.numColumns = layoutColumns;
229 //
230 //    if (DEBUG) {
231 //      if (row == null) {
232 //        System.out.println("Row is null");
233 //      }
234 //      if (row.getColumnNames() == null) {
235 //        System.out.println("Columns are null");
236 //      }
237 //      if (row.getTableData() == null) {
238 //        System.out.println("Data is null");
239 //      }
240 //    }
241 //    columnNames = row.getColumnNames();
242 //    String[] data = row.getTableData();
243 //    if (DEBUG) {
244 //      for (int i = 0; i < row.getColumnCount(); i++) {
245 //        System.out.println("data = " + i + "=" + data[i]);
246 //        System.out.println("column = " + i + "=" + columnNames[i]);
247 //      }
248 //    }
249 //    values = new Text[row.getColumnCount()];
250 //    whereValues = new Button[row.getColumnCount()];
251 //    Label temp = new Label(container, SWT.NULL);
252 //    temp.setText("Column Name");
253 //    temp = new Label(container, SWT.NULL);
254 //    temp.setText("Value");
255 //    temp = new Label(container, SWT.NULL);
256 //    temp.setText("Include in?");
257 //    for (int i = 0; i < row.getColumnCount(); i++) {
258 //      Label label = new Label(container, SWT.NULL);
259 //      label.setText(columnNames[i]);
260 //      values[i] = new Text(container, SWT.BORDER | SWT.SINGLE);
261 //      GridData fullHorizontal = new GridData();
262 //      fullHorizontal.horizontalAlignment = GridData.FILL;
263 //      values[i].setLayoutData(fullHorizontal);
264 //
265 //      if (data[i] == null || data[i].equals("")) {
266 //        values[i].setText('$' + columnNames[i]);
267 //      } else {
268 //        values[i].setText(data[i]);
269 //      }
270 //
271 //      values[i].addModifyListener(new ModifyListener() {
272 //        public void modifyText(ModifyEvent e) {
273 //          updateQuery();
274 //        }
275 //      });
276 //
277 //      whereValues[i] = new Button(container, SWT.CHECK);
278 //      whereValues[i].setText("Where clause");
279 //      whereValues[i].addSelectionListener(new SelectionListener() {
280 //        public void widgetDefaultSelected(SelectionEvent e) {
281 //        }
282 //        public void widgetSelected(SelectionEvent e) {
283 //          updateQuery();
284 //        }
285 //      });
286 //    }
287 //    query = new Label(container, SWT.WRAP);
288 //    GridData gridData = new GridData();
289 //    gridData.horizontalSpan = layoutColumns;
290 //    gridData.horizontalAlignment = GridData.FILL;
291 //    gridData.verticalAlignment = GridData.FILL;
292 //    gridData.grabExcessHorizontalSpace = true;
293 //    gridData.grabExcessVerticalSpace = true;
294 //    query.setLayoutData(gridData);
295 //
296 //    setControl(container);
297 //    updateQuery();
298 //
299 //    setPageComplete(true);
300 //  }
301 //  public void updateQuery() {
302 //    if (DEBUG) {
303 //      System.out.println("Updating delete query");
304 //    }
305 //    StringBuffer whereClause = new StringBuffer();
306 //    int numSelected = 0;
307 //    boolean first = false;
308 //    for (int i = 0; i < columnNames.length; i++) {
309 //      if (whereValues[i].getSelection()) {
310 //        numSelected++;
311 //        if (first) {
312 //          whereClause.append(", ");
313 //        }
314 //
315 //        whereClause.append(columnNames[i]);
316 //        whereClause.append(" = ");
317 //        whereClause.append("'" + values[i].getText() + "'");
318 //
319 //        first = true;
320 //      }
321 //    }
322 //    // if (whereClause.length() > 1) {
323 //    // whereClause.deleteCharAt(whereClause.length() - 1);
324 //    // whereClause.deleteCharAt(whereClause.length() - 1);
325 //    // }
326 //
327 //    String[] arguments = { row.getTable(), whereClause.toString()};
328 //    MessageFormat form = new
329 // MessageFormat(fStore.getString("phpeclipse.sql.delete.template"));
330 //
331 //    String query = form.format(arguments);
332 //
333 //    // String query = "$results = mysql_query(\"DELETE FROM " + row.getTable();
334 //    // if (numSelected > 0) {
335 //    // query += " WHERE " + whereClause.toString() + "\");";
336 //    // } else {
337 //    // query += "\");";
338 //    // }
339 //
340 //    if (numSelected > 0) {
341 //      setMessage("");
342 //    } else {
343 //      setMessage("Warning: no \"where clause\" columns selected, all rows will be
344 // deleted");
345 //    }
346 //
347 //    this.getControl().pack();
348 //    this.query.setText(query);
349 //  }
350 //  public boolean performFinish() {
351 //// PHPSourceConsole console = PHPSourceConsole.getInstance();
352 //// console.clear();
353 //// console.print(query.getText());
354 //    QuantumPlugin.getDefault().getSysClip().setContents(
355 //                      new Object[] { query.getText() },
356 //                      new Transfer[] { TextTransfer.getInstance()});
357 //    return true;
358 //  }
359 //}