How to set the input for the tree view? [message #418540] |
Fri, 18 April 2008 00:49 |
Eclipse User |
|
|
|
Originally posted by: hufangjian31.hotmail.com
Hi,all
I am a newcomer of Eclipse. Now I am working on a tree view which can
display the object in your model. For example, assume we create a library
model that a library have several books. we save it as the name
"MYLIBRARY.library" in a local foder. Now I want my tree view to display
this file and to display the books in this "library" (just like in the EMF
editor and the Content outline View)
I create my class to extend the "Viewpart", but how can can I implement
the "createPartControl" method. I do in two ways but both of them don't
work
1:
TreeViewer tv = new TreeViewer(parent);
tv.setContentProvider(new MNViewContentProvider());
tv.setLabelProvider(new MNViewLabelProvider());
tv.setInput(new File("D:\\local"));
2:
TreeViewer tv = new TreeViewer(parent);
tv.setContentProvider(new MNViewContentProvider());
tv.setLabelProvider(new MNViewLabelProvider());
ResourceSet resourceSet = new ResourceSetImpl();
resourceSet.getResourceFactoryRegistry().getExtensionToFacto ryMap().put( "library",
new EcoreResourceFactoryImpl());
Resource resource =
resourceSet.createResource(URI.createPlatformResourceURI("d:/local "));
tv.setInput(resourceSet);
I have the MNViewContentProvider,MNViewLabelProvider.
in way1 my tree view can display the files in the folder but it seems the
view "regards" them as normal file but not a EMF file.
in way2 it don't work neither.
Is there anybody can help me out?
Thank you very much!
|
|
|
|
Re: How to set the input for the tree view? [message #418553 is a reply to message #418540] |
Fri, 18 April 2008 10:42 |
Ed Merks Messages: 33142 Registered: July 2009 |
Senior Member |
|
|
This is a multi-part message in MIME format.
--------------070902010902060409020904
Content-Type: text/plain; charset=ISO-8859-15; format=flowed
Content-Transfer-Encoding: 7bit
Fangjian,
Comments below.
Fangjian wrote:
> Hi,all
> I am a newcomer of Eclipse. Now I am working on a tree view which can
> display the object in your model. For example, assume we create a
> library model that a library have several books. we save it as the
> name "MYLIBRARY.library" in a local foder. Now I want my tree view to
> display this file and to display the books in this "library" (just
> like in the EMF editor and the Content outline View)
> I create my class to extend the "Viewpart", but how can can I
> implement the "createPartControl" method. I do in two ways but both of
> them don't work
> 1:
> TreeViewer tv = new TreeViewer(parent);
> tv.setContentProvider(new MNViewContentProvider());
> tv.setLabelProvider(new MNViewLabelProvider());
> tv.setInput(new File("D:\\local"));
> 2:
> TreeViewer tv = new TreeViewer(parent);
> tv.setContentProvider(new MNViewContentProvider());
> tv.setLabelProvider(new MNViewLabelProvider());
> ResourceSet resourceSet = new ResourceSetImpl();
> resourceSet.getResourceFactoryRegistry().getExtensionToFacto ryMap().put( "library",
> new EcoreResourceFactoryImpl());
> Resource resource =
> resourceSet.createResource(URI.createPlatformResourceURI("d:/local "));
> tv.setInput(resourceSet);
If you look in the generated editor, you'll see it creates a composed
factory and that his composed factory includes not only the item
provider for your generated model, but also the item provider
adapterFactory = new
ComposedAdapterFactory(ComposedAdapterFactory.Descriptor.Reg istry.INSTANCE);
adapterFactory.addAdapterFactory(new
*ResourceItemProviderAdapterFactory*());
adapterFactory.addAdapterFactory(new
LibraryItemProviderAdapterFactory());
This allows it to create adapters for viewing not only instances from
the Library model, but also instances of ResourceSet and Resource.
Even if you do this, you'd need to add support for viewing instance of
java.io.File if you took approach 1. EMF doesn't support resources that
correspond to folders, so approach 2 doesn't really make sense.
>
> I have the MNViewContentProvider,MNViewLabelProvider.
> in way1 my tree view can display the files in the folder but it seems
> the view "regards" them as normal file but not a EMF file.
I wonder what you did to make folders show child files?
> in way2 it don't work neither.
>
> Is there anybody can help me out? Thank you very much!
If you do come up with a mechanism to show nested folders with files
(which shouldn't be too hard), when you get to the files, to show
children under them, you'd want to use
resourceSet.getResource(URI.createFileURI(<fileSystemPath>), true) to
get the corresponding loaded EMF resource and that's the object you'd
want to display instead of just a java.io.File object. Note that
UI.createPlatformResourceURI should be used only with paths like
/project/file.extension that correspond to paths within the Eclipse
workspace...
>
>
--------------070902010902060409020904
Content-Type: text/html; charset=ISO-8859-15
Content-Transfer-Encoding: 8bit
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-15"
http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Fangjian,<br>
<br>
Comments below.<br>
<br>
Fangjian wrote:
<blockquote
cite="mid:6ca5d114387f8b7e5d16793fc63bd6da$1@www.eclipse.org"
type="cite">Hi,all
<br>
I am a newcomer of Eclipse. Now I am working on a tree view which can
display the object in your model. For example, assume we create a
library model that a library have several books. we save it as the name
"MYLIBRARY.library" in a local foder. Now I want my tree view to
display this file and to display the books in this "library" (just like
in the EMF editor and the Content outline View)
<br>
I create my class to extend the "Viewpart", but how can can I implement
the "createPartControl" method. I do in two ways but both of them don't
work
<br>
1:
<br>
TreeViewer tv = new TreeViewer(parent);
<br>
tv.setContentProvider(new MNViewContentProvider());
<br>
tv.setLabelProvider(new MNViewLabelProvider());
<br>
tv.setInput(new File("D:\\local"));
<br>
2:
<br>
TreeViewer tv = new TreeViewer(parent);
<br>
tv.setContentProvider(new MNViewContentProvider());
<br>
tv.setLabelProvider(new MNViewLabelProvider());
<br>
ResourceSet resourceSet = new ResourceSetImpl();
<br>
resourceSet.getResourceFactoryRegistry().getExtensionToFacto ryMap().put( "library",
new EcoreResourceFactoryImpl());
<br>
Resource resource =
resourceSet.createResource(URI.createPlatformResourceURI("d:/local "));
<br>
tv.setInput(resourceSet);
<br>
</blockquote>
If you look in the generated editor, you'll see it creates a composed
factory and that his composed factory includes not only the item
provider for your generated model, but also the item provider <br>
<br>
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
|
Re: How to set the input for the tree view? [message #418566 is a reply to message #418553] |
Fri, 18 April 2008 22:15 |
Eclipse User |
|
|
|
Originally posted by: hufangjian31.hotmail.com
Hi,
In fact I tried to create the composed factory(I attach the code in the
following),and add a content provider. if I change my view class to
"org.eclipse.ui.navigator.CommonNavigator" my view can display like a EMF
editor. But I want to focus on one folder's files and I want to add drag
and drop support to my View. The commonNavigator Class can not do this.
if I use my own view class, the view can't display anything.
Please take a look at my codes, is do I need to add some extra codes to
let my view to display the files and the nodes in the file?
here is the code(I follow the article "Build an Eclipse plug-in to
navigate content in an EMF model" and change something to fit my
application):
public class LibraryView extends ViewPart {
...
public void createPartControl(Composite parent) {
// TODO Auto-generated method stub
TreeViewer tv = new TreeViewer(parent);
tv.setContentProvider(new MNViewContentProvider());
tv.setLabelProvider(new MNViewLabelProvider());
tv.setInput(new File("D:\\local"));
//return tv.getTree();
...
}
public class MNComposedAdapterFactory
{
private static ComposedAdapterFactory mnCompAdapterFactory;
public final static ComposedAdapterFactory getAdapterFactory()
{
//if (mnCompAdapterFactory == null)
mnCompAdapterFactory = new
ComposedAdapterFactory(createFactoryList());
return mnCompAdapterFactory;
}
public final static ArrayList<AdapterFactory> createFactoryList()
{
ArrayList<AdapterFactory> factories = new
ArrayList<AdapterFactory>();
factories.add(new ResourceItemProviderAdapterFactory());
factories.add(new LibraryItemProviderAdapterFactory());
factories.add(new EcoreItemProviderAdapterFactory());
factories.add(new ReflectiveItemProviderAdapterFactory());
// factories.add(new File(""));
return factories;
}
}
public class CopyOfMNViewContentProvider extends
AdapterFactoryContentProvider {
public CopyOfMNViewContentProvider()
{
super(MNComposedAdapterFactory.getAdapterFactory());
}
private static ResourceSetImpl resourceSet = new ResourceSetImpl();
public Object[] getChildren(Object parentElement)
{
if (parentElement instanceof IFile)
{
String path = ((IFile)parentElement).getFullPath().toString();
URI uri = URI.createPlatformResourceURI(path, true);
parentElement = resourceSet.getResource(uri, true);
}
return super.getChildren(parentElement);
}
public Object[] getElements(Object inputElement)
{
return getChildren(inputElement);
}
public Object getParent(Object element)
{
if (element instanceof IFile)
return ((IResource)element).getParent();
return super.getParent(element);
}
public boolean hasChildren(Object element)
{
if (element instanceof IFile)
return true;
return super.hasChildren(element);
}
}
|
|
|
Re: How to set the input for the tree view? [message #418568 is a reply to message #418566] |
Sat, 19 April 2008 11:15 |
Ed Merks Messages: 33142 Registered: July 2009 |
Senior Member |
|
|
Fangjian,
What object do you use as input to the view? Do you need support for
IFolder to show children of folder? Have you set a breakpoint on
getChildren to see what kind of input object shows up and then trace the
execution path to how it's handled (keeping in mind that EMF itself will
only handle Resource, ResourceSet, and EObjects).
Fangjian wrote:
> Hi,
> In fact I tried to create the composed factory(I attach the code in
> the following),and add a content provider. if I change my view class
> to "org.eclipse.ui.navigator.CommonNavigator" my view can display
> like a EMF editor. But I want to focus on one folder's files and I
> want to add drag and drop support to my View. The commonNavigator
> Class can not do this. if I use my own view class, the view can't
> display anything.
> Please take a look at my codes, is do I need to add some extra codes
> to let my view to display the files and the nodes in the file?
>
> here is the code(I follow the article "Build an Eclipse plug-in to
> navigate content in an EMF model" and change something to fit my
> application):
> public class LibraryView extends ViewPart {
> ...
> public void createPartControl(Composite parent) {
> // TODO Auto-generated method stub
> TreeViewer tv = new TreeViewer(parent);
> tv.setContentProvider(new MNViewContentProvider());
> tv.setLabelProvider(new MNViewLabelProvider());
> tv.setInput(new File("D:\\local"));
> //return tv.getTree();
> ...
> }
>
> public class MNComposedAdapterFactory
> {
> private static ComposedAdapterFactory mnCompAdapterFactory;
>
> public final static ComposedAdapterFactory getAdapterFactory()
> {
> //if (mnCompAdapterFactory == null)
> mnCompAdapterFactory = new
> ComposedAdapterFactory(createFactoryList());
> return mnCompAdapterFactory;
> }
>
> public final static ArrayList<AdapterFactory> createFactoryList()
> {
> ArrayList<AdapterFactory> factories = new
> ArrayList<AdapterFactory>();
> factories.add(new ResourceItemProviderAdapterFactory());
> factories.add(new LibraryItemProviderAdapterFactory());
> factories.add(new EcoreItemProviderAdapterFactory());
> factories.add(new ReflectiveItemProviderAdapterFactory());
> // factories.add(new File(""));
> return factories;
> }
> }
>
>
> public class CopyOfMNViewContentProvider extends
> AdapterFactoryContentProvider {
> public CopyOfMNViewContentProvider()
> {
> super(MNComposedAdapterFactory.getAdapterFactory());
> }
>
> private static ResourceSetImpl resourceSet = new
> ResourceSetImpl(); public Object[] getChildren(Object
> parentElement) {
> if (parentElement instanceof IFile)
> {
> String path =
> ((IFile)parentElement).getFullPath().toString();
> URI uri = URI.createPlatformResourceURI(path, true);
> parentElement = resourceSet.getResource(uri, true);
> }
> return super.getChildren(parentElement);
> }
>
> public Object[] getElements(Object inputElement) {
> return getChildren(inputElement);
> }
>
> public Object getParent(Object element)
> {
> if (element instanceof IFile)
> return ((IResource)element).getParent();
> return super.getParent(element);
> }
>
> public boolean hasChildren(Object element) {
> if (element instanceof IFile)
> return true;
> return super.hasChildren(element);
> }
>
> }
>
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
|
|
Powered by
FUDForum. Page generated in 0.02812 seconds