reduce warnings due to core.runtime deprecations
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / wizards / UpdateRowPage.java
1 package com.quantum.wizards;
2
3 import com.quantum.Messages;
4 import com.quantum.adapters.AdapterFactory;
5 import com.quantum.adapters.DatabaseAdapter;
6 import com.quantum.model.Bookmark;
7 import com.quantum.model.Entity;
8
9 import org.eclipse.swt.SWT;
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 public class UpdateRowPage extends BaseSQLPage implements SQLPage {
22         String[] columnNames;
23         Text[] oldValues;
24         Text[] newValues;
25         Button[] primaryKeys;
26         Button[] setValues;
27         Label query;
28         public UpdateRowPage(String pageName) {
29                 super(pageName);
30         }
31
32         public void createControl(Composite parent) {
33                 System.out.println("page create control"); //$NON-NLS-1$
34                 Composite container = new Composite(parent, SWT.V_SCROLL);
35                 GridLayout layout = new GridLayout();
36                 container.setLayout(layout);
37                 Entity entity = row.getEntity();
38                 
39                 int layoutColumns = 5;
40                 layout.numColumns = layoutColumns;
41
42                 if (row == null) {
43                         System.out.println("Row is null"); //$NON-NLS-1$
44                 }
45                 if (row.getColumnNames() == null) {
46                         System.out.println("Columns are null"); //$NON-NLS-1$
47                 }
48                 if (row.getTableData() == null) {
49                         System.out.println("Data is null"); //$NON-NLS-1$
50                 }
51                 columnNames = row.getColumnNames();
52                 String[] data = row.getTableData();
53                 for (int i = 0; i < row.getColumnCount(); i++) {
54                         System.out.println("data = " + i + "=" + data[i]); //$NON-NLS-1$ //$NON-NLS-2$
55                         System.out.println("column = " + i + "=" + columnNames[i]); //$NON-NLS-1$ //$NON-NLS-2$
56                 }
57                 oldValues = new Text[row.getColumnCount()];
58                 newValues = new Text[row.getColumnCount()];
59                 primaryKeys = new Button[row.getColumnCount()];
60                 setValues = new Button[row.getColumnCount()];
61                 Label temp = new Label(container, SWT.NULL);
62                 temp.setText(Messages.getString("UpdateRowPage.ColumnName")); //$NON-NLS-1$
63                 temp = new Label(container, SWT.NULL);
64                 temp.setText(Messages.getString("UpdateRowPage.OldValue")); //$NON-NLS-1$
65                 temp = new Label(container, SWT.NULL);
66                 temp.setText(""); //$NON-NLS-1$
67                 temp = new Label(container, SWT.NULL);
68                 temp.setText(Messages.getString("UpdateRowPage.NewValue")); //$NON-NLS-1$
69                 temp = new Label(container, SWT.NULL);
70                 temp.setText(Messages.getString("UpdateRowPage._13")); //$NON-NLS-1$
71                 for (int i = 0; i < row.getColumnCount(); i++) {
72                         Label label = new Label(container, SWT.NULL);
73                         label.setText(columnNames[i]);
74                         oldValues[i] = new Text(container, SWT.BORDER | SWT.SINGLE);
75                         oldValues[i].setText(data[i]);
76                         oldValues[i].addModifyListener(new ModifyListener() {
77                                 public void modifyText(ModifyEvent e) {
78                                         updateQuery();
79                                 }                               
80                         });
81                         primaryKeys[i] = new Button(container, SWT.CHECK);
82                         primaryKeys[i].setText("Where"); //$NON-NLS-1$
83                         if (entity != null && entity.getColumn(columnNames[i]).isPrimaryKey()) 
84                                 primaryKeys[i].setSelection(true);
85                         primaryKeys[i].addSelectionListener(new SelectionListener() {
86                                 public void widgetDefaultSelected(SelectionEvent e) {
87                                 }
88                                 public void widgetSelected(SelectionEvent e) {
89                                         updateQuery();
90                                 }
91                         });
92                         newValues[i] = new Text(container, SWT.BORDER | SWT.SINGLE);
93                         newValues[i].setText(data[i]);
94                         newValues[i].addModifyListener(new ModifyListener() {
95                                 public void modifyText(ModifyEvent e) {
96                                         updateQuery();
97                                 }                               
98                         });
99                         setValues[i] = new Button(container, SWT.CHECK);
100                         setValues[i].setText(Messages.getString("UpdateRowPage.SetValue")); //$NON-NLS-1$
101                         setValues[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 | SWT.V_SCROLL);
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                 System.out.println("Updating query"); //$NON-NLS-1$
125                 StringBuffer setClause = new StringBuffer();
126                 StringBuffer whereClause = new StringBuffer();
127                 Bookmark bookmark = row.getBookmark();
128                 Entity entity = row.getEntity();
129                 DatabaseAdapter adapter = AdapterFactory.getInstance().getAdapter(bookmark.getType());
130                 
131                 int numValuesSet = 0;
132                 int numValuesWhere = 0;
133                 for (int i = 0; i < columnNames.length; i++) {
134                         if (primaryKeys[i].getSelection()) {
135                                 String value = oldValues[i].getText();
136                                 if (numValuesWhere > 0) whereClause.append(" AND "); //$NON-NLS-1$
137                                 whereClause.append("("); //$NON-NLS-1$
138                                 whereClause.append(columnNames[i]);
139                                 whereClause.append(" = "); //$NON-NLS-1$
140                 appendColumn(whereClause, entity, columnNames[i], adapter, value);
141                                 whereClause.append(")"); //$NON-NLS-1$
142                                 numValuesWhere++;
143                         } 
144                         if (setValues[i].getSelection()) {
145                                 String value = newValues[i].getText();
146                                 if (numValuesSet > 0) setClause.append(", "); //$NON-NLS-1$
147                                 setClause.append(columnNames[i]);
148                                 setClause.append(" = "); //$NON-NLS-1$
149                 appendColumn(setClause, entity, columnNames[i], adapter, value);
150                                 numValuesSet++;
151                         }
152                 }
153                         String query = "UPDATE " + row.getTable(); //$NON-NLS-1$
154                 query += " SET " + setClause.toString(); //$NON-NLS-1$
155                 query += " WHERE " + whereClause.toString(); //$NON-NLS-1$
156                 this.query.setText(query);
157         }
158
159     /* (non-Javadoc)
160      * @see com.quantum.wizards.BaseSQLPage#getQueryText()
161      */
162     protected String getQueryText() {
163         return query.getText();
164     }
165
166 }