summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSimon Templer2011-11-21 09:08:32 (EST)
committerFabian Steeg2011-12-19 10:58:06 (EST)
commit22efd6bd97a4df90a0a4c1f6b9712d388fbb48b5 (patch)
tree1f13d7d31a80d26edd8b3171dbc9a167ae83cd3a
parentd3429c51ed0d630cfdba2eddc11b4a6adf47ff65 (diff)
downloadorg.eclipse.gef4-22efd6bd97a4df90a0a4c1f6b9712d388fbb48b5.zip
org.eclipse.gef4-22efd6bd97a4df90a0a4c1f6b9712d388fbb48b5.tar.gz
org.eclipse.gef4-22efd6bd97a4df90a0a4c1f6b9712d388fbb48b5.tar.bz2
added support for custom figures being styled according to model and selection
Added ILabeledFigure and IStyleableFigure interfaces to mark figures that allow the label text/icon or the figure style (colors, border width) being adapted according to the model. GraphNode will now update figures that are no GraphLabels, text/icon will only be updated for ILabeledFigures, style only for IStyleableFigures. Let CachedLabel implement ILabeledFigure. Let GraphLabel implement IStyleableFigure. Updated CGraphNode to use super implementation of updateFigureForModel(...) and use the colors and font configured by the model. Added sample snippet CustomFigureJFaceSnippet to examples project.
-rw-r--r--org.eclipse.zest.core/src/org/eclipse/zest/core/widgets/CGraphNode.java26
-rw-r--r--org.eclipse.zest.core/src/org/eclipse/zest/core/widgets/GraphNode.java51
-rw-r--r--org.eclipse.zest.core/src/org/eclipse/zest/core/widgets/ILabeledFigure.java56
-rw-r--r--org.eclipse.zest.core/src/org/eclipse/zest/core/widgets/IStyleableFigure.java50
-rw-r--r--org.eclipse.zest.core/src/org/eclipse/zest/core/widgets/custom/CGraphNode.java26
-rw-r--r--org.eclipse.zest.core/src/org/eclipse/zest/core/widgets/internal/CachedLabel.java3
-rw-r--r--org.eclipse.zest.core/src/org/eclipse/zest/core/widgets/internal/GraphLabel.java3
-rw-r--r--org.eclipse.zest.examples/src/org/eclipse/zest/examples/jface/CustomFigureJFaceSnippet.java182
8 files changed, 321 insertions, 76 deletions
diff --git a/org.eclipse.zest.core/src/org/eclipse/zest/core/widgets/CGraphNode.java b/org.eclipse.zest.core/src/org/eclipse/zest/core/widgets/CGraphNode.java
index 8febe86..fb62165 100644
--- a/org.eclipse.zest.core/src/org/eclipse/zest/core/widgets/CGraphNode.java
+++ b/org.eclipse.zest.core/src/org/eclipse/zest/core/widgets/CGraphNode.java
@@ -1,8 +1,6 @@
package org.eclipse.zest.core.widgets;
import org.eclipse.draw2d.IFigure;
-import org.eclipse.swt.graphics.Color;
-import org.eclipse.swt.graphics.Font;
/**
* A Custom Graph Node
@@ -24,28 +22,4 @@ public class CGraphNode extends GraphNode {
return this.figure;
}
- public void setBackgroundColor(Color c) {
- getFigure().setBackgroundColor(c);
- }
-
- public void setFont(Font font) {
- getFigure().setFont(font);
- }
-
- public Color getBackgroundColor() {
- return getFigure().getBackgroundColor();
- }
-
- public Font getFont() {
- return getFigure().getFont();
- }
-
- public Color getForegroundColor() {
- return getFigure().getForegroundColor();
- }
-
- protected void updateFigureForModel(IFigure currentFigure) {
- // Undefined
- }
-
}
diff --git a/org.eclipse.zest.core/src/org/eclipse/zest/core/widgets/GraphNode.java b/org.eclipse.zest.core/src/org/eclipse/zest/core/widgets/GraphNode.java
index 78ab327..32feae5 100644
--- a/org.eclipse.zest.core/src/org/eclipse/zest/core/widgets/GraphNode.java
+++ b/org.eclipse.zest.core/src/org/eclipse/zest/core/widgets/GraphNode.java
@@ -607,34 +607,40 @@ public class GraphNode extends GraphItem {
return;
}
- if (!(currentFigure instanceof GraphLabel)) {
- return;
- }
- GraphLabel figure = (GraphLabel) currentFigure;
+ IFigure figure = currentFigure;
IFigure toolTip;
- if (!checkStyle(ZestStyles.NODES_HIDE_TEXT)
- && !figure.getText().equals(this.getText())) {
- figure.setText(this.getText());
- }
- if (figure.getIcon() != getImage()) {
- figure.setIcon(getImage());
+ if (figure instanceof ILabeledFigure) {
+ // update label text/icon for figures implementing ILabeledFigure
+ ILabeledFigure labeledFigure = (ILabeledFigure) figure;
+ if (!checkStyle(ZestStyles.NODES_HIDE_TEXT)
+ && !labeledFigure.getText().equals(this.getText())) {
+ labeledFigure.setText(this.getText());
+ }
+ if (labeledFigure.getIcon() != getImage()) {
+ labeledFigure.setIcon(getImage());
+ }
}
- if (highlighted == HIGHLIGHT_ON) {
- figure.setForegroundColor(getForegroundColor());
- figure.setBackgroundColor(getHighlightColor());
- figure.setBorderColor(getBorderHighlightColor());
- } else {
- figure.setForegroundColor(getForegroundColor());
- figure.setBackgroundColor(getBackgroundColor());
- figure.setBorderColor(getBorderColor());
- }
+ if (figure instanceof IStyleableFigure) {
+ // update styles (colors, border) for figures implementing
+ // IStyleableFigure
+ IStyleableFigure styleableFigure = (IStyleableFigure) figure;
+ if (highlighted == HIGHLIGHT_ON) {
+ styleableFigure.setForegroundColor(getForegroundColor());
+ styleableFigure.setBackgroundColor(getHighlightColor());
+ styleableFigure.setBorderColor(getBorderHighlightColor());
+ } else {
+ styleableFigure.setForegroundColor(getForegroundColor());
+ styleableFigure.setBackgroundColor(getBackgroundColor());
+ styleableFigure.setBorderColor(getBorderColor());
+ }
- figure.setBorderWidth(getBorderWidth());
+ styleableFigure.setBorderWidth(getBorderWidth());
- if (figure.getFont() != getFont()) {
- figure.setFont(getFont());
+ if (figure.getFont() != getFont()) {
+ figure.setFont(getFont());
+ }
}
if (this.getTooltip() == null && hasCustomTooltip == false) {
@@ -652,6 +658,7 @@ public class GraphNode extends GraphItem {
this.fishEyeFigure = newFisheyeFigure;
}
}
+
refreshBounds();
}
diff --git a/org.eclipse.zest.core/src/org/eclipse/zest/core/widgets/ILabeledFigure.java b/org.eclipse.zest.core/src/org/eclipse/zest/core/widgets/ILabeledFigure.java
new file mode 100644
index 0000000..f9afc81
--- /dev/null
+++ b/org.eclipse.zest.core/src/org/eclipse/zest/core/widgets/ILabeledFigure.java
@@ -0,0 +1,56 @@
+/*******************************************************************************
+ * Copyright (c) 2011 Simon Templer.
+ * 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:
+ * Simon Templer - introduced this interface to allow custom figures being
+ * configured with text and icon through the GraphNode class, associated
+ * to bug 335136
+ *******************************************************************************/
+package org.eclipse.zest.core.widgets;
+
+import org.eclipse.draw2d.IFigure;
+import org.eclipse.swt.graphics.Image;
+
+/**
+ * Marks a figure that allows being configured with a text and icon through a
+ * {@link GraphNode} from the model.
+ *
+ * @author Simon Templer
+ */
+public interface ILabeledFigure extends IFigure {
+
+ /**
+ * Set the label text. If needed, the figure should adjust its size.
+ *
+ * @param text
+ * the text
+ */
+ public void setText(String text);
+
+ /**
+ * Get the label text.
+ *
+ * @return the label text
+ */
+ public String getText();
+
+ /**
+ * Set the label icon. If needed, the figure should adjust its size.
+ *
+ * @param icon
+ * the icon image or <code>null</code> for no icon
+ */
+ public void setIcon(Image icon);
+
+ /**
+ * Get the label icon.
+ *
+ * @return the icon image
+ */
+ public Image getIcon();
+
+}
diff --git a/org.eclipse.zest.core/src/org/eclipse/zest/core/widgets/IStyleableFigure.java b/org.eclipse.zest.core/src/org/eclipse/zest/core/widgets/IStyleableFigure.java
new file mode 100644
index 0000000..19047ab
--- /dev/null
+++ b/org.eclipse.zest.core/src/org/eclipse/zest/core/widgets/IStyleableFigure.java
@@ -0,0 +1,50 @@
+/*******************************************************************************
+ * Copyright (c) 2011 Simon Templer.
+ * 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:
+ * Simon Templer - introduced this interface to allow custom figures being
+ * configured with foreground/background/border colors, font and border
+ * width through the GraphNode class, associated to bug 335136
+ *******************************************************************************/
+package org.eclipse.zest.core.widgets;
+
+import org.eclipse.draw2d.IFigure;
+import org.eclipse.swt.graphics.Color;
+
+/**
+ * Marks a figure that allows its style being configured through a
+ * {@link GraphNode} from the model.<br>
+ * <br>
+ * If a figure implementing this interface is associated to a {@link GraphNode},
+ * the foreground, background and border colors will be configured according to
+ * the model, as well as the font and the border width.
+ *
+ * @see IFigure#setFont(org.eclipse.swt.graphics.Font)
+ * @see IFigure#setForegroundColor(Color)
+ * @see IFigure#setBackgroundColor(Color)
+ *
+ * @author Simon Templer
+ */
+public interface IStyleableFigure extends IFigure {
+
+ /**
+ * Set the border color
+ *
+ * @param borderColor
+ * the border color
+ */
+ void setBorderColor(Color borderColor);
+
+ /**
+ * Set the border width
+ *
+ * @param borderWidth
+ * the border width
+ */
+ void setBorderWidth(int borderWidth);
+
+}
diff --git a/org.eclipse.zest.core/src/org/eclipse/zest/core/widgets/custom/CGraphNode.java b/org.eclipse.zest.core/src/org/eclipse/zest/core/widgets/custom/CGraphNode.java
index a4142bd..3d81fb9 100644
--- a/org.eclipse.zest.core/src/org/eclipse/zest/core/widgets/custom/CGraphNode.java
+++ b/org.eclipse.zest.core/src/org/eclipse/zest/core/widgets/custom/CGraphNode.java
@@ -11,8 +11,6 @@
package org.eclipse.zest.core.widgets.custom;
import org.eclipse.draw2d.IFigure;
-import org.eclipse.swt.graphics.Color;
-import org.eclipse.swt.graphics.Font;
import org.eclipse.zest.core.widgets.Graph;
import org.eclipse.zest.core.widgets.GraphContainer;
import org.eclipse.zest.core.widgets.GraphNode;
@@ -49,28 +47,4 @@ public class CGraphNode extends GraphNode {
return this.figure;
}
- public void setBackgroundColor(Color c) {
- getFigure().setBackgroundColor(c);
- }
-
- public void setFont(Font font) {
- getFigure().setFont(font);
- }
-
- public Color getBackgroundColor() {
- return getFigure().getBackgroundColor();
- }
-
- public Font getFont() {
- return getFigure().getFont();
- }
-
- public Color getForegroundColor() {
- return getFigure().getForegroundColor();
- }
-
- protected void updateFigureForModel(IFigure currentFigure) {
- // Undefined
- }
-
}
diff --git a/org.eclipse.zest.core/src/org/eclipse/zest/core/widgets/internal/CachedLabel.java b/org.eclipse.zest.core/src/org/eclipse/zest/core/widgets/internal/CachedLabel.java
index 70f6c3d..b83628f 100644
--- a/org.eclipse.zest.core/src/org/eclipse/zest/core/widgets/internal/CachedLabel.java
+++ b/org.eclipse.zest.core/src/org/eclipse/zest/core/widgets/internal/CachedLabel.java
@@ -22,6 +22,7 @@ import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
+import org.eclipse.zest.core.widgets.ILabeledFigure;
/**
* A cached label to improve performance of text drawing under linux
@@ -29,7 +30,7 @@ import org.eclipse.swt.widgets.Display;
* @author Ian Bull
*
*/
-public abstract class CachedLabel extends Label {
+public abstract class CachedLabel extends Label implements ILabeledFigure {
/*
* (non-Javadoc)
diff --git a/org.eclipse.zest.core/src/org/eclipse/zest/core/widgets/internal/GraphLabel.java b/org.eclipse.zest.core/src/org/eclipse/zest/core/widgets/internal/GraphLabel.java
index 4ea465a..e2850b0 100644
--- a/org.eclipse.zest.core/src/org/eclipse/zest/core/widgets/internal/GraphLabel.java
+++ b/org.eclipse.zest.core/src/org/eclipse/zest/core/widgets/internal/GraphLabel.java
@@ -22,6 +22,7 @@ import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.Display;
+import org.eclipse.zest.core.widgets.IStyleableFigure;
/**
* Overrides the Draw2D Label Figure class to ensure that the text is never
@@ -29,7 +30,7 @@ import org.eclipse.swt.widgets.Display;
*
* @author Chris Callendar
*/
-public class GraphLabel extends CachedLabel {
+public class GraphLabel extends CachedLabel implements IStyleableFigure {
private Color borderColor;
private int borderWidth;
diff --git a/org.eclipse.zest.examples/src/org/eclipse/zest/examples/jface/CustomFigureJFaceSnippet.java b/org.eclipse.zest.examples/src/org/eclipse/zest/examples/jface/CustomFigureJFaceSnippet.java
new file mode 100644
index 0000000..4d89aad
--- /dev/null
+++ b/org.eclipse.zest.examples/src/org/eclipse/zest/examples/jface/CustomFigureJFaceSnippet.java
@@ -0,0 +1,182 @@
+/*******************************************************************************
+ * Copyright (c) 2011 Simon Templer.
+ * 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:
+ * Simon Templer - example for using custom figures based on
+ * ILabeledFigure/IStyleableFigure, associated to bug 335136
+ *******************************************************************************/
+package org.eclipse.zest.examples.jface;
+
+import org.eclipse.draw2d.Graphics;
+import org.eclipse.draw2d.GridData;
+import org.eclipse.draw2d.GridLayout;
+import org.eclipse.draw2d.IFigure;
+import org.eclipse.draw2d.Label;
+import org.eclipse.draw2d.RectangleFigure;
+import org.eclipse.jface.viewers.LabelProvider;
+import org.eclipse.jface.viewers.Viewer;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.graphics.Color;
+import org.eclipse.swt.graphics.Image;
+import org.eclipse.swt.layout.FillLayout;
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.swt.widgets.Shell;
+import org.eclipse.zest.core.viewers.EntityConnectionData;
+import org.eclipse.zest.core.viewers.GraphViewer;
+import org.eclipse.zest.core.viewers.IFigureProvider;
+import org.eclipse.zest.core.viewers.IGraphEntityContentProvider;
+import org.eclipse.zest.core.widgets.ILabeledFigure;
+import org.eclipse.zest.core.widgets.IStyleableFigure;
+import org.eclipse.zest.layouts.algorithms.SpringLayoutAlgorithm;
+
+/**
+ * Example demonstrating the use of a custom figure that implements
+ * {@link ILabeledFigure} and {@link IStyleableFigure}.<br>
+ * <br>
+ * The custom figure will be configured according to the label provider and
+ * highlighted when it is selected.
+ */
+public class CustomFigureJFaceSnippet {
+
+ /**
+ * Custom figure shaped like a rectangle.
+ */
+ public static class RectLabelFigure extends RectangleFigure implements
+ ILabeledFigure, IStyleableFigure {
+
+ private Label label;
+
+ private Color borderColor;
+
+ public RectLabelFigure() {
+ super();
+
+ setLayoutManager(new GridLayout(1, true));
+
+ label = new Label();
+ add(label);
+ GridData gd = new GridData(GridData.FILL, GridData.FILL, true, true);
+ setConstraint(label, gd);
+ }
+
+ protected void outlineShape(Graphics graphics) {
+ graphics.setForegroundColor(borderColor);
+
+ super.outlineShape(graphics);
+ }
+
+ public void setBorderColor(Color borderColor) {
+ this.borderColor = borderColor;
+ }
+
+ public void setBorderWidth(int borderWidth) {
+ setLineWidth(borderWidth);
+ }
+
+ public void setText(String text) {
+ label.setText(text);
+ adjustSize();
+ }
+
+ public String getText() {
+ return label.getText();
+ }
+
+ public void setIcon(Image icon) {
+ label.setIcon(icon);
+ adjustSize();
+ }
+
+ public Image getIcon() {
+ return label.getIcon();
+ }
+
+ protected void adjustSize() {
+ setSize(getPreferredSize());
+ }
+
+ }
+
+ static class MyContentProvider implements IGraphEntityContentProvider {
+
+ public Object[] getConnectedTo(Object entity) {
+ if (entity.equals("One")) {
+ return new Object[] { "Two" };
+ }
+ if (entity.equals("Two")) {
+ return new Object[] { "Three" };
+ }
+ if (entity.equals("Three")) {
+ return new Object[] { "One" };
+ }
+ return null;
+ }
+
+ public Object[] getElements(Object inputElement) {
+ return new String[] { "One", "Two", "Three" };
+ }
+
+ public void dispose() {
+ }
+
+ public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
+ }
+
+ }
+
+ static class MyLabelProvider extends LabelProvider implements
+ IFigureProvider {
+ final Image image = Display.getDefault().getSystemImage(
+ SWT.ICON_INFORMATION);
+
+ public Image getImage(Object element) {
+ if (element instanceof EntityConnectionData) {
+ return null;
+ }
+
+ return image;
+ }
+
+ public String getText(Object element) {
+ if (element instanceof EntityConnectionData) {
+ return null;
+ }
+
+ return element.toString();
+ }
+
+ public IFigure getFigure(Object element) {
+ // use a custom figure
+ return new RectLabelFigure();
+ }
+
+ }
+
+ static GraphViewer viewer = null;
+
+ /**
+ * @param args
+ */
+ public static void main(String[] args) {
+ Display d = new Display();
+ Shell shell = new Shell(d);
+ shell.setText("CustomFigureJFaceSnippet");
+ 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());
+ viewer.setInput(new Object());
+ shell.open();
+ while (!shell.isDisposed()) {
+ while (!d.readAndDispatch()) {
+ d.sleep();
+ }
+ }
+ }
+} \ No newline at end of file