RSS

Search Engine

Thursday, August 12, 2010

Your first Zest Project

3.1. Getting started

Create a new Eclipse RCP application "sawan.modi.zest.first". Use the "Eclipse RCP with a view" as a template. Add "org.eclipse.zest.core" and "org.eclipse.zest.layouts" as dependencies to your MANIFEST.MF.

Change the coding of "View.java" to the following. This coding creates a simple graph and connects its elements.

    
package sawan.modi.zest.first;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.ui.part.ViewPart;
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.core.widgets.ZestStyles;
import org.eclipse.zest.layouts.LayoutStyles;
import org.eclipse.zest.layouts.algorithms.SpringLayoutAlgorithm;
import org.eclipse.zest.layouts.algorithms.TreeLayoutAlgorithm;

public class View extends ViewPart {
public static final String ID = "sawan.modi.zest.first.view";
private Graph graph;
private int layout = 1;

public void createPartControl(Composite parent) {
// Graph will hold all other objects
graph = new Graph(parent, SWT.NONE);
// Now a few nodes
GraphNode node1 = new GraphNode(graph, SWT.NONE, "Jim");
GraphNode node2 = new GraphNode(graph, SWT.NONE, "Jack");
GraphNode node3 = new GraphNode(graph, SWT.NONE, "Joe");
GraphNode node4 = new GraphNode(graph, SWT.NONE, "Bill");
// Lets have a directed connection
new GraphConnection(graph, ZestStyles.CONNECTIONS_DIRECTED, node1,
node2);
// Lets have a dotted graph connection
new GraphConnection(graph, ZestStyles.CONNECTIONS_DOT, node2, node3);
// Standard connection
new GraphConnection(graph, SWT.NONE, node3, node1);
// Change line color and line width
GraphConnection graphConnection = new GraphConnection(graph, SWT.NONE,
node1, node4);
graphConnection.changeLineColor(parent.getDisplay().getSystemColor(
SWT.COLOR_GREEN));
// Also set a text
graphConnection.setText("This is a text");
graphConnection.setHighlightColor(parent.getDisplay().getSystemColor(
SWT.COLOR_RED));
graphConnection.setLineWidth(3);
graphConnection.addListener(SWT.SELECTED, new Listener() {

@Override
public void handleEvent(Event event) {
System.out.println("Selected");
}

});
graph.setLayoutAlgorithm(new SpringLayoutAlgorithm(
LayoutStyles.NO_LAYOUT_NODE_RESIZING), true);

}

public void setLayoutManager() {
switch (layout) {
case 1:
graph.setLayoutAlgorithm(new TreeLayoutAlgorithm(
LayoutStyles.NO_LAYOUT_NODE_RESIZING), true);
layout++;
break;
case 2:
graph.setLayoutAlgorithm(new SpringLayoutAlgorithm(
LayoutStyles.NO_LAYOUT_NODE_RESIZING), true);
layout = 1;
break;

}

}

/**
* Passing the focus request to the viewer's control.
*/
public void setFocus() {
}
}

Run you application and you should see the graph.

3.2. Select layout manager via a command

Create a command with the following default handler "sawan.modi.zest.first.handler.ChangeLayout" which will change the layout for the graph. Assign the command to the menu.

    
package sawan.modi.zest.first.handler;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.handlers.HandlerUtil;

import sawan.modi.zest.first.View;

public class ChangeLayout extends AbstractHandler {

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
IViewPart findView = HandlerUtil.getActiveWorkbenchWindow(event)
.getActivePage().findView("sawan.modi.zest.first.view");
View view = (View) findView;
view.setLayoutManager();
return null;
}

}

Run your application, if you select your command the layout of your view should change.

0 comments:

Post a Comment