KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > i18n > HelpStringCustomEditor


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.i18n;
22
23
24 import java.awt.Color JavaDoc;
25 import java.awt.Component JavaDoc;
26 import java.awt.event.KeyEvent JavaDoc;
27 import java.awt.SystemColor JavaDoc;
28 import java.util.List JavaDoc;
29 import javax.swing.DefaultComboBoxModel JavaDoc;
30 import javax.swing.JPanel JavaDoc;
31 import javax.swing.JTextField JavaDoc;
32 import javax.swing.text.BadLocationException JavaDoc;
33 import org.netbeans.beaninfo.editors.StringEditor;
34 import org.openide.awt.Mnemonics;
35 import org.openide.explorer.propertysheet.editors.EnhancedCustomPropertyEditor;
36 import org.openide.util.HelpCtx;
37 import org.openide.ErrorManager;
38
39
40 /**
41  * Custom editor for editing string type formats with help pattern descritions.
42  *
43  * @author Peter Zavadsky
44  */

45 public class HelpStringCustomEditor extends JPanel JavaDoc implements EnhancedCustomPropertyEditor {
46
47     /** Creates new form CodeCustomEditor.
48      * @param value value to be customized
49      * @param items for sleecteing in combo box
50      * @param helpItems patterns described in list
51      * @param comboText label for the combo-box, optionally with an ampersand marking the mnemonic character
52      */

53     public HelpStringCustomEditor(String JavaDoc value, List JavaDoc items, List JavaDoc helpItems, String JavaDoc comboText, String JavaDoc helpID) {
54         initComponents();
55         
56         combo.setModel(new DefaultComboBoxModel JavaDoc(items.toArray()));
57         combo.setSelectedItem(value);
58
59         list.setListData(helpItems.toArray());
60         list.setBackground(javax.swing.UIManager.getDefaults().getColor("TextField..disabledBackground")); // NOI18N
61
// list.setBackground(new Color(SystemColor.window.getRGB()));
62

63         Mnemonics.setLocalizedText(comboLabel, comboText);
64         Mnemonics.setLocalizedText(listLabel, I18nUtil.getBundle().getString("LBL_Arguments"));
65         
66         initAccessibility ();
67         
68         HelpCtx.setHelpIDString(this, helpID);
69     }
70
71     /**
72     * @return property value that is result of <code>CodeCustomEditor</code>.
73     * @exception <code>InvalidStateException</code> when the custom property editor does not represent valid property value
74     */

75     public Object JavaDoc getPropertyValue() throws IllegalStateException JavaDoc {
76         return (String JavaDoc)combo.getSelectedItem();
77     }
78     
79     
80     private void initAccessibility() {
81         this.getAccessibleContext().setAccessibleDescription(I18nUtil.getBundle().getString("ACS_HelpStringCustomEditor"));
82         combo.getAccessibleContext().setAccessibleDescription(I18nUtil.getBundle().getString("ACS_HelpStringCombo"));
83         list.getAccessibleContext().setAccessibleDescription(I18nUtil.getBundle().getString("ACS_HelpStringList"));
84     }
85     
86     /** This method is called from within the constructor to
87      * initialize the form.
88      * WARNING: Do NOT modify this code. The content of this method is
89      * always regenerated by the Form Editor.
90      */

91     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
92
private void initComponents() {
93         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
94
95         combo = new javax.swing.JComboBox JavaDoc();
96         scrollPane = new javax.swing.JScrollPane JavaDoc();
97         list = new javax.swing.JList JavaDoc();
98         comboLabel = new javax.swing.JLabel JavaDoc();
99         listLabel = new javax.swing.JLabel JavaDoc();
100
101         setLayout(new java.awt.GridBagLayout JavaDoc());
102
103         combo.setEditable(true);
104         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
105         gridBagConstraints.gridx = 0;
106         gridBagConstraints.gridy = 1;
107         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
108         gridBagConstraints.weightx = 1.0;
109         gridBagConstraints.insets = new java.awt.Insets JavaDoc(5, 12, 0, 11);
110         add(combo, gridBagConstraints);
111
112         list.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
113         list.addKeyListener(new java.awt.event.KeyAdapter JavaDoc() {
114             public void keyPressed(java.awt.event.KeyEvent JavaDoc evt) {
115                 listKeyPressed(evt);
116             }
117         });
118         list.addMouseListener(new java.awt.event.MouseAdapter JavaDoc() {
119             public void mouseClicked(java.awt.event.MouseEvent JavaDoc evt) {
120                 listMouseClicked(evt);
121             }
122         });
123
124         scrollPane.setViewportView(list);
125
126         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
127         gridBagConstraints.gridx = 0;
128         gridBagConstraints.gridy = 3;
129         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
130         gridBagConstraints.weightx = 1.0;
131         gridBagConstraints.weighty = 1.0;
132         gridBagConstraints.insets = new java.awt.Insets JavaDoc(5, 12, 11, 11);
133         add(scrollPane, gridBagConstraints);
134
135         comboLabel.setLabelFor(combo);
136         comboLabel.setText("comboLabel");
137         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
138         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
139         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 12, 0, 11);
140         add(comboLabel, gridBagConstraints);
141
142         listLabel.setLabelFor(list);
143         listLabel.setText("listLabel");
144         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
145         gridBagConstraints.gridx = 0;
146         gridBagConstraints.gridy = 2;
147         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
148         gridBagConstraints.insets = new java.awt.Insets JavaDoc(11, 12, 0, 11);
149         add(listLabel, gridBagConstraints);
150
151     }// </editor-fold>//GEN-END:initComponents
152

