/* * SIP Communicator, the OpenSource Java VoIP and Instant Messaging client. * * Distributable under LGPL license. * See terms of license at gnu.org. */ package net.java.sip.communicator.swing; import java.awt.*; /** * Represents a LayoutManager which centers the first * Component within its Container and, if the * preferred size of the Component is larger than the size of the * Container, scales the former within the bounds of the latter * while preserving the aspect ratio. FitLayout is appropriate for * Containers which display a single image or video * Component in its entirety for which preserving the aspect ratio * is important. * * @author Lubomir Marinov */ public class FitLayout implements LayoutManager { /* * Does nothing because this LayoutManager lays out only the first Component * of the parent Container and thus doesn't need String associations. */ public void addLayoutComponent(String name, Component comp) { } /** * Gets the first Component of a specific * Container if there is such a Component. * * @param parent the Container to retrieve the first * Component of * @return the first Component of a specific * Container if there is such a Component; * otherwise, null */ protected Component getComponent(Container parent) { Component[] components = parent.getComponents(); return (components.length > 0) ? components[0] : null; } protected void layoutComponent(Component component, Rectangle bounds, float alignmentX, float alignmentY) { Dimension componentSize = component.getPreferredSize(); boolean scale = false; double ratio = 1; if ((componentSize.width != bounds.width) && (componentSize.width > 0)) { scale = true; ratio = bounds.width / (double) componentSize.width; } if ((componentSize.height != bounds.height) && (componentSize.height > 0)) { scale = true; ratio = Math.min(bounds.height / (double) componentSize.height, ratio); } if (scale) { componentSize.width = (int) (componentSize.width * ratio); componentSize.height = (int) (componentSize.height * ratio); } component.setBounds(bounds.x + Math.round((bounds.width - componentSize.width) * alignmentX), bounds.y + Math.round((bounds.height - componentSize.height) * alignmentY), componentSize.width, componentSize.height); } /* * Scales the first Component if its preferred size is larger than the size * of its parent Container in order to display the Component in its entirety * and then centers it within the display area of the parent. */ public void layoutContainer(Container parent) { Component component = getComponent(parent); if (component != null) layoutComponent(component, new Rectangle(parent.getSize()), Component.CENTER_ALIGNMENT, Component.CENTER_ALIGNMENT); } /* * Since this LayoutManager lays out only the first Component of the * specified parent Container, the minimum size of the Container is the * minimum size of the mentioned Component. */ public Dimension minimumLayoutSize(Container parent) { Component component = getComponent(parent); return (component != null) ? component.getMinimumSize() : new Dimension(0, 0); } /* * Since this LayoutManager lays out only the first Component of the * specified parent Container, the preferred size of the Container is the * preferred size of the mentioned Component. */ public Dimension preferredLayoutSize(Container parent) { Component component = getComponent(parent); return (component != null) ? component.getPreferredSize() : new Dimension(0, 0); } /* * Does nothing because this LayoutManager lays out only the first Component * of the parent Container and thus doesn't need String associations. */ public void removeLayoutComponent(Component comp) { } }