KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lobobrowser > html > style > HtmlLength


1 /*
2     GNU LESSER GENERAL PUBLIC LICENSE
3     Copyright (C) 2006 The Lobo Project
4
5     This library is free software; you can redistribute it and/or
6     modify it under the terms of the GNU Lesser General Public
7     License as published by the Free Software Foundation; either
8     version 2.1 of the License, or (at your option) any later version.
9
10     This library is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13     Lesser General Public License for more details.
14
15     You should have received a copy of the GNU Lesser General Public
16     License along with this library; if not, write to the Free Software
17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19     Contact info: xamjadmin@users.sourceforge.net
20 */

21 /*
22  * Created on Nov 19, 2005
23  */

24 package org.lobobrowser.html.style;
25
26 public final class HtmlLength {
27     // Note: Preferred type has higher value
28
public static final int PIXELS = 1;
29     public static final int LENGTH = 2;
30     public static final int MULTI_LENGTH = 0;
31     
32     public static final HtmlLength[] EMPTY_ARRAY = new HtmlLength[0];
33     
34     private final int lengthType;
35     private volatile int value;
36     
37     public HtmlLength(String JavaDoc spec) throws IndexOutOfBoundsException JavaDoc, NumberFormatException JavaDoc {
38         spec = spec.trim();
39         int length = spec.length();
40         char lastChar = spec.charAt(length - 1);
41         String JavaDoc parseable;
42         if(lastChar == '%') {
43             this.lengthType = LENGTH;
44             parseable = spec.substring(0, length - 1).trim();
45         }
46         else if(lastChar == '*') {
47             this.lengthType = MULTI_LENGTH;
48             if(length <= 1) {
49                 parseable = "1";
50             }
51             else {
52                 parseable = spec.substring(0, length - 1).trim();
53             }
54         }
55         else {
56             this.lengthType = PIXELS;
57             parseable = spec;
58         }
59         this.value = Integer.parseInt(parseable);
60     }
61     
62     public HtmlLength(int pixels) {
63         this.lengthType = PIXELS;
64         this.value = pixels;
65     }
66     
67     /**
68      * @return Returns the lengthType.
69      */

70     public final int getLengthType() {
71         return lengthType;
72     }
73
74     /**
75      * @return Returns the spec.
76      */

77     public final int getRawValue() {
78         return this.value;
79     }
80     
81     public final int getLength(int availLength) {
82         int lt = this.lengthType;
83         if(lt == LENGTH) {
84             return (availLength * this.value) / 100;
85         }
86         else {
87             return this.value;
88         }
89     }
90     
91     public final void divideBy(int denominator) {
92         int val = this.value;
93         val = val / denominator;
94         this.value = val;
95     }
96     
97     public final boolean isPreferredOver(HtmlLength otherLength) {
98         if(otherLength == null) {
99             return true;
100         }
101         if(this.lengthType > otherLength.lengthType) {
102             return true;
103         }
104         return this.value > otherLength.value;
105     }
106 }
107
Popular Tags