Why figures do not display? [message #1385756] |
Wed, 11 June 2014 02:32 |
Jingang Zhou Messages: 57 Registered: December 2010 |
Member |
|
|
Hi, all,
I create a ScrollingGraphicalViewer to show my figures, but no figure displays. I debugged the source and it seems all object (figures, editparts, models) are created, no exceptions. Why the figures do not display?
I briefly depict what I did, then paste my code.
1. model objects. In my model, there are two kinds of elements, directory and file. A directory may contain other directories or files. I default use Java File class for this purpose.
2. one figure object. I create one kind of figure for file, which can have nested figures for its chidren if the file is a directory. (This figure can display in a standalone Java application).
3. two editpart objects. one for file, the other for root.
public class FileEditPart extends AbstractGraphicalEditPart{
public FileEditPart(File file){
setModel(file);
}
@Override
protected IFigure createFigure() {
FileFigure figure = new FileFigure(((File)getModel()).getName(), null, null);
return figure;
}
@Override
public IFigure getContentPane() {
return ((FileFigure)getFigure()).getContentPane();
}
@Override
protected List<File> getModelChildren() {
if (getModel().isDirectory()){
return Arrays.asList(getModel().listFiles());
}
return new ArrayList<File>();
}
public File getModel(){
return (File)super.getModel();
}
}
public class RootEditPart extends AbstractGraphicalEditPart {
//root denotes the directory under which to show the file elements.
public RootEditPart(String root){
setModel(root);
}
@Override
protected IFigure createFigure() {
Figure figure = new FreeformLayer();
figure.setBorder(new MarginBorder(3));
figure.setLayoutManager(new FreeformLayout());
return figure;
}
@Override
protected List<File> getModelChildren() {
List<File> children = Arrays.asList(new File(getModel()).listFiles());
return children;
}
@Override
public String getModel() {
return (String) super.getModel();
}
}
4. an editpart factory object, for create each editpart object.
public class FileEditPartFactory implements EditPartFactory {
@Override
public EditPart createEditPart(EditPart context, Object model) {
if (model instanceof File){
return new FileEditPart((File)model);
}else if (model instanceof String){
return new RootEditPart((String)model);
}
return null;
}
}
5. a view to display the elements (figures). I use a DirectoryDialog for users to select a directory as the root.
public class GEFView extends ViewPart {
private Shell shell;
private ScrollingGraphicalViewer viewer;
@Override
public void createPartControl(Composite parent) {
viewer = new ScrollingGraphicalViewer();
viewer.createControl(parent);
viewer.getControl().setBackground(ColorConstants.white);
viewer.setRootEditPart(new ScalableFreeformRootEditPart());
viewer.setEditPartFactory(new FileEditPartFactory());
shell = parent.getShell();
shell.getDisplay().asyncExec(new Runnable(){
@Override
public void run() {
String file = new DirectoryDialog(shell).open();
if (file != null){
viewer.setContents(file);
}
}
});
}
@Override
public void setFocus() {
}
}
Anything missing? Any clues and comments will be appriciated.
Thanks.
|
|
|
|
Re: Why figures do not display? [message #1385896 is a reply to message #1385883] |
Thu, 12 June 2014 07:44 |
|
Overriding refreshVisuals() in your EditParts will do the trick. You will probably have to set the figures constraint relative to the parent EditPart as well, so for FileEditPart do something like the following in refreshVisuals().
getParent().setLayoutConstraint(this, figure, layout);
layout here will have to be a draw2d Rectangle. You can calculate that by giving it x and y values, and getFigure().getPreferredSize().width for width, and dto. for height.
[Updated on: Thu, 12 June 2014 07:45] Report message to a moderator
|
|
|
|
Re: Why figures do not display? [message #1386045 is a reply to message #1385930] |
Fri, 13 June 2014 07:49 |
|
Robin Zhou wrote on Thu, 12 June 2014 12:11Thank you Stephan.
I extends the addChildVisual() to layout my figures.
Best regards.
This is not the right place, if the constraints might change dependent on your model. As already indicated, refreshVisuals() is intended for this purpose.
|
|
|
Powered by
FUDForum. Page generated in 0.03551 seconds