Add predefined Velocity and CSS templates if wiki builder is assigned to a project
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / view / tableview / TableViewActionGroup.java
1 package com.quantum.view.tableview;
2
3 import java.util.Iterator;
4 import java.util.Vector;
5
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;
19
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;
37
38
39 /**
40  * @author Julen Parra
41  * @author BC Holmes
42  */
43 public class TableViewActionGroup extends ActionGroup {
44
45         abstract class SQLWizardAction extends SelectionListenerAction {
46                 
47                 /**
48                  * @param text
49                  */
50                 protected SQLWizardAction(String text, ISelectionProvider selectionProvider) {
51                         super(text);
52                         selectionProvider.addSelectionChangedListener(this);
53                         setEnabled(!selectionProvider.getSelection().isEmpty());
54                 }
55                 
56                 protected abstract SQLPage createSQLPage();
57                 
58                 protected abstract String getTitle();
59                 
60                 public void run() {
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);
66                         dialog.open();
67                 }
68                 
69                 protected boolean updateSelection(IStructuredSelection selection) {
70                         return selection != null && !selection.isEmpty();
71                 }
72         };
73         
74         class SortFilterAction extends SQLWizardAction {
75
76                 public SortFilterAction(ISelectionProvider selectionProvider) {
77                         super(Messages.getString(TableViewActionGroup.class, "filterSort"), 
78                                         selectionProvider);
79                         setImageDescriptor(ImageStore.getImageDescriptor(ImageStore.FILTER));
80                         setToolTipText(Messages.getString(TableViewActionGroup.class, "filterSort"));
81                         setEnabled(sortFilterApplies());
82                 }
83                 
84                 private boolean sortFilterApplies() {
85                         SQLResultSetResults results = getSelectedSQLResults();
86                         return results != null && !results.isMetaData() && results.getEntity() != null;
87                 }
88                 
89                 protected SQLPage createSQLPage() {
90                         return new SortFilterPage("page1");
91                 }
92
93                 protected String getTitle() {
94                         return Messages.getString(TableViewActionGroup.class, "filterSortTitle");
95                 }
96                 
97                 protected boolean updateSelection(IStructuredSelection selection) {
98                         return sortFilterApplies();
99                 }
100         }
101         
102         class PHPInsertAction extends Action {
103                 public PHPInsertAction() {
104                         setText(Messages.getString("tableview.phpinsert"));
105                 }
106                 
107                 public void run() {
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 =
113                                 new WizardDialog(
114                                         tableView.getSite().getShell(),
115                                         wizard);
116                         dialog.open();
117                 }
118         };
119         
120         class PHPDeleteAction extends Action {
121                 public PHPDeleteAction() {
122                         setText(Messages.getString("tableview.phpdelete"));
123                 }
124                 
125                 public void run() {
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 =
131                                 new WizardDialog(
132                                         tableView.getSite().getShell(),
133                                         wizard);
134                         dialog.open();
135                 }
136         };
137         class PHPSelectAction extends Action {
138                 public PHPSelectAction() {
139                         setText(Messages.getString("tableview.phpselect"));
140                 }
141                 
142                 public void run() {
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 =
148                                 new WizardDialog(
149                                         tableView.getSite().getShell(),
150                                         wizard);
151                         dialog.open();
152                 }
153         };
154         class PHPUpdateAction extends Action {
155                 public PHPUpdateAction() {
156                         setText(Messages.getString("tableview.phpupdate"));
157                 }
158                 
159                 public void run() {
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 =
165                                 new WizardDialog(
166                                         tableView.getSite().getShell(),
167                                         wizard);
168                         dialog.open();
169                 }
170         };
171         class InsertAction extends Action {
172                 public InsertAction() {
173                         setText(Messages.getString("tableview.insert"));
174                 }
175                 
176                 public void run() {
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 =
182                                 new WizardDialog(
183                                         tableView.getSite().getShell(),
184                                         wizard);
185                         dialog.open();
186                 }
187         };
188         
189         class DeleteAction extends Action {
190                 public DeleteAction() {
191                         setText(Messages.getString("tableview.delete"));
192                 }
193                 
194                 public void run() {
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 =
200                                 new WizardDialog(
201                                         tableView.getSite().getShell(),
202                                         wizard);
203                         dialog.open();
204                 }
205         };
206         
207         class UpdateAction extends Action {
208                 public UpdateAction() {
209                         setText(Messages.getString("tableview.update"));
210                 }
211                 
212                 public void run() {
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 =
218                                 new WizardDialog(
219                                         tableView.getSite().getShell(),
220                                         wizard);
221                         dialog.open();
222                 }
223         };
224         
225         
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;
236     
237     private CopyAction copyAction;
238     private SelectAllAction selectAllAction;
239     private InsertAction insertRowAction;
240     private DeleteAction deleteRowAction;
241     private UpdateAction updateRowAction;
242     
243     private PHPInsertAction phpInsertRowAction;
244     private PHPDeleteAction phpDeleteRowAction;
245     private PHPSelectAction phpSelectRowAction;
246     private PHPUpdateAction phpUpdateRowAction;
247     
248     private SortFilterAction sortFilterAction;
249     
250     private Vector extensionActions = new Vector();
251
252         public TableViewActionGroup(TableView tableView) {
253                 this.tableView = tableView;
254                 
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);
264                 
265                 this.copyAction = new CopyAction(this.tableView);
266                 this.selectAllAction = new SelectAllAction(this.tableView);
267                 
268                 this.insertRowAction = new InsertAction();
269                 this.deleteRowAction = new DeleteAction();
270                 this.updateRowAction = new UpdateAction();
271                 
272                 this.phpInsertRowAction = new PHPInsertAction();
273                 this.phpDeleteRowAction = new PHPDeleteAction();
274                 this.phpSelectRowAction = new PHPSelectAction();
275                 this.phpUpdateRowAction = new PHPUpdateAction();
276                 
277                 this.sortFilterAction = new SortFilterAction(this.tableView);
278                 
279                 try {
280                         ProcessServiceMembers.process(tableView, this.extensionActions);
281                 } catch (WorkbenchException e) {
282                         e.printStackTrace();
283                 }
284     }
285         
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);
295         
296         actionBars.setGlobalActionHandler(
297                         IWorkbenchActionConstants.COPY, this.copyAction);
298                 actionBars.setGlobalActionHandler(
299                                 IWorkbenchActionConstants.SELECT_ALL, this.selectAllAction);
300     }
301         
302     
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());
311
312         SQLResultSetResults resultSet = getSelectedSQLResults();
313         
314         if (resultSet != null && !resultSet.isMetaData() && resultSet.getEntity() != null) {
315                 menuManager.add(this.insertRowAction);
316                 menuManager.add(this.updateRowAction);
317                 menuManager.add(this.deleteRowAction);
318                 
319                 menuManager.add(this.phpSelectRowAction);
320                 menuManager.add(this.phpUpdateRowAction);
321                 menuManager.add(this.phpDeleteRowAction);
322                 menuManager.add(this.phpInsertRowAction);
323                 
324             menuManager.add(new Separator());
325         }
326         
327                 createExtensionMenu(menuManager);
328         
329         createMarkerForActionsProvidedByOtherPlugins(menuManager);
330         }
331
332     /**
333          * @return
334          */
335         private SQLResultSetResults getSelectedSQLResults() {
336                 return this.tableView.getSelectedResultSet();
337         }
338
339         protected SQLResultSetResults.Row getSelectedRow() {
340                 IStructuredSelection selection = getTableRowSelection();
341                 
342                 return selection == null || selection.isEmpty() 
343                                 ? null 
344                                 : (SQLResultSetResults.Row) selection.getFirstElement();
345         }
346         /**
347          * @return
348          */
349         private IStructuredSelection getTableRowSelection() {
350                 ResultSetViewer viewer = this.tableView.getSelectedResultSetViewer();
351                 IStructuredSelection selection = viewer == null ? null : (IStructuredSelection) viewer.getSelection();
352                 return selection;
353         }
354
355         /**
356          * @param menuManager
357          */
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);
364                 }
365                 if (this.extensionActions.size() > 0) {
366                         menuManager.add(subMenuExtension);
367                 }
368         }
369         
370         /**
371          * This method supports an earlier API for other plug-ins to add functionality to
372          * QuantumDB.
373          * 
374          * @return
375          */
376         private TableRow createTableRow() {
377                 
378                 SQLResultSetResults results = this.tableView.getSelectedResultSet();
379                 IStructuredSelection selection = getTableRowSelection();
380                 
381                 if (results != null) {
382                         StringMatrix data = new StringMatrix();
383                         data.addMatrixHeader(results.getColumnNames());
384                         if (selection != null && !selection.isEmpty()) {
385                                 int rowNumber = 0;
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(), 
392                                                                                 rowNumber++);
393                                         }
394                                 }
395                         } else {
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$
399                                 }
400                         }
401                         
402                         return new TableRow(results.getEntity(), results.getBookmark(), 
403                                         results.getEntity() == null 
404                                                         ? null 
405                                                         : results.getEntity().getQualifiedName(), 
406                                         data);
407                 } else {
408                         return null;
409                 }
410         }
411
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());
416     }
417 }