KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.awt.Dialog JavaDoc;
26 import java.awt.event.ActionEvent JavaDoc;
27 import java.awt.event.ActionListener JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Locale JavaDoc;
33 import javax.swing.DefaultListCellRenderer JavaDoc;
34 import javax.swing.Icon JavaDoc;
35 import javax.swing.ImageIcon JavaDoc;
36 import javax.swing.JLabel JavaDoc;
37 import javax.swing.JList JavaDoc;
38 import javax.swing.JPanel JavaDoc;
39 import javax.swing.SwingUtilities JavaDoc;
40 import org.openide.DialogDescriptor;
41 import org.openide.DialogDisplayer;
42 import org.openide.util.HelpCtx;
43 import org.openide.util.NbBundle;
44 import org.openide.util.RequestProcessor;
45 import org.openide.util.Utilities;
46
47
48 /**
49  * Customizer for locale node (<code>PropertiesLocaleNode</code>).
50  *
51  * @author Peter Zavadsky
52  * @see PropertiesLocaleNode
53  */

54 public class LocaleNodeCustomizer extends JPanel JavaDoc {
55
56     /** Customized properties file entry which represents one properties file. */
57     private PropertiesFileEntry entry;
58
59     /** Creates new <code>LocaleNodeCustomizer</code>. */
60     public LocaleNodeCustomizer(PropertiesFileEntry entry) {
61         this.entry = entry;
62     
63         initComponents();
64         initAccessibility();
65         
66         Locale JavaDoc locale = getLocale(entry);
67         
68         if(new Locale JavaDoc("", "").equals(locale)) { // NOI18N
69
changeNameButton.setEnabled(false);
70             nameText.setText(NbBundle.getBundle(LocaleNodeCustomizer.class).getString("LAB_defaultLanguage"));//NOI18N
71
} else
72             nameText.setText(locale.toString());
73         
74         removeKeyButton.setEnabled(false);
75         
76         HelpCtx.setHelpIDString(this, Util.HELP_ID_EDITLOCALE);
77     }
78
79     /** Updates name of the <code>entry</code>. */
80     private void updateName(Locale JavaDoc locale) {
81         // Don't rename to "Default" locale node or to the same one.
82
if(locale.equals(new Locale JavaDoc("", "")) || locale.equals(getLocale(entry)) )
83             return;
84
85         String JavaDoc newName = Util.assembleName(
86             entry.getDataObject().getPrimaryFile().getName(),
87             locale.toString()
88         );
89
90         entry.getNodeDelegate().setName(newName);
91         
92         nameText.setText(locale.toString());
93     }
94
95     /** Utility method. Gets icon for key item in key list. */
96     private static Icon JavaDoc getKeyIcon() {
97         return new ImageIcon JavaDoc(Utilities.loadImage("org/netbeans/modules/properties/propertiesKey.gif")); // NOI18N
98
}
99     
100     /** Gets locale which represents the entry. Utility method.
101      * @param entry entry which <code>Locale</code> to get */

102     static Locale JavaDoc getLocale(PropertiesFileEntry entry) {
103         String JavaDoc localeSuffix = Util.getLocaleSuffix(entry);
104         String JavaDoc languageCode = Util.getLanguage(localeSuffix);
105         
106         if(languageCode == null)
107             return new Locale JavaDoc("", ""); // NOI18N
108

109         String JavaDoc countryCode = Util.getCountry(localeSuffix);
110         
111         if(countryCode == null)
112             return new Locale JavaDoc(languageCode, ""); // NOI18N
113

114         String JavaDoc variant = Util.getVariant(localeSuffix);
115         
116         if(variant == null)
117             return new Locale JavaDoc(languageCode, countryCode);
118         
119         return new Locale JavaDoc(languageCode, countryCode, variant);
120     }
121
122     /** Retrievs keys in entry. Utility method.
123      * @param entry entry which keys to get */

124     private static String JavaDoc[] retrieveKeys(PropertiesFileEntry entry) {
125         List JavaDoc<String JavaDoc> keysList = new ArrayList JavaDoc<String JavaDoc>();
126         
127         if(entry == null) {
128             return new String JavaDoc[0];
129         }
130         
131         for (Iterator JavaDoc<Element.ItemElem> it = entry.getHandler().getStructure().allItems(); it.hasNext(); ) {
132             String JavaDoc key = it.next().getKey();
133             if (key != null && !(keysList.contains(key))) {
134                 keysList.add(key);
135             }
136         }
137         
138         String JavaDoc[] keys = new String JavaDoc[keysList.size()];
139         keysList.toArray(keys);
140         return keys;
141     }
142     
143     
144     private void initAccessibility() {
145         this.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(LocaleNodeCustomizer.class).getString("ACS_LocaleNodeCustomizer"));
146                 
147         nameText.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(LocaleNodeCustomizer.class).getString("ACS_CTL_NameText"));
148         addKeyButton.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(LocaleNodeCustomizer.class).getString("ACS_CTL_AddKey"));
149         changeNameButton.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(LocaleNodeCustomizer.class).getString("ACS_CTL_ChangeNameButton"));
150         removeKeyButton.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(LocaleNodeCustomizer.class).getString("ACS_CTL_RemoveKey"));
151         keyList.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(LocaleNodeCustomizer.class).getString("ACS_CTL_KeyList"));
152     }
153     
154     /** This method is called from within the constructor to
155      * initialize the form.
156      * WARNING: Do NOT modify this code. The content of this method is
157      * always regenerated by the Form Editor.
158      */

