1 56 package org.objectstyle.cayenne.modeler.util; 57 58 import java.awt.Component ; 59 import java.awt.event.FocusAdapter ; 60 import java.awt.event.FocusEvent ; 61 import java.util.EventObject ; 62 63 import javax.swing.DefaultCellEditor ; 64 import javax.swing.JTable ; 65 import javax.swing.JTextField ; 66 import javax.swing.event.ChangeEvent ; 67 import javax.swing.table.TableCellEditor ; 68 import javax.swing.text.JTextComponent ; 69 70 77 public class CayenneTable extends JTable { 78 79 public CayenneTable() { 80 super(); 81 this.setRowHeight(25); 82 this.setRowMargin(3); 83 } 84 85 protected void createDefaultEditors() { 86 super.createDefaultEditors(); 87 88 JTextField textField = CayenneWidgetFactory.createTextField(0); 89 final DefaultCellEditor textEditor = new DefaultCellEditor (textField); 90 91 textField.addFocusListener(new FocusAdapter () { 94 public void focusLost(FocusEvent e) { 95 if (!e.isTemporary()) { 96 textEditor.cancelCellEditing(); 97 } 98 } 99 }); 100 101 setDefaultEditor(Object .class, textEditor); 102 setDefaultEditor(String .class, textEditor); 103 } 104 105 public CayenneTableModel getCayenneModel() { 106 return (CayenneTableModel) getModel(); 107 } 108 109 113 protected void cancelEditing() { 114 editingCanceled(new ChangeEvent (this)); 115 } 116 117 public void select(Object row) { 118 if (row == null) { 119 return; 120 } 121 cancelEditing(); 122 123 CayenneTableModel model = getCayenneModel(); 124 int ind = model.getObjectList().indexOf(row); 125 126 if (ind >= 0) { 127 getSelectionModel().setSelectionInterval(ind, ind); 128 } 129 } 130 131 public void select(int index) { 132 cancelEditing(); 133 134 CayenneTableModel model = getCayenneModel(); 135 if (index >= model.getObjectList().size()) { 136 index = model.getObjectList().size() - 1; 137 } 138 139 if (index >= 0) { 140 getSelectionModel().setSelectionInterval(index, index); 141 } 142 } 143 144 147 public void editingStopped(ChangeEvent e) { 148 super.editingStopped(e); 149 150 int row = getSelectedRow(); 152 if (row >= 0 && this.getRowCount() > 0 && getSelectedTextComponent() != null) { 153 row++; 154 155 if (row >= this.getRowCount()) { 156 row = 0; 157 } 158 select(row); 159 } 160 } 161 162 public JTextComponent getSelectedTextComponent() { 163 int row = getSelectedRow(); 164 int column = getSelectedColumn(); 165 if (row < 0 || column < 0) { 166 return null; 167 } 168 169 TableCellEditor editor = this.getCellEditor(row, column); 170 if (editor instanceof DefaultCellEditor ) { 171 Component comp = ((DefaultCellEditor ) editor).getComponent(); 172 if (comp instanceof JTextComponent ) { 173 return (JTextComponent ) comp; 174 } 175 } 176 return null; 177 } 178 179 182 public boolean editCellAt(int row, int column, EventObject e) { 183 boolean edit = super.editCellAt(row, column, e); 184 185 if (edit) { 186 JTextComponent t = getSelectedTextComponent(); 187 if (t != null) { 188 if (!t.isFocusOwner()) { 189 t.requestFocus(); 190 } 191 192 t.setCaretPosition(t.getDocument().getLength()); 193 } 194 } 195 return edit; 196 } 197 } 198 | Popular Tags |