Hello All,
I have been struggling a lot... Here is my problem. I have a ViewPart that has a custom viewer. One of the components of this viewer is a CTabFolder, that contains a TreeViewer on each page. These pages are changed dynamically (listening to the EditorPart of the Workbench). I want these treeviewers to have a popup menu the elements of which are enabled or not according to the selection.
//---------------------------------------------------------- -------------------------
public class OntologyView extends ViewPart implements IPartListener2 {
...
@Override
public void createPartControl(Composite parent) {
this.ontologyViewer = new OntologyViewer(parent, SWT.NONE);
this.getSite().setSelectionProvider(this.ontologyViewer);
MenuManager menuMgr = new MenuManager();
menuMgr.setRemoveAllWhenShown(true);
final Menu menu = menuMgr.createContextMenu(this.ontologyViewer.getFolder());
this.ontologyViewer.getFolder().setMenu(menu);
getSite().registerContextMenu(menuMgr, this.ontologyViewer);
}
}
//---------------------------------------------------------- -------------------------
public class OntologyViewer extends Composite implements ISelectionProvider {
public OntologyViewer(Composite parent, int style) {
super(parent, style);
GridLayout layout = new GridLayout(2, false);
this.setLayout(layout);
this.folder = new CTabFolder(this, SWT.BORDER|SWT.BOTTOM|SWT.MULTI);
this.folder.setSimple(false);
// Create filter and search bar
this.filter = new StringFilter();
Label searchLabel = new Label(this, SWT.NONE);
searchLabel.setText("Limit view: ");
final Text searchText = new Text(this, SWT.BORDER | SWT.SEARCH);
searchText.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL));
searchText.addKeyListener(new KeyAdapter() {
public void keyReleased(KeyEvent ke) {
filter.setSearchText(searchText.getText());
// Apply the filter to all class trees
refreshAllTreeViewers(null);
}
});
// Layout the viewer
GridData gridData = new GridData();
gridData.verticalAlignment = GridData.FILL;
gridData.horizontalSpan = 2;
gridData.grabExcessHorizontalSpace = true;
gridData.grabExcessVerticalSpace = true;
gridData.horizontalAlignment = GridData.FILL;
this.folder.setLayoutData(gridData);
}
// This is called when the editor changes
public void refresh(IWorkbenchPartReference partRef) {
IWorkbenchPage page = partRef.getPage();
this.currentEditor = (AbstractEditor) page.getActiveEditor();
AbstractEditorInput input = (AbstractEditorInput) this.currentEditor.getEditorInput();
Model model = input.getDataset().getTBox().getModel();
CTabItem[] tabs = this.folder.getItems();
for (int i = 0; i < tabs.length; i++) {
CTabItem item = tabs[i];
item.dispose();
}
String[] propUriArray = input.getPropertyUriArray();
boolean isSelectionDone = false;
viewerList.clear();
for (int i = 0; i < propUriArray.length; i++) {
String propUri = propUriArray[i];
if (input.getConfig().getPropertyTypeName(propUri).equals("literalProperty")) {
continue;
}
CTabItem item = new CTabItem(this.folder, SWT.NONE);
Property property = model.createProperty(propUri);
String namespace = model.getNsURIPrefix(property.getNameSpace());
item.setText(namespace+":"+property.getLocalName()+" ");
ClassHierarchyViewer classHierarchyViewer = new ClassHierarchyViewer(this.folder, SWT.BORDER | SWT.SINGLE | SWT.FULL_SELECTION | SWT.V_SCROLL, property);
classHierarchyViewer.getTree().setHeaderVisible(true);
classHierarchyViewer.setUseHashlookup(true); // enables multiple parents
// Action
classHierarchyViewer.addDoubleClickListener(new DoubleClickListener(page, classHierarchyViewer) );
// Columns
TreeViewerColumn mainColumn = new TreeViewerColumn(classHierarchyViewer, SWT.NONE);
mainColumn.getColumn().setWidth(250);
mainColumn.getColumn().setText("Range Class by Property");
mainColumn.setLabelProvider(new MainColumnLabelProvider(this));
TreeViewerColumn bindingColumn = new TreeViewerColumn(classHierarchyViewer, SWT.TRAIL);
bindingColumn.getColumn().setWidth(100);
bindingColumn.getColumn().setText("Binding");
bindingColumn.setLabelProvider(new BindingColumnLabelProvider(this));
// Contents
ContentProvider contents = new ContentProvider();
classHierarchyViewer.setContentProvider(contents);
classHierarchyViewer.setInput(property);
classHierarchyViewer.expandAll();
// Add filter
classHierarchyViewer.addFilter(this.filter);
item.setControl(classHierarchyViewer.getControl());
this.treeControlMap.put(classHierarchyViewer.getControl(), classHierarchyViewer);
viewerList.add(classHierarchyViewer);
if (!isSelectionDone) {
this.folder.setSelection(item);
isSelectionDone = true;
}
}
}
...
// ISelectionProvider
@Override
public void addSelectionChangedListener(ISelectionChangedListener listener) {
// TODO Auto-generated method stub
}
@Override
public ISelection getSelection() {
CTabItem item = this.folder.getSelection();
try {
ClassHierarchyViewer viewer = this.treeControlMap.get(item.getControl());
return viewer.getSelection();
} catch (Exception e) {
return null;
}
}
@Override
public void removeSelectionChangedListener(ISelectionChangedListener listener) throws NullPointerException {
CTabItem item = this.folder.getSelection();
if (item == null) {
return;
}
ClassHierarchyViewer viewer = this.treeControlMap.get(item.getControl());
viewer.removeSelectionChangedListener(listener);
}
@Override
public void setSelection(ISelection selection) {
CTabItem item = this.folder.getSelection();
ClassHierarchyViewer viewer = this.treeControlMap.get(item.getControl());
viewer.setSelection(selection);
}
}
//---------------------------------------------------------- -------------------------
And the relevant parts of my plugin.xml:
<handler
class="net.nmzi.notabene.handlers.EditBindingHandler"
commandId="net.nmzi.notabene.commands.editBinding">
<activeWhen>
<or>
<with
variable="selection">
<iterate
operator="or">
<instanceof
value="com.hp.hpl.jena.rdf.model.Resource">
</instanceof>
</iterate>
</with>
<with
variable="activeMenuSelection">
<iterate
operator="or">
<instanceof
value="com.hp.hpl.jena.rdf.model.Resource">
</instanceof>
</iterate>
</with>
</or>
</activeWhen>
<enabledWhen>
<or>
<!-- <with
variable="selection">
<iterate
operator="or">
<instanceof
value="com.hp.hpl.jena.rdf.model.Resource">
</instanceof>
</iterate>
</with> -->
<with
variable="activeMenuSelection">
<iterate
operator="or">
<instanceof
value="com.hp.hpl.jena.rdf.model.Resource">
</instanceof>
</iterate>
</with>
</or>
</enabledWhen>
</handler>
...
<menuContribution
locationURI="popup:net.nmzi.notabene.views.ontology">
<command
commandId="net.nmzi.notabene.commands.editBinding"
style="push">
</command>
</menuContribution>
//---------------------------------------------------------- -------------------------
I cannot get the right-click to display the menu...
Any clue about this?
Thanks for any help!
Best,