summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorujhelyiz2011-04-04 17:14:23 (EDT)
committerFabian Steeg2011-04-04 17:14:23 (EDT)
commit32ff401b1bcd59ac6acb6ff20309ee8b339baf7c (patch)
treefcf5005d2c9dd2a231ca556e071ce899acf4e9d3
parent45980cb33ea7cb060cf1ae8caaf4e9a18a71c6f7 (diff)
downloadorg.eclipse.gef4-32ff401b1bcd59ac6acb6ff20309ee8b339baf7c.zip
org.eclipse.gef4-32ff401b1bcd59ac6acb6ff20309ee8b339baf7c.tar.gz
org.eclipse.gef4-32ff401b1bcd59ac6acb6ff20309ee8b339baf7c.tar.bz2
Added support for filling a JFace graph using an ITreeContentProvider
-rw-r--r--org.eclipse.zest.examples/src/org/eclipse/zest/examples/jface/GraphJFaceSnippet9.java147
-rw-r--r--org.eclipse.zest.jface/src/org/eclipse/zest/core/viewers/GraphViewer.java8
-rw-r--r--org.eclipse.zest.jface/src/org/eclipse/zest/core/viewers/internal/TreeModelEntityFactory.java118
3 files changed, 272 insertions, 1 deletions
diff --git a/org.eclipse.zest.examples/src/org/eclipse/zest/examples/jface/GraphJFaceSnippet9.java b/org.eclipse.zest.examples/src/org/eclipse/zest/examples/jface/GraphJFaceSnippet9.java
new file mode 100644
index 0000000..aa62eb9
--- /dev/null
+++ b/org.eclipse.zest.examples/src/org/eclipse/zest/examples/jface/GraphJFaceSnippet9.java
@@ -0,0 +1,147 @@
+/*******************************************************************************
+ * Copyright 2005-2007, CHISEL Group, University of Victoria, Victoria, BC, Canada.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * The Chisel Group, University of Victoria
+ *******************************************************************************/
+package org.eclipse.zest.examples.jface;
+
+import org.eclipse.jface.viewers.ISelectionChangedListener;
+import org.eclipse.jface.viewers.ITreeContentProvider;
+import org.eclipse.jface.viewers.LabelProvider;
+import org.eclipse.jface.viewers.SelectionChangedEvent;
+import org.eclipse.jface.viewers.Viewer;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.events.SelectionAdapter;
+import org.eclipse.swt.events.SelectionEvent;
+import org.eclipse.swt.graphics.Image;
+import org.eclipse.swt.layout.FillLayout;
+import org.eclipse.swt.widgets.Button;
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.swt.widgets.Shell;
+import org.eclipse.zest.core.viewers.GraphViewer;
+import org.eclipse.zest.layouts.algorithms.TreeLayoutAlgorithm;
+
+/**
+ * This snippet shows how to use the ITreeContentProvider to build a graph.
+ *
+ * @author Ian Bull
+ *
+ */
+public class GraphJFaceSnippet9 {
+
+ /**
+ * The Content Provider
+ *
+ * @author irbull
+ *
+ */
+ static class MyContentProvider implements ITreeContentProvider {
+
+ private static final String n1 = "First", n2 = "Second", n3 = "Third",
+ n4 = "Forth";
+
+ public Object[] getElements(Object inputElement) {
+ return new String[] { n1 };
+ }
+
+ public void dispose() {
+
+ }
+
+ public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
+
+ }
+
+ public Object[] getChildren(Object parentElement) {
+ if (parentElement.equals(n1)) {
+ return new Object[] { n2, n3 };
+ }
+ if (parentElement.equals(n2)) {
+ return new Object[] { n4 };
+ }
+ return null;
+ }
+
+ public Object getParent(Object element) {
+ if (element.equals(n2)) {
+ return n1;
+ } else if (element.equals(n3)) {
+ return n1;
+ } else if (element.equals(n4)) {
+ return new Object[] { n2 };
+ }
+ return null;
+ }
+
+ public boolean hasChildren(Object element) {
+ return element.equals("First") || element.equals("Second");
+ }
+ }
+
+ static class MyLabelProvider extends LabelProvider {
+ final Image image = Display.getDefault().getSystemImage(
+ SWT.ICON_WARNING);
+
+ public Image getImage(Object element) {
+ if (element instanceof String) {
+ return image;
+ }
+ return null;
+ }
+
+ public String getText(Object element) {
+ if (element instanceof String) {
+ return element.toString();
+ }
+ return null;
+ }
+
+ }
+
+ static GraphViewer viewer = null;
+
+ /**
+ * @param args
+ */
+ public static void main(String[] args) {
+ Display d = new Display();
+ Shell shell = new Shell(d);
+ shell.setLayout(new FillLayout(SWT.VERTICAL));
+ shell.setSize(400, 400);
+ Button button = new Button(shell, SWT.PUSH);
+ button.setText("Reload");
+ button.addSelectionListener(new SelectionAdapter() {
+
+ public void widgetSelected(SelectionEvent e) {
+ viewer.setInput(new Object());
+ }
+
+ });
+ viewer = new GraphViewer(shell, SWT.NONE);
+
+ viewer.setContentProvider(new MyContentProvider());
+ viewer.setLabelProvider(new MyLabelProvider());
+ viewer.setLayoutAlgorithm(new TreeLayoutAlgorithm());
+ viewer.addSelectionChangedListener(new ISelectionChangedListener() {
+ public void selectionChanged(SelectionChangedEvent event) {
+ System.out.println("Selection changed: "
+ + (event.getSelection()));
+ }
+
+ });
+ viewer.setInput(new Object());
+
+ shell.open();
+ while (!shell.isDisposed()) {
+ while (!d.readAndDispatch()) {
+ d.sleep();
+ }
+ }
+
+ }
+}
diff --git a/org.eclipse.zest.jface/src/org/eclipse/zest/core/viewers/GraphViewer.java b/org.eclipse.zest.jface/src/org/eclipse/zest/core/viewers/GraphViewer.java
index b74921f..64184a0 100644
--- a/org.eclipse.zest.jface/src/org/eclipse/zest/core/viewers/GraphViewer.java
+++ b/org.eclipse.zest.jface/src/org/eclipse/zest/core/viewers/GraphViewer.java
@@ -18,6 +18,7 @@ import org.eclipse.jface.viewers.IContentProvider;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.ISelectionProvider;
+import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
@@ -31,6 +32,7 @@ import org.eclipse.zest.core.viewers.internal.GraphModelEntityFactory;
import org.eclipse.zest.core.viewers.internal.GraphModelEntityRelationshipFactory;
import org.eclipse.zest.core.viewers.internal.GraphModelFactory;
import org.eclipse.zest.core.viewers.internal.IStylingGraphModelFactory;
+import org.eclipse.zest.core.viewers.internal.TreeModelEntityFactory;
import org.eclipse.zest.core.widgets.Graph;
import org.eclipse.zest.core.widgets.GraphItem;
import org.eclipse.zest.core.widgets.ZestStyles;
@@ -196,9 +198,11 @@ public class GraphViewer extends AbstractStructuredGraphViewer implements
super.setContentProvider(contentProvider);
} else if (contentProvider instanceof IGraphEntityRelationshipContentProvider) {
super.setContentProvider(contentProvider);
+ } else if (contentProvider instanceof ITreeContentProvider) {
+ super.setContentProvider(contentProvider);
} else {
throw new IllegalArgumentException(
- "Invalid content provider, only IGraphContentProvider, IGraphEntityContentProvider, or IGraphEntityRelationshipContentProvider are supported.");
+ "Invalid content provider, only IGraphContentProvider, IGraphEntityContentProvider, IGraphEntityRelationshipContentProvider and ITreeContentProvider are supported.");
}
}
@@ -369,6 +373,8 @@ public class GraphViewer extends AbstractStructuredGraphViewer implements
modelFactory = new GraphModelEntityFactory(this);
} else if (getContentProvider() instanceof IGraphEntityRelationshipContentProvider) {
modelFactory = new GraphModelEntityRelationshipFactory(this);
+ } else if (getContentProvider() instanceof ITreeContentProvider) {
+ modelFactory = new TreeModelEntityFactory(this);
}
}
return modelFactory;
diff --git a/org.eclipse.zest.jface/src/org/eclipse/zest/core/viewers/internal/TreeModelEntityFactory.java b/org.eclipse.zest.jface/src/org/eclipse/zest/core/viewers/internal/TreeModelEntityFactory.java
new file mode 100644
index 0000000..8fbdab4
--- /dev/null
+++ b/org.eclipse.zest.jface/src/org/eclipse/zest/core/viewers/internal/TreeModelEntityFactory.java
@@ -0,0 +1,118 @@
+/*
+ * Tree Model Entity Factory
+ *
+ * Authors:
+ * Sushant Sirsikar - initial implementation
+ * Zoltan Ujhelyi - adaptation to Zest 2.0
+ */
+package org.eclipse.zest.core.viewers.internal;
+
+import org.eclipse.jface.viewers.ITreeContentProvider;
+import org.eclipse.zest.core.viewers.AbstractStructuredGraphViewer;
+import org.eclipse.zest.core.viewers.EntityConnectionData;
+import org.eclipse.zest.core.widgets.Graph;
+import org.eclipse.zest.core.widgets.GraphConnection;
+import org.eclipse.zest.core.widgets.GraphNode;
+
+/**
+ * A model factory for creating graph content based on an ITreeContentProvider
+ *
+ * @author sushant sirsikar
+ *
+ */
+public class TreeModelEntityFactory extends AbstractStylingModelFactory {
+
+ AbstractStructuredGraphViewer viewer = null;
+
+ public TreeModelEntityFactory(AbstractStructuredGraphViewer viewer) {
+ super(viewer);
+ this.viewer = viewer;
+ }
+
+ @Override
+ public Graph createGraphModel(Graph model) {
+ doBuildGraph(model);
+ return model;
+ }
+
+ @Override
+ protected void doBuildGraph(Graph model) {
+ super.doBuildGraph(model);
+ Object inputElement = getViewer().getInput();
+ ITreeContentProvider provider = (ITreeContentProvider) getContentProvider();
+ Object[] entities = provider.getElements(inputElement);
+ if (entities == null)
+ return;
+ for (Object data : entities) {
+ createGraphNodes(model, data, provider);
+
+ }
+ }
+
+ private GraphNode createGraphNodes(Graph model, Object data,
+ ITreeContentProvider provider) {
+ GraphNode node = createNode(model, data);
+ if (provider.hasChildren(data)) {
+ for (Object child : provider.getChildren(data)) {
+ GraphNode childNode = createGraphNodes(model, child, provider);
+ EntityConnectionData connectionData = new EntityConnectionData(
+ node, childNode);
+ createConnection(model, connectionData, data, child);
+ }
+ }
+ return node;
+ }
+
+ @Override
+ public void refresh(Graph graph, Object element, boolean updateLabels) {
+ if (element == null) {
+ return;
+ }
+ GraphNode node = viewer.getGraphModelNode(element);
+ if (node == null) {
+ GraphConnection conn = viewer.getGraphModelConnection(element);
+ if (conn != null) {
+ refresh(graph, conn.getSource().getData(), updateLabels);
+ refresh(graph, conn.getDestination().getData(), updateLabels);
+ return;
+ }
+ }
+ if (node == null)
+ return;
+
+ reconnect(graph, element, updateLabels);
+
+ if (updateLabels)
+ update(node);
+ }
+
+ /**
+ * Reconnects all the connections.
+ *
+ * @param graph
+ * @param element
+ * @param updateLabels
+ */
+ private void reconnect(Graph graph, Object element, boolean updateLabels) {
+ GraphNode sourceNode = viewer.getGraphModelNode(element);
+ Object[] children = ((ITreeContentProvider) getContentProvider())
+ .getChildren(element);
+ EntityConnectionData data = null;
+ GraphConnection oldConnection = null;
+ GraphConnection newConnection = null;
+ for (Object child : children) {
+ GraphNode destNode = viewer.getGraphModelNode(child);
+ data = new EntityConnectionData(sourceNode, destNode);
+ oldConnection = viewer.getGraphModelConnection(data);
+ if (oldConnection != null) {
+ viewer.removeGraphModelConnection(oldConnection);
+ newConnection = createConnection(graph, data,
+ sourceNode.getData(), destNode.getData());
+ if (updateLabels) {
+ styleItem(newConnection);
+ }
+ }
+ refresh(graph, child, updateLabels);
+ }
+ }
+}