added file extension php5
[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.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.sql.TableRow;
23 //import com.quantum.view.PHPSourceConsole;
24 import com.quantum.view.tableview.TableAdapter;
25
26 public class PHPDeleteRowPage extends WizardPage implements SQLPage {
27   TableRow row;
28   String[] columnNames;
29   Text[] values;
30   Button[] whereValues;
31   Label query;
32   IPreferenceStore fStore;
33   private final static boolean DEBUG = false;
34
35   public PHPDeleteRowPage(String pageName) {
36     super(pageName);
37   }
38
39   public void init(TableRow row, TableAdapter adapter) {
40     this.row = row;
41   }
42
43   public void createControl(Composite parent) {
44     System.out.println("page create control");
45     fStore = QuantumPlugin.getDefault().getPreferenceStore();
46     Composite container = new Composite(parent, SWT.NULL);
47     GridLayout layout = new GridLayout();
48     container.setLayout(layout);
49     int layoutColumns = 3;
50     layout.numColumns = layoutColumns;
51
52     if (DEBUG) {
53       if (row == null) {
54         System.out.println("Row is null");
55       }
56       if (row.getColumnNames() == null) {
57         System.out.println("Columns are null");
58       }
59       if (row.getTableData() == null) {
60         System.out.println("Data is null");
61       }
62     }
63     columnNames = row.getColumnNames();
64     String[] data = row.getTableData();
65     if (DEBUG) {
66       for (int i = 0; i < row.getColumnCount(); i++) {
67         System.out.println("data = " + i + "=" + data[i]);
68         System.out.println("column = " + i + "=" + columnNames[i]);
69       }
70     }
71     values = new Text[row.getColumnCount()];
72     whereValues = new Button[row.getColumnCount()];
73     Label temp = new Label(container, SWT.NULL);
74     temp.setText("Column Name");
75     temp = new Label(container, SWT.NULL);
76     temp.setText("Value");
77     temp = new Label(container, SWT.NULL);
78     temp.setText("Include in?");
79     for (int i = 0; i < row.getColumnCount(); i++) {
80       Label label = new Label(container, SWT.NULL);
81       label.setText(columnNames[i]);
82       values[i] = new Text(container, SWT.BORDER | SWT.SINGLE);
83       GridData fullHorizontal = new GridData();
84       fullHorizontal.horizontalAlignment = GridData.FILL;
85       values[i].setLayoutData(fullHorizontal);
86
87       if (data[i] == null || data[i].equals("")) {
88         values[i].setText('$' + columnNames[i]);
89       } else {
90         values[i].setText(data[i]);
91       }
92
93       values[i].addModifyListener(new ModifyListener() {
94         public void modifyText(ModifyEvent e) {
95           updateQuery();
96         }
97       });
98
99       whereValues[i] = new Button(container, SWT.CHECK);
100       whereValues[i].setText("Where clause");
101       whereValues[i].addSelectionListener(new SelectionListener() {
102         public void widgetDefaultSelected(SelectionEvent e) {
103         }
104         public void widgetSelected(SelectionEvent e) {
105           updateQuery();
106         }
107       });
108     }
109     query = new Label(container, SWT.WRAP);
110     GridData gridData = new GridData();
111     gridData.horizontalSpan = layoutColumns;
112     gridData.horizontalAlignment = GridData.FILL;
113     gridData.verticalAlignment = GridData.FILL;
114     gridData.grabExcessHorizontalSpace = true;
115     gridData.grabExcessVerticalSpace = true;
116     query.setLayoutData(gridData);
117
118     setControl(container);
119     updateQuery();
120
121     setPageComplete(true);
122   }
123   public void updateQuery() {
124     if (DEBUG) {
125       System.out.println("Updating delete query");
126     }
127     StringBuffer whereClause = new StringBuffer();
128     int numSelected = 0;
129     boolean first = false;
130     for (int i = 0; i < columnNames.length; i++) {
131       if (whereValues[i].getSelection()) {
132         numSelected++;
133         if (first) {
134           whereClause.append(", ");
135         }
136
137         whereClause.append(columnNames[i]);
138         whereClause.append(" = ");
139         whereClause.append("'" + values[i].getText() + "'");
140
141         first = true;
142       }
143     }
144     //    if (whereClause.length() > 1) {
145     //      whereClause.deleteCharAt(whereClause.length() - 1);
146     //      whereClause.deleteCharAt(whereClause.length() - 1);
147     //    }
148
149     String[] arguments = { row.getTable(), whereClause.toString()};
150     MessageFormat form = new MessageFormat(fStore.getString("phpeclipse.sql.delete.template"));
151
152     String query = form.format(arguments);
153
154     //    String query = "$results = mysql_query(\"DELETE FROM " + row.getTable();
155     //    if (numSelected > 0) {
156     //          query += " WHERE " + whereClause.toString() + "\");";
157     //    } else {
158     //          query += "\");";
159     //    }
160
161     if (numSelected > 0) {
162       setMessage("");
163     } else {
164       setMessage("Warning: no \"where clause\" columns selected, all rows will be deleted");
165     }
166
167     this.getControl().pack();
168     this.query.setText(query);
169   }
170   public boolean performFinish() {
171 //    PHPSourceConsole console = PHPSourceConsole.getInstance();
172 //    console.clear();
173 //    console.print(query.getText());
174     QuantumPlugin.getDefault().getSysClip().setContents(
175                         new Object[] { query.getText() },
176                         new Transfer[] { TextTransfer.getInstance()});
177     return true;
178   }
179 }