BaNg@taobao

Just Do It!

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  20 Posts :: -1 Stories :: 202 Comments :: 0 Trackbacks
最近发现一个很怪的问题,GEF的PaletteGroup在3.3及以下都有border,而在3.4却消失了,我觉得好像是GEF 3.4 PaletteViewer的开发者Hudson和Shah认为不可见的边框更“友好”,可是好多人都不习惯了,觉得是bug了,在给gef newsgroup上一个人回答问题的时候顺便把这个hack了一下,效果还不错,哈哈!下面是步骤:

1 定义一个HackedGroupEditPart:

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.MarginBorder;
import org.eclipse.draw2d.geometry.Insets;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.internal.ui.palette.editparts.GroupEditPart;
import org.eclipse.gef.palette.PaletteContainer;
import org.eclipse.jface.action.Separator;

public class HackedGroupEditPart extends GroupEditPart {

    
public HackedGroupEditPart(PaletteContainer group) {
        
super(group);
    }
    @Override
    
protected void refreshVisuals() {
        
super.refreshVisuals();
        Insets insets 
= getContentPane().getBorder().getInsets(getContentPane());
        getContentPane().setBorder(
new MarginBorder(insets){
            @Override
            
public void paint(IFigure figure, Graphics graphics, Insets insets) {
                Rectangle r 
= getPaintRectangle(figure, insets);
                r.height
--;
                graphics.setForegroundColor(ColorConstants.buttonDarker);
                graphics.drawLine(r.x, r.bottom(), r.right(), r.bottom());
            }
        });
    }
    
}

2 使用这个加了边框的GroupEditPart,在编辑器定义里面重写 createPaletteViewerProvider 方法,如下:

public class LivingEditor extends GraphicalEditorWithFlyoutPalette {
.
.
@Override
protected PaletteViewerProvider createPaletteViewerProvider() {
        
return new PaletteViewerProvider(getEditDomain())
        {
            @Override
            
public PaletteViewer createPaletteViewer(Composite parent) {
                
                
//add border for GroupEditPart
                final PaletteEditPartFactory pepf = new PaletteEditPartFactory()
                {
                    @Override
                    
protected EditPart createGroupEditPart(
                            EditPart parentEditPart, Object model) {
                        
return new HackedGroupEditPart((PaletteContainer)model);
                    }
                };
                PaletteViewer pViewer 
= new PaletteViewer(){
                    {
                        setEditPartFactory(pepf);
                    }
                };
                pViewer.createControl(parent);
                configurePaletteViewer(pViewer);
                hookPaletteViewer(pViewer);
                
return pViewer;
            }
        };
    }


}

我的Palette定义,使用了三个PaletteGroup,但不改一下根本看不出来group之间的分隔

public class PaletteProvider {

    
public static PaletteRoot provide(LivingEditor livingEditor) {
        PaletteRoot root 
= new PaletteRoot();
        createStandardTool(root);
        createConnectionTool(root);
        createModelTool(root);
        
return root;
    }

    
private static void createModelTool(PaletteRoot root) {
        PaletteGroup model_group 
= new PaletteGroup("model");
        CombinedTemplateCreationEntry person_entry 
= new CombinedTemplateCreationEntry("Person",
                
"Create a person",
                
new SimpleFactory(Person.class),
                Activator.imageDescriptorFromPlugin(Activator.PLUGIN_ID, 
"icon/person16.png"),
                Activator.imageDescriptorFromPlugin(Activator.PLUGIN_ID, 
"icon/person24.png")
                );
        model_group.add(person_entry);
        CombinedTemplateCreationEntry dog_entry 
= new CombinedTemplateCreationEntry("Dog",
                
"Create a dog",
                
new SimpleFactory(Dog.class),
                Activator.imageDescriptorFromPlugin(Activator.PLUGIN_ID, 
"icon/dog16.gif"),
                Activator.imageDescriptorFromPlugin(Activator.PLUGIN_ID, 
"icon/dog24.gif"));
        model_group.add(dog_entry);
        root.add(model_group);
    }

    
private static void createConnectionTool(PaletteRoot root) {
        ConnectionCreationToolEntry relation 
= new ConnectionCreationToolEntry("relation",
                
"relation between person and dog",
                
new SimpleFactory(Relation.class),
                Activator.imageDescriptorFromPlugin(Activator.PLUGIN_ID, 
"icon/connection16.gif"),
                Activator.imageDescriptorFromPlugin(Activator.PLUGIN_ID, 
"icon/connection24.gif")
                );
        PaletteGroup conn_group 
= new PaletteGroup("relations");
        conn_group.add(relation);
        root.add(conn_group);
        
    }

    
private static void createStandardTool(PaletteRoot root) {
        PaletteGroup std 
= new PaletteGroup("Standard");
        SelectionToolEntry selection 
= new SelectionToolEntry();
        std.add(selection);
        
        root.add(std);
    }

可以看一下修改后的对比:



不过这种方法使用了internal API,不适应版本更新,只适合救急。我已经报告了这个误会,3.5应该不会有了。

如果你也有这个困扰,就试试吧。



posted on 2008-08-17 22:40 Always BaNg. 阅读(1847) 评论(0)  编辑  收藏 所属分类: JavaEclipse源代码分析

只有注册用户登录后才能发表评论。


网站导航: