001/*-------------------------------------------------------------------------+ 002| | 003| Copyright 2005-2011 The ConQAT Project | 004| | 005| Licensed under the Apache License, Version 2.0 (the "License"); | 006| you may not use this file except in compliance with the License. | 007| You may obtain a copy of the License at | 008| | 009| http://www.apache.org/licenses/LICENSE-2.0 | 010| | 011| Unless required by applicable law or agreed to in writing, software | 012| distributed under the License is distributed on an "AS IS" BASIS, | 013| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 014| See the License for the specific language governing permissions and | 015| limitations under the License. | 016+-------------------------------------------------------------------------*/ 017package org.conqat.lib.commons.treemap; 018 019import java.awt.geom.Rectangle2D; 020 021/** 022 * A very simple tree map layouter just dividing the given rectangle along the 023 * longer side. 024 * 025 * @author Benjamin Hummel 026 */ 027public class SimpleTreeMapAlgorithm implements ITreeMapLayoutAlgorithm { 028 029 /** {@inheritDoc} */ 030 @Override 031 public <T> void layout(ITreeMapNode<T> tree, Rectangle2D target) { 032 tree.setLayoutRectangle(target); 033 layoutChildren(tree); 034 } 035 036 /** Layouts the children of the given node (if it has any). */ 037 private <T> void layoutChildren(ITreeMapNode<T> node) { 038 if (node.getChildren().isEmpty()) { 039 return; 040 } 041 042 Rectangle2D rect = node.getLayoutRectangle(); 043 double sum = node.getArea(); 044 double x = rect.getMinX(); 045 double y = rect.getMinY(); 046 if (rect.getWidth() > rect.getHeight()) { 047 for (ITreeMapNode<T> child : node.getChildren()) { 048 double width = rect.getWidth() * child.getArea() / sum; 049 child.setLayoutRectangle(new Rectangle2D.Double(x, y, width, rect.getHeight())); 050 layoutChildren(child); 051 x += width; 052 } 053 } else { 054 for (ITreeMapNode<T> child : node.getChildren()) { 055 double height = rect.getHeight() * child.getArea() / sum; 056 child.setLayoutRectangle(new Rectangle2D.Double(x, y, rect.getWidth(), height)); 057 layoutChildren(child); 058 y += height; 059 } 060 } 061 } 062}