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.gauger;
21  
22  /**
23   * Factory class that creates transform implementations based on dimensions of
24   * the gauger.
25   *
26   * @author <a href="mailto:ales.pucelj@cosylab.com">Ales Pucelj</a>
27   * @version $id$
28   */
29  public class ScaleTransformFactory
30  {
31  	/** Scale shape */
32  	public static final short AUTOMATIC_SHAPE = 0;
33  
34  	/** Horizontal linear scale */
35  	public static final short HORIZONTAL_LINEAR_SHAPE = 1;
36  
37  	/** Horizontal polar scale */
38  	public static final short HORIZONTAL_CIRCULAR_SHAPE = 2;
39  
40  	/** Circular polar scale */
41  	public static final short FULL_CIRCULAR_SHAPE = 3;
42  
43  	/** Vertical polar scale */
44  	public static final short VERTICAL_CIRCULAR_SHAPE = 4;
45  
46  	/** Vertical linear scale */
47  	public static final short VERTICAL_LINEAR_SHAPE = 5;
48  
49  	/** Constants used when changing scale shapes */
50  	private static final double FROM_LINEAR_TO_HALF_CIRCULAR = 3.5;
51  
52  	/** */
53  	private static final double FROM_HALF_CIRCULAR_TO_FULL_CIRCULAR = 1.7;
54  
55  	/** */
56  	private static final double FROM_FULL_CIRCULAR_TO_HALF_CIRCULAR = 0.65;
57  
58  	/** */
59  	private static final int HEIGHT_SMALLER_THAN_THIS_ALWAYS_LINEAR = 95;
60  
61  	/** */
62  	private static final int WIDTH_SMALLER_THAN_THIS_ALWAYS_LINEAR = 120;
63  
64  	/**
65  	 * Determines the optimal renderer for this scale, given the width and
66  	 * height of the area.
67  	 *
68  	 * @param w int
69  	 * @param h int
70  	 *
71  	 * @return int
72  	 */
73  	protected static int getOptimalRenderer(int w, int h)
74  	{
75  		double ratio = (1.0 * w) / h;
76  		short shape;
77  
78  		if(ratio > FROM_LINEAR_TO_HALF_CIRCULAR) {
79  			shape = HORIZONTAL_LINEAR_SHAPE;
80  		} else if(ratio > FROM_HALF_CIRCULAR_TO_FULL_CIRCULAR) {
81  			shape = HORIZONTAL_CIRCULAR_SHAPE;
82  		} else if(ratio > FROM_FULL_CIRCULAR_TO_HALF_CIRCULAR) {
83  			shape = FULL_CIRCULAR_SHAPE;
84  		} else if(ratio > 1 / FROM_LINEAR_TO_HALF_CIRCULAR) {
85  			shape = VERTICAL_CIRCULAR_SHAPE;
86  		} else {
87  			shape = VERTICAL_LINEAR_SHAPE;
88  		}
89  
90  		if((w < 1.5 * WIDTH_SMALLER_THAN_THIS_ALWAYS_LINEAR)
91  		    && (h < 1.5 * HEIGHT_SMALLER_THAN_THIS_ALWAYS_LINEAR)) {
92  			shape = HORIZONTAL_LINEAR_SHAPE;
93  		}
94  
95  		if(w < WIDTH_SMALLER_THAN_THIS_ALWAYS_LINEAR) {
96  			shape = VERTICAL_LINEAR_SHAPE;
97  		}
98  
99  		if(h < HEIGHT_SMALLER_THAN_THIS_ALWAYS_LINEAR) {
100 			shape = HORIZONTAL_LINEAR_SHAPE;
101 		}
102 
103 		return shape;
104 	}
105 
106 	/**
107 	 * Creates a new instance of transform that is best suited for given size.
108 	 *
109 	 * @param w int
110 	 * @param h int
111 	 *
112 	 * @return ScaleTransform
113 	 */
114 	public static ScaleTransform createTransform(int w, int h)
115 	{
116 		int newValue = getOptimalRenderer(w, h);
117 
118 		switch(newValue) {
119 		case HORIZONTAL_LINEAR_SHAPE:
120 			return new LinearHorizontalTransform();
121 
122 		case HORIZONTAL_CIRCULAR_SHAPE:
123 			return new PolarHorizontalTransform();
124 
125 		case FULL_CIRCULAR_SHAPE:
126 			return new PolarFullTransform();
127 
128 		case VERTICAL_CIRCULAR_SHAPE:
129 			return new PolarVerticalTransform();
130 
131 		case VERTICAL_LINEAR_SHAPE:
132 			return new LinearVerticalTransform();
133 
134 		default:
135 			return new LinearVerticalTransform();
136 		}
137 	}
138 }
139 
140 /* __oOo__ */