01: /*
02: JSmooth: a VM wrapper toolkit for Windows
03: Copyright (C) 2003 Rodrigo Reyes <reyes@charabia.net>
04:
05: This program is free software; you can redistribute it and/or modify
06: it under the terms of the GNU General Public License as published by
07: the Free Software Foundation; either version 2 of the License, or
08: (at your option) any later version.
09:
10: This program 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
13: GNU General Public License for more details.
14:
15: You should have received a copy of the GNU General Public License
16: along with this program; if not, write to the Free Software
17: Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18:
19: */
20:
21: package net.charabia.jsmoothgen.application.gui.util;
22:
23: import javax.swing.*;
24: import java.awt.event.*;
25: import java.awt.*;
26: import java.util.*;
27:
28: public class LayoutLengthDescriptor {
29: public static final int PIXEL = 1;
30: public static final int PERCENT = 2;
31:
32: private int m_length;
33: private int m_unit;
34:
35: public LayoutLengthDescriptor(String s) {
36: try {
37: s = s.trim();
38:
39: StringBuffer len = new StringBuffer();
40: StringBuffer unit = new StringBuffer();
41:
42: int offset = 0;
43:
44: for (; offset < s.length(); offset++) {
45: char c = s.charAt(offset);
46: if (Character.isDigit(c) == false)
47: break;
48: len.append(c);
49: }
50:
51: for (; offset < s.length(); offset++) {
52: char c = s.charAt(offset);
53: if (Character.isWhitespace(c) == false)
54: break;
55: }
56:
57: for (; offset < s.length(); offset++) {
58: char c = s.charAt(offset);
59: unit.append(c);
60: }
61: System.out.println("len: " + len);
62: System.out.println("unit: " + unit);
63:
64: m_length = Integer.parseInt(len.toString());
65:
66: String sunit = unit.toString();
67: if (sunit.equals("px"))
68: m_unit = PIXEL;
69: else if (sunit.equals("%"))
70: m_unit = PERCENT;
71:
72: } catch (Exception exc) {
73: throw new RuntimeException("Error parsing " + s);
74: }
75: }
76:
77: public int getLength(int totalLength) {
78: if (m_unit == PIXEL)
79: return m_length;
80: if (totalLength == 0)
81: return 0;
82:
83: return (totalLength * 100) / totalLength;
84: }
85:
86: }
|