View Javadoc

1   /*
2    * Copyright 2004-2008 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16   package org.seasar.eclipse.common.util;
17  
18  /**
19   * @author y-komori
20   * 
21   */
22  public class GeometryUtil {
23      /**
24       * <code>value</code>の表す数値または割合を元に実際の値を計算します。<br>
25       * <ul>
26       * <li><code>value</code>が数値のみからなる場合、そのままint値に変換した値を返します。<br>
27       * <li><code>value</code>が%で終わる数値を表す場合、<code>parentSize</code>に対する<code>value</code>の割合を返します。
28       * </ul>
29       * 
30       * @param value
31       *            計算対象
32       * @param parentSize
33       *            親サイズ
34       * @return 計算結果
35       * @throws NumberFormatException
36       */
37      public static int calcSize(final String value, final int parentSize) {
38          int result;
39          if (value.endsWith("%")) {
40              int percent = Integer.parseInt(value.substring(0,
41                      value.length() - 1));
42              result = parentSize * percent / 100;
43          } else {
44              result = Integer.parseInt(value);
45          }
46          return result;
47      }
48  
49      /**
50       * <code>value</code>を元に実際の位置を計算します。<br>
51       * <ul>
52       * <li><code>value</code>が数値のみからなる場合、そのままint値に変換した値を返します。<br>
53       * <li><code>value</code>が%で終わる数値を表す場合、<code>parentSize</code>に対する<code>value</code>の割合を返します。
54       * <li><code>value</code>が top または left の場合、0を返します。
55       * <li><code>value</code>が center または middle の場合、(<code>parentSize</code> -
56       * <code>targetSize</code>) / 2 を返します。
57       * <li><code>value</code>が bottom または right の場合、(<code>parentSize</code> -
58       * <code>targetSize</code> を返します。
59       * </ul>
60       * 
61       * @param value
62       *            計算対象
63       * @param parentSize
64       *            親Boxサイズ
65       * @param targetSize
66       *            自Boxサイズ
67       * @return 計算結果
68       * @throws NumberFormatException
69       */
70      public static int calcPosition(final String value, final int parentSize,
71              final int targetSize) {
72          int position = 0;
73          if ("top".equalsIgnoreCase(value) || "left".equalsIgnoreCase(value)) {
74              position = 0;
75          } else if ("center".equalsIgnoreCase(value)
76                  || "middle".equalsIgnoreCase(value)) {
77              position = (parentSize - targetSize) / 2;
78          } else if ("bottom".equalsIgnoreCase(value)
79                  || "right".equalsIgnoreCase(value)) {
80              position = parentSize - targetSize;
81          } else {
82              position = calcSize(value, parentSize);
83          }
84          position = (position >= 0) ? position : 0;
85          return position;
86      }
87  
88  }