| 1 | package tmcsim.simulationmanager.model; |
|---|
| 2 | |
|---|
| 3 | import javax.swing.JTextField; |
|---|
| 4 | import javax.swing.text.AttributeSet; |
|---|
| 5 | import javax.swing.text.BadLocationException; |
|---|
| 6 | import javax.swing.text.Document; |
|---|
| 7 | import javax.swing.text.PlainDocument; |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | /** |
|---|
| 11 | * UpperCaseField extends from JTextField to provide a text field that only |
|---|
| 12 | * displays upper case characters. |
|---|
| 13 | * |
|---|
| 14 | * @author Matthew Cechini |
|---|
| 15 | * @version |
|---|
| 16 | */ |
|---|
| 17 | @SuppressWarnings("serial") |
|---|
| 18 | public class UpperCaseField extends JTextField { |
|---|
| 19 | |
|---|
| 20 | /** |
|---|
| 21 | * Constructor. |
|---|
| 22 | * @param cols Number of columns for the text field. |
|---|
| 23 | */ |
|---|
| 24 | public UpperCaseField(int cols) { |
|---|
| 25 | super(cols); |
|---|
| 26 | } |
|---|
| 27 | |
|---|
| 28 | protected Document createDefaultModel() { |
|---|
| 29 | return new UpperCaseDocument(); |
|---|
| 30 | } |
|---|
| 31 | |
|---|
| 32 | /** |
|---|
| 33 | * The UpperCaseDocument extends from PlainDocument to overload the |
|---|
| 34 | * insertString method to ensure that all characters are upper case. |
|---|
| 35 | * @author Matthew Cechini |
|---|
| 36 | */ |
|---|
| 37 | class UpperCaseDocument extends PlainDocument { |
|---|
| 38 | |
|---|
| 39 | public void insertString(int offs, String str, AttributeSet a) |
|---|
| 40 | throws BadLocationException { |
|---|
| 41 | |
|---|
| 42 | if (str == null) { |
|---|
| 43 | return; |
|---|
| 44 | } |
|---|
| 45 | char[] upper = str.toCharArray(); |
|---|
| 46 | for (int i = 0; i < upper.length; i++) { |
|---|
| 47 | upper[i] = Character.toUpperCase(upper[i]); |
|---|
| 48 | } |
|---|
| 49 | super.insertString(offs, new String(upper), a); |
|---|
| 50 | } |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | } |
|---|