initial quantum version
[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
7 import java.util.Vector;
8
9 import com.quantum.Messages;
10 import com.quantum.extensions.ExtensionAction;
11 import com.quantum.sql.TableRow;
12 import com.quantum.util.StringMatrix;
13 import com.quantum.wizards.DeleteRowPage;
14 import com.quantum.wizards.InsertRowPage;
15 import com.quantum.wizards.SQLRowWizard;
16 import com.quantum.wizards.UpdateRowPage;
17
18 import org.eclipse.jface.action.Action;
19 import org.eclipse.jface.action.IMenuListener;
20 import org.eclipse.jface.action.IMenuManager;
21 import org.eclipse.jface.action.MenuManager;
22 import org.eclipse.jface.wizard.WizardDialog;
23 import org.eclipse.swt.widgets.Table;
24 import org.eclipse.swt.widgets.TableColumn;
25 import org.eclipse.swt.widgets.TableItem;
26
27
28 public final class TableViewMenuListener implements IMenuListener {
29         private final TableView tableView;
30         private final Table table;
31         private final Action UTF16EncodingAction;
32         private final TableAdapter ta;
33         private final Action defaultEncodingAction;
34         private final Action UTF8EncodingAction;
35         private final Vector extensionVector;
36         
37         public TableViewMenuListener(
38                 TableView view, 
39                 Table table, 
40                 Action UTF16EncodingAction, 
41                 TableAdapter ta, 
42                 Action defaultEncodingAction, 
43                 Action UTF8EncodingAction,
44                 Vector extensionVector ) {
45                         
46                 super();
47                 this.tableView = view;
48                 this.table = table;
49                 this.UTF16EncodingAction = UTF16EncodingAction;
50                 this.ta = ta;
51                 this.defaultEncodingAction = defaultEncodingAction;
52                 this.UTF8EncodingAction = UTF8EncodingAction;
53                 this.extensionVector = extensionVector;
54         }
55         public void menuAboutToShow(IMenuManager mgr) {
56                 if (ta.getTable() != null) {
57                         TableItem[] selection = table.getSelection();
58                         TableColumn[] columns = table.getColumns();
59                         // Copy in columnNames the names of the columns of the selected rows
60                         String columnNames[] = new String[columns.length];
61                         for (int i = 0; i < columns.length; i++) {
62                                 columnNames[i] = columns[i].getText();
63                         }
64                         // Copy in data the values of the data columns of the selected rows
65                         StringMatrix data = new StringMatrix();
66                         data.addMatrixHeader(columnNames);
67                         if (selection != null && selection.length > 0) {
68                                 for (int iRow = 0; iRow < selection.length; iRow++) {
69                                         TableItem sel = selection[iRow];
70                                         for (int i = 0; i < columns.length; i++) {
71                                                 data.addAt(columnNames[i], sel.getText(i), iRow );
72                                         }
73                                 }
74                         } else {
75                                 // Create dummy values in case nothing selected
76                                 for (int i = 0; i < columns.length; i++) {
77                                         data.addAt(columnNames[i], "", 0); //$NON-NLS-1$
78                                 }
79                         }
80                         final TableRow row =
81                                 new TableRow(ta.getEntity(), ta.getBookmark(), ta.getTable(), data);
82                         Action updateAction = new Action() {
83                                 public void run() {
84                                         UpdateRowPage page = new UpdateRowPage(""); //$NON-NLS-1$
85                                         SQLRowWizard wizard = new SQLRowWizard();
86                                         wizard.init(Messages.getString("TableView.UpdateRow"), page, row, ta); //$NON-NLS-1$
87                                         WizardDialog dialog =
88                                                 new WizardDialog(
89                                                         tableView.getSite().getShell(),
90                                                         wizard);
91                                         dialog.open();
92                                 }
93                         };
94                         updateAction.setText(Messages.getString("tableview.update")); //$NON-NLS-1$
95                         Action insertAction = new Action() {
96                                 public void run() {
97                                         InsertRowPage page = new InsertRowPage(""); //$NON-NLS-1$
98                                         SQLRowWizard wizard = new SQLRowWizard();
99                                         wizard.init(Messages.getString("TableView.InsertRow"), page, row, ta); //$NON-NLS-1$
100                                         WizardDialog dialog =
101                                                 new WizardDialog(
102                                                         tableView.getSite().getShell(),
103                                                         wizard);
104                                         dialog.open();
105                                 }
106                         };
107                         insertAction.setText(Messages.getString("tableview.insert")); //$NON-NLS-1$
108                         Action deleteAction = new Action() {
109                                 public void run() {
110                                         DeleteRowPage page = new DeleteRowPage(""); //$NON-NLS-1$
111                                         SQLRowWizard wizard = new SQLRowWizard();
112                                         wizard.init(Messages.getString("TableView.DeleteRow"), page, row, ta); //$NON-NLS-1$
113                                         WizardDialog dialog =
114                                                 new WizardDialog(
115                                         tableView.getSite().getShell(),
116                                                         wizard);
117                                         dialog.open();
118                                 }
119                         };
120                         deleteAction.setText(Messages.getString("tableview.delete")); //$NON-NLS-1$
121                         mgr.add(insertAction);
122                         mgr.add(updateAction);
123                         mgr.add(deleteAction);
124
125                         MenuManager subMenuExtension = new MenuManager("Extensions"); 
126                         for (int i = 0; i < extensionVector.size(); i++) {
127                                 ExtensionAction extensionAction = (ExtensionAction) extensionVector.get(i);
128                                 extensionAction.addRowData(row);
129                                 subMenuExtension.add(extensionAction);
130                         }
131                         if (extensionVector.size() > 0) mgr.add(subMenuExtension);
132
133                 }
134                 mgr.add(defaultEncodingAction);
135                 mgr.add(UTF8EncodingAction);
136                 mgr.add(UTF16EncodingAction);
137         }
138 }