【学习】Eclipse官方Zest的两个例子(二)

阅读更多

紧接上文。

 

总结:

  1. 采用了JFace View的方式;
  2. 三个图形点都增加了图标;
  3. 三条线都加了Tooltip;
  4. 感觉三个图形点会根据图标大小字体大小自动调整图形点的大小;

 

代码:

/**
 * This snippet shows how to use the IGraphContentProvider to create a graph with Zest.
 * In this example, getElements returns 3 edges:
 * 	* Rock2Paper
 *  * Paper2Scissors
 *  * Scissors2Rock
 * 
 * And for each of these, the source and destination are returned in getSource and getDestination.
 * 
 * A label provider is also used to create the text and icons for the graph.
 * 
 * @author Ian Bull
 * 
 */
public class GraphJFaceSnippet2 {
	
	static class MyContentProvider implements IGraphContentProvider {
		public Object getSource(Object rel) {
			if ("Rock2Paper".equals(rel)) {
				return "Rock";
			} else if ("Paper2Scissors".equals(rel)) {
				return "Paper";
			} else if ("Scissors2Rock".equals(rel)) {
				return "Scissors";
			}
			return null;
		}

		public Object[] getElements(Object input) {
			return new Object[] { "Rock2Paper", "Paper2Scissors", "Scissors2Rock" };
		}

		public Object getDestination(Object rel) {
			if ("Rock2Paper".equals(rel)) {
				return "Paper";
			} else if ("Paper2Scissors".equals(rel)) {
				return "Scissors";
			} else if ("Scissors2Rock".equals(rel)) {
				return "Rock";
			}
			return null;
		}
		public double getWeight(Object connection) {return 0;}
		public void dispose() {}
		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {}
	}

	static class MyLabelProvider extends LabelProvider {
		final Image image = Display.getDefault().getSystemImage(SWT.ICON_WARNING);

		public Image getImage(Object element) {
			if (element.equals("Rock") || element.equals("Paper") || element.equals("Scissors")) {
				return image;
			}
			return null;
		}

		public String getText(Object element) {
			return element.toString();
		}

	}

	static GraphViewer viewer = null;

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Display d = new Display();
		Shell shell = new Shell(d);
		shell.setText("GraphJFaceSnippet2");
		shell.setLayout(new FillLayout(SWT.VERTICAL));
		shell.setSize(400, 400);
		
		viewer = new GraphViewer(shell, SWT.NONE);
		viewer.setContentProvider(new MyContentProvider());
		viewer.setLabelProvider(new MyLabelProvider());
		viewer.setLayoutAlgorithm(new SpringLayoutAlgorithm(LayoutStyles.NO_LAYOUT_NODE_RESIZING));
		viewer.setInput(new Object());
		
		shell.open();
		while (!shell.isDisposed()) {
			while (!d.readAndDispatch()) {
				d.sleep();
			}
		}

	}
}

 

 

效果图:


【学习】Eclipse官方Zest的两个例子(二)_第1张图片
 

 

  • 【学习】Eclipse官方Zest的两个例子(二)_第2张图片
  • 大小: 12.7 KB
  • 查看图片附件

你可能感兴趣的:(eclipse,GEF,Draw2d,Zest)