wangting
2024-12-26 fa261e8c1220b31af54e8167e4de9c3320b1af27
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
269
package com.vci.client.common;
 
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
 
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
 
import com.vci.client.framework.util.ClientHelper;
import com.vci.client.logon.base.BaseJDialog;
import com.vci.client.ui.image.BundleImage;
import com.vci.client.ui.swing.KJButton;
import com.vci.client.ui.swing.VCISwingUtil;
 
public class IconSelectDialog extends BaseJDialog {
 
    /**
     * 
     */
    private static final long serialVersionUID = -4411155196219867954L;
 
    private List<String> allIcons = new LinkedList<String>();
    private HashMap<String, Integer> iconIndexs = new LinkedHashMap<String, Integer>();
    private List<JRadioButtonExt> allIconRbtns = new LinkedList<JRadioButtonExt>();
    private KJButton btnOk = new KJButton(getI18nString("btnOk"), "ok.gif");
    private KJButton btnCancel = new KJButton(getI18nString("btnCancel"), "cancel.gif");
    private String type;
    private JButton button;
    private JLabel lab;
    private String cruurentSelectedIcon = "";
    private BundleImage bundleImage = new BundleImage();
    public IconSelectDialog(JDialog dialog, String title, boolean isModel, String type, JButton button){
        super(dialog, title, isModel);
        this.type = type;
        this.button = button;
        init();
    }
    
    public IconSelectDialog(Frame frame, String title, boolean isModel, JLabel lab){
        super(frame, title, isModel);
        this.type = type;
        this.lab = lab;
        init();
    }
    
    public IconSelectDialog(String title, boolean isModel, String type, JButton button){
        super();
        this.button = button;
        init();
    }
    
    private void init(){
        this.setSize(new Dimension(500,350));
        Dimension screenSize = Toolkit.getDefaultToolkit ().getScreenSize ();
        this.setLocation ((screenSize.width - 500) / 2, (screenSize.height - 350) / 2);
        initControl();
        initActionListener();
        if(button != null){
            String btnIcon = button.getIcon().toString();
            btnIcon = btnIcon.substring(btnIcon.lastIndexOf("/") + 1);
            if(this.iconIndexs.containsKey(btnIcon)){
                cruurentSelectedIcon = btnIcon;
                this.allIconRbtns.get(this.iconIndexs.get(btnIcon)).setSelected(true);
            }
        }
    }
    
    private void initControl(){
        setLayout(new BorderLayout());
        JScrollPane palIcons = getIconsPanel();
        add(palIcons, BorderLayout.CENTER);
        JPanel palBtns = new JPanel();
        palBtns.add(btnOk);
        palBtns.add(btnCancel);
        add(palBtns, BorderLayout.SOUTH);
    }
    
    private JScrollPane getIconsPanel(){
        JScrollPane scrollPanel = new JScrollPane();
        scrollPanel.setBorder(BorderFactory.createLineBorder(Color.black));
        scrollPanel.setBackground(Color.white);
        
        JPanel panel = new JPanel();
        
        panel.setLayout(new GridBagLayout());
//        panel.setBorder(BorderFactory.createLineBorder(Color.black));
        panel.setBackground(Color.white);
        //panel.setLayout(new FlowLayout(FlowLayout.LEFT));
        loadSupportedIcons(panel);
        this.setBackground(Color.white);
        
        //pal.add(panel);
        scrollPanel.setViewportView(panel);
        return scrollPanel;
    }
    
