`

IMB的Zest文档学习 代码

阅读更多

第一个程序:

public class FirstZest { 

       public static void main(String[] args) { 
              // SWT 
              Display display = new Display(); 
              Shell shell = new Shell(display); 
              shell.setText("First Zest Program Demo"); 
              shell.setLayout(new FillLayout()); 
              shell.setSize(300, 300); 

              // 创建 Graph 
              Graph graph = new Graph(shell, SWT.NONE); 
              // 创建一个图形节点
              GraphNode startNode = new GraphNode(graph, SWT.NONE, "Start"); 
              // 创建另外一个图形节点
              GraphNode endNode = new GraphNode(graph, SWT.NONE, "End"); 
              // 创建节点关联
              new GraphConnection(graph, SWT.NONE, startNode, endNode); 
              // 设置布局管理器
              graph.setLayoutAlgorithm(new SpringLayoutAlgorithm( 
                            LayoutStyles.NO_LAYOUT_NODE_RESIZING), true); 

              // 显示 SWT 
              shell.open(); 
              while (!shell.isDisposed()) { 
                     while (!display.readAndDispatch()) { 
                            display.sleep(); 
                     } 
              } 
       } 
 } 

 

第二个程序:设置图形的图像和连线箭头

public class FirstZest2 { 

	public static void main(String[] args) { 
		// SWT 
		Display display = new Display(); 
		Shell shell = new Shell(display); 
		shell.setText("First Zest Program Demo"); 
		shell.setLayout(new FillLayout()); 
		shell.setSize(300, 300); 

		Graph graph = new Graph(shell, SWT.NONE); 
		graph.setConnectionStyle(ZestStyles.CONNECTIONS_DIRECTED); 
		Image startIcon = JFaceResources.getImage(Dialog.DLG_IMG_MESSAGE_INFO);
		GraphNode startNode = new GraphNode(graph, SWT.NONE, "Start", startIcon);  
		GraphNode endNode = new GraphNode(graph, SWT.NONE, "End"); 
		new GraphConnection(graph, SWT.NONE, startNode, endNode); 
		graph.setLayoutAlgorithm(new SpringLayoutAlgorithm( 
				LayoutStyles.NO_LAYOUT_NODE_RESIZING), true); 

		shell.open(); 
		while (!shell.isDisposed()) { 
			while (!display.readAndDispatch()) { 
				display.sleep(); 
			} 
		} 
	} 
} 

 

 

设置图形的事件,选中的加粗:

 public class FirstZest3 { 

       public static void main(String[] args) { 
              Display display = new Display(); 
              Shell shell = new Shell(display); 
              shell.setText("First Zest Program Demo"); 
              shell.setLayout(new FillLayout()); 
              shell.setSize(300, 300); 

              Graph graph = new Graph(shell, SWT.NONE); 
              GraphNode startNode = new GraphNode(graph, SWT.NONE, "Start"); 
              GraphNode endNode = new GraphNode(graph, SWT.NONE, "End"); 
              new GraphConnection(graph, SWT.NONE, startNode, endNode); 
              graph.setLayoutAlgorithm(new SpringLayoutAlgorithm( 
                            LayoutStyles.NO_LAYOUT_NODE_RESIZING), true); 
              
              // 注册对象选择侦听事件
              graph.addSelectionListener(new SelectionAdapter() { 
                    @Override 
                    public void widgetSelected(SelectionEvent e) { 
                           List selection = ((Graph) e.widget).getSelection(); 
                           // 确认只选择了一个对象
                           if (selection.size() == 1) { 
                                  Object o = selection.get(0); 
                                  // 图形节点对象
                                  if (o instanceof GraphNode) { 
                                         // 改变边线宽度
                                         ((GraphNode) o).setBorderWidth(3); 
                                  // 图形关联对象
                                  } else if (o instanceof GraphConnection) { 
                                         // 改变连线宽度
                                         ((GraphConnection) o).setLineWidth(3); 
                                  } 
                           } 
                    } 
              }); 

              shell.open(); 
              while (!shell.isDisposed()) { 
                     while (!display.readAndDispatch()) { 
                            display.sleep(); 
                     } 
              } 
       } 
 } 

 

 

测试不同的布局管理器算法:

 public class FirstZest4 { 

       public static void main(String[] args) { 
              // SWT 
              Display display = new Display(); 
              Shell shell = new Shell(display); 
              shell.setText("First Zest Program Demo"); 
              shell.setLayout(new FillLayout()); 
              shell.setSize(300, 300); 


              Graph graph = new Graph(shell, SWT.NONE); 
              graph.setConnectionStyle(ZestStyles.CONNECTIONS_DIRECTED); 
              for (int i = 0; i < 10; i++) { 
                 GraphNode node1 = new GraphNode(graph, ZestStyles.NODES_FISHEYE, 
                         "Begin"); 
                 GraphNode node2 = new GraphNode(graph, ZestStyles.NODES_FISHEYE, 
                         "Middle"); 
                 GraphNode node3 = new GraphNode(graph, ZestStyles.NODES_FISHEYE, 
                         "Finish"); 
                 new GraphConnection(graph, SWT.NONE, node1, node2); 
                 new GraphConnection(graph, SWT.NONE, node2, node3); 
              } 
              graph.setLayoutAlgorithm(new GridLayoutAlgorithm(
                     LayoutStyles.NO_LAYOUT_NODE_RESIZING), true); 
              /* 
              graph.setLayoutAlgorithm(new SpringLayoutAlgorithm( 
                     LayoutStyles.NO_LAYOUT_NODE_RESIZING), true); 
              graph.setLayoutAlgorithm(new RadialLayoutAlgorithm( 
                     LayoutStyles.NO_LAYOUT_NODE_RESIZING), true); 
              graph.setLayoutAlgorithm(new TreeLayoutAlgorithm(
                     LayoutStyles.NO_LAYOUT_NODE_RESIZING), true); 
              graph.setLayoutAlgorithm(new DirectedGraphLayoutAlgorithm( 
                     LayoutStyles.NO_LAYOUT_NODE_RESIZING), true); 
              */

              // 显示 SWT 
              shell.open(); 
              while (!shell.isDisposed()) { 
                     while (!display.readAndDispatch()) { 
                            display.sleep(); 
                     } 
              } 
       } 
 } 

 

 

定制布局管理器:

public class FirstZest5 { 

       public static void main(String[] args) { 
              // SWT 
              Display display = new Display(); 
              Shell shell = new Shell(display); 
              shell.setText("First Zest Program Demo"); 
              shell.setLayout(new FillLayout()); 
              shell.setSize(300, 300); 

              Graph graph = new Graph(shell, SWT.NONE); 
              graph.setConnectionStyle(ZestStyles.CONNECTIONS_DIRECTED); 
              GraphNode node1 = new GraphNode(graph, SWT.NONE, "Node 1"); 
              GraphNode node2 = new GraphNode(graph, SWT.NONE, "Node 2"); 
              GraphNode node3 = new GraphNode(graph, SWT.NONE, "Node 3"); 
              new GraphConnection(graph, SWT.NONE, node1, node2); 
              new GraphConnection(graph, SWT.NONE, node2, node3); 
              graph.setLayoutAlgorithm(new AbstractLayoutAlgorithm(SWT.NONE) { 
                 int totalNodes; 

                 protected int getCurrentLayoutStep() { 
                     return 0; 
                 } 

                 protected int getTotalNumberOfLayoutSteps() { 
                     return totalNodes; 
                 } 

                 protected boolean isValidConfiguration(boolean asynchronous, 
                         boolean continuous) { 
                     return true; 
              } 

                 protected void postLayoutAlgorithm(InternalNode[] entitiesToLayout, 
                         InternalRelationship[] relationshipsToConsider) { 
                 } 

                 protected void preLayoutAlgorithm(InternalNode[] entitiesToLayout, 
                         InternalRelationship[] relationshipsToConsider, double x, 
                         double y, double width, double height) { 
                 } 
                 public void setLayoutArea(double x, double y, double width, 
                         double height) { 
                 }

				@Override
				protected void applyLayoutInternal(
						InternalNode[] entitiesToLayout,
						InternalRelationship[] relationshipsToConsider,
						double boundsX, double boundsY, double boundsWidth,
						double boundsHeight) {
                    // 需要布局的对象数量
                    totalNodes = entitiesToLayout.length; 
                    // 设置固定间隔距离
                    double spcaing = 100; 
                    // 开始位置坐标
                    int startX = 10; 
                    // 触发布局进程
                    fireProgressStarted(totalNodes); 
                    // 循环所有对象,按照设定的布局算法来排列对象
                    for (int curNode = 0; curNode < entitiesToLayout.length; curNode++) { 
                        LayoutEntity layoutEntity = entitiesToLayout[curNode] 
                                .getLayoutEntity(); 
                        // 设置一个对象显示的位置
                        layoutEntity.setLocationInLayout(startX, layoutEntity 
                                .getYInLayout()+10); 
                        // 保持相同的间隔
                        startX += spcaing; 
                        // 让对象在新位置上显示
                        fireProgressEvent(curNode, totalNodes); 
                    } 
                    // 结束布局进程
                    fireProgressEnded(totalNodes); 
					// TODO Auto-generated method stub
				}
              }, true); 
              
              // 显示 SWT 
              shell.open(); 
              while (!shell.isDisposed()) { 
                     while (!display.readAndDispatch()) { 
                            display.sleep(); 
                     } 
              } 
       } 
 } 

 

 

定制图形:

public class FirstZest6 { 

	public static void main(String[] args) { 
		// SWT 
		Display display = new Display(); 
		Shell shell = new Shell(display); 
		shell.setText("First Zest Program Demo"); 
		shell.setLayout(new FillLayout()); 
		shell.setSize(300, 300); 

		Graph graph = new Graph(shell, SWT.NONE); 
		GraphNode startNode = new GraphNode(graph, SWT.NONE, "Start"); 
		EllipseGraphNode ellipseNode = new EllipseGraphNode(graph, SWT.NONE, "ellipse"); 
		GraphNode endNode = new GraphNode(graph, SWT.NONE, "End"); 

		// 创建节点关联
		new GraphConnection(graph, SWT.NONE, startNode, ellipseNode); 
		new GraphConnection(graph, SWT.NONE, ellipseNode, endNode); 
		// 设置布局管理器
		graph.setLayoutAlgorithm(new SpringLayoutAlgorithm( 
				LayoutStyles.NO_LAYOUT_NODE_RESIZING), true); 

		// 显示 SWT 
		shell.open(); 
		while (!shell.isDisposed()) { 
			while (!display.readAndDispatch()) { 
				display.sleep(); 
			} 
		} 
	} 
} 

class EllipseGraphNode extends CGraphNode { 
	public EllipseGraphNode(IContainer graphModel, int style, IFigure figure) { 
		super(graphModel, style, figure); 
	}

	public EllipseGraphNode(IContainer graphModel, int style, String name) { 
		super(graphModel, style, createEllipseFigure(name)); 
	}

	private static IFigure createEllipseFigure(String name) { 
		EllipseFigure circleFigure = new EllipseFigure(name); 
		circleFigure.setSize(-1, -1); 
		return circleFigure; 
	}

} 

class EllipseFigure extends Figure { 


	public EllipseFigure(String name) { 
		// 定义显示名称
		Label label = new Label(name); 
		// 定义图形节点的显示布局
		ToolbarLayout layout = new ToolbarLayout(); 
		setLayoutManager(layout); 
		setBorder(new LineBorder(ColorConstants.white, 1)); 
		setOpaque(true); 

		add(label); 
	} 
	// 重写这个方法,实现自己需要显示的图形,更多的信息可以参考 Zest 的源代码
	@Override 
	public void paint(Graphics graphics) { 
		super.paint(graphics); 
		// 获取默认的矩形信息
		Rectangle rectangle = getBounds().getCopy(); 
		graphics.setLineWidth(2); 
		// 画椭圆
		graphics.drawOval(rectangle); 
	} 
} 

 

 

设置图形不让拖动:

package com.iteye.xmind.draw2d.zest2;

import java.util.Iterator;
import java.util.List;

import org.eclipse.draw2d.SWTEventDispatcher;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.zest.core.widgets.Graph;
import org.eclipse.zest.core.widgets.GraphConnection;
import org.eclipse.zest.core.widgets.GraphNode;
import org.eclipse.zest.layouts.LayoutStyles;
import org.eclipse.zest.layouts.algorithms.SpringLayoutAlgorithm;

public class FirstZest7 { 

       public static void main(String[] args) { 
              // SWT 
              Display display = new Display(); 
              Shell shell = new Shell(display); 
              shell.setText("First Zest Program Demo"); 
              shell.setLayout(new FillLayout()); 
              shell.setSize(300, 300); 

              // 创建 Graph 
              Graph graph = new Graph(shell, SWT.NONE); 
              // 创建一个图形节点
              GraphNode startNode = new GraphNode(graph, SWT.NONE, "Start"); 
              // 创建另外一个图形节点
              GraphNode endNode = new GraphNode(graph, SWT.NONE, "End"); 
              // 创建节点关联
              new GraphConnection(graph, SWT.NONE, startNode, endNode); 
              // 设置布局管理器
              graph.setLayoutAlgorithm(new SpringLayoutAlgorithm( 
                            LayoutStyles.NO_LAYOUT_NODE_RESIZING), true); 
      		graph.getLightweightSystem().setEventDispatcher( 
    				new SWTEventDispatcher() { 
    					public void dispatchMouseMoved(MouseEvent me) { 
    						List selection = ((Graph) me.widget).getSelection(); 
    						for (Iterator iterator = selection.iterator(); iterator.hasNext();) { 
    							Object object = (Object) iterator.next(); 
    							if (object instanceof GraphNode) { 
    								String nodeName = ((GraphNode) object).getText(); 
    								if ("Start".equalsIgnoreCase(nodeName)) { 
    									// 如果是 Start 图形节点,就无法被移动
    								} else { 
    									// 其他图形节点,不受影响
    									super.dispatchMouseMoved(me); 
    								} 
    							} 
    						} 
    					} 
    				}); 
              // 显示 SWT 
              shell.open(); 
              while (!shell.isDisposed()) { 
                     while (!display.readAndDispatch()) { 
                            display.sleep(); 
                     } 
              } 
       } 
 } 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics