KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > properties > PropertiesTableCellEditor


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20
21 package org.netbeans.modules.properties;
22
23
24 import java.awt.Component JavaDoc;
25 import javax.swing.DefaultCellEditor JavaDoc;
26 import javax.swing.JLabel JavaDoc;
27 import javax.swing.JTable JavaDoc;
28 import javax.swing.JTextField JavaDoc;
29 import javax.swing.event.DocumentListener JavaDoc;
30 import javax.swing.text.Caret JavaDoc;
31 import javax.swing.text.JTextComponent JavaDoc;
32 import org.openide.awt.Mnemonics;
33 import org.openide.util.NbBundle;
34
35
36 /**
37  * @author Petr Jiricka
38  */

39 public class PropertiesTableCellEditor extends DefaultCellEditor JavaDoc {
40
41     /** Value holding info if the editing cell is a key or value. */
42     private boolean isKeyCell;
43
44     /** Generated serial version UID. */
45     static final long serialVersionUID =-5292598860635851664L;
46     
47     /** Document listener. */
48     private DocumentListener JavaDoc listener;
49     
50     /** Value component */
51     private JTextComponent JavaDoc valueComponent;
52     
53     /** Comment component */
54     private JTextComponent JavaDoc commentComponent;
55
56     /** Class representing settings used in table view. */
57     private final TableViewSettings settings;
58     
59     
60     /** Constructs a PropertiesTableCellEditor that uses a text field.
61     * @param x a JTextField object ...
62     */

63     public PropertiesTableCellEditor(JTextField JavaDoc tf, final JTextComponent JavaDoc commentComponent,
64         final JTextComponent JavaDoc valueComponent, final JLabel JavaDoc valueLabel, DocumentListener JavaDoc listener) {
65         super(tf);
66         // Number of clicks needed to edit an editable cell.
67
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 JavaDoc)editorComponent).addActionListener(delegate);
74         
75         settings = TableViewSettings.getDefault();
76     }
77
78     
79     /** Overrides superclass method.
80     * It sets the cursot at the beginnig of edited cell, in case of searching it highlights the found text.
81     * At the end it request for focus so the editor component (JTextField) has it, not the table.
82     * This is also a hack with reason to figure out which cell is going to be edited, if a key or a value.
83     */

84     public Component JavaDoc getTableCellEditorComponent(JTable JavaDoc table,
85         Object JavaDoc value, boolean isSelected, int row, int column) {
86             
87         // Key or value? Only in the first column are keys.
88
isKeyCell = (column == 0) ? true : false;
89         
90         valueComponent.getDocument().removeDocumentListener(listener);
91         commentComponent.getDocument().removeDocumentListener(listener);
92         final JTextField JavaDoc textField = (JTextField JavaDoc)super.getTableCellEditorComponent(table, value, isSelected, row, column);
93         valueComponent.getDocument().addDocumentListener(listener);
94         commentComponent.getDocument().addDocumentListener(listener);
95         Caret JavaDoc caret = textField.getCaret();
96         caret.setVisible(true);
97         caret.setDot(0);
98         
99         textField.setFont(settings.getFont());
100         
101         // Check for search results.
102
// If search was performed, highlight the found string.
103
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); // removes property
106
caret.setDot(result[2]);
107             caret.moveDot(result[3]);
108         }
109
110         return textField;
111     }
112
113
114     /** Inner class which is cell editor delegate. */
115     private class PropertiesEditorDelegate extends DefaultCellEditor.EditorDelegate JavaDoc {
116
117         /** Reference to text component showing comments on bundle edit table. */
118         JTextComponent JavaDoc commentComponent;
119         /** Reference to text component showing key or value respectively on bundle edit table. */
120         JTextComponent JavaDoc valueComponent;
121         /** Reference to the value label. */
122         JLabel JavaDoc valueLabel;
123
124         /** Generated serial version UID. */
125         static final long serialVersionUID =9082979978712223677L;
126         
127         
128         /** Constructor. */
129         public PropertiesEditorDelegate(JTextComponent JavaDoc commentComponent, JTextComponent JavaDoc valueComponent, JLabel JavaDoc valueLabel) {
130             this.commentComponent = commentComponent;
131             this.valueComponent = valueComponent;
132             this.valueLabel = valueLabel;
133         }
134         
135
136         /** Overrides superclass method. */
137         public void setValue(Object JavaDoc x) {
138             // PENDING - due to a compiler error explicitly do "super" code instead of calling super
139
this.value = x;
140             //super.setValue(x);
141
PropertiesTableModel.StringPair sp = (PropertiesTableModel.StringPair)x;
142
143             // set values as they deserve
144
if (sp != null) {
145                 //!!! text area is required due to multiline values
146
// otherwise the textfield removes new lines
147
((JTextField JavaDoc)getComponent()).setText(sp.getValue());
148                 commentComponent.setText(sp.getComment());
149             } else {
150                 ((JTextField JavaDoc)getComponent()).setText(""); // NOI18N
151
commentComponent.setText(""); // NOI18N
152
}
153         }
154
155         /** Overrides superclass method. */
156         public Object JavaDoc getCellEditorValue() {
157             String JavaDoc value = ((JTextField JavaDoc)getComponent()).getText();
158             
159             // Cell is a properties key.
160
if(isKeyCell) {
161                 Mnemonics.setLocalizedText(valueLabel, NbBundle.getBundle(PropertyPanel.class).getString("LBL_KeyLabel"));
162             }
163             // Cell is a properties value.
164
else {
165                 Mnemonics.setLocalizedText(valueLabel, NbBundle.getBundle(BundleEditPanel.class).getString("LBL_ValueLabel"));
166             }
167             
168             // the cell is a properties key
169
return new PropertiesTableModel.StringPair(commentComponent.getText(),value,isKeyCell);
170         }
171
172     } // End of inner PropertiesEditorDelegate class.
173
}
174
Popular Tags