KickJava   Java API By Example, From Geeks To Geeks.

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


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.beans.PropertyChangeEvent JavaDoc;
25 import java.beans.PropertyChangeListener JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.util.ResourceBundle JavaDoc;
28 import javax.swing.JButton JavaDoc;
29 import javax.swing.JPanel JavaDoc;
30 import javax.swing.SwingUtilities JavaDoc;
31 import org.netbeans.api.java.classpath.ClassPath;
32
33 import org.openide.filesystems.FileObject;
34 import org.openide.loaders.DataObject;
35 import org.openide.util.HelpCtx;
36 import org.openide.util.WeakListeners;
37 import org.openide.util.Lookup;
38 import org.netbeans.api.javahelp.Help;
39 import org.netbeans.api.project.Project;
40 import javax.swing.BoxLayout JavaDoc;
41
42
43 /**
44  * Panel which provides GUI for i18n action.
45  * Customizes <code>I18nString</code> object and is used by <code>I18nSupport<code> for i18n-izing
46  * one source.
47  *
48  * @author Peter Zavadsky
49  */

50 public class I18nPanel extends JPanel JavaDoc {
51
52     /** <code>I18nString</code> cusomized in this panel. */
53     private I18nString i18nString;
54     
55     /** Helper bundle used for i18n-zing strings in this source. */
56     private ResourceBundle JavaDoc bundle;
57
58     /** Helper property change support. */
59     private PropertyChangeListener JavaDoc propListener;
60     
61     /** Generated serial version ID. */
62     static final long serialVersionUID =-6982482491017390786L;
63
64     private Project project;
65
66     private FileObject file;
67     
68     static final long ALL_BUTTONS = 0xffffff;
69     static final long NO_BUTTONS = 0x0;
70     static final long REPLACE_BUTTON = 0xf0000;
71     static final long SKIP_BUTTON = 0x0f000;
72     static final long INFO_BUTTON = 0x00f00;
73     static final long CANCEL_BUTTON = 0x000f0;
74     static final long HELP_BUTTON = 0x0000f;
75
76
77     
78     /**
79      * Creates new I18nPanel. In order to correctly localize
80      * classpath for property bundle chooser, the dialog must know the
81      * project and a file to choose the bundle for.
82      * @param propertyPanel panel for customizing i18n strings
83      * @param project the Project to choose bundles from
84      * @param file the FileObject to choose bundles for
85      */

86     public I18nPanel(PropertyPanel propertyPanel, Project project, FileObject file) {
87         this(propertyPanel, true, project, file);
88     }
89
90     /** Creates i18n panel.
91      * @param propertyPanel panel for customizing i18n strings
92      * @param withButtons if panel with replace, skip ect. buttons should be added
93      * @param project the Project to choose bundles from
94      * @param file the FileObject to choose bundles for
95      */

96     public I18nPanel(PropertyPanel propertyPanel, boolean withButtons, Project project, FileObject file) {
97         this.project = project;
98         this.file = file;
99         this.propertyPanel = propertyPanel;
100         this.propertyPanel.setFile(file);
101         this.propertyPanel.setEnabled(project != null);
102
103         // Init bundle.
104
bundle = I18nUtil.getBundle();
105         
106         initComponents();
107         myInitComponents();
108         initAccessibility();
109         
110         if(!withButtons)
111             remove(buttonsPanel);
112         
113         // create empty panel
114
emptyPanel = new EmptyPropertyPanel();
115         contentsShown = true;
116
117         showBundleMessage("TXT_SearchingForStrings");
118     }
119
120
121     private boolean contentsShown;
122
123     public void showBundleMessage(String JavaDoc bundleKey) {
124
125         emptyPanel.setBundleText(bundleKey);
126         if (contentsShown) {
127             contentsPanelPlaceholder.remove(propertyPanel);
128             contentsPanelPlaceholder.add(emptyPanel);
129             contentsPanelPlaceholder.validate();
130             contentsPanelPlaceholder.repaint();
131             contentsShown = false;
132         }
133         buttonsEnableDisable();
134     }
135
136     public void showPropertyPanel() {
137         if (!contentsShown) {
138             contentsPanelPlaceholder.remove(emptyPanel);
139             contentsPanelPlaceholder.add(propertyPanel);
140             contentsPanelPlaceholder.validate();
141             contentsPanelPlaceholder.repaint();
142             contentsShown = true;
143         }
144         buttonsEnableDisable();
145     }
146
147
148     
149     /**
150      * Reset associated project to a new value
151      */

152 // public void setProject(Project project) {
153
//// ((ResourcePanel)resourcePanel).setProject(project);
154
// propertyPanel.setEnabled(project != null);
155
//
156
// }
157
//
158
// public Project getProject() {
159
// return ((ResourcePanel)resourcePanel).getProject();
160
// }
161

162     /**
163      * Sets the file associated with this panel -- the one, which
164      * is localized
165      */

166     public void setFile(FileObject file) {
167 // ((ResourcePanel)resourcePanel).setFile(file);
168
propertyPanel.setFile(file);
169     }
170     
171     /**
172      * Gets the file associated with this panel -- the one, which
173      * is localized
174      */

175     public FileObject getFile() {
176 // return ((ResourcePanel)resourcePanel).getFile();
177
return propertyPanel.getFile();
178     }
179
180     
181     /** Overrides superclass method to set default button. */
182     public void addNotify() {
183         super.addNotify();
184         
185         if(SwingUtilities.isDescendingFrom(replaceButton, this))
186             getRootPane().setDefaultButton(replaceButton);
187     }
188     
189     /** Getter for <code>i18nString</code>. */
190     public I18nString getI18nString() {
191         return i18nString;
192     }
193     
194     /** Setter for i18nString property. */
195     public void setI18nString(I18nString i18nString) {
196         this.i18nString = i18nString;
197
198         propertyPanel.setI18nString(i18nString);
199 // ((ResourcePanel)resourcePanel).setI18nString(i18nString);
200

201         showPropertyPanel();
202     }
203
204     /** Replace button accessor. */
205     JButton JavaDoc getReplaceButton() {
206         return replaceButton;
207     }
208     
209     /** Skip button accessor. */
210     JButton JavaDoc getSkipButton() {
211         return skipButton;
212     }
213
214     /** Info button accessor. */
215     JButton JavaDoc getInfoButton() {
216         return infoButton;
217     }
218     
219     /** Cancel/Close button accessor. */
220     JButton JavaDoc getCancelButton() {
221         return cancelButton;
222     }
223     
224     /** Enables/disables buttons based on the contents of the dialog. */
225     private void buttonsEnableDisable() {
226         if (contentsShown) {
227             enableButtons(ALL_BUTTONS);
228             boolean isBundle = (i18nString != null) && (i18nString.getSupport().getResourceHolder().getResource() != null);
229             boolean keyEmpty = (getI18nString()==null ||
230                                 getI18nString().getKey()==null ||
231                                 getI18nString().getKey().trim().length()==0);
232             replaceButton.setEnabled(isBundle && !keyEmpty);
233         } else
234             enableButtons(CANCEL_BUTTON | HELP_BUTTON);
235         
236     }
237
238     public void setDefaultResource(DataObject dataObject) {
239         if (dataObject != null) {
240             // look for peer Bundle.properties
241
FileObject fo = dataObject.getPrimaryFile();
242             ClassPath cp = ClassPath.getClassPath( fo, ClassPath.SOURCE );
243             
244             FileObject folder = cp.findResource( cp.getResourceName( fo.getParent() ) );
245             
246             while( folder != null && cp.contains( folder ) ) {
247                 
248                 String JavaDoc rn = cp.getResourceName( folder ) + "/Bundle.properties"; // NOI18N
249

250                 FileObject peer = cp.findResource( rn );
251                 if (peer != null) {
252                     try {
253                         DataObject peerDataObject = DataObject.find(peer);
254 // ((ResourcePanel)resourcePanel).setResource(peerDataObject);
255
propertyPanel.setResource(peerDataObject);
256                         return;
257                     } catch (IOException JavaDoc ex) {
258                         // no default resource
259
}
260                 }
261                 folder = folder.getParent();
262             }
263         }
264     }
265     
266     /** Creates <code>ResourcePanel</code>. */
267 // private JPanel createResourcePanel() {
268
// return new ResourcePanel(project, file);
269
// }
270

271     private void initAccessibility() {
272         this.getAccessibleContext().setAccessibleDescription(bundle.getString("ACS_I18nPanel"));
273         skipButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACS_CTL_SkipButton"));
274         cancelButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACS_CTL_CancelButton"));
275         replaceButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACS_CTL_ReplaceButton"));
276         infoButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACS_CTL_InfoButton"));
277         helpButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACS_CTL_HelpButton"));
278     }
279     
280     /** This method is called from within the constructor to
281      * initialize the form.
282      * WARNING: Do NOT modify this code. The content of this method is
283      * always regenerated by the FormEditor.
284      */

285     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
286
private void initComponents() {
287         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
288
289         fillPanel = new javax.swing.JPanel JavaDoc();
290         buttonsPanel = new javax.swing.JPanel JavaDoc();
291         replaceButton = new javax.swing.JButton JavaDoc();
292         skipButton = new javax.swing.JButton JavaDoc();
293         infoButton = new javax.swing.JButton JavaDoc();
294         cancelButton = new javax.swing.JButton JavaDoc();
295         helpButton = new javax.swing.JButton JavaDoc();
296         contentsPanelPlaceholder = new javax.swing.JPanel JavaDoc();
297
298         setLayout(new java.awt.GridBagLayout JavaDoc());
299         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
300         gridBagConstraints.gridx = 0;
301         gridBagConstraints.gridy = 1;
302         gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
303         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
304         gridBagConstraints.weightx = 1.0;
305         add(fillPanel, gridBagConstraints);
306
307         buttonsPanel.setLayout(new java.awt.GridLayout JavaDoc(1, 5, 5, 0));
308
309         org.openide.awt.Mnemonics.setLocalizedText(replaceButton, bundle.getString("CTL_ReplaceButton")); // NOI18N
310
buttonsPanel.add(replaceButton);
311
312         org.openide.awt.Mnemonics.setLocalizedText(skipButton, bundle.getString("CTL_SkipButton")); // NOI18N
313
buttonsPanel.add(skipButton);
314
315         org.openide.awt.Mnemonics.setLocalizedText(infoButton, bundle.getString("CTL_InfoButton")); // NOI18N
316
buttonsPanel.add(infoButton);
317
318         org.openide.awt.Mnemonics.setLocalizedText(cancelButton, bundle.getString("CTL_CloseButton")); // NOI18N
319
buttonsPanel.add(cancelButton);
320
321         org.openide.awt.Mnemonics.setLocalizedText(helpButton, bundle.getString("CTL_HelpButton")); // NOI18N
322
helpButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
323             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
324                 helpButtonActionPerformed(evt);
325             }
326         });
327         buttonsPanel.add(helpButton);
328
329         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
330         gridBagConstraints.gridx = 1;
331         gridBagConstraints.gridy = 1;
332         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
333         gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
334         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
335         gridBagConstraints.insets = new java.awt.Insets JavaDoc(17, 0, 11, 11);
336         add(buttonsPanel, gridBagConstraints);
337
338         contentsPanelPlaceholder.setLayout(new javax.swing.BoxLayout JavaDoc(contentsPanelPlaceholder, javax.swing.BoxLayout.Y_AXIS));
339         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
340         gridBagConstraints.gridx = 0;
341         gridBagConstraints.gridy = 0;
342         gridBagConstraints.gridwidth = 2;
343         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
344         gridBagConstraints.weightx = 1.0;
345         gridBagConstraints.weighty = 1.0;
346         add(contentsPanelPlaceholder, gridBagConstraints);
347     }// </editor-fold>//GEN-END:initComponents
348

