OSDN Git Service

srcディレクトリとdocディレクトリを作成
[xdf/git-repos.git] / src / xdf-swing / src / main / java / jp / ac / aiit / xdf / component / swing / event / UIEventListener.java
1 package jp.ac.aiit.xdf.component.swing.event;
2
3 import java.awt.event.ActionEvent;
4 import java.awt.event.ActionListener;
5 import java.awt.event.FocusEvent;
6 import java.awt.event.FocusListener;
7 import java.awt.event.KeyEvent;
8 import java.awt.event.KeyListener;
9 import java.awt.event.MouseEvent;
10 import java.awt.event.MouseListener;
11
12 import javax.swing.AbstractButton;
13 import javax.swing.event.ChangeEvent;
14 import javax.swing.event.ChangeListener;
15
16 import jp.ac.aiit.xdf.core.action.Action;
17 import jp.ac.aiit.xdf.core.model.ObjectModel;
18
19 /**
20  * @author Pin.Yuan
21  * 実コンポーネントにユーザ定義アクションをListenerにてバンディングする
22  *
23  */
24 public class UIEventListener implements ActionListener, MouseListener, KeyListener, ChangeListener, FocusListener {
25         private ObjectModel model;
26         //private Map<String, Action> actions;
27         private SwingEventType event;
28         private Action action;
29         private Object param;
30         
31         /**
32          * @param model
33          * @param event
34          * @param action
35          */
36         public UIEventListener(ObjectModel model, SwingEventType event, Action action) {
37                 this.model = model;
38                 //this.actions = model.getActions();
39                 this.event = event;
40                 this.action = action;
41         }
42         
43         /**
44          * 発生したイベントに対応づけられたアクションを実行する
45          * @param e 発生したイベントの種類
46          */
47         public void invokeAction(SwingEventType e) {
48                 /* 
49                  * TODO ここで発生したイベントに関する情報をアクションにセットする必要がある。(まずは仕様を決める)
50                  *      例としてはKeyEventの場合にどのキーが押されたかや修飾キーが押されていたかどうかなど
51                  */
52                 //Action action = actions.get(event);
53                 SwingEvent event = new SwingEvent(e, this.model, param);
54                 
55                 if( this.action != null ) {
56                         this.action.execute(event);
57                 }
58         }
59         
60         @Override
61         public void actionPerformed(ActionEvent e) {
62                 if (model.tagname().equals("checkbox")||model.tagname().equals("radio")){
63                         AbstractButton checkbox = (AbstractButton)e.getSource();
64                         // チェックボックスチェックされた際に、"check"イベントを発生する
65                         if (checkbox.isSelected() && event.equals(SwingEventType.CHECK)){
66                                 this.action.execute(new SwingEvent(SwingEventType.CHECK, this.model, null));
67                         }
68                         // チェックボックスチェック外された際に、"uncheck"イベントを発生する
69                         if ((!checkbox.isSelected()) && event.equals(SwingEventType.UNCHECK)){
70                                 this.action.execute(new SwingEvent(SwingEventType.UNCHECK, this.model, null));
71                         }
72                 }else {
73                         this.action.execute(new SwingEvent(SwingEventType.ACTIONED, this.model, null));
74                 }
75         }
76
77         @Override
78         public void mouseClicked(MouseEvent e) {
79                 if (e.getClickCount() == 1){ 
80                         if (event.equals(SwingEventType.CLICK))
81                                 invokeAction(SwingEventType.CLICK);
82                 } else if (e.getClickCount() == 2) { 
83                         if (event.equals(SwingEventType.DOUBLECLICK))
84                                 invokeAction(SwingEventType.DOUBLECLICK); 
85                 }
86         }
87
88         @Override
89         public void mouseEntered(MouseEvent e) { }
90
91         @Override
92         public void mouseExited(MouseEvent e) { }
93
94         @Override
95         public void mousePressed(MouseEvent e) { }
96
97         @Override
98         public void mouseReleased(MouseEvent e) { }
99
100         @Override
101         public void keyTyped(KeyEvent e) {
102                 if (model.tagname().equals("textfield")) {
103                         switch(event){
104                                 case PRESSENTER:
105                                         if (e.getKeyCode() == KeyEvent.VK_ENTER ){
106                                                 // System.out.println("Key press: enter");
107                                                 invokeAction(event);
108                                                 break;
109                                         } 
110                                         break;
111                                 case EDIT:
112                                         invokeAction(event);
113                                         break;
114                         }
115                 } else if (model.tagname().equals("textarea")){
116                         switch(event){
117                                 case EDIT:
118                                         invokeAction(event);
119                                         break;
120                         }
121                 }
122                 
123         }
124
125         @Override
126         public void keyPressed(KeyEvent e) { }
127
128         @Override
129         public void keyReleased(KeyEvent e) { }
130
131         @Override
132         public void stateChanged(ChangeEvent e) {
133                 invokeAction(SwingEventType.TAB_CHANGE);
134         }
135
136         @Override
137         public void focusGained(FocusEvent e) {
138                 if (event.equals(SwingEventType.FOCUS)) {
139                         invokeAction(SwingEventType.FOCUS);
140                 }
141         }
142
143         @Override
144         public void focusLost(FocusEvent e) {
145                 if (event.equals(SwingEventType.FOCUSLOSE)) {
146                         invokeAction(SwingEventType.FOCUSLOSE);
147                 }
148         }
149
150 }