Source code: com/arranger/jarl/widget/filter/ComplexRect.java
1 package com.arranger.jarl.widget.filter;
2
3 import com.arranger.jarl.util.StringTools;
4 import com.arranger.jarl.util.ObjectUtil;
5 import com.arranger.jarl.util.JarlInfoUtil;
6 import com.arranger.jarl.base.IJarlObjectInfo;
7
8 import java.lang.reflect.Field;
9
10 /**
11 * ComplexRect created on Apr 5, 2003
12 */
13 public class ComplexRect {
14
15 public static ObjectUtil.IConversionFunction COMPLEX_CONVERSION = new ComplexRectConversionFunction();
16 public static IJarlObjectInfo.IJarlObjectDisplay COMPLEX_DISPLAY = new ComplexRectDisplay();
17
18 protected double m_realMin;
19 protected double m_realMax;
20 protected double m_imagMin;
21 protected double m_imagMax;
22
23 public ComplexRect(double realMin, double realMax, double imagMin, double imagMax) {
24 if (realMin > realMax) {
25 m_realMin = realMax;
26 m_realMax = realMin;
27 } else {
28 m_realMin = realMin;
29 m_realMax = realMax;
30 }
31 if (imagMin > imagMax) {
32 m_imagMin = imagMax;
33 m_imagMax = imagMin;
34 } else {
35 m_imagMin = imagMin;
36 m_imagMax = imagMax;
37 }
38 }
39
40 public double getRealMin() {
41 return m_realMin;
42 }
43
44 public double getRealMax() {
45 return m_realMax;
46 }
47
48 public double getImagMin() {
49 return m_imagMin;
50 }
51
52 public double getImagMax() {
53 return m_imagMax;
54 }
55
56 public static ComplexRect parseComplexRect(String complexRect) {
57 double[] results = StringTools.getDoubleArrayFromDelimitedString(complexRect, ",");
58 return new ComplexRect(results[0], results[1], results[2], results[3]);
59 }
60
61 public static ComplexRect adjust(ComplexRect complexRect, double width, double height) {
62 double complexWidth = complexRect.m_realMax - complexRect.m_realMin;
63 double complexHeight = complexRect.m_imagMax - complexRect.m_imagMin;
64
65 double imageWHRatio = width / height;
66 double complexWHRatio = complexWidth / complexHeight;
67
68 if (imageWHRatio == complexWHRatio) {
69 return complexRect;
70 }
71
72 // Expand vertically
73 if (imageWHRatio < complexWHRatio) {
74 double newHeight = complexWidth / imageWHRatio;
75 double heightDifference = Math.abs(newHeight - complexHeight);
76 return new ComplexRect(complexRect.m_realMin, complexRect.m_realMax,
77 complexRect.m_imagMin - (heightDifference / 2),
78 complexRect.m_imagMax + (heightDifference / 2));
79 } else {
80 // Expand horizontally
81 double newWidth = complexHeight * imageWHRatio;
82 double widthDifference = Math.abs(newWidth - complexWidth);
83 return new ComplexRect(complexRect.m_realMin - (widthDifference / 2),
84 complexRect.m_realMax + (widthDifference / 2),
85 complexRect.m_imagMin,
86 complexRect.m_imagMax);
87 }
88 }
89
90 public static class ComplexRectConversionFunction extends ObjectUtil.BaseConversionFunction {
91
92 public void setField(Field field, String configData, Object object) throws Exception {
93 ComplexRect complexRect = ComplexRect.parseComplexRect(configData);
94 field.set(object, complexRect);
95 }
96 }
97
98 public static class ComplexRectDisplay extends JarlInfoUtil.BaseObjectDisplay {
99 protected String _getDisplayValue(Object object) {
100 ComplexRect complexRect = (ComplexRect)object;
101 return "[" + complexRect.getRealMax() + ", " +
102 complexRect.getRealMin() + ", " +
103 complexRect.getImagMax() + ", " +
104 complexRect.getImagMin() + "]";
105 }
106 }
107 }