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