wangting
2024-11-27 3b3fd904b9b34e77445d749bca8c28beadcaf3db
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
package com.vci.client.uif.engine.client.controls;
 
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.util.HashMap;
import java.util.Iterator;
 
import com.vci.client.omd.provider.ApProvider;
import com.vci.client.omd.provider.EnumProvider;
import com.vci.client.portal.utility.PRMItem;
import com.vci.client.ui.swing.components.VCIJCheckBox;
import com.vci.client.ui.swing.components.VCIJPanel;
import com.vci.client.ui.swing.components.VCIJScrollPane;
import com.vci.corba.omd.atm.AttribItem;
import com.vci.corba.omd.etm.EnumChild;
import com.vci.corba.omd.etm.EnumItem;
 
public class CheckBoxControl extends AbstractCustomControl {
    /**
     * 
     */
    private static final long serialVersionUID = 1386018173187653248L;
    private String value = "";
    private final HashMap<String, VCIJCheckBox> cbxKeyMap = new HashMap<String, VCIJCheckBox>();
    private final HashMap<String, VCIJCheckBox> cbxValueMap = new HashMap<String, VCIJCheckBox>();
    
    private boolean required = false;
    private Object obj = null;
    
    public CheckBoxControl(PRMItem item, boolean required, boolean editable){
        super(item);
        setRequired(required);
        setEditable(editable);
        init();
    }
    
    private void init(){
        setLayout(new BorderLayout());
 
        VCIJScrollPane pp = new VCIJScrollPane(getCheckBoxDataPanel());
        pp.setBorder(null);
        add(pp, BorderLayout.CENTER);
    }
    
    private VCIJPanel getCheckBoxDataPanel(){
        
        VCIJPanel pal = new VCIJPanel(new FlowLayout(FlowLayout.LEADING));
        
        PRMItem item = getPRMItem();
        
        String field = item.getItemField();
        String[] fields = field.split("\\.");
        AttribItem attr = ApProvider.getInstance().getAbItemByName(fields[fields.length - 1]);
        String enumName = ApProvider.getInstance().getOtherValueByType(attr.other, "enumName");
        EnumItem enumItem = EnumProvider.getInstance().getEnumByName(enumName);
        for (EnumChild child : enumItem.children) {
            VCIJCheckBox cbx = new VCIJCheckBox(child.name);
            cbx.setRequired(isRequired());
            cbx.setEnabled(isEditable());
            cbx.setObj(child);
            pal.add(cbx);
 
            cbxKeyMap.put(child.name, cbx);
            cbxValueMap.put(child.value, cbx);
        }
        return pal;
    }
 
    private void setCheckBoxValue(String value){
        clearSelected();
        String[] values = value.split(";");
        for(String key : values){
            VCIJCheckBox cbx = cbxValueMap.get(key);
            if(cbx != null){
                cbx.setSelected(true);
            }
        }
    }
    
    private void clearSelected(){
        for(Iterator<String> it = cbxValueMap.keySet().iterator(); it.hasNext();){
            String key = it.next();
            VCIJCheckBox cbx = cbxValueMap.get(key);
            selectCheckbox(cbx, false);
        }
    }
    
    private void selectCheckbox(VCIJCheckBox cbx, boolean selected){
        cbx.setSelected(selected);
    }
 
    @Override
    public String getValue() {
        StringBuffer sb = new StringBuffer();
        for(Iterator<String> it = cbxValueMap.keySet().iterator(); it.hasNext();){
            String key = it.next();
            VCIJCheckBox cbx = cbxValueMap.get(key);
            if(cbx.isSelected()){
                sb.append(key).append(";");
            }
        }
        value = sb.toString();
        if(value != null && !"".equals(value) && value.endsWith(";")){
            value = value.substring(0, value.length() - 1);
        }
        return value;
    }
 
    @Override
    public void setValue(String value) {
        if(value == null || "".equals(value)) return;
        this.value = value;
        setCheckBoxValue(value);
    }
 
    
    @Override
    public void setEditable(boolean editable) {
        super.setEditable(editable);
        for(Iterator<String> it = cbxValueMap.keySet().iterator(); it.hasNext();){
            String key = it.next();
            VCIJCheckBox cbx = cbxValueMap.get(key);
            cbx.setEnabled(editable);
        }
    }
 
    @Override
    public void setEnabled(boolean enabled) {
        setEditable(enabled);
    }
 
    @Override
    public boolean isRequired() {
        return required;
    }
 
    public void setRequired(boolean required) {
        this.required = required;
    }
 
    @Override
    public Object getObj() {
        return obj;
    }
 
    @Override
    public void setObj(Object obj) {
        this.obj = obj;
    }
    
}