1 19 20 21 package org.netbeans.modules.properties; 22 23 24 import java.awt.Component ; 25 import javax.swing.DefaultCellEditor ; 26 import javax.swing.JLabel ; 27 import javax.swing.JTable ; 28 import javax.swing.JTextField ; 29 import javax.swing.event.DocumentListener ; 30 import javax.swing.text.Caret ; 31 import javax.swing.text.JTextComponent ; 32 import org.openide.awt.Mnemonics; 33 import org.openide.util.NbBundle; 34 35 36 39 public class PropertiesTableCellEditor extends DefaultCellEditor { 40 41 42 private boolean isKeyCell; 43 44 45 static final long serialVersionUID =-5292598860635851664L; 46 47 48 private DocumentListener listener; 49 50 51 private JTextComponent valueComponent; 52 53 54 private JTextComponent commentComponent; 55 56 57 private final TableViewSettings settings; 58 59 60 63 public PropertiesTableCellEditor(JTextField tf, final JTextComponent commentComponent, 64 final JTextComponent valueComponent, final JLabel valueLabel, DocumentListener listener) { 65 super(tf); 66 this.clickCountToStart = 1; 68 this.listener = listener; 69 this.valueComponent = valueComponent; 70 this.commentComponent = commentComponent; 71 valueComponent.setDocument(tf.getDocument()); 72 this.delegate = new PropertiesEditorDelegate(commentComponent, valueComponent, valueLabel); 73 ((JTextField )editorComponent).addActionListener(delegate); 74 75 settings = TableViewSettings.getDefault(); 76 } 77 78 79 84 public Component getTableCellEditorComponent(JTable table, 85 Object value, boolean isSelected, int row, int column) { 86 87 isKeyCell = (column == 0) ? true : false; 89 90 valueComponent.getDocument().removeDocumentListener(listener); 91 commentComponent.getDocument().removeDocumentListener(listener); 92 final JTextField textField = (JTextField )super.getTableCellEditorComponent(table, value, isSelected, row, column); 93 valueComponent.getDocument().addDocumentListener(listener); 94 commentComponent.getDocument().addDocumentListener(listener); 95 Caret caret = textField.getCaret(); 96 caret.setVisible(true); 97 caret.setDot(0); 98 99 textField.setFont(settings.getFont()); 100 101 int[] result = (int[])table.getClientProperty(FindPerformer.TABLE_SEARCH_RESULT); 104 if(result != null && row == result[0] && column == result[1]) { 105 table.putClientProperty(FindPerformer.TABLE_SEARCH_RESULT, null); caret.setDot(result[2]); 107 caret.moveDot(result[3]); 108 } 109 110 return textField; 111 } 112 113 114 115 private class PropertiesEditorDelegate extends DefaultCellEditor.EditorDelegate { 116 117 118 JTextComponent commentComponent; 119 120 JTextComponent valueComponent; 121 122 JLabel valueLabel; 123 124 125 static final long serialVersionUID =9082979978712223677L; 126 127 128 129 public PropertiesEditorDelegate(JTextComponent commentComponent, JTextComponent valueComponent, JLabel valueLabel) { 130 this.commentComponent = commentComponent; 131 this.valueComponent = valueComponent; 132 this.valueLabel = valueLabel; 133 } 134 135 136 137 public void setValue(Object x) { 138 this.value = x; 140 PropertiesTableModel.StringPair sp = (PropertiesTableModel.StringPair)x; 142 143 if (sp != null) { 145 ((JTextField )getComponent()).setText(sp.getValue()); 148 commentComponent.setText(sp.getComment()); 149 } else { 150 ((JTextField )getComponent()).setText(""); commentComponent.setText(""); } 153 } 154 155 156 public Object getCellEditorValue() { 157 String value = ((JTextField )getComponent()).getText(); 158 159 if(isKeyCell) { 161 Mnemonics.setLocalizedText(valueLabel, NbBundle.getBundle(PropertyPanel.class).getString("LBL_KeyLabel")); 162 } 163 else { 165 Mnemonics.setLocalizedText(valueLabel, NbBundle.getBundle(BundleEditPanel.class).getString("LBL_ValueLabel")); 166 } 167 168 return new PropertiesTableModel.StringPair(commentComponent.getText(),value,isKeyCell); 170 } 171 172 } } 174 | Popular Tags |