Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2009 Anyware Technologies 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: Anyware Technologies - initial API and implementation $Id: ToolbarLayoutExt.java,v 1.1 2009/04/20 15:08:17 jlescot Exp $ /
 
 package org.eclipse.emf.ecoretools.diagram.edit.figures;
 
 import java.util.List;
 
A ToolbarLayout with the following additional capabilities:
  • the last child figure of the toolbar can fill the free space ( false by default)

Author(s):
Simon Bernard
Since:
0.9
 
 public class ToolbarLayoutExt extends ToolbarLayout {
 
 	private boolean expandLastItemMajorAxis = false;
 
 	public void layout(IFigure parent) {
 		List<?> children = parent.getChildren();
 		int numChildren = children.size();
 		Rectangle clientArea = .t(parent.getClientArea());
 		int x = clientArea.x;
 		int y = clientArea.y;
 		int availableHeight = clientArea.height;
 
 		Dimension prefSizes[] = new Dimension[numChildren];
 		Dimension minSizes[] = new Dimension[numChildren];
 
 		// Calculate the width and height hints. If it's a vertical
 		// ToolBarLayout,
 		// then ignore the height hint (set it to -1); otherwise, ignore the
 		// width hint. These hints will be passed to the children of the parent
 		// figure when getting their preferred size.
 		int wHint = -1;
 		int hHint = -1;
 		if (isHorizontal()) {
 		} else {
 		}
 
 		/*
 		 * Calculate sum of preferred heights of all children(totalHeight).
 		 * Calculate sum of minimum heights of all children(minHeight). Cache
 		 * Preferred Sizes and Minimum Sizes of all children.
 		 * 
 		 * totalHeight is the sum of the preferred heights of all children
 		 * totalMinHeight is the sum of the minimum heights of all children
 		 * prefMinSumHeight is the sum of the difference between all children's
 		 * preferred heights and minimum heights. (This is used as a ratio to
 		 * calculate how much each child will shrink).
 		 */
 		IFigure child;
 		int totalHeight = 0;
 		int totalMinHeight = 0;
 		int prefMinSumHeight = 0;
 
 		for (int i = 0; i < numChildreni++) {
 			child = (IFigurechildren.get(i);
 
 			prefSizes[i] = .t(getChildPreferredSize(childwHinthHint));
 			minSizes[i] = .t(getChildMinimumSize(childwHinthHint));
 
 			totalHeight += prefSizes[i].;
 			totalMinHeight += minSizes[i].;
 		}
 		totalHeight += (numChildren - 1) * ;
 		totalMinHeight += (numChildren - 1) * ;
 		prefMinSumHeight = totalHeight - totalMinHeight;
 		/*
 		 * The total amount that the children must be shrunk is the sum of the
 		 * preferred Heights of the children minus Max(the available area and
 		 * the sum of the minimum heights of the children).
 		 * 
 		 * amntShrinkHeight is the combined amount that the children must shrink
 		 * amntShrinkCurrentHeight is the amount each child will shrink
 		 * respectively
 		 */
 		int amntShrinkHeight = totalHeight - Math.max(availableHeighttotalMinHeight);
		int heightNotUsed = 0;
		if (amntShrinkHeight < 0) {
			heightNotUsed = -amntShrinkHeight;
			amntShrinkHeight = 0;
		}
		for (int i = 0; i < numChildreni++) {
			int amntShrinkCurrentHeight = 0;
			int prefHeight = prefSizes[i].;
			int minHeight = minSizes[i].;
			int prefWidth = prefSizes[i].;
			int minWidth = minSizes[i].;
			Rectangle newBounds = new Rectangle(xyprefWidthprefHeight);
			child = (IFigurechildren.get(i);
			if (prefMinSumHeight != 0)
				amntShrinkCurrentHeight = (prefHeight - minHeight) * amntShrinkHeight / (prefMinSumHeight);
			int width = Math.min(prefWidth.t(child.getMaximumSize()).);
				width = .t(child.getMaximumSize()).;
			width = Math.max(minWidth, Math.min(clientArea.widthwidth));
			newBounds.width = width;
			int adjust = clientArea.width - width;
			switch () {
				adjust = 0;
				break;
				adjust /= 2;
				break;
				break;
			}
			newBounds.x += adjust;
			// the last children take all the free space
			if (isExpandLastItemMajorAxis() && i == numChildren - 1) {
				newBounds.height += heightNotUsed;
			}
			newBounds.height -= amntShrinkCurrentHeight;
			child.setBounds(.t(newBounds));
			amntShrinkHeight -= amntShrinkCurrentHeight;
			prefMinSumHeight -= (prefHeight - minHeight);
			y += newBounds.height + ;
		}
	}
	public boolean isExpandLastItemMajorAxis() {
	}
	public void setExpandLastItemMajorAxis(boolean expandLastItemMajorAxis) {
		this. = expandLastItemMajorAxis;
	}
New to GrepCode? Check out our FAQ X