159     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
160
private void initComponents() {
161         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
162
163         nameLabel = new javax.swing.JLabel JavaDoc();
164         keyLabel = new javax.swing.JLabel JavaDoc();
165         jScrollPane1 = new javax.swing.JScrollPane JavaDoc();
166         keyList = new JList JavaDoc(retrieveKeys(entry));
167         addKeyButton = new javax.swing.JButton JavaDoc();
168         removeKeyButton = new javax.swing.JButton JavaDoc();
169         jPanel1 = new javax.swing.JPanel JavaDoc();
170         nameText = new javax.swing.JTextField JavaDoc();
171         changeNameButton = new javax.swing.JButton JavaDoc();
172
173         setLayout(new java.awt.GridBagLayout JavaDoc());
174
175         nameLabel.setLabelFor(nameText);
176         org.openide.awt.Mnemonics.setLocalizedText(nameLabel, NbBundle.getBundle(LocaleNodeCustomizer.class).getString("LBL_Name")); // NOI18N
177
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
178         gridBagConstraints.gridx = 0;
179         gridBagConstraints.gridy = 0;
180         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
181         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 12, 0, 0);
182         add(nameLabel, gridBagConstraints);
183
184         keyLabel.setLabelFor(keyList);
185         org.openide.awt.Mnemonics.setLocalizedText(keyLabel, NbBundle.getBundle(LocaleNodeCustomizer.class).getString("LBL_Keys")); // NOI18N
186
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
187         gridBagConstraints.gridx = 0;
188         gridBagConstraints.gridy = 1;
189         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
190         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
191         gridBagConstraints.insets = new java.awt.Insets JavaDoc(11, 12, 0, 0);
192         add(keyLabel, gridBagConstraints);
193
194         keyList.setCellRenderer(new DefaultListCellRenderer JavaDoc() {
195             public Component JavaDoc getListCellRendererComponent(
196                 JList JavaDoc list,
197                 Object JavaDoc value, // value to display
198
int index, // cell index
199
boolean isSelected, // is the cell selected
200
boolean cellHasFocus) // the list and the cell have the focus
201
{
202                 JLabel JavaDoc label = (JLabel JavaDoc)super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
203
204                 label.setText(value.toString());
205
206                 label.setIcon(getKeyIcon());
207
208                 return label;
209             }
210         });
211         keyList.setPrototypeCellValue("0123456789012345678901234567890123456789");
212         keyList.addListSelectionListener(new javax.swing.event.ListSelectionListener JavaDoc() {
213             public void valueChanged(javax.swing.event.ListSelectionEvent JavaDoc evt) {
214                 keyListValueChanged(evt);
215             }
216         });
217         jScrollPane1.setViewportView(keyList);
218
219         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
220         gridBagConstraints.gridx = 1;
221         gridBagConstraints.gridy = 1;
222         gridBagConstraints.gridheight = 2;
223         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
224         gridBagConstraints.weightx = 1.0;
225         gridBagConstraints.weighty = 1.0;
226         gridBagConstraints.insets = new java.awt.Insets JavaDoc(11, 11, 5, 0);
227         add(jScrollPane1, gridBagConstraints);
228
229         org.openide.awt.Mnemonics.setLocalizedText(addKeyButton, NbBundle.getBundle(LocaleNodeCustomizer.class).getString("CTL_AddKey")); // NOI18N
230
addKeyButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
231             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
232                 addKeyButtonActionPerformed(evt);
233             }
234         });
235         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
236         gridBagConstraints.gridx = 2;
237         gridBagConstraints.gridy = 1;
238         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
239         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHEAST;
240         gridBagConstraints.insets = new java.awt.Insets JavaDoc(11, 11, 0, 11);
241         add(addKeyButton, gridBagConstraints);
242
243         org.openide.awt.Mnemonics.setLocalizedText(removeKeyButton, NbBundle.getBundle(LocaleNodeCustomizer.class).getString("CTL_RemoveKey")); // NOI18N
244
removeKeyButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
245             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
246                 removeKeyButtonActionPerformed(evt);
247             }
248         });
249         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
250         gridBagConstraints.gridx = 2;
251         gridBagConstraints.gridy = 2;
252         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
253         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHEAST;
254         gridBagConstraints.insets = new java.awt.Insets JavaDoc(5, 11, 5, 11);
255         add(removeKeyButton, gridBagConstraints);
256
257         jPanel1.setLayout(new java.awt.GridBagLayout JavaDoc());
258
259         nameText.setEditable(false);
260         nameText.selectAll();
261         nameText.addFocusListener(new java.awt.event.FocusAdapter JavaDoc() {
262             public void focusGained(java.awt.event.FocusEvent JavaDoc evt) {
263                 nameTextFocusGained(evt);
264             }
265         });
266         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
267         gridBagConstraints.gridx = 0;
268         gridBagConstraints.gridy = 0;
269         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
270         gridBagConstraints.weightx = 1.0;
271         jPanel1.add(nameText, gridBagConstraints);
272
273         changeNameButton.setText("...");
274         changeNameButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
275             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
276                 changeNameButtonActionPerformed(evt);
277             }
278         });
279         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
280         gridBagConstraints.gridx = 1;
281         gridBagConstraints.gridy = 0;
282         gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
283         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 5, 0, 0);
284         jPanel1.add(changeNameButton, gridBagConstraints);
285
286         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
287         gridBagConstraints.gridwidth = 2;
288         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
289         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 12, 0, 11);
290         add(jPanel1, gridBagConstraints);
291     }// </editor-fold>//GEN-END:initComponents
292

