KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.IOException JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Locale JavaDoc;
30 import javax.swing.DefaultListCellRenderer JavaDoc;
31 import javax.swing.JLabel JavaDoc;
32 import javax.swing.JList JavaDoc;
33 import javax.swing.JPanel JavaDoc;
34 import javax.swing.Icon JavaDoc;
35 import javax.swing.ImageIcon JavaDoc;
36 import org.openide.util.HelpCtx;
37 import org.openide.util.NbBundle;
38 import org.openide.util.Utilities;
39
40
41 /**
42  * Customizer for bundle node (<code>PropertiesDataNode</code>) which represents
43  * bundle of .properties files with same basic name and set of Locales.
44  *
45  * @author Peter Zavadsky
46  * @see PropertiesDataNode
47  * @see PropertiesDataObject
48  */

49 public class BundleNodeCustomizer extends JPanel JavaDoc {
50     
51     /** Properties data object to customize. */
52     private PropertiesDataObject propDataObject;
53
54     /** Creates new form BundleNodeCustomizer */
55     public BundleNodeCustomizer(PropertiesDataObject propDataObject) {
56         this.propDataObject = propDataObject;
57         
58         initComponents();
59         initAccessibility();
60         
61         nameText.setText(propDataObject.getNodeDelegate().getName());
62
63         localesList.setListData(retrieveLocales(propDataObject));
64         
65         removeLocales.setEnabled(false);
66         
67         HelpCtx.setHelpIDString(this, Util.HELP_ID_ADDLOCALE);
68     }
69
70     /** Utility method. Gets icon for key item in key list. */
71     private static Icon JavaDoc getLocaleIcon() {
72         return new ImageIcon JavaDoc(Utilities.loadImage("org/netbeans/modules/properties/propertiesLocale.gif")); // NOI18N
73
}
74     
75     /** Retrieves entry locales. Utility method.
76      * @param propDataObject properties data object to retrieve entry names for */

77     private static Locale JavaDoc[] retrieveLocales(PropertiesDataObject propDataObject) {
78         List JavaDoc<Locale JavaDoc> entryList = new ArrayList JavaDoc<Locale JavaDoc>();
79
80         entryList.add(LocaleNodeCustomizer.getLocale((PropertiesFileEntry)propDataObject.getPrimaryEntry()));
81         
82         for (Iterator JavaDoc it = propDataObject.secondaryEntries().iterator(); it.hasNext(); ) {
83             entryList.add(LocaleNodeCustomizer.getLocale((PropertiesFileEntry)it.next()));
84         }
85         
86         Locale JavaDoc[] entryLocales = new Locale JavaDoc[entryList.size()];
87         entryList.toArray(entryLocales);
88         
89         return entryLocales;
90     }
91
92     
93     private void initAccessibility() {
94         this.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(LocaleNodeCustomizer.class).getString("ACS_BundleNodeCustomizer"));
95                  
96         nameText.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(LocaleNodeCustomizer.class).getString("ACS_CTL_NameText"));
97         localesList.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(LocaleNodeCustomizer.class).getString("ACS_CTL_LocalesList"));
98         addLocale.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(LocaleNodeCustomizer.class).getString("ACS_CTL_AddLocale"));
99         removeLocales.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(LocaleNodeCustomizer.class).getString("ACS_CTL_RemoveLocale"));
100     }
101     
102     /** This method is called from within the constructor to
103      * initialize the form.
104      * WARNING: Do NOT modify this code. The content of this method is
105      * always regenerated by the Form Editor.
106      */

