1) Added missing strings for italic, underline and strike through.
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.sql / src / net / sourceforge / phpdt / sql / wizards / InsertRowPage.java
1 package net.sourceforge.phpdt.sql.wizards;
2
3 import org.eclipse.jface.wizard.WizardPage;
4 import org.eclipse.swt.SWT;
5 import org.eclipse.swt.events.ModifyEvent;
6 import org.eclipse.swt.events.ModifyListener;
7 import org.eclipse.swt.layout.GridData;
8 import org.eclipse.swt.layout.GridLayout;
9 import org.eclipse.swt.widgets.Composite;
10 import org.eclipse.swt.widgets.Label;
11 import org.eclipse.swt.widgets.Text;
12
13 import net.sourceforge.phpdt.sql.Messages;
14 import net.sourceforge.phpdt.sql.adapters.AdapterFactory;
15 import net.sourceforge.phpdt.sql.adapters.DatabaseAdapter;
16 import net.sourceforge.phpdt.sql.sql.MultiSQLServer;
17 import net.sourceforge.phpdt.sql.sql.TableRow;
18 import net.sourceforge.phpdt.sql.sql.metadata.ObjectMetaData;
19 import net.sourceforge.phpdt.sql.view.BookmarkView;
20 import net.sourceforge.phpdt.sql.view.bookmark.BookmarkNode;
21 import net.sourceforge.phpdt.sql.view.bookmark.TreeNode;
22 import net.sourceforge.phpdt.sql.view.tableview.TableAdapter;
23
24 public class InsertRowPage extends WizardPage implements SQLPage {
25   TableRow row;
26   String[] columnNames;
27   Text[] values;
28   Label query;
29   public InsertRowPage(String pageName) {
30     super(pageName);
31   }
32
33   public void init(TableRow row, TableAdapter adapter) {
34     this.row = row;
35   }
36
37   public void createControl(Composite parent) {
38     if (DEBUG) {
39       System.out.println("page create control"); //$NON-NLS-1$
40     }
41
42     Composite container = new Composite(parent, SWT.NULL);
43     GridLayout layout = new GridLayout();
44     container.setLayout(layout);
45     layout.numColumns = 2;
46
47     if (DEBUG) {
48       if (row == null) {
49         System.out.println("Row is null"); //$NON-NLS-1$
50       }
51       if (row.getColumnNames() == null) {
52         System.out.println("Columns are null"); //$NON-NLS-1$
53       }
54       if (row.getTableData() == null) {
55         System.out.println("Data is null"); //$NON-NLS-1$
56       }
57     }
58
59     columnNames = row.getColumnNames();
60     String[] data = row.getTableData();
61
62     if (DEBUG) {
63       for (int i = 0; i < row.getColumnCount(); i++) {
64         System.out.println("data = " + i + "=" + data[i]); //$NON-NLS-1$ //$NON-NLS-2$
65         System.out.println("column = " + i + "=" + columnNames[i]); //$NON-NLS-1$ //$NON-NLS-2$
66       }
67     }
68
69     values = new Text[row.getColumnCount()];
70     new Label(container, SWT.NULL).setText(Messages.getString("InsertRowPage.ColumnName")); //$NON-NLS-1$
71     new Label(container, SWT.NULL).setText(Messages.getString("InsertRowPage.Value")); //$NON-NLS-1$
72     for (int i = 0; i < row.getColumnCount(); i++) {
73       Label label = new Label(container, SWT.NULL);
74       label.setText(columnNames[i]);
75       values[i] = new Text(container, SWT.BORDER | SWT.SINGLE);
76       GridData fullHorizontal = new GridData();
77       fullHorizontal.horizontalAlignment = GridData.FILL;
78       values[i].setLayoutData(fullHorizontal);
79
80       //values[i].setText(data[i]);
81       values[i].addModifyListener(new ModifyListener() {
82         public void modifyText(ModifyEvent e) {
83           updateQuery();
84         }
85       });
86     }
87     query = new Label(container, SWT.WRAP);
88     GridData gridData = new GridData();
89     gridData.horizontalSpan = layout.numColumns;
90     gridData.horizontalAlignment = GridData.FILL;
91     gridData.verticalAlignment = GridData.FILL;
92     gridData.grabExcessHorizontalSpace = true;
93     gridData.grabExcessVerticalSpace = true;
94     query.setLayoutData(gridData);
95
96     setControl(container);
97     updateQuery();
98
99     setPageComplete(true);
100   }
101   public void updateQuery() {
102     if (DEBUG) {
103       System.out.println("Updating query"); //$NON-NLS-1$
104     }
105
106     StringBuffer valuesClause = new StringBuffer();
107     BookmarkNode bookmark = row.getBookmarkNode();
108     TreeNode node = bookmark.find(row.getTable());
109     ObjectMetaData metadata = null;
110     if (node != null)
111       metadata = node.getMetaData();
112     DatabaseAdapter adapter = AdapterFactory.getInstance().getAdapter(bookmark.getType());
113
114     int numValues = 0;
115     for (int i = 0; i < columnNames.length; i++) {
116       String value = values[i].getText();
117       if (numValues > 0)
118         valuesClause.append(", "); //$NON-NLS-1$
119       if (adapter != null && metadata != null && value != "") //$NON-NLS-1$
120         valuesClause.append(adapter.quote(value, metadata.getColumnType(columnNames[i])));
121       else
122         valuesClause.append(value);
123       numValues++;
124     }
125     String query = "INSERT INTO " + row.getTable(); //$NON-NLS-1$
126     query += " VALUES (" + valuesClause; //$NON-NLS-1$
127     query += " )"; //$NON-NLS-1$
128     this.query.setText(query);
129   }
130   public boolean performFinish() {
131     MultiSQLServer server = MultiSQLServer.getInstance();
132     BookmarkView bookmarkView = BookmarkView.getInstance();
133     BookmarkNode bookmark = bookmarkView.getCurrentBookmark();
134     server.execute(bookmark.getConnection(), query.getText());
135     return true;
136   }
137 }