View Javadoc
1 package com.bonevich.erj.ui.editor; 2 3 import com.bonevich.erj.model.*; 4 import com.bonevich.erj.ErjConstants; 5 import com.bonevich.javax.swing.AbstractComboBoxEditor; 6 7 import java.awt.*; 8 import javax.swing.*; 9 import javax.swing.text.*; 10 11 /*** 12 * A <code>ComboxBoxEditor</code> implementation that manages the editing 13 * of <code>DataType</code> objects in a combo box. Provides a JTextField 14 * component in which the type can be edited. Also initializes an internal 15 * Document class implementation to control the text editing. 16 */ 17 public final class DataTypeComboBoxEditor extends AbstractComboBoxEditor 18 { 19 private JTextField _editor; 20 private DataType _item; 21 private DataTypeDocument _doc; 22 { 23 _editor = new JTextField(); 24 _doc = new DataTypeDocument(); 25 _editor.setDocument(_doc); 26 _editor.setBorder(BorderFactory.createEmptyBorder()); 27 } 28 29 public Component getEditorComponent() 30 { 31 //System.err.println("(editor) getEditorComponent: " + _textField); 32 return _editor; 33 } 34 35 public Object getItem() 36 { 37 return _item; 38 } 39 40 public void setItem(Object item) 41 { 42 //System.err.println("(editor) setItem: " + item.getClass().getName()); 43 if (item instanceof DataType) 44 { 45 _item = (DataType)item; 46 _doc.setType(_item); 47 int pos = _doc.getInitString().length(); 48 _editor.setCaretPosition(_item.takesDescriptor() ? pos - 1 : pos); 49 } 50 } 51 52 public String getDescriptor() 53 { 54 return _doc.getDescriptor(); 55 } 56 57 public void setDescriptor(String descriptor) 58 { 59 _doc.setDescriptor(descriptor); 60 } 61 62 /*** A <code>Document</code> implementation to control display and 63 * editing of <code>DataType</code> (i.e. the user can edit the descriptor of 64 * a type, if it takes one, but cannot modify the name of the type). 65 */ 66 private static final class DataTypeDocument extends PlainDocument 67 { 68 ////////////////////////////////////////////////////////// 69 // Attributes 70 private String _initString; 71 private DataType _type; 72 73 public DataType getType() 74 { 75 return _type; 76 } 77 public void setType(DataType type) 78 { 79 _type = type; 80 try { 81 super.remove(0, getLength()); 82 init(); 83 super.insertString(0, _initString, null); 84 //System.err.println("(doc) _initString = " + _initString); 85 } catch (BadLocationException e) { 86 System.err.println("Failed to insert string: " + _initString); 87 } 88 } 89 90 public void insertString(int offset, String s, AttributeSet set) 91 throws BadLocationException 92 { 93 if (s.equals(_initString)) 94 { 95 super.insertString(offset, s, set); 96 } else { 97 //System.err.println("(doc) trying to insert: " + s); 98 try { 99 Integer.parseInt(s); 100 } catch (Exception e) { 101 // only integers and commas are allowed 102 if (s.charAt(0) != ',') return; 103 } 104 105 if (canInsert(offset)) 106 { 107 super.insertString(offset, s, set); 108 } 109 } 110 } 111 112 public void remove(int offset, int length) throws BadLocationException 113 { 114 if (canRemove(offset)) // && offset > 0) 115 { 116 super.remove(offset,length); 117 } 118 } 119 120 private boolean canInsert(int offset) 121 { 122 //System.err.println("(doc) offset = " + offset + "; length = " + getLength()); 123 return offset > _type.getName().length() && offset < getLength(); 124 } 125 126 private boolean canRemove(int offset) 127 { 128 //System.err.println("(doc) offset = " + offset + "; length = " + getLength()); 129 return offset > _type.getName().length() && offset < getLength() - 1; 130 } 131 132 private void init() 133 { 134 _initString = _type.takesDescriptor() 135 ? _type.getName() + "(" + _type.getDefaultDescriptor() + ")" 136 : _type.getName(); 137 } 138 139 public String getInitString() 140 { 141 return _initString; 142 } 143 144 public String getDescriptor() 145 { 146 String descriptor = null; 147 if (_type.takesDescriptor()) 148 { 149 int offset = _type.getName().length() + 1; 150 int textLength = getLength(); 151 int length = textLength - offset - 1; 152 try { 153 descriptor = getText(offset, length); 154 //System.err.println("descriptor = " + descriptor); 155 } catch (BadLocationException e) { 156 System.err.println("(doc) failed to extract descriptor with getText(" + offset + "," + length + "); length = " + textLength); 157 descriptor = ErjConstants.EMPTY_STR; 158 } 159 } 160 return descriptor; 161 } 162 163 public void setDescriptor(String descriptor) 164 { 165 if (_type.takesDescriptor()) 166 { 167 int offset = _type.getName().length() + 1; 168 int textLength = getLength(); 169 int length = textLength - offset - 1; 170 try { 171 remove(offset, length); 172 insertString(offset, descriptor, null); 173 } catch (BadLocationException e) { 174 System.err.println("(doc) failed to insert descriptor with insert(" + offset + "," + descriptor + ", null)"); 175 } 176 } 177 } 178 179 } /* end class DataTypeDocument */ 180 181 } /* end class DataTypeComboBoxEditor */

This page was automatically generated by Maven