1 package com.quantum.view.tableview;
3 import java.util.Iterator;
4 import java.util.Vector;
6 import org.eclipse.jface.action.Action;
7 import org.eclipse.jface.action.IMenuManager;
8 import org.eclipse.jface.action.IToolBarManager;
9 import org.eclipse.jface.action.MenuManager;
10 import org.eclipse.jface.action.Separator;
11 import org.eclipse.jface.viewers.ISelectionProvider;
12 import org.eclipse.jface.viewers.IStructuredSelection;
13 import org.eclipse.jface.wizard.WizardDialog;
14 import org.eclipse.ui.IActionBars;
15 import org.eclipse.ui.IWorkbenchActionConstants;
16 import org.eclipse.ui.WorkbenchException;
17 import org.eclipse.ui.actions.ActionGroup;
18 import org.eclipse.ui.actions.SelectionListenerAction;
20 import com.quantum.ImageStore;
21 import com.quantum.Messages;
22 import com.quantum.extensions.ExtensionAction;
23 import com.quantum.extensions.ProcessServiceMembers;
24 import com.quantum.php.wizards.PHPDeleteRowPage;
25 import com.quantum.php.wizards.PHPInsertRowPage;
26 import com.quantum.php.wizards.PHPSelectRowPage;
27 import com.quantum.php.wizards.PHPUpdateRowPage;
28 import com.quantum.sql.SQLResultSetResults;
29 import com.quantum.sql.TableRow;
30 import com.quantum.util.StringMatrix;
31 import com.quantum.wizards.DeleteRowPage;
32 import com.quantum.wizards.InsertRowPage;
33 import com.quantum.wizards.SQLPage;
34 import com.quantum.wizards.SQLRowWizard;
35 import com.quantum.wizards.SortFilterPage;
36 import com.quantum.wizards.UpdateRowPage;
43 public class TableViewActionGroup extends ActionGroup {
45 abstract class SQLWizardAction extends SelectionListenerAction {
50 protected SQLWizardAction(String text, ISelectionProvider selectionProvider) {
52 selectionProvider.addSelectionChangedListener(this);
53 setEnabled(!selectionProvider.getSelection().isEmpty());
56 protected abstract SQLPage createSQLPage();
58 protected abstract String getTitle();
61 SQLPage page = createSQLPage();
62 SQLRowWizard wizard = new SQLRowWizard();
63 wizard.init(getTitle(), page, getSelectedSQLResults(), getSelectedRow());
64 WizardDialog dialog = new WizardDialog(
65 tableView.getSite().getShell(), wizard);
69 protected boolean updateSelection(IStructuredSelection selection) {
70 return selection != null && !selection.isEmpty();
74 class SortFilterAction extends SQLWizardAction {
76 public SortFilterAction(ISelectionProvider selectionProvider) {
77 super(Messages.getString(TableViewActionGroup.class, "filterSort"),
79 setImageDescriptor(ImageStore.getImageDescriptor(ImageStore.FILTER));
80 setToolTipText(Messages.getString(TableViewActionGroup.class, "filterSort"));
81 setEnabled(sortFilterApplies());
84 private boolean sortFilterApplies() {
85 SQLResultSetResults results = getSelectedSQLResults();
86 return results != null && !results.isMetaData() && results.getEntity() != null;
89 protected SQLPage createSQLPage() {
90 return new SortFilterPage("page1");
93 protected String getTitle() {
94 return Messages.getString(TableViewActionGroup.class, "filterSortTitle");
97 protected boolean updateSelection(IStructuredSelection selection) {
98 return sortFilterApplies();
102 class PHPInsertAction extends Action {
103 public PHPInsertAction() {
104 setText(Messages.getString("tableview.phpinsert"));
108 PHPInsertRowPage page = new PHPInsertRowPage(""); //$NON-NLS-1$
109 SQLRowWizard wizard = new SQLRowWizard();
110 wizard.init(Messages.getString("TableView.PHPInsertRow"),
111 page, getSelectedSQLResults(), null); //$NON-NLS-1$
112 WizardDialog dialog =
114 tableView.getSite().getShell(),
120 class PHPDeleteAction extends Action {
121 public PHPDeleteAction() {
122 setText(Messages.getString("tableview.phpdelete"));
126 PHPDeleteRowPage page = new PHPDeleteRowPage(""); //$NON-NLS-1$
127 SQLRowWizard wizard = new SQLRowWizard();
128 wizard.init(Messages.getString("TableView.PHPDeleteRow"),
129 page, getSelectedSQLResults(), getSelectedRow()); //$NON-NLS-1$
130 WizardDialog dialog =
132 tableView.getSite().getShell(),
137 class PHPSelectAction extends Action {
138 public PHPSelectAction() {
139 setText(Messages.getString("tableview.phpselect"));
143 PHPSelectRowPage page = new PHPSelectRowPage(""); //$NON-NLS-1$
144 SQLRowWizard wizard = new SQLRowWizard();
145 wizard.init(Messages.getString("TableView.PHPSelectRow"),
146 page, getSelectedSQLResults(), getSelectedRow()); //$NON-NLS-1$
147 WizardDialog dialog =
149 tableView.getSite().getShell(),
154 class PHPUpdateAction extends Action {
155 public PHPUpdateAction() {
156 setText(Messages.getString("tableview.phpupdate"));
160 PHPUpdateRowPage page = new PHPUpdateRowPage(""); //$NON-NLS-1$
161 SQLRowWizard wizard = new SQLRowWizard();
162 wizard.init(Messages.getString("TableView.PHPUpdateRow"),
163 page, getSelectedSQLResults(), getSelectedRow()); //$NON-NLS-1$
164 WizardDialog dialog =
166 tableView.getSite().getShell(),
171 class InsertAction extends Action {
172 public InsertAction() {
173 setText(Messages.getString("tableview.insert"));
177 InsertRowPage page = new InsertRowPage(""); //$NON-NLS-1$
178 SQLRowWizard wizard = new SQLRowWizard();
179 wizard.init(Messages.getString("TableView.InsertRow"),
180 page, getSelectedSQLResults(), null); //$NON-NLS-1$
181 WizardDialog dialog =
183 tableView.getSite().getShell(),
189 class DeleteAction extends Action {
190 public DeleteAction() {
191 setText(Messages.getString("tableview.delete"));
195 DeleteRowPage page = new DeleteRowPage(""); //$NON-NLS-1$
196 SQLRowWizard wizard = new SQLRowWizard();
197 wizard.init(Messages.getString("TableView.DeleteRow"),
198 page, getSelectedSQLResults(), getSelectedRow()); //$NON-NLS-1$
199 WizardDialog dialog =
201 tableView.getSite().getShell(),
207 class UpdateAction extends Action {
208 public UpdateAction() {
209 setText(Messages.getString("tableview.update"));
213 UpdateRowPage page = new UpdateRowPage(""); //$NON-NLS-1$
214 SQLRowWizard wizard = new SQLRowWizard();
215 wizard.init(Messages.getString("TableView.UpdateRow"),
216 page, getSelectedSQLResults(), getSelectedRow()); //$NON-NLS-1$
217 WizardDialog dialog =
219 tableView.getSite().getShell(),
226 private final TableView tableView;
227 private SelectionListenerAction closeAction;
228 private SelectionListenerAction closeAllAction;
229 private SelectionListenerAction nextAction;
230 private SelectionListenerAction previousAction;
231 private SelectionListenerAction refreshAction;
232 private SelectionListenerAction fullModeAction;
233 private SelectionListenerAction defaultEncodingAction;
234 private SelectionListenerAction utf8EncodingAction;
235 private SelectionListenerAction utf16EncodingAction;
237 private CopyAction copyAction;
238 private SelectAllAction selectAllAction;
239 private InsertAction insertRowAction;
240 private DeleteAction deleteRowAction;
241 private UpdateAction updateRowAction;
243 private PHPInsertAction phpInsertRowAction;
244 private PHPDeleteAction phpDeleteRowAction;
245 private PHPSelectAction phpSelectRowAction;
246 private PHPUpdateAction phpUpdateRowAction;
248 private SortFilterAction sortFilterAction;
250 private Vector extensionActions = new Vector();
252 public TableViewActionGroup(TableView tableView) {
253 this.tableView = tableView;
255 this.closeAction = new CloseResultSetAction(this.tableView, this.tableView);
256 this.closeAllAction = new CloseAllResultSetsAction(this.tableView, this.tableView);
257 this.defaultEncodingAction = new ChangeEncodingAction(this.tableView, this.tableView, "", "default");
258 this.utf8EncodingAction = new ChangeEncodingAction(this.tableView, this.tableView, "UTF-8", "utf8");
259 this.utf16EncodingAction = new ChangeEncodingAction(this.tableView, this.tableView, "UTF-16", "utf16");
260 this.nextAction = new NextPageAction(this.tableView, this.tableView);
261 this.previousAction = new PreviousPageAction(this.tableView, this.tableView);
262 this.refreshAction = new RefreshTableAction(this.tableView, this.tableView);
263 this.fullModeAction = new FullModeAction(this.tableView, this.tableView);
265 this.copyAction = new CopyAction(this.tableView);
266 this.selectAllAction = new SelectAllAction(this.tableView);
268 this.insertRowAction = new InsertAction();
269 this.deleteRowAction = new DeleteAction();
270 this.updateRowAction = new UpdateAction();
272 this.phpInsertRowAction = new PHPInsertAction();
273 this.phpDeleteRowAction = new PHPDeleteAction();
274 this.phpSelectRowAction = new PHPSelectAction();
275 this.phpUpdateRowAction = new PHPUpdateAction();
277 this.sortFilterAction = new SortFilterAction(this.tableView);
280 ProcessServiceMembers.process(tableView, this.extensionActions);
281 } catch (WorkbenchException e) {
286 public void fillActionBars(IActionBars actionBars) {
287 IToolBarManager toolBar = actionBars.getToolBarManager();
288 toolBar.add(this.previousAction);
289 toolBar.add(this.nextAction);
290 toolBar.add(this.fullModeAction);
291 toolBar.add(this.closeAction);
292 toolBar.add(this.closeAllAction);
293 toolBar.add(this.refreshAction);
294 toolBar.add(this.sortFilterAction);
296 actionBars.setGlobalActionHandler(
297 IWorkbenchActionConstants.COPY, this.copyAction);
298 actionBars.setGlobalActionHandler(
299 IWorkbenchActionConstants.SELECT_ALL, this.selectAllAction);
303 public void fillContextMenu(IMenuManager menuManager) {
304 menuManager.add(this.defaultEncodingAction);
305 menuManager.add(this.utf8EncodingAction);
306 menuManager.add(this.utf16EncodingAction);
307 menuManager.add(new Separator());
308 menuManager.add(this.copyAction);
309 menuManager.add(this.selectAllAction);
310 menuManager.add(new Separator());
312 SQLResultSetResults resultSet = getSelectedSQLResults();
314 if (resultSet != null && !resultSet.isMetaData() && resultSet.getEntity() != null) {
315 menuManager.add(this.insertRowAction);
316 menuManager.add(this.updateRowAction);
317 menuManager.add(this.deleteRowAction);
319 menuManager.add(this.phpSelectRowAction);
320 menuManager.add(this.phpUpdateRowAction);
321 menuManager.add(this.phpDeleteRowAction);
322 menuManager.add(this.phpInsertRowAction);
324 menuManager.add(new Separator());
327 createExtensionMenu(menuManager);
329 createMarkerForActionsProvidedByOtherPlugins(menuManager);
335 private SQLResultSetResults getSelectedSQLResults() {
336 return this.tableView.getSelectedResultSet();
339 protected SQLResultSetResults.Row getSelectedRow() {
340 IStructuredSelection selection = getTableRowSelection();
342 return selection == null || selection.isEmpty()
344 : (SQLResultSetResults.Row) selection.getFirstElement();
349 private IStructuredSelection getTableRowSelection() {
350 ResultSetViewer viewer = this.tableView.getSelectedResultSetViewer();
351 IStructuredSelection selection = viewer == null ? null : (IStructuredSelection) viewer.getSelection();
358 private void createExtensionMenu(IMenuManager menuManager) {
359 MenuManager subMenuExtension = new MenuManager("Extensions");
360 for (int i = 0; i < this.extensionActions.size(); i++) {
361 ExtensionAction extensionAction = (ExtensionAction) this.extensionActions.get(i);
362 extensionAction.addRowData(createTableRow());
363 subMenuExtension.add(extensionAction);
365 if (this.extensionActions.size() > 0) {
366 menuManager.add(subMenuExtension);
371 * This method supports an earlier API for other plug-ins to add functionality to
376 private TableRow createTableRow() {
378 SQLResultSetResults results = this.tableView.getSelectedResultSet();
379 IStructuredSelection selection = getTableRowSelection();
381 if (results != null) {
382 StringMatrix data = new StringMatrix();
383 data.addMatrixHeader(results.getColumnNames());
384 if (selection != null && !selection.isEmpty()) {
386 for (Iterator i = selection.iterator(); i.hasNext(); ) {
387 SQLResultSetResults.Row row = (SQLResultSetResults.Row) i.next();
388 for (int j = 0, length = results.getColumnCount(); j < length; j++) {
389 Object object = row.get(j);
390 data.addAt(results.getColumnName(j+1),
391 object == null ? null : object.toString(),
396 // Create dummy values in case nothing selected
397 for (int i = 0, length = results.getColumnCount(); i < length; i++) {
398 data.addAt(results.getColumnName(i+1), "", 0); //$NON-NLS-1$
402 return new TableRow(results.getEntity(), results.getBookmark(),
403 results.getEntity() == null
405 : results.getEntity().getQualifiedName(),
412 private void createMarkerForActionsProvidedByOtherPlugins(IMenuManager menuManager) {
413 menuManager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
414 menuManager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS + "-end")); //$NON-NLS-1$
415 menuManager.add(new Separator());