293     private void nameTextFocusGained(java.awt.event.FocusEvent JavaDoc evt) {//GEN-FIRST:event_nameTextFocusGained
294
// Accessibility
295
nameText.selectAll();
296     }//GEN-LAST:event_nameTextFocusGained
297

298     private void changeNameButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_changeNameButtonActionPerformed
299
final Dialog JavaDoc[] dialog = new Dialog JavaDoc[1];
300         final LocalePanel panel = new LocalePanel(getLocale(entry));
301
302         DialogDescriptor dd = new DialogDescriptor(
303             panel,
304             NbBundle.getBundle(PropertiesDataNode.class).getString("CTL_NewLocaleTitle"),
305             true,
306             DialogDescriptor.OK_CANCEL_OPTION,
307             DialogDescriptor.OK_OPTION,
308             new ActionListener JavaDoc() {
309                 public void actionPerformed(ActionEvent JavaDoc evt2) {
310                     // OK pressed
311
if (evt2.getSource() == DialogDescriptor.OK_OPTION) {
312                         dialog[0].setVisible(false);
313                         dialog[0].dispose();
314
315                         updateName(panel.getLocale());
316                     // Cancel pressed
317
} else if (evt2.getSource() == DialogDescriptor.CANCEL_OPTION) {
318                         dialog[0].setVisible(false);
319                         dialog[0].dispose();
320                     }
321                 }
322             }
323         );
324         dialog[0] = DialogDisplayer.getDefault().createDialog(dd);
325         dialog[0].setVisible(true);
326     }//GEN-LAST:event_changeNameButtonActionPerformed
327

328     private void keyListValueChanged(javax.swing.event.ListSelectionEvent JavaDoc evt) {//GEN-FIRST:event_keyListValueChanged
329
if(keyList.isSelectionEmpty())
330             removeKeyButton.setEnabled(false);
331         else
332             removeKeyButton.setEnabled(true);
333     }//GEN-LAST:event_keyListValueChanged
334

335     private void removeKeyButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_removeKeyButtonActionPerformed
336
Object JavaDoc[] selectedValues = keyList.getSelectedValues();
337
338         PropertiesStructure ps = entry.getHandler().getStructure();
339         
340         for(int i=0; i<selectedValues.length; i++) {
341             ps.deleteItem((String JavaDoc)selectedValues[i]);
342         }
343         
344         updateKeyList();
345     }//GEN-LAST:event_removeKeyButtonActionPerformed
346

347     private void addKeyButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_addKeyButtonActionPerformed
348
try {
349             entry.getNodeDelegate().getNewTypes()[0].create();
350
351             updateKeyList();
352         } catch(IOException JavaDoc ioe) {
353             org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.INFORMATIONAL, ioe);
354         }
355     }//GEN-LAST:event_addKeyButtonActionPerformed
356

357     /** Updates keys. Utility method. */
358     private void updateKeyList() {
359         // Very ugly, but now there is incosistency gap in the properties structure.
360
// REmove threads when changed parsing.
361
PropertiesRequestProcessor.getInstance().post(new Runnable JavaDoc() {
362             public void run() {
363                 SwingUtilities.invokeLater(new Runnable JavaDoc() {
364                     public void run() {
365                         // Update keys.
366
keyList.setListData(retrieveKeys(entry));
367                     }
368                 });
369             }
370         });
371     }
372
373     
374     // Variables declaration - do not modify//GEN-BEGIN:variables
375
private javax.swing.JButton JavaDoc addKeyButton;
376     private javax.swing.JButton JavaDoc changeNameButton;
377     private javax.swing.JPanel JavaDoc jPanel1;
378     private javax.swing.JScrollPane JavaDoc jScrollPane1;
379     private javax.swing.JLabel JavaDoc keyLabel;
380     private javax.swing.JList JavaDoc keyList;
381     private javax.swing.JLabel JavaDoc nameLabel;
382     private javax.swing.JTextField JavaDoc nameText;
383     private javax.swing.JButton JavaDoc removeKeyButton;
384     // End of variables declaration//GEN-END:variables
385

386 }
387
Popular Tags