(refs #7362) Creation of new SCL modules in SCL module browser
[simantics/platform.git] / bundles / org.simantics.scl.ui / src / org / simantics / scl / ui / modulebrowser / SCLModuleBrowser.java
1 package org.simantics.scl.ui.modulebrowser;
2
3 import org.eclipse.jface.action.Action;
4 import org.eclipse.jface.action.IMenuListener;
5 import org.eclipse.jface.action.IMenuManager;
6 import org.eclipse.jface.action.IToolBarManager;
7 import org.eclipse.jface.action.MenuManager;
8 import org.eclipse.jface.viewers.DoubleClickEvent;
9 import org.eclipse.jface.viewers.IDoubleClickListener;
10 import org.eclipse.jface.viewers.ISelection;
11 import org.eclipse.jface.viewers.IStructuredSelection;
12 import org.eclipse.swt.SWT;
13 import org.eclipse.swt.widgets.Composite;
14 import org.eclipse.swt.widgets.Menu;
15 import org.eclipse.ui.part.ViewPart;
16 import org.simantics.scl.osgi.SCLOsgi;
17 import org.simantics.scl.ui.Activator;
18 import org.simantics.scl.ui.editor2.OpenSCLModule;
19
20 public class SCLModuleBrowser extends ViewPart {
21
22     SCLModuleTree content;
23     
24     @Override
25     public void createPartControl(Composite parent) {
26         this.content = new SCLModuleTree(parent, SWT.NONE, SCLOsgi.MODULE_REPOSITORY);
27         setPartName("SCL Modules");
28         
29         // Opening modules
30         content.addDoubleClickListener(new IDoubleClickListener() {
31             @Override
32             public void doubleClick(DoubleClickEvent event) {
33                 ISelection selection = event.getSelection();
34                 if(!(selection instanceof IStructuredSelection))
35                     return;
36                 ModuleNameTreeEntry entry = (ModuleNameTreeEntry)((IStructuredSelection)selection).getFirstElement();
37                 if(entry.isModule)
38                     OpenSCLModule.openModule(entry.fullName);
39             }
40         });
41         
42         // Toolbar
43         IToolBarManager toolBarManager = getViewSite().getActionBars().getToolBarManager();
44         toolBarManager.add(new Action("Refresh modules",
45                 Activator.imageDescriptorFromPlugin("org.simantics.scl.ui", "icons/arrow_refresh.png")) {
46             @Override
47             public void run() {
48                 refresh();
49             }
50         });
51         
52         // Context menu
53         MenuManager menuMgr = new MenuManager();
54         menuMgr.setRemoveAllWhenShown(true);
55         menuMgr.addMenuListener(new IMenuListener() {
56             public void menuAboutToShow(IMenuManager manager) {
57                 ModuleNameTreeEntry entry = (ModuleNameTreeEntry)content.getStructuredSelection().getFirstElement();
58                 if(!entry.isModule)
59                     manager.add(new Action("New Module...") {
60                         @Override
61                         public void run() {
62                             CreateModuleDialog dialog = new CreateModuleDialog(content.getControl().getShell(), SCLModuleBrowser.this);
63                             dialog.setPackage(entry.fullName);
64                             dialog.open();
65                         }
66                     });
67             }
68         });
69         Menu menu = menuMgr.createContextMenu(content.getControl());
70         content.getControl().setMenu(menu);
71         getSite().registerContextMenu(menuMgr, content);
72     }
73     
74     public void refresh() {
75         SCLOsgi.MODULE_REPOSITORY.getSourceRepository().checkUpdates();
76         content.recalculateInput();
77     }
78
79     @Override
80     public void setFocus() {
81         content.getControl().setFocus();
82     }
83
84 }