阅读:18954回复:0
调色板拖放实现
yGEF--调色板拖拽的实现
1.调色板添加内容格式new NodeCreateFactory(Industry.class) controlGroup.add(new CombinedTemplateCreationEntry("行业", "rer", new NodeCreateFactory(Industry.class), null, null)); 2. NodeCreateFactory 代码 package lxgef.editor.commmand; import lxgef.editor.model.Industry; import lxgef.editor.model.Programmer; import org.eclipse.gef.requests.CreationFactory; public class NodeCreateFactory implements yCreationFactory{ private Class<?> template; public NodeCreateFactory(Class<?> t) { // TODO Auto-generated constructor stub this.template=t; } @Override public Object getNewObject() { // TODO Auto-generated method stub if(template==Programmer.class){ Programmer pro=new Programmer(); pro.setName("前端"); return pro; } if(template ==Industry.class){ Industry in=new Industry(); in.setJob("前端"); return in; } return null; } @Override public Object getObjectType() { // TODO Auto-generated method stub return template; } } 3. 在根控制器加入策略 installEditPolicy(EditPolicy.LAYOUT_ROLE, new AppLayoutPolicy()); 重新事件监听函数改变后的执行 @Override public void propertyChange(PropertyChangeEvent evt){ if(evt.getPropertyName().equals(Node.PROPERTY_LAYOUT)){ refreshVisuals(); } if(evt.getPropertyName().equals(Node.PROPERTY_ADD)){ refreshChildren(); } if(evt.getPropertyName().equals(Node.PROPERTY_REMOVE)){ refreshChildren(); } } 4. 在 ApplayoutPolicy类中添加 ProgrammerPart为最外层控制器 IndustryCreateCommand 生成你所需的内容 @Override protected Command getCreateCommand(CreateRequest request) { // TODO Auto-generated method stub if(request.getType() == REQ_CREATE && getHost() instanceof ProgrammerPart) { IndustryCreateCommand cmd = new IndustryCreateCommand(); cmd.setProgrammer(getHost().getModel()); cmd.setIndustry(request.getNewObject()); Rectangle constraint = (Rectangle)getConstraintFor(request); constraint.x = (constraint.x < 0) ? 0 : constraint.x; constraint.y = (constraint.y < 0) ? 0 : constraint.y; constraint.width = (constraint.width <= 0) ? 100 : constraint.width; constraint.height = (constraint.height <= 0) ? 100 : constraint.height; cmd.setLayout(constraint); return cmd; } return null; } 5.IndustryCreateCommand 类具体实现 package lxgef.editor.commmand; import lxgef.editor.model.Industry; import lxgef.editor.model.Programmer; import org.eclipse.draw2d.geometry.Rectangle; import org.eclipse.gef.commands.Command; public class IndustryCreateCommand extends Command { private Industry indus; private Programmer pro; public IndustryCreateCommand(){ super(); indus=null; pro=null; } public void setIndustry(Object s){ if(s instanceof Industry){ this.indus=(Industry)s; } } public void setProgrammer(Object s){ if(s instanceof Programmer){ this.pro=(Programmer)s; } } public void setLayout(Rectangle r) { if(indus == null) { return ; } indus.setLayout(r); } @Override public boolean canExecute() { if(indus == null ||pro==null) { return false; } return true; } @Override public void execute() { pro.addChild(indus); } @Override public boolean canUndo() { if (pro == null || indus == null) return false; return pro.contains(indus); } @Override public void undo() { pro.removeChild(indus); } } 最后执行execute增加子元素 6.根模型增加子元素放法,firePropertyChange会执行part中propertyChange方法刷新子元素 public boolean addChild(Node child) { boolean b=this.children.add(child); if(b){ child.setParent(this); getListeners().firePropertyChange(PROPERTY_ADD, null, child); } return b; } 最终效果图: 图片:3A$2YHUHN`YN`$WI`O([N8U.png [img]file:///C:\Users\zhou\AppData\Roaming\Tencent\Users\846297343\QQ\WinTemp\RichOle\3A$2YHUHN`YN`$WI`O([N8U.png[/img] |
|