]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/ContextMenuInitializer.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.browsing.ui.swt / src / org / simantics / browsing / ui / swt / ContextMenuInitializer.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.browsing.ui.swt;
13
14 import org.eclipse.jface.action.GroupMarker;
15 import org.eclipse.jface.action.IMenuListener;
16 import org.eclipse.jface.action.IMenuManager;
17 import org.eclipse.jface.action.MenuManager;
18 import org.eclipse.jface.viewers.ISelectionProvider;
19 import org.eclipse.swt.widgets.Control;
20 import org.eclipse.swt.widgets.Menu;
21 import org.eclipse.ui.IWorkbenchActionConstants;
22 import org.eclipse.ui.IWorkbenchPartSite;
23 import org.eclipse.ui.IWorkbenchSite;
24 import org.eclipse.ui.part.IPageSite;
25
26 /**
27  * @author Tuukka Lehtonen
28  */
29 public class ContextMenuInitializer implements IContextMenuInitializer {
30
31     private final String text;
32     private final String id;
33
34     public ContextMenuInitializer(String id) {
35         this("", id);
36     }
37
38     public ContextMenuInitializer(String text, String id) {
39         this.text = text;
40         this.id = id;
41     }
42
43     @Override
44     public IMenuManager createContextMenu(Control control, ISelectionProvider selectionProvider, IWorkbenchSite site) {
45         final MenuManager mm = new MenuManager(text, id);
46
47         mm.setRemoveAllWhenShown(true);
48         mm.addMenuListener(new IMenuListener() {
49             @Override
50             public void menuAboutToShow(IMenuManager manager) {
51                 initializeOpenedMenu(manager);
52             }
53         });
54
55         Menu menu = mm.createContextMenu(control);
56         control.setMenu(menu);
57
58         if (site != null) {
59             if (site instanceof IWorkbenchPartSite) {
60                 ((IWorkbenchPartSite) site).registerContextMenu(mm.getId(), mm, selectionProvider);
61             } else if (site instanceof IPageSite) {
62                 ((IPageSite) site).registerContextMenu(mm.getId(), mm, selectionProvider);
63             }
64         }
65
66         return mm;
67     }
68
69     protected void initializeOpenedMenu(IMenuManager mm) {
70         mm.add(new GroupMarker(IWorkbenchActionConstants.WB_START));
71 //        mm.add(new GroupMarker(IWorkbenchActionConstants.NEW_EXT));
72 //        mm.add(new GroupMarker(IWorkbenchActionConstants.OPEN_EXT));
73 //        mm.add(new GroupMarker(IWorkbenchActionConstants.IMPORT_EXT));
74 //        mm.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
75 //        mm.add(new GroupMarker(IWorkbenchActionConstants.WB_END));
76         //mm.add(new Separator());
77     }
78
79 }