1 package com.quantum.wizards;
3 import java.util.Arrays;
5 import org.eclipse.jface.viewers.CellEditor;
6 import org.eclipse.jface.viewers.ICellModifier;
7 import org.eclipse.jface.viewers.ILabelProviderListener;
8 import org.eclipse.jface.viewers.IStructuredContentProvider;
9 import org.eclipse.jface.viewers.ITableLabelProvider;
10 import org.eclipse.jface.viewers.TableViewer;
11 import org.eclipse.jface.viewers.TextCellEditor;
12 import org.eclipse.jface.viewers.Viewer;
13 import org.eclipse.swt.SWT;
14 import org.eclipse.swt.events.ModifyEvent;
15 import org.eclipse.swt.events.ModifyListener;
16 import org.eclipse.swt.graphics.Image;
17 import org.eclipse.swt.layout.GridData;
18 import org.eclipse.swt.layout.GridLayout;
19 import org.eclipse.swt.widgets.Composite;
20 import org.eclipse.swt.widgets.Label;
21 import org.eclipse.swt.widgets.Table;
22 import org.eclipse.swt.widgets.TableColumn;
23 import org.eclipse.swt.widgets.TableItem;
24 import org.eclipse.swt.widgets.Text;
26 import com.quantum.Messages;
27 import com.quantum.adapters.DatabaseAdapter;
28 import com.quantum.model.Bookmark;
29 import com.quantum.model.Entity;
30 import com.quantum.sql.SQLResultSetResults;
32 public class InsertRowPage extends BaseSQLPage implements SQLPage {
34 class InsertRowTableValues {
35 private String colNames = null;
36 private String values = null;
38 public InsertRowTableValues() {
41 * @return Returns the colNames.
43 public String getColNames() {
47 * @param colNames The colNames to set.
49 public void setColNames(String colNames) {
50 this.colNames = colNames;
53 * @return Returns the values.
55 public String getValues() {
59 * @param values The values to set.
61 public void setValues(String values) {
66 class LabelProviderImpl implements ITableLabelProvider {
67 public Image getColumnImage(Object element, int columnIndex) {
70 public String getColumnText(Object element, int columnIndex) {
72 InsertRowTableValues insertRow = (InsertRowTableValues)element;
73 switch (columnIndex) {
75 sReturn = insertRow.getColNames();
78 sReturn = insertRow.getValues();
85 public void addListener(ILabelProviderListener listener) {}
86 public void dispose() {}
87 public boolean isLabelProperty(Object element, String property) {
90 public void removeListener(ILabelProviderListener listener) {}
93 class ContentProviderImpl implements IStructuredContentProvider {
94 public Object[] getElements(Object inputElement) {
98 public void dispose() {}
99 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {}
102 class CellModifierImpl implements ICellModifier {
104 public boolean canModify(Object element, String property) {
108 public Object getValue(Object element, String property) {
109 System.out.println("getValue called");
111 // Find the index of the column
112 int colIndx = getColumnNamesAsList(colNames).indexOf(property);
113 System.out.println("colIndx : " + colIndx);
115 Object rResult = null;
116 InsertRowTableValues insertVal = (InsertRowTableValues)element;
120 rResult = insertVal.getColNames();
123 rResult = insertVal.getValues();
133 public void modify(Object element, String property, Object value) {
134 int colIndx = getColumnNamesAsList(colNames).indexOf(property);
136 TableItem item = (TableItem) element;
137 InsertRowTableValues insertVal = (InsertRowTableValues)item.getData();
140 case 0: // field names
142 case 1: // field values
143 insertVal.setValues(value.toString());
153 String[] columnNames;
157 InsertRowTableValues[] insertTable = null;
158 TableViewer tableViewer = null;
161 public InsertRowPage(String pageName) {
165 public void createControl(Composite parent) {
166 System.out.println("page create control"); //$NON-NLS-1$
167 Composite container = new Composite(parent, SWT.NULL);
168 container.setLayout(new GridLayout());
169 container.setLayoutData(new GridData(GridData.FILL_BOTH | GridData.VERTICAL_ALIGN_BEGINNING));
171 // init values to be displayed on the table
172 columnNames = this.results.getColumnNames();
173 int nLen = columnNames.length;
174 insertTable = new InsertRowTableValues[nLen];
176 for (int nCtr=0; nCtr<nLen; nCtr++) {
177 insertTable[nCtr] = new InsertRowTableValues();
178 insertTable[nCtr].setColNames(columnNames[nCtr]);
179 insertTable[nCtr].setValues("");
182 createTable(container);
184 query = new Label(container, SWT.WRAP);
185 GridData gridData = new GridData();
186 gridData.horizontalSpan = 1;
187 gridData.horizontalAlignment = GridData.FILL;
188 gridData.verticalAlignment = GridData.FILL;
189 query.setLayoutData(gridData);
191 setControl(container);
194 setPageComplete(true);
196 public void updateQuery() {
197 System.out.println("Updating query"); //$NON-NLS-1$
198 StringBuffer valuesClause = new StringBuffer();
199 StringBuffer namesClause = new StringBuffer();
200 Bookmark bookmark = this.results.getBookmark();
201 Entity entity = this.results.getEntity();
202 DatabaseAdapter adapter = bookmark.getAdapter();
205 for (int i = 0; i < columnNames.length; i++) {
206 String name = insertTable[i].getColNames();
207 String value = insertTable[i].getValues();
208 if (value != null && value.length() > 0) {
209 if (numColumns > 0) {
210 valuesClause.append(", "); //$NON-NLS-1$
211 namesClause.append(", ");
213 appendColumn(valuesClause, entity, name, adapter, value);
214 namesClause.append(name);
219 String query = "INSERT INTO " + this.results.getEntity().getQuotedTableName(); //$NON-NLS-1$
220 if (numColumns > 0) {
221 query += " (" + namesClause + ")";
222 query += " VALUES " + "(" + valuesClause; //$NON-NLS-1$
223 query += " )"; //$NON-NLS-1$
225 this.query.setText(query);
228 * @see com.quantum.wizards.BaseSQLPage#getQueryText()
230 protected String getQueryText() {
231 return this.query.getText();
234 private void createTable(Composite composite) {
235 System.out.println("Creating table...");
236 int style = SWT.SINGLE | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.HIDE_SELECTION;
237 Table table = new Table(composite, style);
238 table.setHeaderVisible(true);
239 table.setLinesVisible(true);
240 table.setLayoutData(new GridData(GridData.FILL_BOTH | GridData.VERTICAL_ALIGN_BEGINNING));
242 colNames = new String[] { Messages.getString("InsertRowPage.ColumnName"), Messages.getString("InsertRowPage.Value") };
244 createTableColumn(table, colNames[0], SWT.LEFT, 0, 200);
245 createTableColumn(table, colNames[1], SWT.LEFT, 1, 500);
246 this.tableViewer = new TableViewer(table);
247 this.tableViewer.setColumnProperties(colNames);
249 CellEditor[] editor = new CellEditor[colNames.length];
250 TextCellEditor txtEditorField = new TextCellEditor(table);
251 txtEditorField.getControl().setEnabled(false);
252 editor[0] = txtEditorField;
254 TextCellEditor txtEditorValues = new TextCellEditor(table);
255 editor[1] = txtEditorValues;
257 this.tableViewer.setCellEditors(editor);
258 this.tableViewer.setLabelProvider(new LabelProviderImpl());
259 this.tableViewer.setContentProvider(new ContentProviderImpl());
260 this.tableViewer.setCellModifier(new CellModifierImpl());
261 this.tableViewer.setInput(insertTable);
264 private void updateView() {
265 this.tableViewer.update(insertTable, null);