OSDN Git Service

initial commit
[xdf/git-repos.git] / xdf-swing / src / main / java / jp / ac / aiit / xdf / component / swing / attribute / WidthHeightGetProcessor.java
1 package jp.ac.aiit.xdf.component.swing.attribute;
2
3 import java.awt.Component;
4 import java.awt.Dimension;
5
6 /**
7  * コンポーネントの高さや幅を取得するプロセッサの統合クラス
8  * インスタンスの生成方法によって、コンポーネントの最小幅、期待幅、最大幅、最小高、期待高、最大高のどれを取得するかを選択することが出来る。
9  * 
10  * @author Shunichi Takagi
11  *
12  */
13 public class WidthHeightGetProcessor implements AttributeGetProcessor {
14         private String widthOrHeight;
15         private String where;
16         
17         /**
18          * コンポーネントの最小幅、期待幅、最大幅、最小高、期待高、最大高のどれを取得するか設定されたインスタンスの生成
19          * 
20          * @param widthOrHeight 高さを取得するか幅を取得するか(幅を取得する場合はwidth、高さを取得する場合はheightを指定する)
21          * @param where 最小値、期待値、最大値のどれを取得するか(最小値を取得する場合はmin、期待値を取得する場合はpref、最大値を取得する場合はmaxを指定する)
22          */
23         public WidthHeightGetProcessor(String widthOrHeight, String where) {
24                 this.widthOrHeight = widthOrHeight;
25                 this.where = where;
26         }
27         
28         @Override
29         public Object invokeGet(Object target, String name) {
30                 if( target instanceof Component ) {
31                         Dimension dim = getDimension((Component) target, where);
32                         if( dim == null ) {
33                                 return null;
34                         } else {
35                                 if( widthOrHeight.equals("width") ) {
36                                         return dim.getWidth();
37                                 } else if( widthOrHeight.equals("height") ) {
38                                         return dim.getHeight();
39                                 }
40                         }
41                 }
42                 
43                 return null;
44         }
45
46         private Dimension getDimension(Component c, String where) {
47                 if( where.equals("min") ) {
48                         return c.getMinimumSize();
49                 } else if( where.equals("pref") ) {
50                         return c.getPreferredSize();
51                 } else if( where.equals("max") ) {
52                         return c.getMaximumSize();
53                 } else {
54                         return null;
55                 }
56         }
57 }