ludc
2025-01-16 986aa62ed00bee39363bab41b4eeb8259d446efd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package com.vci.client.portal.UI.v3.comptdesign.compt.al;
 
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Observable;
import java.util.Observer;
 
import javax.swing.SwingUtilities;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
 
import com.vci.client.portal.UI.v3.comptdesign.UIComptDesignDialog;
import com.vci.client.portal.UI.v3.comptdesign.compt.BaseComptPanel;
import com.vci.client.portal.UI.v3.comptdesign.compt.popupcompt.BasePopupDialog;
import com.vci.client.ui.swing.components.VCIJTextField;
 
public abstract class BaseActionListener<T extends BasePopupDialog> 
        extends Observable implements ActionListener, Observer {
 
    private BaseComptPanel ownedComptPanel = null; 
    private UIComptDesignDialog ownedUIComptDesignDialog = null;
    private VCIJTextField txt = null;
    private List<VCIJTextField> clearTxtList = null;
    
    private BasePopupDialog popupDialog = null;
    
    protected KeyListener escapeKeyListener = new KeyAdapter() {
        public void keyReleased(KeyEvent e) {
            if(e.getKeyChar() == KeyEvent.VK_ESCAPE){
                if(getOwnedUIComptDesignDialog() != null){
                    getOwnedUIComptDesignDialog().setVisible(false);
                }
            }
        }
    };
    
    private class HidePopupDialogHandler implements MouseListener, FocusListener, ActionListener{
 
        @Override
        public void focusGained(FocusEvent e) {
            // TODO Auto-generated method stub
            
        }
 
        @Override
        public void focusLost(FocusEvent e) {
            // TODO Auto-generated method stub
            hidePopupDialog();
        }
 
        @Override
        public void mouseClicked(MouseEvent e) {
            // TODO Auto-generated method stub
            hidePopupDialog();
        }
 
        @Override
        public void mousePressed(MouseEvent e) {
            // TODO Auto-generated method stub
            
        }
 
        @Override
        public void mouseReleased(MouseEvent e) {
            // TODO Auto-generated method stub
            
        }
 
        @Override
        public void mouseEntered(MouseEvent e) {
            // TODO Auto-generated method stub
            
        }
 
        @Override
        public void mouseExited(MouseEvent e) {
            // TODO Auto-generated method stub
            
        }
 
        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            hidePopupDialog();
        }
    }
    
    private HidePopupDialogHandler hideHander = null;
    private HidePopupDialogHandler getHideHandler(){
        if(hideHander == null){
            hideHander = new HidePopupDialogHandler();
        }
        return hideHander;
    }
    public BaseActionListener(
            BaseComptPanel ownedComptPanel,
            UIComptDesignDialog ownedUIComptDesignDialog, 
            VCIJTextField txt,
            List<VCIJTextField> clearTxtList){
        this.ownedComptPanel = ownedComptPanel;
        this.ownedUIComptDesignDialog = ownedUIComptDesignDialog;
        this.txt = txt;
        this.clearTxtList = clearTxtList;
        
        if(ownedComptPanel != null){
            ownedComptPanel.getTxtRefBtnALMap().put(txt.hashCode(), this);
            Container pc = txt.getParent();
            while(pc != null){
                pc.addMouseListener(getHideHandler());
                pc.addFocusListener(getHideHandler());
                pc = pc.getParent();
            }
        }
        if(txt != null){
            txt.getDocument().addDocumentListener(new DocumentListener() {
                @Override
                public void removeUpdate(DocumentEvent e) {
                    txt_DocumentListener_response(null);
                }
                @Override
                public void insertUpdate(DocumentEvent e) {
                    txt_DocumentListener_response(null);
                }
                @Override
                public void changedUpdate(DocumentEvent e) {
                    txt_DocumentListener_response(null);
                }
            });
            txt.addKeyListener(escapeKeyListener);
        }
    }
    protected void txt_DocumentListener_response(DocumentEvent e){
        if(getPopupDialog() != null){
            if(!getPopupDialog().isVisible()){
                return;
            }
            getPopupDialog().setIgnoreTxtInputValue(false);
            getPopupDialog().loadData();
            getTxt().requestFocus();
        }
    }
    
    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        // Observable 作为被观察者,表明已更新
        setChanged();
        // 本ActionListener执行时,其它观察者做出响应(隐藏那些按钮弹出的PopupDialog)
        notifyObservers(this);
        
        // 按钮响应事件
        btn_actionPerformed(e);
    }
 
 
    private Map<Integer, BasePopupDialog> dialogMap = new HashMap<Integer, BasePopupDialog>();
    
    protected void btn_actionPerformed(ActionEvent e){
        BasePopupDialog popupDialog = null;
        if(dialogMap.containsKey(getTxt().hashCode())){
            popupDialog = getBtnActionShowPopupDialog();
        } else {
            popupDialog = getBtnActionShowPopupDialog();
            dialogMap.put(getTxt().hashCode(), popupDialog);
        }
        if(popupDialog == null) return;
        
        if(!popupDialog.isBuildCompleted()){
            popupDialog.buildDialog();
        }
        
        @SuppressWarnings("unchecked")
        Runnable dialogCallback = getBtnActionShowPopupDialogCallback((T)popupDialog);
        if(dialogCallback != null){
            popupDialog.setDialogCallback(dialogCallback);
        }
        
        if(popupDialog.isVisible()){
            return;
        }
        
        showPopupDialog(popupDialog);
    }
 
    protected void showPopupDialog(BasePopupDialog popupDialog){
        getOwnedComptPanel().getTxtRefBtnALMap().get(getTxt().hashCode()).setPopupDialog(popupDialog);
        setPopupDialog(popupDialog);
        popupDialog.setIgnoreTxtInputValue(true);
        Point p  =  new Point(0, getTxt().getHeight() + 1);
        SwingUtilities.convertPointToScreen(p, getTxt());
        popupDialog.setLocation(p);
        Dimension size = getTxt().getSize();
        size.height = 250;
        popupDialog.setSize(size);
        popupDialog.loadData();
        popupDialog.requestFocus();
        popupDialog.setVisible(true);
    }
    
    protected abstract BasePopupDialog getBtnActionShowPopupDialog();
    
    protected abstract Runnable getBtnActionShowPopupDialogCallback(T popupDialog);
    
    protected void clearTxtText(){
        clearTxtText(clearTxtList);
    }
    
    protected void clearTxtText(List<VCIJTextField> txts){
        if(txts != null){
            for (VCIJTextField txt : txts) {
                txt.setText("");
            }
        }
    }
    
    @Override
    public void update(Observable o, Object arg) {
        hidePopupDialog();
    }
    public BaseComptPanel getOwnedComptPanel() {
        return ownedComptPanel;
    }
    public void setOwnedComptPanel(BaseComptPanel ownedComptPanel) {
        this.ownedComptPanel = ownedComptPanel;
    }
    public UIComptDesignDialog getOwnedUIComptDesignDialog() {
        return ownedUIComptDesignDialog;
    }
 
    public void setOwnedUIComptDesignDialog(UIComptDesignDialog ownedUIComptDesignDialog) {
        this.ownedUIComptDesignDialog = ownedUIComptDesignDialog;
    }
    public VCIJTextField getTxt() {
        return txt;
    }
    public void setTxt(VCIJTextField txt) {
        this.txt = txt;
    }
    public BasePopupDialog getPopupDialog() {
        return popupDialog;
    }
    public void setPopupDialog(BasePopupDialog popupDialog) {
        this.popupDialog = popupDialog;
    }
 
    /**
     * 隐藏PopupDialog
     */
    public void hidePopupDialog(){
        if(getPopupDialog() != null){
            getPopupDialog().setVisible(false);
        }
    }
    
}