Statistics
| Revision:

svn-document-layout / trunk / org.gvsig.app.document.layout2.app / org.gvsig.app.document.layout2.app.mainplugin / src / main / java / org / gvsig / app / project / documents / layout / fframes / gui / numberFormat / CharComboBox.java @ 272

History | View | Annotate | Download (2.27 KB)

1
package org.gvsig.app.project.documents.layout.fframes.gui.numberFormat;
2

    
3
import java.awt.event.FocusEvent;
4
import java.awt.event.FocusListener;
5
import java.util.ArrayList;
6

    
7
import javax.swing.JComboBox;
8

    
9
import org.gvsig.i18n.Messages;
10

    
11
/**
12
 * This is is a customized JComboBox used to select a single character.
13
 * It has the following properties:
14
 * <ul>
15
 * <li>it is editable</li>
16
 * <li>only allows a single char (if more chars are introduced, the string
17
 * is trimmed to the first character</li>
18
 * <li>optionally, it can show a special item (NoneItem) that represents that no
19
 * character wants to be selected.</li></ul>
20
 * 
21
 * @author Cesar Martinez Izquierdo
22
 *
23
 */
24
public class CharComboBox extends JComboBox {
25
        private static final long serialVersionUID = 2194202669931887456L;
26
        /**
27
         * This item represents that no item is selected
28
         */
29
        private String noneItem = Messages.getText("None");
30
        
31
        public CharComboBox(String[] items, boolean showNoneItem) {
32
                super();
33
                this.setEditable(true);
34
                for (int i=0; i<items.length; i++) {
35
                        String item = items[i];
36
                        if (item!=null && item.length()>0) {
37
                                this.addItem(new String(item.substring(0, 1)));
38
                        }
39
                }
40
                if (showNoneItem) {
41
                        this.addItem(noneItem);
42
                }
43
                this.addFocusListener(new FocusListener() {
44

    
45
                        public void focusGained(FocusEvent e) {        
46
                        }
47

    
48
                        public void focusLost(FocusEvent e) {
49
                                String item = getSelectedItem();
50
                                if (item!=null) {
51
                                        setSelectedItem(item);
52
                                }
53
                        }
54
                        
55
                });
56
        }
57
        
58
        public CharComboBox() {
59
                this(new String[]{" "}, true);
60
        }
61
        
62
        /**
63
         * Returns the selected char, or null if the NoneItem is
64
         * selected.
65
         * @see javax.swing.JComboBox#getSelectedItem()
66
         */
67
        public String getSelectedItem() {
68
                String item = (String) super.getSelectedItem();
69
                if (item!=null && item!=noneItem) {
70
                        if (item.length()>0) {
71
                                return item.substring(0, 1);
72
                        }
73
                        else {
74
                                return " ";
75
                        }
76
                }
77
                else {
78
                        return null;
79
                }
80
        }
81
        
82
        // Ensure all the added items are Strings
83
        public void addItem(Object obj) {
84
                super.addItem(String.valueOf(obj));
85
        }
86
        
87
        /**
88
         * Sets the text to show as the NoneItem. The text has to be
89
         * provided in its localized form (if translations are in use).
90
         * 
91
         * @param text The localized text to show as the NoneItem
92
         */
93
        public void setNoneItemText(String text) {
94
                noneItem = text;
95
        }
96
        
97
        
98
        
99
        
100
        
101
        
102

    
103
}