diff --git a/workspace/org.grammaticalframework.eclipse.ui/src/org/grammaticalframework/eclipse/ui/labeling/GFLabelProvider.java b/workspace/org.grammaticalframework.eclipse.ui/src/org/grammaticalframework/eclipse/ui/labeling/GFLabelProvider.java index e85760b6..ee361cf2 100644 --- a/workspace/org.grammaticalframework.eclipse.ui/src/org/grammaticalframework/eclipse/ui/labeling/GFLabelProvider.java +++ b/workspace/org.grammaticalframework.eclipse.ui/src/org/grammaticalframework/eclipse/ui/labeling/GFLabelProvider.java @@ -65,7 +65,7 @@ protected TextStyle getTypeTextStyle() { } /** - * Format a teo-part label, with the name regular and the type greyed and italic + * Format a two-part label, with the name regular and the type greyed and italic * @param name * @param type * @return diff --git a/workspace/org.grammaticalframework.eclipse.ui/src/org/grammaticalframework/eclipse/ui/views/GFLibraryTreeView.java b/workspace/org.grammaticalframework.eclipse.ui/src/org/grammaticalframework/eclipse/ui/views/GFLibraryTreeView.java index 24d96b85..ef176f5c 100644 --- a/workspace/org.grammaticalframework.eclipse.ui/src/org/grammaticalframework/eclipse/ui/views/GFLibraryTreeView.java +++ b/workspace/org.grammaticalframework.eclipse.ui/src/org/grammaticalframework/eclipse/ui/views/GFLibraryTreeView.java @@ -10,20 +10,29 @@ package org.grammaticalframework.eclipse.ui.views; import org.apache.log4j.Logger; -import org.eclipse.swt.graphics.Image; +import org.eclipse.swt.events.ModifyEvent; +import org.eclipse.swt.events.ModifyListener; +import org.eclipse.swt.layout.GridData; +import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Text; import org.eclipse.ui.ide.IDE; +import org.eclipse.ui.ide.ResourceUtil; import org.eclipse.ui.part.*; import org.eclipse.jface.viewers.*; import org.eclipse.jface.action.*; import org.eclipse.ui.*; +import org.eclipse.xtext.resource.IEObjectDescription; +import org.eclipse.xtext.ui.editor.XtextEditor; +import org.eclipse.xtext.ui.editor.model.XtextDocument; +import org.eclipse.xtext.ui.resource.IResourceSetProvider; import org.eclipse.swt.widgets.Menu; import org.eclipse.swt.SWT; import org.eclipse.core.resources.IFile; -import org.eclipse.core.resources.IFolder; -import org.grammaticalframework.eclipse.scoping.GFScopingHelper; -import org.grammaticalframework.eclipse.ui.labeling.GFImages; - +import org.eclipse.core.resources.IResource; +import org.eclipse.emf.common.util.URI; +import org.eclipse.emf.ecore.resource.Resource; +import org.eclipse.emf.ecore.resource.ResourceSet; import com.google.inject.Inject; /** @@ -43,11 +52,7 @@ public class GFLibraryTreeView extends ViewPart { */ protected static final Logger log = Logger.getLogger(GFLibraryTreeView.class); - /** - * Image helper - */ - @Inject - private GFImages images; + private Text searchField; private TreeViewer viewer; // private DrillDownAdapter drillDownAdapter; @@ -58,23 +63,52 @@ public class GFLibraryTreeView extends ViewPart { private IPartListener2 listener; @Inject - FolderContentsTreeContentProvider contentProvider; + GFScopeContentProvider contentProvider; + + @Inject + GFScopeLabelProvider labelProvider; @Inject TreeSorter sorter; + + @Inject + private IResourceSetProvider resourceSetProvider; /** * This is a callback that will allow us to create the viewer and initialize * it. */ public void createPartControl(Composite parent) { - viewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL); + + parent.setLayout(new GridLayout(1, true)); + + searchField = new Text(parent, SWT.BORDER | SWT.SEARCH | SWT.ICON_SEARCH | SWT.ICON_CANCEL ); + searchField.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false)); + searchField.addModifyListener(new ModifyListener() { + @Override + public void modifyText(ModifyEvent e) { + // TODO Auto-generated method stub + viewer.refresh(); + } + }); + + viewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER); + viewer.getControl().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); // drillDownAdapter = new DrillDownAdapter(viewer); viewer.setContentProvider(contentProvider); - viewer.setLabelProvider(new TreeLabelProvider() { + viewer.setLabelProvider(labelProvider); + viewer.addFilter(new ViewerFilter() { @Override - public Image getImage(Object element) { - return images.forLibraryReference(); + public boolean select(Viewer viewer, Object parentElement, Object element) { + String search = searchField.getText().trim().toLowerCase(); + if (search.isEmpty()) { + return true; + } + if (element instanceof IEObjectDescription) { + String name = ((IEObjectDescription)element).getName().toString().toLowerCase(); + return (name.contains(search)); + } + return true; } }); // viewer.setSorter(new NameSorter()); @@ -82,8 +116,7 @@ public Image getImage(Object element) { viewer.setInput(null); // our listener below will take care of this // Create the help context id for the viewer's control - // PlatformUI.getWorkbench().getHelpSystem().setHelp(viewer.getControl(), - // "org.grammaticalframework.eclipse.ui.viewer"); + // PlatformUI.getWorkbench().getHelpSystem().setHelp(viewer.getControl(), "org.grammaticalframework.eclipse.ui.viewer"); makeActions(); hookContextMenu(); hookDoubleClickAction(); @@ -92,14 +125,31 @@ public Image getImage(Object element) { // Add a listener which updates the view each time the active editor is changed listener = new IPartListener2() { public void partActivated(IWorkbenchPartReference partRef) { + + // TODO Put this in an async block + try { + // TODO Check the file is a GF source file + IEditorPart editor = partRef.getPage().getActiveEditor(); - IEditorInput input = editor.getEditorInput(); - if (input instanceof IFileEditorInput) { - IFile file = ((IFileEditorInput) input).getFile(); - IFolder extFolder = GFScopingHelper.getExernalFolder(file); - viewer.setInput(extFolder); - } + XtextEditor xEditor = (XtextEditor)editor; + XtextDocument doc = (XtextDocument)xEditor.getDocument(); + URI uri = doc.getResourceURI(); + IResource iresource = ResourceUtil.getResource(editor.getEditorInput()); + ResourceSet resourceSet = resourceSetProvider.get(iresource.getProject()); + Resource resource = resourceSet.getResource(uri, true); + viewer.setInput(resource); + +// IEditorInput input = editor.getEditorInput(); +// if (input instanceof IFileEditorInput) { +// IFile file = ((IFileEditorInput) input).getFile(); +// +// // TODO Check the file is a GF source file +// viewer.setInput(file); +// +//// IFolder extFolder = GFScopingHelper.getExernalFolder(file); +//// viewer.setInput(extFolder); +// } } catch (Exception _) { viewer.setInput(null); } diff --git a/workspace/org.grammaticalframework.eclipse.ui/src/org/grammaticalframework/eclipse/ui/views/GFScopeContentProvider.java b/workspace/org.grammaticalframework.eclipse.ui/src/org/grammaticalframework/eclipse/ui/views/GFScopeContentProvider.java new file mode 100644 index 00000000..fc8d6b31 --- /dev/null +++ b/workspace/org.grammaticalframework.eclipse.ui/src/org/grammaticalframework/eclipse/ui/views/GFScopeContentProvider.java @@ -0,0 +1,143 @@ +/** + * GF Eclipse Plugin + * http://www.grammaticalframework.org/eclipse/ + * John J. Camilleri, 2012 + * + * The research leading to these results has received funding from the + * European Union's Seventh Framework Programme (FP7/2007-2013) under + * grant agreement no. FP7-ICT-247914. + */ +package org.grammaticalframework.eclipse.ui.views; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.eclipse.emf.ecore.resource.Resource; +import org.eclipse.jface.viewers.ITreeContentProvider; +import org.eclipse.jface.viewers.Viewer; +import org.eclipse.xtext.resource.IEObjectDescription; +import org.eclipse.xtext.scoping.IGlobalScopeProvider; +import org.eclipse.xtext.scoping.IScope; +import org.grammaticalframework.eclipse.gF.GFPackage; +import org.grammaticalframework.eclipse.scoping.TagEntry; + +import com.google.inject.Inject; + +public class GFScopeContentProvider implements ITreeContentProvider { + + class ModuleItem { + private String name; + + public ModuleItem(String name) { + super(); + this.name = name; + } + + public String getName() { + return name; + } + + /* Code below auto-generated by Eclipse */ + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + getOuterType().hashCode(); + result = prime * result + ((name == null) ? 0 : name.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + ModuleItem other = (ModuleItem) obj; + if (!getOuterType().equals(other.getOuterType())) + return false; + if (name == null) { + if (other.name != null) + return false; + } else if (!name.equals(other.name)) + return false; + return true; + } + + private GFScopeContentProvider getOuterType() { + return GFScopeContentProvider.this; + } + + } + + @Inject + IGlobalScopeProvider scopeProvider; + + private Map> tagMap; + + @Override + public void dispose() { + // TODO Auto-generated method stub + + } + + @Override + public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { + tagMap = new HashMap>(); + + if (newInput == null) + return; + + // Get scope + Resource resource = (Resource)newInput; + IScope scope = scopeProvider.getScope(resource, GFPackage.Literals.EXP__IDENT, null); + Iterable items = scope.getAllElements(); + + // Organise into modules + for (IEObjectDescription ieObjectDescription : items) { + ModuleItem module = new ModuleItem(ieObjectDescription.getUserData(TagEntry.USER_DATA_KEY_MODULENAME)); + if (!tagMap.containsKey(module)) { + tagMap.put(module, new ArrayList()); + } + tagMap.get(module).add(ieObjectDescription); + } + } + + /** + * Get list module names in the scope + */ + @Override + public Object[] getElements(Object inputElement) { + return tagMap.keySet().toArray(); + } + + /** + * Get the children of any module in the scope + */ + @Override + public Object[] getChildren(Object parentElement) { + if (parentElement instanceof ModuleItem) { + List items = tagMap.get(parentElement); + return items.toArray(); + } else { + return null; + } + } + + @Override + public Object getParent(Object element) { + // TODO Auto-generated method stub + return null; + } + + @Override + public boolean hasChildren(Object element) { + return (element instanceof ModuleItem); + } + +} diff --git a/workspace/org.grammaticalframework.eclipse.ui/src/org/grammaticalframework/eclipse/ui/views/GFScopeLabelProvider.java b/workspace/org.grammaticalframework.eclipse.ui/src/org/grammaticalframework/eclipse/ui/views/GFScopeLabelProvider.java new file mode 100644 index 00000000..dfd01333 --- /dev/null +++ b/workspace/org.grammaticalframework.eclipse.ui/src/org/grammaticalframework/eclipse/ui/views/GFScopeLabelProvider.java @@ -0,0 +1,60 @@ +/** + * GF Eclipse Plugin + * http://www.grammaticalframework.org/eclipse/ + * John J. Camilleri, 2012 + * + * The research leading to these results has received funding from the + * European Union's Seventh Framework Programme (FP7/2007-2013) under + * grant agreement no. FP7-ICT-247914. + */ +package org.grammaticalframework.eclipse.ui.views; + +import org.eclipse.jface.viewers.StyledCellLabelProvider; +import org.eclipse.jface.viewers.StyledString; +import org.eclipse.jface.viewers.ViewerCell; +import org.eclipse.xtext.resource.IEObjectDescription; +import org.grammaticalframework.eclipse.scoping.TagEntry; +import org.grammaticalframework.eclipse.ui.labeling.GFImages; +import org.grammaticalframework.eclipse.ui.labeling.GFLabelProvider; +import org.grammaticalframework.eclipse.ui.views.GFScopeContentProvider.ModuleItem; + +import com.google.inject.Inject; + +public class GFScopeLabelProvider extends StyledCellLabelProvider { + + /** + * Image helper + */ + @Inject + private GFImages images; + + @Inject + private GFLabelProvider labelProvider; + + @Override + public void update(ViewerCell cell) { + if (cell.getElement() instanceof ModuleItem) { + ModuleItem moduleItem = (ModuleItem)cell.getElement(); + + cell.setImage(images.forLibraryReference()); + cell.setText(moduleItem.getName()); + + } else if (cell.getElement() instanceof IEObjectDescription) { + IEObjectDescription item = (IEObjectDescription)cell.getElement(); + +// IEObjectDescription item = (IEObjectDescription)element; +// item.getUserData(TagEntry.USER_DATA_KEY_TYPE); + cell.setImage(images.forAnyJudgement()); + + String name = item.getName().toString(); + String type = item.getUserData(TagEntry.USER_DATA_KEY_ARGS); + StyledString styledString = labelProvider.twoPartLabel(name, type); + + cell.setText(styledString.getString()); + cell.setStyleRanges(styledString.getStyleRanges()); + } + + super.update(cell); + } + +}