107     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
108
private void initComponents() {
109         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
110
111         nameLabel = new javax.swing.JLabel JavaDoc();
112         nameText = new javax.swing.JTextField JavaDoc();
113         localesLabel = new javax.swing.JLabel JavaDoc();
114         jScrollPane1 = new javax.swing.JScrollPane JavaDoc();
115         localesList = new javax.swing.JList JavaDoc();
116         addLocale = new javax.swing.JButton JavaDoc();
117         removeLocales = new javax.swing.JButton JavaDoc();
118
119         setLayout(new java.awt.GridBagLayout JavaDoc());
120
121         nameLabel.setLabelFor(nameText);
122         org.openide.awt.Mnemonics.setLocalizedText(nameLabel, NbBundle.getBundle(LocaleNodeCustomizer.class).getString("LBL_Name")); // NOI18N
123
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
124         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
125         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 12, 0, 0);
126         add(nameLabel, gridBagConstraints);
127
128         nameText.addActionListener(new java.awt.event.ActionListener JavaDoc() {
129             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
130                 nameTextActionPerformed(evt);
131             }
132         });
133         nameText.addFocusListener(new java.awt.event.FocusAdapter JavaDoc() {
134             public void focusLost(java.awt.event.FocusEvent JavaDoc evt) {
135                 nameTextFocusLost(evt);
136             }
137         });
138         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
139         gridBagConstraints.gridwidth = 2;
140         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
141         gridBagConstraints.weightx = 1.0;
142         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 12, 0, 11);
143         add(nameText, gridBagConstraints);
144
145         localesLabel.setLabelFor(localesList);
146         org.openide.awt.Mnemonics.setLocalizedText(localesLabel, NbBundle.getBundle(LocaleNodeCustomizer.class).getString("LBL_Locales")); // NOI18N
147
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
148         gridBagConstraints.gridx = 0;
149         gridBagConstraints.gridy = 1;
150         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
151         gridBagConstraints.insets = new java.awt.Insets JavaDoc(11, 12, 0, 0);
152         add(localesLabel, gridBagConstraints);
153
154         localesList.setCellRenderer(new DefaultListCellRenderer JavaDoc() {
155             public Component JavaDoc getListCellRendererComponent(
156                 JList JavaDoc list,
157                 Object JavaDoc value, // value to display
158
int index, // cell index
159
boolean isSelected, // is the cell selected
160
boolean cellHasFocus) // the list and the cell have the focus
161
{
162                 JLabel JavaDoc label = (JLabel JavaDoc)super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
163
164                 if(value instanceof String JavaDoc)
165                 return label;
166
167                 Locale JavaDoc locale = (Locale JavaDoc)value;
168
169                 if(locale.equals(new Locale JavaDoc("", ""))) // NOI18N
170
label.setText(NbBundle.getBundle(BundleNodeCustomizer.class).getString("LAB_defaultLanguage"));//NOI18N
171
else {
172                     label.setText(locale.toString() +
173                         (locale.getLanguage().equals("") ? "" : " - " + locale.getDisplayLanguage()) + // NOI18N
174
(locale.getCountry().equals("") ? "" : " / " + locale.getDisplayCountry()) + // NOI18N
175
(locale.getVariant().equals("") ? "" : " / " + locale.getDisplayVariant()) // NOI18N
176
);
177                 }
178
179                 label.setIcon(getLocaleIcon());
180
181                 return label;
182             }
183         });
184         localesList.setPrototypeCellValue("0123456789012345678901234567890123456789");
185         localesList.addListSelectionListener(new javax.swing.event.ListSelectionListener JavaDoc() {
186             public void valueChanged(javax.swing.event.ListSelectionEvent JavaDoc evt) {
187                 localesListValueChanged(evt);
188             }
189         });
190         jScrollPane1.setViewportView(localesList);
191
192         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
193         gridBagConstraints.gridx = 1;
194         gridBagConstraints.gridy = 1;
195         gridBagConstraints.gridheight = 2;
196         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
197         gridBagConstraints.weightx = 1.0;
198         gridBagConstraints.weighty = 1.0;
199         gridBagConstraints.insets = new java.awt.Insets JavaDoc(11, 12, 5, 0);
200         add(jScrollPane1, gridBagConstraints);
201
202         org.openide.awt.Mnemonics.setLocalizedText(addLocale, NbBundle.getBundle(LocaleNodeCustomizer.class).getString("CTL_AddLocale")); // NOI18N
203
addLocale.addActionListener(new java.awt.event.ActionListener JavaDoc() {
204             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
205                 addLocaleActionPerformed(evt);
206             }
207         });
208         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
209         gridBagConstraints.gridx = 2;
210         gridBagConstraints.gridy = 1;
211         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
212         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
213         gridBagConstraints.insets = new java.awt.Insets JavaDoc(11, 11, 0, 11);
214         add(addLocale, gridBagConstraints);
215
216         org.openide.awt.Mnemonics.setLocalizedText(removeLocales, NbBundle.getBundle(LocaleNodeCustomizer.class).getString("CTL_RemoveLocale")); // NOI18N
217
removeLocales.addActionListener(new java.awt.event.ActionListener JavaDoc() {
218             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
219                 removeLocalesActionPerformed(evt);
220             }
221         });
222         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
223         gridBagConstraints.gridx = 2;
224         gridBagConstraints.gridy = 2;
225         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
226         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
227         gridBagConstraints.weighty = 1.0;
228         gridBagConstraints.insets = new java.awt.Insets JavaDoc(5, 11, 5, 11);
229         add(removeLocales, gridBagConstraints);
230     }// </editor-fold>//GEN-END:initComponents
231