153     private void listKeyPressed(java.awt.event.KeyEvent JavaDoc evt) {//GEN-FIRST:event_listKeyPressed
154
String JavaDoc selected = (String JavaDoc)list.getSelectedValue();
155         if(evt.getKeyCode() == KeyEvent.VK_ENTER && selected != null) {
156             evt.consume();
157             insertInFormat(selected);
158         }
159     }//GEN-LAST:event_listKeyPressed
160

161     private void listMouseClicked(java.awt.event.MouseEvent JavaDoc evt) {//GEN-FIRST:event_listMouseClicked
162
String JavaDoc selected = (String JavaDoc)list.getSelectedValue();
163         if(evt.getClickCount() == 2 && selected != null) {
164             insertInFormat(selected);
165         }
166     }//GEN-LAST:event_listMouseClicked
167

168     /** Helper method. */
169     private void insertInFormat(String JavaDoc selected) {
170         int index = selected.indexOf(' ');
171         
172         if(index < 0 || index > selected.length())
173             return;
174         
175         String JavaDoc replace = selected.substring(0, index);
176         
177         JTextField JavaDoc textField = (JTextField JavaDoc)combo.getEditor().getEditorComponent();
178         try {
179             textField.getDocument().insertString(textField.getCaretPosition(), replace, null); // NOI18N
180
} catch(BadLocationException JavaDoc ble) {
181             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ble);
182         }
183     }
184
185     // Variables declaration - do not modify//GEN-BEGIN:variables
186
private javax.swing.JComboBox JavaDoc combo;
187     private javax.swing.JLabel JavaDoc comboLabel;
188     private javax.swing.JList JavaDoc list;
189     private javax.swing.JLabel JavaDoc listLabel;
190     private javax.swing.JScrollPane JavaDoc scrollPane;
191     // End of variables declaration//GEN-END:variables
192

193     /** Nested class. <code>PropertyEditor</code>.
194      * @see I18nOptions#PROP_INIT_JAVA_CODE */

195     public static class InitCodeEditor extends StringEditor {
196         /** Overrides superclass method. */
197         public Component JavaDoc getCustomEditor() {
198             return new HelpStringCustomEditor(
199                 (String JavaDoc)getValue(),
200                 I18nUtil.getInitFormatItems(),
201                 I18nUtil.getInitHelpItems(),
202                 I18nUtil.getBundle().getString("LBL_InitCodeFormat"),
203                 I18nUtil.PE_BUNDLE_CODE_HELP_ID
204             );
205         }
206     } // End of nested class InitCodeEditor.
207

208     /** Nested class. <code>PropertyEditor</code>.
209      * @see I18nOptions#PROP_INIT_REPLACE_CODE */

210     public static class ReplaceCodeEditor extends StringEditor {
211         /** Overrides superclass method. */
212         public Component JavaDoc getCustomEditor() {
213             return new HelpStringCustomEditor(
214                 (String JavaDoc)getValue(),
215                 I18nUtil.getReplaceFormatItems(),
216                 I18nUtil.getReplaceHelpItems(),
217                 I18nUtil.getBundle().getString("LBL_ReplaceCodeFormat"),
218                 I18nUtil.PE_REPLACE_CODE_HELP_ID
219             );
220         }
221     } // End of nested class ReplaceCodeEditor.
222

223     /** Nested class. <code>PropertyEditor</code>.
224      * @see I18nOptions#PROP_REGULAR_EXPRESSION */

225     public static class RegExpEditor extends StringEditor {
226         /** Overrides superclass method. */
227         public Component JavaDoc getCustomEditor() {
228             return new HelpStringCustomEditor(
229                 (String JavaDoc)getValue(),
230                 I18nUtil.getRegExpItems(),
231                 I18nUtil.getRegExpHelpItems(),
232                 I18nUtil.getBundle().getString("LBL_NonI18nRegExpFormat"),
233                 I18nUtil.PE_I18N_REGEXP_HELP_ID
234             );
235         }
236     } // End of nested class RegExpEditor.
237

238     /** Nested class. <code>PropertyEditor</code>.
239      * @see I18nOptions#PROP_I18N_REGULAR_EXPRESSION */

240     public static class I18nRegExpEditor extends StringEditor {
241         /** Overrides superclass method. */
242         public Component JavaDoc getCustomEditor() {
243             return new HelpStringCustomEditor(
244                 (String JavaDoc)getValue(),
245                 I18nUtil.getI18nRegExpItems(),
246                 I18nUtil.getRegExpHelpItems(),
247                 I18nUtil.getBundle().getString("LBL_I18nRegExpFormat"),
248                 I18nUtil.PE_TEST_REGEXP_HELP_ID
249             );
250         }
251     } // End of nested class I18nRegExpEditor.
252

253 }
254
Popular Tags