8b485412114aa082d6070cd42db7287e01e00939
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / wizards / PHPDeleteRowPage.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.events.ModifyEvent;
9 import org.eclipse.swt.events.ModifyListener;
10 import org.eclipse.swt.events.SelectionEvent;
11 import org.eclipse.swt.events.SelectionListener;
12 import org.eclipse.swt.layout.GridData;
13 import org.eclipse.swt.layout.GridLayout;
14 import org.eclipse.swt.widgets.Button;
15 import org.eclipse.swt.widgets.Composite;
16 import org.eclipse.swt.widgets.Label;
17 import org.eclipse.swt.widgets.Text;
18
19 import com.quantum.QuantumPlugin;
20 import com.quantum.sql.TableRow;
21 import com.quantum.view.PHPSourceConsole;
22 import com.quantum.view.tableview.TableAdapter;
23
24 public class PHPDeleteRowPage extends WizardPage implements SQLPage {
25   TableRow row;
26   String[] columnNames;
27   Text[] values;
28   Button[] whereValues;
29   Label query;
30   IPreferenceStore fStore;
31   private final static boolean DEBUG = false;
32
33   public PHPDeleteRowPage(String pageName) {
34     super(pageName);
35   }
36
37   public void init(TableRow row, TableAdapter adapter) {
38     this.row = row;
39   }
40
41   public void createControl(Composite parent) {
42     System.out.println("page create control");
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 = 3;
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     columnNames = row.getColumnNames();
62     String[] data = row.getTableData();
63     if (DEBUG) {
64       for (int i = 0; i < row.getColumnCount(); i++) {
65         System.out.println("data = " + i + "=" + data[i]);
66         System.out.println("column = " + i + "=" + columnNames[i]);
67       }
68     }
69     values = new Text[row.getColumnCount()];
70     whereValues = new Button[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     temp = new Label(container, SWT.NULL);
76     temp.setText("Include in?");
77     for (int i = 0; i < row.getColumnCount(); i++) {
78       Label label = new Label(container, SWT.NULL);
79       label.setText(columnNames[i]);
80       values[i] = new Text(container, SWT.BORDER | SWT.SINGLE);
81       GridData fullHorizontal = new GridData();
82       fullHorizontal.horizontalAlignment = GridData.FILL;
83       values[i].setLayoutData(fullHorizontal);
84
85       if (data[i] == null || data[i].equals("")) {
86         values[i].setText('$' + columnNames[i]);
87       } else {
88         values[i].setText(data[i]);
89       }
90
91       values[i].addModifyListener(new ModifyListener() {
92         public void modifyText(ModifyEvent e) {
93           updateQuery();
94         }
95       });
96
97       whereValues[i] = new Button(container, SWT.CHECK);
98       whereValues[i].setText("Where clause");
99       whereValues[i].addSelectionListener(new SelectionListener() {
100         public void widgetDefaultSelected(SelectionEvent e) {
101         }
102         public void widgetSelected(SelectionEvent e) {
103           updateQuery();
104         }
105       });
106     }
107     query = new Label(container, SWT.WRAP);
108     GridData gridData = new GridData();
109     gridData.horizontalSpan = layoutColumns;
110     gridData.horizontalAlignment = GridData.FILL;
111     gridData.verticalAlignment = GridData.FILL;
112     gridData.grabExcessHorizontalSpace = true;
113     gridData.grabExcessVerticalSpace = true;
114     query.setLayoutData(gridData);
115
116     setControl(container);
117     updateQuery();
118
119     setPageComplete(true);
120   }
121   public void updateQuery() {
122     if (DEBUG) {
123       System.out.println("Updating delete query");
124     }
125     StringBuffer whereClause = new StringBuffer();
126     int numSelected = 0;
127     boolean first = false;
128     for (int i = 0; i < columnNames.length; i++) {
129       if (whereValues[i].getSelection()) {
130         numSelected++;
131         if (first) {
132           whereClause.append(", ");
133         }
134
135         whereClause.append(columnNames[i]);
136         whereClause.append(" = ");
137         whereClause.append("'" + values[i].getText() + "'");
138
139         first = true;
140       }
141     }
142     //    if (whereClause.length() > 1) {
143     //      whereClause.deleteCharAt(whereClause.length() - 1);
144     //      whereClause.deleteCharAt(whereClause.length() - 1);
145     //    }
146
147     String[] arguments = { row.getTable(), whereClause.toString()};
148     MessageFormat form = new MessageFormat(fStore.getString("phpeclipse.sql.delete.template"));
149
150     String query = form.format(arguments);
151
152     //    String query = "$results = mysql_query(\"DELETE FROM " + row.getTable();
153     //    if (numSelected > 0) {
154     //          query += " WHERE " + whereClause.toString() + "\");";
155     //    } else {
156     //          query += "\");";
157     //    }
158
159     if (numSelected > 0) {
160       setMessage("");
161     } else {
162       setMessage("Warning: no \"where clause\" columns selected, all rows will be deleted");
163     }
164
165     this.getControl().pack();
166     this.query.setText(query);
167   }
168   public boolean performFinish() {
169     PHPSourceConsole console = PHPSourceConsole.getInstance();
170     console.clear();
171     console.print(query.getText());
172     return true;
173   }
174 }