349     private void myInitComponents() {
350 // resourcePanel = createResourcePanel();
351
// contentsPanel = new JPanel();
352
// contentsPanel.setLayout(new BoxLayout(contentsPanel, BoxLayout.Y_AXIS));
353

354 // contentsPanel.add(resourcePanel);
355
// contentsPanel.add(propertyPanel);
356
contentsShown = true;
357         contentsPanelPlaceholder.add(propertyPanel);
358         
359       
360         propertyPanel.addPropertyChangeListener(PropertyPanel.PROP_STRING,
361                                                 new PropertyChangeListener JavaDoc() {
362                                                     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
363                                                         buttonsEnableDisable();
364                                                     }
365                                                 });
366         
367         propertyPanel.addPropertyChangeListener(PropertyPanel.PROP_RESOURCE,
368 // WeakListeners.propertyChange(
369
new PropertyChangeListener JavaDoc() {
370                         public void propertyChange(PropertyChangeEvent JavaDoc evt) {
371                             if(PropertyPanel.PROP_RESOURCE.equals(evt.getPropertyName())) {
372                                 buttonsEnableDisable();
373     // ((PropertyPanel)propertyPanel).updateAllValues();
374
}
375                         }
376                     }
377 // },resourcePanel
378
// )
379
);
380         
381     }
382
383   private void helpButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_helpButtonActionPerformed
384
HelpCtx help = new HelpCtx(I18nUtil.HELP_ID_AUTOINSERT);
385       
386       String JavaDoc sysprop = System.getProperty("org.openide.actions.HelpAction.DEBUG"); // NOI18N
387

