added .cvsignore for bin directory
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / view / tableview / TableViewMenuListener.java
1 /*
2  * Created on 28-jul-2003
3  *
4  */
5 package com.quantum.view.tableview;
6 import java.util.Vector;
7 import org.eclipse.jface.action.Action;
8 import org.eclipse.jface.action.IMenuListener;
9 import org.eclipse.jface.action.IMenuManager;
10 import org.eclipse.jface.action.MenuManager;
11 import org.eclipse.jface.wizard.WizardDialog;
12 import org.eclipse.swt.widgets.Table;
13 import org.eclipse.swt.widgets.TableColumn;
14 import org.eclipse.swt.widgets.TableItem;
15 import com.quantum.Messages;
16 import com.quantum.extensions.ExtensionAction;
17 import com.quantum.sql.TableRow;
18 import com.quantum.util.StringMatrix;
19 import com.quantum.wizards.DeleteRowPage;
20 import com.quantum.wizards.InsertRowPage;
21 import com.quantum.wizards.PHPDeleteRowPage;
22 import com.quantum.wizards.PHPInsertRowPage;
23 import com.quantum.wizards.PHPSelectRowPage;
24 import com.quantum.wizards.PHPUpdateRowPage;
25 import com.quantum.wizards.SQLRowWizard;
26 import com.quantum.wizards.UpdateRowPage;
27 public final class TableViewMenuListener implements IMenuListener {
28   private final TableView tableView;
29   private final Table table;
30   private final Action UTF16EncodingAction;
31   private final TableAdapter ta;
32   private final Action defaultEncodingAction;
33   private final Action UTF8EncodingAction;
34   private final Vector extensionVector;
35   public TableViewMenuListener(TableView view, Table table,
36       Action UTF16EncodingAction, TableAdapter ta,
37       Action defaultEncodingAction, Action UTF8EncodingAction,
38       Vector extensionVector) {
39     super();
40     this.tableView = view;
41     this.table = table;
42     this.UTF16EncodingAction = UTF16EncodingAction;
43     this.ta = ta;
44     this.defaultEncodingAction = defaultEncodingAction;
45     this.UTF8EncodingAction = UTF8EncodingAction;
46     this.extensionVector = extensionVector;
47   }
48   public void menuAboutToShow(IMenuManager mgr) {
49     if (ta.getTable() != null) {
50       TableItem[] selection = table.getSelection();
51       TableColumn[] columns = table.getColumns();
52       // Copy in columnNames the names of the columns of the selected rows
53       String columnNames[] = new String[columns.length];
54       for (int i = 0; i < columns.length; i++) {
55         columnNames[i] = columns[i].getText();
56       }
57       // Copy in data the values of the data columns of the selected rows
58       StringMatrix data = new StringMatrix();
59       data.addMatrixHeader(columnNames);
60       if (selection != null && selection.length > 0) {
61         for (int iRow = 0; iRow < selection.length; iRow++) {
62           TableItem sel = selection[iRow];
63           for (int i = 0; i < columns.length; i++) {
64             data.addAt(columnNames[i], sel.getText(i), iRow);
65           }
66         }
67       } else {
68         // Create dummy values in case nothing selected
69         for (int i = 0; i < columns.length; i++) {
70           data.addAt(columnNames[i], "", 0); //$NON-NLS-1$
71         }
72       }
73       final TableRow row = new TableRow(ta.getEntity(), ta.getBookmark(), ta
74           .getTable(), data);
75       Action phpSelectAction = new Action() {
76         public void run() {
77           PHPSelectRowPage page = new PHPSelectRowPage(""); //$NON-NLS-1$
78           SQLRowWizard wizard = new SQLRowWizard();
79           wizard.init(Messages.getString("TableView.UpdateRow"), page, row, ta); //$NON-NLS-1$
80           WizardDialog dialog = new WizardDialog(tableView.getSite().getShell()
81               .getShell(), wizard);
82           dialog.open();
83         }
84       };
85       phpSelectAction.setText(Messages.getString("tableview.phpselect")); //$NON-NLS-1$
86       Action phpUpdateAction = new Action() {
87         public void run() {
88           PHPUpdateRowPage page = new PHPUpdateRowPage(""); //$NON-NLS-1$
89           SQLRowWizard wizard = new SQLRowWizard();
90           wizard.init(Messages.getString("TableView.UpdateRow"), page, row, ta); //$NON-NLS-1$
91           WizardDialog dialog = new WizardDialog(
92               tableView.getSite().getShell(), wizard);
93           dialog.open();
94         }
95       };
96       phpUpdateAction.setText(Messages.getString("tableview.phpupdate")); //$NON-NLS-1$
97       Action phpInsertAction = new Action() {
98         public void run() {
99           PHPInsertRowPage page = new PHPInsertRowPage(""); //$NON-NLS-1$
100           SQLRowWizard wizard = new SQLRowWizard();
101           wizard.init(Messages.getString("TableView.InsertRow"), page, row, ta); //$NON-NLS-1$
102           WizardDialog dialog = new WizardDialog(
103               tableView.getSite().getShell(), wizard);
104           dialog.open();
105         }
106       };
107       phpInsertAction.setText(Messages.getString("tableview.phpinsert")); //$NON-NLS-1$
108       Action phpDeleteAction = new Action() {
109         public void run() {
110           PHPDeleteRowPage page = new PHPDeleteRowPage(""); //$NON-NLS-1$
111           SQLRowWizard wizard = new SQLRowWizard();
112           wizard.init(Messages.getString("TableView.DeleteRow"), page, row, ta); //$NON-NLS-1$
113           WizardDialog dialog = new WizardDialog(
114               tableView.getSite().getShell(), wizard);
115           dialog.open();
116         }
117       };
118       phpDeleteAction.setText(Messages.getString("tableview.phpdelete")); //$NON-NLS-1$
119       Action updateAction = new Action() {
120         public void run() {
121           UpdateRowPage page = new UpdateRowPage(""); //$NON-NLS-1$
122           SQLRowWizard wizard = new SQLRowWizard();
123           wizard.init(Messages.getString("TableView.UpdateRow"), page, row, ta); //$NON-NLS-1$
124           WizardDialog dialog = new WizardDialog(
125               tableView.getSite().getShell(), wizard);
126           dialog.open();
127         }
128       };
129       updateAction.setText(Messages.getString("tableview.update")); //$NON-NLS-1$
130       Action insertAction = new Action() {
131         public void run() {
132           InsertRowPage page = new InsertRowPage(""); //$NON-NLS-1$
133           SQLRowWizard wizard = new SQLRowWizard();
134           wizard.init(Messages.getString("TableView.InsertRow"), page, row, ta); //$NON-NLS-1$
135           WizardDialog dialog = new WizardDialog(
136               tableView.getSite().getShell(), wizard);
137           dialog.open();
138         }
139       };
140       insertAction.setText(Messages.getString("tableview.insert")); //$NON-NLS-1$
141       Action deleteAction = new Action() {
142         public void run() {
143           DeleteRowPage page = new DeleteRowPage(""); //$NON-NLS-1$
144           SQLRowWizard wizard = new SQLRowWizard();
145           wizard.init(Messages.getString("TableView.DeleteRow"), page, row, ta); //$NON-NLS-1$
146           WizardDialog dialog = new WizardDialog(
147               tableView.getSite().getShell(), wizard);
148           dialog.open();
149         }
150       };
151       deleteAction.setText(Messages.getString("tableview.delete")); //$NON-NLS-1$
152       mgr.add(phpSelectAction);
153       mgr.add(phpInsertAction);
154       mgr.add(phpUpdateAction);
155       mgr.add(phpDeleteAction);
156       mgr.add(insertAction);
157       mgr.add(updateAction);
158       mgr.add(deleteAction);
159       MenuManager subMenuExtension = new MenuManager("Extensions");
160       for (int i = 0; i < extensionVector.size(); i++) {
161         ExtensionAction extensionAction = (ExtensionAction) extensionVector
162             .get(i);
163         extensionAction.addRowData(row);
164         subMenuExtension.add(extensionAction);
165       }
166       if (extensionVector.size() > 0)
167         mgr.add(subMenuExtension);
168     }
169     mgr.add(defaultEncodingAction);
170     mgr.add(UTF8EncodingAction);
171     mgr.add(UTF16EncodingAction);
172   }
173 }