KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > layoutmgr > MinOptMaxUtil


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 /* $Id: MinOptMaxUtil.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.layoutmgr;
21
22 import org.apache.fop.datatypes.PercentBaseContext;
23 import org.apache.fop.fo.Constants;
24 import org.apache.fop.fo.properties.LengthRangeProperty;
25 import org.apache.fop.traits.MinOptMax;
26
27 /**
28  * Utilities for MinOptMax and LengthRangeProperty.
29  */

30 public class MinOptMaxUtil {
31
32     /**
33      * Restricts a MinOptMax using the values from a LengthRangeProperty.
34      * @param mom MinOptMax to restrict
35      * @param lr restricting source
36      * @param context Percentage evaluation context
37      */

38     public static void restrict(MinOptMax mom, LengthRangeProperty lr,
39                                 PercentBaseContext context) {
40         if (lr.getEnum() != Constants.EN_AUTO) {
41             if (lr.getMinimum(context).getEnum() != Constants.EN_AUTO) {
42                 int min = lr.getMinimum(context).getLength().getValue(context);
43                 if (min > mom.min) {
44                     mom.min = min;
45                     fixAfterMinChanged(mom);
46                 }
47             }
48             if (lr.getMaximum(context).getEnum() != Constants.EN_AUTO) {
49                 int max = lr.getMaximum(context).getLength().getValue(context);
50                 if (max < mom.max) {
51                     mom.max = max;
52                     if (mom.max < mom.opt) {
53                         mom.opt = mom.max;
54                         mom.min = mom.opt;
55                     }
56                 }
57             }
58             if (lr.getOptimum(context).getEnum() != Constants.EN_AUTO) {
59                 int opt = lr.getOptimum(context).getLength().getValue(context);
60                 if (opt > mom.min) {
61                     mom.opt = opt;
62                     if (mom.opt > mom.max) {
63                         mom.max = mom.opt;
64                     }
65                 }
66             }
67         }
68     }
69
70     /**
71      * Extend the minimum length to the given length.
72      * @param mom the min/opt/max trait
73      * @param len the new minimum length
74      * @param optToLen if set adjusts the optimum length to be the smaller of the
75      * minimum length and the given length
76      */

77     public static void extendMinimum(MinOptMax mom, int len, boolean optToLen) {
78         if (mom.min < len) {
79             mom.min = len;
80             mom.opt = Math.max(mom.min, mom.opt);
81             if (optToLen) {
82                 mom.opt = Math.min(mom.min, len);
83             }
84             mom.max = Math.max(mom.opt, mom.max);
85         }
86     }
87     
88     /**
89      * After a calculation on a MinOptMax, this can be called to set opt to
90      * a new effective value.
91      * @param mom MinOptMax to adjust
92      */

93     public static void fixAfterMinChanged(MinOptMax mom) {
94         if (mom.min > mom.opt) {
95             mom.opt = mom.min;
96             if (mom.opt > mom.max) {
97                 mom.max = mom.opt;
98             }
99         }
100     }
101     
102     /**
103      * Converts a LengthRangeProperty to a MinOptMax.
104      * @param prop LengthRangeProperty
105      * @param context Percentage evaluation context
106      * @return the requested MinOptMax instance
107      */

108     public static MinOptMax toMinOptMax(LengthRangeProperty prop, PercentBaseContext context) {
109         MinOptMax mom = new MinOptMax(
110                 (prop.getMinimum(context).isAuto()
111                         ? 0 : prop.getMinimum(context).getLength().getValue(context)),
112                 (prop.getOptimum(context).isAuto()
113                         ? 0 : prop.getOptimum(context).getLength().getValue(context)),
114                 (prop.getMinimum(context).isAuto()
115                         ? Integer.MAX_VALUE
116                         : prop.getMaximum(context).getLength().getValue(context)));
117         return mom;
118     }
119     
120 }
121
Popular Tags