| package org.xtext.example.mydsl.ui.views; |
| |
| import org.eclipse.jface.action.Action; |
| import org.eclipse.jface.action.IMenuListener; |
| import org.eclipse.jface.action.IMenuManager; |
| import org.eclipse.jface.action.IToolBarManager; |
| import org.eclipse.jface.action.MenuManager; |
| import org.eclipse.jface.action.Separator; |
| import org.eclipse.jface.dialogs.MessageDialog; |
| import org.eclipse.jface.text.source.SourceViewer; |
| import org.eclipse.swt.widgets.Composite; |
| import org.eclipse.swt.widgets.Menu; |
| import org.eclipse.ui.IActionBars; |
| import org.eclipse.ui.ISharedImages; |
| import org.eclipse.ui.IWorkbenchActionConstants; |
| import org.eclipse.ui.PlatformUI; |
| import org.eclipse.ui.part.ViewPart; |
| import org.xtext.example.mydsl.ui.MyXtextEmbeddedEditorProvider; |
| |
| import com.google.inject.Inject; |
| |
| public class XtextSampleView extends ViewPart { |
| |
| /** |
| * The ID of the view as specified by the extension. |
| */ |
| public static final String ID = "org.xtext.example.mydsl.views.views.XtextSampleView"; |
| |
| private SourceViewer viewer; |
| private Action action1; |
| private Action action2; |
| |
| @Inject |
| protected MyXtextEmbeddedEditorProvider xtextEmbeddedEditorProvider; |
| /** |
| * The constructor. |
| */ |
| public XtextSampleView() { |
| } |
| |
| /** |
| * This is a callback that will allow us to create the viewer and initialize |
| * it. |
| */ |
| public void createPartControl(Composite parent) { |
| viewer = xtextEmbeddedEditorProvider.getXtextEmbeddedEditor(parent).getViewer(); |
| |
| makeActions(); |
| hookContextMenu(); |
| contributeToActionBars(); |
| } |
| |
| private void hookContextMenu() { |
| MenuManager menuMgr = new MenuManager("#PopupMenu"); |
| menuMgr.setRemoveAllWhenShown(true); |
| menuMgr.addMenuListener(new IMenuListener() { |
| public void menuAboutToShow(IMenuManager manager) { |
| XtextSampleView.this.fillContextMenu(manager); |
| } |
| }); |
| Menu menu = menuMgr.createContextMenu(viewer.getControl()); |
| viewer.getControl().setMenu(menu); |
| getSite().registerContextMenu(menuMgr, viewer); |
| } |
| |
| private void contributeToActionBars() { |
| IActionBars bars = getViewSite().getActionBars(); |
| fillLocalPullDown(bars.getMenuManager()); |
| fillLocalToolBar(bars.getToolBarManager()); |
| } |
| |
| private void fillLocalPullDown(IMenuManager manager) { |
| manager.add(action1); |
| manager.add(new Separator()); |
| manager.add(action2); |
| } |
| |
| private void fillContextMenu(IMenuManager manager) { |
| manager.add(action1); |
| manager.add(action2); |
| // Other plug-ins can contribute there actions here |
| manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS)); |
| } |
| |
| private void fillLocalToolBar(IToolBarManager manager) { |
| manager.add(action1); |
| manager.add(action2); |
| } |
| |
| private void makeActions() { |
| action1 = new Action() { |
| public void run() { |
| showMessage("Action 1 executed"); |
| } |
| }; |
| action1.setText("Action 1"); |
| action1.setToolTipText("Action 1 tooltip"); |
| action1.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages() |
| .getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK)); |
| |
| action2 = new Action() { |
| public void run() { |
| showMessage("Action 2 executed"); |
| } |
| }; |
| action2.setText("Action 2"); |
| action2.setToolTipText("Action 2 tooltip"); |
| action2.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages() |
| .getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK)); |
| } |
| |
| private void showMessage(String message) { |
| MessageDialog.openInformation(viewer.getControl().getShell(), |
| "Xtext Sample View", message); |
| } |
| |
| /** |
| * Passing the focus request to the viewer's control. |
| */ |
| public void setFocus() { |
| viewer.getControl().setFocus(); |
| } |
| } |