wangting
2025-01-13 20a1d1809d95d3395403bf2517e49c7def65fedd
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
package com.vci.client.uif.engine.client.controls;
 
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.util.List;
import java.util.Map;
 
import com.vci.client.portal.custom.ICustomDefine;
import com.vci.client.portal.utility.PRMItem;
import com.vci.client.ui.swing.VCISwingUtil;
import com.vci.client.ui.swing.components.VCIJTextField;
import com.vci.client.uif.engine.client.custom.ICustomAttributeInteceptor;
import com.vci.client.uif.engine.client.renderer.UserNameInTableCellRenderer;
import com.vci.corba.common.VCIError;
 
/**
 * txt带search的公用选择控制模块。
 * <p>Description: </p>
 * @author caicong
 * @time 2014-5-8
 */
public class BaseCustomControl extends AbstractCustomControl implements ICustomDefine{
 
    private static final long serialVersionUID = -2815707317189936866L;
    private final Image img = VCISwingUtil.createImageIcon("search.png").getImage();
    private String value = "";
    private boolean editable = true;
    private ICustomAttributeInteceptor inteceptor = null ;
    private PRMItem item;
    
    public BaseCustomControl(){
        super();
    }
    
    public BaseCustomControl(PRMItem item) {
        super(item);
        this.item = item;
        // 1:只读 0:非只读
        editable = item.getItemIsEditable().equals("1") ? false:true;
        init();
        setEditable(editable);
        if(editable){
            txt.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        }
    }
    
    @SuppressWarnings("serial")
    private final VCIJTextField txt = new VCIJTextField(""){
        @Override
        public void paintComponent(Graphics g){
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g;
            if(img != null){
                g2.drawImage(img, getWidth() - img.getWidth(null), getHeight() - img.getHeight(null), img.getWidth(null),  img.getHeight(null), null);
            }
            BaseCustomControl.this.drawRequiredFlag(g);
        }
        @Override
        public void mouseClicked(MouseEvent e) {
            super.mouseClicked(e);
            if(!editable || !isEnabled()) {
                return;
            }
            if (isClickImage(e)) {
                try {
                    if(inteceptor == null){
                        inteceptor = getCustomInteceptorInstance();
                    }
                    if(inteceptor != null){
                        //通过接口获取id-name的字符串。
                        String sotoreAndDisplayValue = inteceptor.getCustomAttributeSaveValue(item, getParentComponent());//父组件对象需考虑。
                        //将id 赋值给value,name赋值给txt。
                        setValue(sotoreAndDisplayValue);//待确定赋值方式。
                    }
                } catch (Exception e1) {
                    e1.printStackTrace();
                } 
            } 
        }
        
        private boolean isClickImage(MouseEvent e){
            boolean res = false;
            int imgWidth = img.getWidth(null);
            int imgHeight = img.getHeight(null);
            int width = getWidth();
            int height = getHeight();
            int pointX = e.getX();
            int pointY = e.getY();
            if(width - imgWidth < pointX  && pointX <  width 
                    && height - imgHeight < pointY && pointY < height){
                res = true;
            }
            // 只读的情况下,点击文本框任何区域,就将响应自事件处理
            // 非只读时,只有点击图片区域,才响应自定义事件处理
            if(!txt.isEditable()){
                res = true;
            }
            return res;
        }
    };
    
    @Override
    public String getValue() {
        return value;
    }
    @Override
    public void setValue(String sotoreAndDisplayValue) {
        String displayValue = "";
        if(!sotoreAndDisplayValue.contains(ICustomAttributeInteceptor.SPLIT_CHAR)) {
            ICustomAttributeInteceptor inteceptor = getCustomInteceptorInstance();
            if(inteceptor != null){
                try {
                    displayValue = inteceptor.getCustomAttributeDisplayValue(sotoreAndDisplayValue, getPRMItem());
                } catch (VCIError e) {
                    e.printStackTrace();
                }
            }
            if(displayValue != null){
                sotoreAndDisplayValue = sotoreAndDisplayValue + ICustomAttributeInteceptor.SPLIT_CHAR + displayValue;  
            }
        }
        String oldValue = txt.getText();
        String[] values = sotoreAndDisplayValue.split(ICustomAttributeInteceptor.SPLIT_CHAR);
        String newValue = "";
        if(values.length > 0){
            this.value = values[0];
            this.txt.setText(values[0]);
        }
        if(values.length >= 2){
            this.txt.setText(values[1]);
            newValue = this.txt.getText();
        }
        if(sotoreAndDisplayValue.equals(ICustomAttributeInteceptor.SPLIT_CHAR)){
            newValue = "";
            this.value = newValue;
            this.txt.setText(this.value);
        }
        firePropertyChange("valueChange", oldValue, newValue);
    }
    
    protected ICustomAttributeInteceptor getCustomInteceptorInstance(){
        ICustomAttributeInteceptor res = null;
        String className = getPRMItem().getItemStyle();
        if(className == null || "".equals(className)){
            className = getPRMItem().getItemValue();
        }
        try{
            if(className == null || "".equals(className)) return res;
            Object obj = Class.forName(className).getConstructor().newInstance();
            if(obj == null) return res;
            if(obj instanceof ICustomAttributeInteceptor){
                res = (ICustomAttributeInteceptor)obj;
                res.setCustomControl(this);
            }
        }catch(Exception ex){
            ex.printStackTrace();
        }
        return res;
    }
    
    @Override
    public void setEditable(boolean editable) {
        this.editable = editable;
    }
    
    protected void init(){
        setLayout(new GridBagLayout());
        add(txt, getGBC(0, 0, 1, 1, 1.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.BOTH, 0));
        txt.setEditable(false);
        if (editable) {
            txt.setBackground(Color.WHITE);
        }
    }
    
    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        setEditable(enabled);
        txt.setEnabled(enabled);
    }
    
    private UserNameInTableCellRenderer uncr = null;
    @Override
    public void getCustomValue(String columnName,
            Map<String, PRMItem> customItemMap, List<Map<String, String>> datas)
            throws VCIError {
        if(uncr == null) uncr = new UserNameInTableCellRenderer();
        uncr.getCustomValue(columnName, customItemMap, datas);
    }
 
    public VCIJTextField getTxt() {
        return txt;
    }
}