    private void loadSupportedIcons(JPanel imgPal){
        //String[] icons = getI18nString("allSupprotedIcons").split(",");
        String[] icons = VCISwingUtil.getImages();
        ButtonGroup btnGrp = new ButtonGroup();
        
        GridBagConstraints g = new GridBagConstraints();
 
        int index = 0;
        for(String icon : icons){
            ImageIcon imgIcon = bundleImage.createImageIcon(icon);
            if (imgIcon.getIconWidth() > 16)
                continue;
            
            JRadioButton rdImage = new JRadioButton();
            rdImage.setBackground(Color.white);
            rdImage.setName(icon);
            rdImage.addActionListener(iconRbtnAction);
            btnGrp.add(rdImage);
            JLabel iLabel = new JLabel(imgIcon);
            int rem = index % 10;
            int mer = index / 10;
            g.gridx = 2 * rem;     
            g.gridy = mer;
            g.anchor = GridBagConstraints.EAST;
            g.insets = new Insets(20, 3, 3, 3);
            g.gridwidth = 1;
            imgPal.add(rdImage, g);
            g.gridx = 2 * rem + 1;
            if(rem == 10){
                g.gridwidth = 0;
            }
            imgPal.add(iLabel, g);
            
            index++;
 
//            final String name = loadPicsWithPath[i];
//            rdImage.addItemListener(new ItemListener() {
//                @Override
//                public void itemStateChanged(ItemEvent e) {
//                    // TODO Auto-generated method stub
//                    imageName = name;
//                }
//            });    
            
//            JLabelExt lbl = new JLabelExt(bundleImage.createImageIcon(icon));
//            lbl.setBackground(Color.white);
//            JRadioButtonExt rbtn = new JRadioButtonExt(lbl);
//            lbl.setRbtn(rbtn);
//            rbtn.setBackground(Color.white);
//            btnGrp.add(rbtn);
//            allIcons.add(icon);
//            allIconRbtns.add(rbtn);
//            iconIndexs.put(icon, index++);
//            rbtn.addActionListener(iconRbtnAction);
//            pal.add(rbtn);
//            pal.add(lbl);
        }
    }
 
    private String getI18nString(String spCode){
        return ClientHelper.getI18nStringForFramework(this.getClass().getName() + "." + spCode, this.getLocale());
    }
 
    private void initActionListener(){
        btnOk.addActionListener(okAction);
        btnCancel.addActionListener(cancelAction);
    }
 
    private ActionListener iconRbtnAction = new ActionListener(){
        public void actionPerformed(ActionEvent e) {
            iconRbtnAction(e);
        }
    }; 
    
    private ActionListener okAction = new ActionListener(){
        public void actionPerformed(ActionEvent e) {
            ok();
        }
    };
    private ActionListener cancelAction = new ActionListener(){
        public void actionPerformed(ActionEvent e) {
            close();
        }
    };
    
    private void iconRbtnAction(ActionEvent e){
        JRadioButton rbtn = (JRadioButton)e.getSource();
        cruurentSelectedIcon = rbtn.getName();
    }
    
    private void ok(){
        if(!cruurentSelectedIcon.equals("")){
            if(button != null){
                button.setIcon(bundleImage.createImageIcon(cruurentSelectedIcon));
            }else if(lab != null){
                lab.setIcon(bundleImage.createImageIcon(cruurentSelectedIcon));
            }
            close();
        }
    }
    
    private void close(){
        this.dispose();
        this.setVisible(false);
    }
    
    public String getSelectedIcon(){
        return cruurentSelectedIcon;
    }
    
    class JRadioButtonExt extends JRadioButton{
        /**
         * 
         */
        private static final long serialVersionUID = 103883668562849532L;
        private JLabel lbl;
 
        public JLabel getLbl() {
            return lbl;
        }
 
        public void setLbl(JLabel lbl) {
            this.lbl = lbl;
        }
        public JRadioButtonExt(JLabel lbl) {
            super();
            this.lbl = lbl;
        }
    }
    
    class JLabelExt extends JLabel{
        /**
         * 
         */
        private static final long serialVersionUID = -958222271031558762L;
        private JRadioButtonExt rbtn;
 
        public JRadioButtonExt getRbtn() {
            return rbtn;
        }
 
        public void setRbtn(JRadioButtonExt rbtn) {
            this.rbtn = rbtn;
        }
 
        public JLabelExt(ImageIcon icon) {
            super(icon);
        }
        
    }
}