232     private void localesListValueChanged(javax.swing.event.ListSelectionEvent JavaDoc evt) {//GEN-FIRST:event_localesListValueChanged
233
if(localesList.isSelectionEmpty()
234             || new Locale JavaDoc("", "").equals(localesList.getSelectedValue()))
235             
236             removeLocales.setEnabled(false);
237         else
238             removeLocales.setEnabled(true);
239     }//GEN-LAST:event_localesListValueChanged
240

241     private void removeLocalesActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_removeLocalesActionPerformed
242
Object JavaDoc[] selectedValues = localesList.getSelectedValues();
243
244         String JavaDoc basicName = propDataObject.getPrimaryFile().getName();
245         
246         for(int i=0; i<selectedValues.length; i++) {
247             PropertiesFileEntry entry = propDataObject.getBundleStructure().getEntryByFileName(basicName + PropertiesDataLoader.PRB_SEPARATOR_CHAR + selectedValues[i].toString());
248             try {
249                 entry.delete();
250             } catch(IOException JavaDoc ioe) {
251                 org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.INFORMATIONAL, ioe);
252             }
253         }
254         
255         localesList.setListData(retrieveLocales(propDataObject));
256     }//GEN-LAST:event_removeLocalesActionPerformed
257

258     private void addLocaleActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_addLocaleActionPerformed
259
try {
260             propDataObject.getNodeDelegate().getNewTypes()[0].create();
261         } catch(IOException JavaDoc ioe) {
262             org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.INFORMATIONAL, ioe);
263         }
264         
265         localesList.setListData(retrieveLocales(propDataObject));
266     }//GEN-LAST:event_addLocaleActionPerformed
267

268     private void nameTextFocusLost(java.awt.event.FocusEvent JavaDoc evt) {//GEN-FIRST:event_nameTextFocusLost
269
nameTextHandler();
270     }//GEN-LAST:event_nameTextFocusLost
271

272     private void nameTextActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_nameTextActionPerformed
273
nameTextHandler();
274     }//GEN-LAST:event_nameTextActionPerformed
275

276     /** Name text field event handler delegate. */
277     private void nameTextHandler() {
278         String JavaDoc newName = nameText.getText();
279         
280         if(newName == null || "".equals(newName)) // NOI18N
281
return;
282         
283         propDataObject.getNodeDelegate().setName(newName);
284     }
285
286     // Variables declaration - do not modify//GEN-BEGIN:variables
287
private javax.swing.JButton JavaDoc addLocale;
288     private javax.swing.JScrollPane JavaDoc jScrollPane1;
289     private javax.swing.JLabel JavaDoc localesLabel;
290     private javax.swing.JList JavaDoc localesList;
291     private javax.swing.JLabel JavaDoc nameLabel;
292     private javax.swing.JTextField JavaDoc nameText;
293     private javax.swing.JButton JavaDoc removeLocales;
294     // End of variables declaration//GEN-END:variables
295

296 }
297
Popular Tags