View Javadoc

1   /*
2    * Copyright (c) 2003-2008 by Cosylab d. d.
3    *
4    * This file is part of CosyBeans-Common.
5    *
6    * CosyBeans-Common is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * CosyBeans-Common is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with CosyBeans-Common.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  
20  package com.cosylab.gui.components.wheelswitch;
21  
22  import java.awt.Component;
23  import java.awt.Container;
24  import java.awt.Dimension;
25  import java.awt.LayoutManager;
26  
27  import com.cosylab.gui.components.Wheelswitch;
28  
29  /**
30   * Implementation of <code>java.awt.LayoutManager</code> to be used for
31   * the layout of the <code>Wheelswitch</code> component.
32   * 
33   * @author <a href="mailto:jernej.kamenik@cosylab.com">Jernej Kamenik</a>
34   * @version $id$
35   */
36  public class WheelswitchLayout implements LayoutManager {
37    
38  	/**
39  	 * Constructor for WheelswitchLayout
40  	 */
41  	public WheelswitchLayout() {
42  		super();
43  	}
44  	
45  	/**
46  	 * Empty implementation.
47  	 * 
48  	 * @see LayoutManager#addLayoutComponent(String, Component)
49  	 */
50  	public void addLayoutComponent(String name, Component comp) {
51  	}
52  
53  	/**
54  	 * Empty implementation.
55  	 * 
56  	 * @see LayoutManager#removeLayoutComponent(Component)
57  	 */
58  	public void removeLayoutComponent(Component comp) {
59  	}
60  
61  	/**
62  	 * @see LayoutManager#preferredLayoutSize(Container)
63  	 */
64  	public Dimension preferredLayoutSize(Container parent) {
65  		return parent.getPreferredSize();
66  	}
67  
68  	/**
69  	 * @see LayoutManager#minimumLayoutSize(Container)
70  	 */
71  	public Dimension minimumLayoutSize(Container parent) {
72  		return parent.getMinimumSize();
73  	}
74  
75  	/**
76  	 * @see LayoutManager#layoutContainer(Container)
77  	 */
78  	public void layoutContainer(Container parent) {
79  		Wheelswitch wswitch = (Wheelswitch)parent;
80  
81  		int numberOfComponents = wswitch.getComponentCount();
82  		if (numberOfComponents<2) return;
83  		
84  		int height = wswitch.getHeight();
85  		int width = wswitch.getWidth();
86  
87          if (width==0 || height==0) return;
88  
89  		int preferredWidth = wswitch.getPreferredSize().width;
90  		int preferredHeight = wswitch.getPreferredSize().height;
91  
92  		double widthRatio = 1.*width/preferredWidth;
93  		double heightRatio = 1.*height/preferredHeight;
94  
95  		double ratio = Math.min(widthRatio,heightRatio);
96  
97  		int x = (int)Math.ceil((width-preferredWidth*ratio)/2.);
98  		int y = 0;
99  		int compWidth = 0;
100 		int compHeight = 0;
101 		Component comp = null;
102 		for (int i=numberOfComponents-1; i>=0; i--) {
103 			comp = wswitch.getComponent(i);
104 			compHeight = (int)(comp.getPreferredSize().height*ratio);
105 			compWidth = (int)(comp.getPreferredSize().width*ratio);
106 			y = (int)((height-compHeight)/2.);
107 			x += compWidth;
108 			comp.setBounds(width-x,y,compWidth,compHeight);
109 		}
110 	}
111 }