1 21 24 package org.lobobrowser.html.style; 25 26 public final class HtmlLength { 27 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 spec) throws IndexOutOfBoundsException , NumberFormatException { 38 spec = spec.trim(); 39 int length = spec.length(); 40 char lastChar = spec.charAt(length - 1); 41 String 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 70 public final int getLengthType() { 71 return lengthType; 72 } 73 74 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 |