388       if("true".equals(sysprop) || "full".equals(sysprop)) // NOI18N
389
System.err.println ("I18n module: Help button showing: " + help); // NOI18N, please do not comment out
390

391       Help helpSystem = (Help)Lookup.getDefault().lookup(Help.class);
392       helpSystem.showHelp(help);
393   }//GEN-LAST:event_helpButtonActionPerformed
394

395     private void enableButtons(long buttonMask) {
396         replaceButton.setEnabled((buttonMask & REPLACE_BUTTON) != 0);
397         skipButton.setEnabled((buttonMask & SKIP_BUTTON) != 0);
398         infoButton.setEnabled((buttonMask & INFO_BUTTON) != 0);
399         cancelButton.setEnabled((buttonMask & CANCEL_BUTTON) != 0);
400         helpButton.setEnabled((buttonMask & HELP_BUTTON) != 0);
401     }
402
403         
404
405
406     // Variables declaration - do not modify//GEN-BEGIN:variables
407
private javax.swing.JPanel JavaDoc buttonsPanel;
408     private javax.swing.JButton JavaDoc cancelButton;
409     private javax.swing.JPanel JavaDoc contentsPanelPlaceholder;
410     private javax.swing.JPanel JavaDoc fillPanel;
411     private javax.swing.JButton JavaDoc helpButton;
412     private javax.swing.JButton JavaDoc infoButton;
413     private javax.swing.JButton JavaDoc replaceButton;
414     private javax.swing.JButton JavaDoc skipButton;
415     // End of variables declaration//GEN-END:variables
416

417     private EmptyPropertyPanel emptyPanel;
418 // private javax.swing.JPanel resourcePanel;
419
private PropertyPanel propertyPanel;
420 // private javax.swing.JPanel contentsPanel;
421

422 }
423
Popular Tags