2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
43
package org.netbeans.modules.i18n;
46
import java.beans.PropertyChangeEvent;
47
import java.beans.PropertyChangeListener;
48
import java.io.IOException;
49
import java.util.ResourceBundle;
50
import javax.swing.JButton;
51
import javax.swing.JPanel;
52
import javax.swing.SwingUtilities;
53
import org.netbeans.api.java.classpath.ClassPath;
55
import org.openide.filesystems.FileObject;
56
import org.openide.loaders.DataObject;
57
import org.openide.util.HelpCtx;
58
import org.openide.util.Lookup;
59
import org.netbeans.api.javahelp.Help;
60
import org.netbeans.api.project.Project;
64
* Panel which provides GUI for i18n action.
65
* Customizes {@code I18nString} object and is used by {@code I18nSupport} for i18n-izing
68
* @author Peter Zavadsky
70
public class I18nPanel extends JPanel {
72
/** {@code I18nString} cusomized in this panel. */
73
private I18nString i18nString;
75
/** Helper bundle used for i18n-zing strings in this source. */
76
private ResourceBundle bundle;
78
/** Helper property change support. */
79
private PropertyChangeListener propListener;
81
/** Generated serial version ID. */
82
static final long serialVersionUID =-6982482491017390786L;
84
private Project project;
86
private FileObject file;
88
static final long ALL_BUTTONS = 0xffffff;
89
static final long NO_BUTTONS = 0x0;
90
static final long REPLACE_BUTTON = 0xf0000;
91
static final long SKIP_BUTTON = 0x0f000;
92
static final long INFO_BUTTON = 0x00f00;
93
static final long CANCEL_BUTTON = 0x000f0;
94
static final long HELP_BUTTON = 0x0000f;
99
* Creates new I18nPanel. In order to correctly localize
100
* classpath for property bundle chooser, the dialog must know the
101
* project and a file to choose the bundle for.
103
* @param propertyPanel panel for customizing i18n strings
104
* @param project the Project to choose bundles from
105
* @param file the FileObject to choose bundles for
107
public I18nPanel(PropertyPanel propertyPanel, Project project, FileObject file) {
108
this(propertyPanel, true, project, file);
112
* Creates i18n panel.
114
* @param propertyPanel panel for customizing i18n strings
115
* @param withButtons if panel with replace, skip ect. buttons should be added
116
* @param project the Project to choose bundles from
117
* @param file the FileObject to choose bundles for
119
public I18nPanel(PropertyPanel propertyPanel, boolean withButtons, Project project, FileObject file) {
120
this.project = project;
122
this.propertyPanel = propertyPanel;
123
this.propertyPanel.setFile(file);
124
this.propertyPanel.setEnabled(project != null);
127
bundle = I18nUtil.getBundle();
134
remove(buttonsPanel);
137
// create empty panel
138
emptyPanel = new EmptyPropertyPanel();
139
contentsShown = true;
141
showBundleMessage("TXT_SearchingForStrings"); //NOI18N
145
private boolean contentsShown;
147
public void showBundleMessage(String bundleKey) {
149
emptyPanel.setBundleText(bundleKey);
151
contentsPanelPlaceholder.remove(propertyPanel);
152
contentsPanelPlaceholder.add(emptyPanel);
153
contentsPanelPlaceholder.validate();
154
contentsPanelPlaceholder.repaint();
155
contentsShown = false;
157
buttonsEnableDisable();
160
public void showPropertyPanel() {
161
if (!contentsShown) {
162
contentsPanelPlaceholder.remove(emptyPanel);
163
contentsPanelPlaceholder.add(propertyPanel);
164
contentsPanelPlaceholder.validate();
165
contentsPanelPlaceholder.repaint();
166
contentsShown = true;
168
buttonsEnableDisable();
174
* Reset associated project to a new value
176
// public void setProject(Project project) {
177
//// ((ResourcePanel)resourcePanel).setProject(project);
178
// propertyPanel.setEnabled(project != null);
182
// public Project getProject() {
183
// return ((ResourcePanel)resourcePanel).getProject();
187
* Sets the file associated with this panel -- the one, which
190
public void setFile(FileObject file) {
191
// ((ResourcePanel)resourcePanel).setFile(file);
192
propertyPanel.setFile(file);
196
* Gets the file associated with this panel -- the one, which
199
public FileObject getFile() {
200
// return ((ResourcePanel)resourcePanel).getFile();
201
return propertyPanel.getFile();
205
/** Overrides superclass method to set default button. */
207
public void addNotify() {
210
if (SwingUtilities.isDescendingFrom(replaceButton, this)) {
211
getRootPane().setDefaultButton(replaceButton);
215
/** Getter for <code>i18nString</code>. */
216
public I18nString getI18nString() {
220
/** Setter for i18nString property. */
221
public void setI18nString(I18nString i18nString) {
222
this.i18nString = i18nString;
224
propertyPanel.setI18nString(i18nString);
225
// ((ResourcePanel)resourcePanel).setI18nString(i18nString);
230
/** Replace button accessor. */
231
JButton getReplaceButton() {
232
return replaceButton;
235
/** Skip button accessor. */
236
JButton getSkipButton() {
240
/** Info button accessor. */
241
JButton getInfoButton() {
245
/** Cancel/Close button accessor. */
246
JButton getCancelButton() {
250
/** Enables/disables buttons based on the contents of the dialog. */
251
private void buttonsEnableDisable() {
253
enableButtons(ALL_BUTTONS);
254
boolean isBundle = (i18nString != null)
255
&& (i18nString.getSupport().getResourceHolder().getResource() != null);
256
boolean keyEmpty = (getI18nString() == null)
257
|| (getI18nString().getKey() == null)
258
|| (getI18nString().getKey().trim().length() == 0);
259
replaceButton.setEnabled(isBundle && !keyEmpty);
261
enableButtons(CANCEL_BUTTON | HELP_BUTTON);
265
public void setDefaultResource(DataObject dataObject) {
266
if (dataObject != null) {
267
// look for peer Bundle.properties
268
FileObject fo = dataObject.getPrimaryFile();
269
ClassPath cp = ClassPath.getClassPath(fo, ClassPath.SOURCE);
271
FileObject folder = cp.findResource(cp.getResourceName(fo.getParent()));
273
while (folder != null && cp.contains(folder)) {
275
String rn = cp.getResourceName(folder) + "/Bundle.properties"; // NOI18N
277
FileObject peer = cp.findResource(rn);
280
DataObject peerDataObject = DataObject.find(peer);
281
// ((ResourcePanel)resourcePanel).setResource(peerDataObject);
282
propertyPanel.setResource(peerDataObject);
284
} catch (IOException ex) {
285
// no default resource
288
folder = folder.getParent();
293
/** Creates <code>ResourcePanel</code>. */
294
// private JPanel createResourcePanel() {
295
// return new ResourcePanel(project, file);
298
private void initAccessibility() {
299
this.getAccessibleContext().setAccessibleDescription(
300
bundle.getString("ACS_I18nPanel")); //NOI18N
301
skipButton.getAccessibleContext().setAccessibleDescription(
302
bundle.getString("ACS_CTL_SkipButton")); //NOI18N
303
cancelButton.getAccessibleContext().setAccessibleDescription(
304
bundle.getString("ACS_CTL_CancelButton")); //NOI18N
305
replaceButton.getAccessibleContext().setAccessibleDescription(
306
bundle.getString("ACS_CTL_ReplaceButton")); //NOI18N
307
infoButton.getAccessibleContext().setAccessibleDescription(
308
bundle.getString("ACS_CTL_InfoButton")); //NOI18N
309
helpButton.getAccessibleContext().setAccessibleDescription(
310
bundle.getString("ACS_CTL_HelpButton")); //NOI18N
313
/** This method is called from within the constructor to
314
* initialize the form.
315
* WARNING: Do NOT modify this code. The content of this method is
316
* always regenerated by the FormEditor.
318
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
319
private void initComponents() {
320
java.awt.GridBagConstraints gridBagConstraints;
322
fillPanel = new javax.swing.JPanel();
323
buttonsPanel = new javax.swing.JPanel();
324
replaceButton = new javax.swing.JButton();
325
skipButton = new javax.swing.JButton();
326
infoButton = new javax.swing.JButton();
327
cancelButton = new javax.swing.JButton();
328
helpButton = new javax.swing.JButton();
329
contentsPanelPlaceholder = new javax.swing.JPanel();
331
setLayout(new java.awt.GridBagLayout());
332
gridBagConstraints = new java.awt.GridBagConstraints();
333
gridBagConstraints.gridx = 0;
334
gridBagConstraints.gridy = 1;
335
gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
336
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
337
gridBagConstraints.weightx = 1.0;
338
add(fillPanel, gridBagConstraints);
340
buttonsPanel.setLayout(new java.awt.GridLayout(1, 5, 5, 0));
342
org.openide.awt.Mnemonics.setLocalizedText(replaceButton, bundle.getString("CTL_ReplaceButton")); // NOI18N
343
buttonsPanel.add(replaceButton);
345
org.openide.awt.Mnemonics.setLocalizedText(skipButton, bundle.getString("CTL_SkipButton")); // NOI18N
346
buttonsPanel.add(skipButton);
348
org.openide.awt.Mnemonics.setLocalizedText(infoButton, bundle.getString("CTL_InfoButton")); // NOI18N
349
buttonsPanel.add(infoButton);
351
org.openide.awt.Mnemonics.setLocalizedText(cancelButton, bundle.getString("CTL_CloseButton")); // NOI18N
352
buttonsPanel.add(cancelButton);
354
org.openide.awt.Mnemonics.setLocalizedText(helpButton, bundle.getString("CTL_HelpButton")); // NOI18N
355
helpButton.addActionListener(new java.awt.event.ActionListener() {
356
public void actionPerformed(java.awt.event.ActionEvent evt) {
357
helpButtonActionPerformed(evt);
360
buttonsPanel.add(helpButton);
362
gridBagConstraints = new java.awt.GridBagConstraints();
363
gridBagConstraints.gridx = 1;
364
gridBagConstraints.gridy = 1;
365
gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
366
gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
367
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
368
gridBagConstraints.insets = new java.awt.Insets(17, 0, 11, 11);
369
add(buttonsPanel, gridBagConstraints);
371
contentsPanelPlaceholder.setLayout(new javax.swing.BoxLayout(contentsPanelPlaceholder, javax.swing.BoxLayout.Y_AXIS));
372
gridBagConstraints = new java.awt.GridBagConstraints();
373
gridBagConstraints.gridx = 0;
374
gridBagConstraints.gridy = 0;
375
gridBagConstraints.gridwidth = 2;
376
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
377
gridBagConstraints.weightx = 1.0;
378
gridBagConstraints.weighty = 1.0;
379
add(contentsPanelPlaceholder, gridBagConstraints);
380
}// </editor-fold>//GEN-END:initComponents
382
private void myInitComponents() {
383
// resourcePanel = createResourcePanel();
384
// contentsPanel = new JPanel();
385
// contentsPanel.setLayout(new BoxLayout(contentsPanel, BoxLayout.Y_AXIS));
387
// contentsPanel.add(resourcePanel);
388
// contentsPanel.add(propertyPanel);
389
contentsShown = true;
390
contentsPanelPlaceholder.add(propertyPanel);
393
propertyPanel.addPropertyChangeListener(
394
PropertyPanel.PROP_STRING,
395
new PropertyChangeListener() {
396
public void propertyChange(PropertyChangeEvent evt) {
397
buttonsEnableDisable();
401
propertyPanel.addPropertyChangeListener(
402
PropertyPanel.PROP_RESOURCE,
403
// WeakListeners.propertyChange(
404
new PropertyChangeListener() {
405
public void propertyChange(PropertyChangeEvent evt) {
406
if(PropertyPanel.PROP_RESOURCE.equals(evt.getPropertyName())) {
407
buttonsEnableDisable();
408
// ((PropertyPanel)propertyPanel).updateAllValues();
418
private void helpButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_helpButtonActionPerformed
419
HelpCtx help = new HelpCtx(I18nUtil.HELP_ID_AUTOINSERT);
421
String sysprop = System.getProperty("org.openide.actions.HelpAction.DEBUG"); // NOI18N
423
if ("true".equals(sysprop) || "full".equals(sysprop)) { // NOI18N
424
System.err.println ("I18n module: Help button showing: " + help); // NOI18N, please do not comment out
426
Help helpSystem = Lookup.getDefault().lookup(Help.class);
427
helpSystem.showHelp(help);
428
}//GEN-LAST:event_helpButtonActionPerformed
430
private void enableButtons(long buttonMask) {
431
replaceButton.setEnabled((buttonMask & REPLACE_BUTTON) != 0);
432
skipButton.setEnabled((buttonMask & SKIP_BUTTON) != 0);
433
infoButton.setEnabled((buttonMask & INFO_BUTTON) != 0);
434
cancelButton.setEnabled((buttonMask & CANCEL_BUTTON) != 0);
435
helpButton.setEnabled((buttonMask & HELP_BUTTON) != 0);
441
// Variables declaration - do not modify//GEN-BEGIN:variables
442
private javax.swing.JPanel buttonsPanel;
443
private javax.swing.JButton cancelButton;
444
private javax.swing.JPanel contentsPanelPlaceholder;
445
private javax.swing.JPanel fillPanel;
446
private javax.swing.JButton helpButton;
447
private javax.swing.JButton infoButton;
448
private javax.swing.JButton replaceButton;
449
private javax.swing.JButton skipButton;
450
// End of variables declaration//GEN-END:variables
452
private EmptyPropertyPanel emptyPanel;
453
// private javax.swing.JPanel resourcePanel;
454
private PropertyPanel propertyPanel;
455
// private javax.swing.JPanel contentsPanel;