[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Newsgroup Home]
[news.eclipse.modeling.gmf] Re: Layout issue with Polygon in Composite Figure

Hi Julia,


thanks for the hints! Although I already knew these documentation resources I rather tried composing the figure just with .gmfgraph which wont work for Polygons in the given scenario.


So I followed your advice and created RhombFigure (everyone feel free to use, modify and distribute):


__________________________________________________________________________ package org.eclipse.draw2d;

import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.RectangleFigure;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.PointList;
import org.eclipse.draw2d.geometry.Rectangle;

/**
* Draws a filled rhomb whose size is determined by its bounds.
* Each corner of the rhomb matches the middle of the corresponding edge * given by {@link #getBounds()}.
*/
public class RhombFigure extends RectangleFigure {
private PointList cachedPointList;
private Rectangle oldBounds;


/**
* Constructs a new RhombFigure, sets <code>fill</code> to <code>true</code>. */
public RhombFigure() {
super();
this.setFill(true);
}


	/**
	 * Fills the rhomb.
	 * @see org.eclipse.draw2d.Shape#fillShape(org.eclipse.draw2d.Graphics)
	 */
	@Override
	protected void fillShape(Graphics graphics) {
		graphics.pushState();
		graphics.fillPolygon(getPointList());
		graphics.popState();
	}

	/**
	 * Outlines the rhomb.
	 * @see org.eclipse.draw2d.Shape#outlineShape(org.eclipse.draw2d.Graphics)
	 */
	@Override
	protected void outlineShape(Graphics graphics) {
		graphics.pushState();
		graphics.drawPolygon(getPointList());
		graphics.popState();
	}

/**
* Returns the PointList for a rhomb style polygon filling the <code>Rectangle</code>
* returned by {@link org.eclipse.draw2d.RectangleFigure#getBounds()}.
* * @return the desired <code>PointList</code> containing absolute coordinates
*/
private PointList getPointList() {
Rectangle bounds = getBounds();


		if (oldBounds == null || !(bounds.equals(oldBounds))) {
			// need to recalculate since bounds changed
			
			// use a copy - otherwise oldBounds never changes ;o)
			this.oldBounds = bounds.getCopy();

			// leave 1 px margin on each edge to avoid clipping
			int left = bounds.x + 1;
			int right = (left + bounds.width) - 2;
			int center = left + (int)Math.floor((right - left) / 2);

			int top = bounds.y + 1;
			int bottom = top + bounds.height - 2;
			int middle = top + (int)Math.floor((bottom - top) / 2);

			this.cachedPointList = new PointList();
			this.cachedPointList.addPoint(new Point(center, top));
			this.cachedPointList.addPoint(new Point(right, middle));
			this.cachedPointList.addPoint(new Point(center, bottom));
			this.cachedPointList.addPoint(new Point(left, middle));
		}
		
		return this.cachedPointList;
	}
}
__________________________________________________________________________

And modified my .gmfgraph to use the new Custom Figure:


<descriptors name="ResourceFigure"> <actualFigure xsi:type="gmfgraph:Rectangle" name="ResourceFigure" outline="false" fill="false"> <layout xsi:type="gmfgraph:StackLayout"/> <children xsi:type="gmfgraph:Rectangle" name="Inner" outline="false" fill="false"> <layout xsi:type="gmfgraph:GridLayout" equalWidth="false"/> <foregroundColor xsi:type="gmfgraph:ConstantColor" value="gray"/> <children xsi:type="gmfgraph:CustomFigure" name="MyRhombFigure" qualifiedClassName="org.eclipse.draw2d.RhombFigure"> <layoutData xsi:type="gmfgraph:GridLayoutData" verticalAlignment="BEGINNING"/> <backgroundColor xsi:type="gmfgraph:ConstantColor" value="yellow"/> <size x="30" y="30"/> </children> <children xsi:type="gmfgraph:Rectangle" name="Body"> <layoutData xsi:type="gmfgraph:GridLayoutData" grabExcessHorizontalSpace="true" grabExcessVerticalSpace="true" verticalAlignment="FILL" horizontalAlignment="FILL"/> <backgroundColor xsi:type="gmfgraph:ConstantColor" value="gray"/> <size x="120" y="200"/> </children> </children> </actualFigure> </descriptors>



Maybe someone might find this useful...


Thanks all

Christian