GMF

转自 http://blog.youkuaiyun.com/wu_07/archive/2009/06/15/4271423.aspx GMF 经验点滴积累(一) 收藏 1、修改Connection的默认路由(Router)风格(默认的初始风格为oblique)。 覆盖DiagramConnectionsPreferencePage的initDefaults方法: view plaincopy to clipboardprint? public static void initDefaults(IPreferenceStore preferenceStore) { preferenceStore.setDefault(IPreferenceConstants.PREF_LINE_STYLE, Routing.RECTILINEAR); } 2、选中子图形的同时选中父图形(为了能拖动子图形时,父图形也被拖动),这种方法有一个问题,即因为同时选择中了两个对象,因此属性页中无法对子图形的属性进行编辑,需要修改属性分区(*PropertySection )的setInput代码。 父图形EditPart: view plaincopy to clipboardprint? /** * @generated NOT */ protected LayoutEditPolicy createLayoutEditPolicy() { FlowLayoutEditPolicy lep = new FlowLayoutEditPolicy() { @Override protected Command createAddCommand(EditPart child, EditPart after) { // TODO Auto-generated method stub return null; } @Override protected Command createMoveChildCommand(EditPart child, EditPart after) { // TODO Auto-generated method stub return null; } @Override protected Command getCreateCommand(CreateRequest request) { // TODO Auto-generated method stub return null; } @Override protected EditPolicy createChildEditPolicy(EditPart child) { if (child instanceof ShapeEditPart) { return ((ShapeEditPart) child).getPrimaryDragEditPolicy(); } NonResizableEditPolicy policy = (NonResizableEditPolicy) super .createChildEditPolicy(child); // disable drag functionality policy.setDragAllowed(false); return policy; } }; return lep; } 子图形EditPart: view plaincopy to clipboardprint? /** * @generated NOT */ public EditPolicy getPrimaryDragEditPolicy() { return new NonResizableEditPolicy() { protected void showSelection(){ super.showSelection(); IWorkbenchPart wp=PlatformUI.getWorkbench(). getActiveWorkbenchWindow().getActivePage().getActivePart(); if (wp instanceof IDiagramWorkbenchPart) { IDiagramWorkbenchPart diagramPart=(IDiagramWorkbenchPart)wp; diagramPart.getDiagramGraphicalViewer().appendSelection(getHost().getParent()); } } } ; } 另外变通的方法,即拖动子图形的时候拖动父图形,但这种方法无法选择中子图形: view plaincopy to clipboardprint? //In the child Edit part add: @Override public DragTracker getDragTracker(Request request) { return getParent().getDragTracker(request); } 3、修改自动布局(Arrange All、Arrange Selection)方式 默认的自动布局方式是从上到下,其Provider为TopDownProvider,如果我们需要修改为其他或自定义的布局方式,如从左到右的布局方式,可以采用定义扩展和修改代码两种方式: (1)定义扩展(extensions),注意这里的Priority不能为Lowest view plaincopy to clipboardprint? <extension point="org.eclipse.gmf.runtime.diagram.ui.layoutProviders"><layoutprovider class="org.eclipse.gmf.runtime.diagram.ui.providers.LeftRightProvider"><priority name="Low"></priority></layoutprovider></extension> (2)代码硬编程,主要方法为在??DiagramEditPart中覆盖安装EditPolicy.CONTAINER_ROLE ,这里参考了Gmf例子taipan中的PortEditPart相关代码 view plaincopy to clipboardprint? private LeftRightProvider layoutProvider = new LeftRightProvider(); protected void createDefaultEditPolicies() { super.createDefaultEditPolicies(); installEditPolicy(EditPolicyRoles.SEMANTIC_ROLE, new DFDDiagramItemSemanticEditPolicy()); installEditPolicy(EditPolicyRoles.CANONICAL_ROLE, new DFDDiagramCanonicalEditPolicy()); installEditPolicy(EditPolicy.CONTAINER_ROLE, new ContainerEditPolicy() { protected Command getArrangeCommand(ArrangeRequest request) { // DeferredCommand is patched to arrange all children when no viewAdapters is specified if (RequestConstants.REQ_ARRANGE_DEFERRED.equals(request.getType())) { String layoutType = request.getLayoutType(); TransactionalEditingDomain editingDomain = ((IGraphicalEditPart) getHost()).getEditingDomain(); return new ICommandProxy(new DeferredLayoutCommand(editingDomain, request.getViewAdaptersToArrange(), (IGraphicalEditPart) getHost(), layoutType) { @Override protected CommandResult doExecuteWithResult(IProgressMonitor progressMonitor, IAdaptable info) throws ExecutionException { if (viewAdapters == null || viewAdapters.isEmpty()) { viewAdapters = new ArrayList(DFDDiagramEditPart.this.getChildren()); } return super.doExecuteWithResult(progressMonitor, info); } }); } // Snap to grid command is stripped off to prevent loops String layoutDesc = request.getLayoutType() != null ? request.getLayoutType() : LayoutType.DEFAULT; boolean offsetFromBoundingBox = false; List editparts = new ArrayList(); if ((ActionIds.ACTION_ARRANGE_ALL.equals(request.getType())) || (ActionIds.ACTION_TOOLBAR_ARRANGE_ALL.equals(request.getType()))) { editparts = ((IGraphicalEditPart) getHost()).getChildren(); request.setPartsToArrange(editparts); } if ((ActionIds.ACTION_ARRANGE_SELECTION.equals(request.getType())) || (ActionIds.ACTION_TOOLBAR_ARRANGE_SELECTION.equals(request.getType()))) { editparts = request.getPartsToArrange(); if (!(((GraphicalEditPart) ((EditPart) editparts.get(0)).getParent()).getContentPane().getLayoutManager() instanceof XYLayout)) { return null; } offsetFromBoundingBox = true; } if (RequestConstants.REQ_ARRANGE_RADIAL.equals(request.getType())) { editparts = request.getPartsToArrange(); offsetFromBoundingBox = true; layoutDesc = LayoutType.RADIAL; } if (editparts.isEmpty()) { return null; } List nodes = new ArrayList(editparts.size()); ListIterator li = editparts.listIterator(); while (li.hasNext()) { IGraphicalEditPart ep = (IGraphicalEditPart) li.next(); View view = ep.getNotationView(); if (ep.isActive() && view != null && view instanceof Node) { Rectangle bounds = ep.getFigure().getBounds(); nodes.add(new LayoutNode((Node) view, bounds.width, bounds.height)); } } if (nodes.isEmpty()) { return null; } List hints = new ArrayList(2); hints.add(layoutDesc); hints.add(getHost()); IAdaptable layoutHint = new ObjectAdapter(hints); Runnable layoutRun = layoutNodes(nodes, offsetFromBoundingBox, layoutHint); return ((IInternalLayoutRunnable) layoutRun).getCommand(); } public Runnable layoutNodes(List nodes, boolean offsetFromBoundingBox, IAdaptable layoutHint) { return layoutProvider.layoutLayoutNodes(nodes, offsetFromBoundingBox, layoutHint); } }); // removeEditPolicy(org.eclipse.gmf.runtime.diagram.ui.editpolicies.EditPolicyRoles.POPUPBAR_ROLE); } 4、通过设置WorkspaceViewerPreference,设定默认打印题头和显示页面分割线 view plaincopy to clipboardprint? /** * class ??DiagramEditorUtil * @generated NOT */ public static boolean openDiagram(Resource diagram) throws PartInitException { IWorkbenchPage page = PlatformUI.getWorkbench() .getActiveWorkbenchWindow().getActivePage(); DiagramEditor editor = (DiagramEditor) page.openEditor(new URIEditorInput(diagram.getURI()), DfdDiagramEditor.ID); //打开编辑器后设定WorkspaceViewerPreference,包括是否打印题头和显示页面分割符 IPreferenceStore preferences; preferences =editor.getWorkspaceViewerPreferenceStore(); preferences.setValue(WorkspaceViewerProperties.HEADER_PREFIX+WorkspaceViewerProperties.PRINT_TITLE_SUFFIX, true); preferences.setValue(WorkspaceViewerProperties.VIEWPAGEBREAKS,true); // preferences.setValue(WorkspaceViewerProperties.HEADER_PREFIX+WorkspaceViewerProperties.PRINT_TEXT_SUFFIX, // getPartName()); return true; } 5、以代码方式触发ARRANGE_ALL action view plaincopy to clipboardprint? IGraphicalEditPart rp = (IGraphicalEditPart) editor .getDiagramGraphicalViewer().getContents(); try { ArrangeRequest arrangeRequest = new ArrangeRequest( ActionIds.ACTION_ARRANGE_ALL); List l = rp.getChildren(); arrangeRequest.setPartsToArrange(l); Command arrangeCmd = rp.getCommand(arrangeRequest); arrangeCmd.execute(); //System.out.println("ARRANGEAll"); } catch (Exception e) { e.printStackTrace(); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值