carbidecpp22devenv/plugins/org.eclipse.gmf.templates.legacy_1.0.0.v20090614-0839/graphdef.codegen.templates/xtras/ScalablePolygon.xpt
/*
* Copyright (c) 2006 Borland Software Corporation
*
* 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:
* Michael Golubev (Borland) - initial API and implementation
*/
«IMPORT "http://www.eclipse.org/gmf/2006/GraphicalDefinition"»
«IMPORT "http://www.eclipse.org/emf/2002/Ecore"»
«DEFINE behaviour FOR gmfgraph::ScalablePolygon-»
/**
* @generated
*/
private final org.eclipse.draw2d.geometry.PointList myTemplate = new org.eclipse.draw2d.geometry.PointList();
/**
* @generated
*/
private org.eclipse.draw2d.geometry.Rectangle myTemplateBounds;
/**
* @generated
*/
public void addPoint(org.eclipse.draw2d.geometry.Point point){
myTemplate.addPoint(point);
myTemplateBounds = null;
}
/**
* @generated
*/
protected void fillShape(org.eclipse.draw2d.Graphics graphics) {
org.eclipse.draw2d.geometry.Rectangle bounds = getBounds();
graphics.pushState();
graphics.translate(bounds.x, bounds.y);
graphics.fillPolygon(scalePointList());
graphics.popState();
}
/**
* @generated
*/
protected void outlineShape(org.eclipse.draw2d.Graphics graphics) {
org.eclipse.draw2d.geometry.Rectangle bounds = getBounds();
graphics.pushState();
graphics.translate(bounds.x, bounds.y);
graphics.drawPolygon(scalePointList());
graphics.popState();
}
/**
* @generated
*/
private org.eclipse.draw2d.geometry.Rectangle getTemplateBounds(){
if (myTemplateBounds == null){
myTemplateBounds = myTemplate.getBounds().getCopy().union(0, 0);
//just safety -- we are going to use this as divider
if (myTemplateBounds.width < 1){
myTemplateBounds.width = 1;
}
if (myTemplateBounds.height < 1){
myTemplateBounds.height = 1;
}
}
return myTemplateBounds;
}
/**
* @generated
*/
private int[] scalePointList() {
org.eclipse.draw2d.geometry.Rectangle pointsBounds = getTemplateBounds();
org.eclipse.draw2d.geometry.Rectangle actualBounds = getBounds();
float xScale = ((float) actualBounds.width) / pointsBounds.width;
float yScale = ((float) actualBounds.height) / pointsBounds.height;
if (xScale == 1 && yScale == 1) {
return myTemplate.toIntArray();
}
int[] scaled = (int[]) myTemplate.toIntArray().clone();
for (int i = 0; i < scaled.length; i += 2) {
scaled[i] = (int) Math.floor(scaled[i] * xScale);
scaled[i + 1] = (int) Math.floor(scaled[i + 1] * yScale);
}
return scaled;
}
«ENDDEFINE»