~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to i18n/src/org/netbeans/modules/i18n/I18nPanel.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
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]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
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.
 
29
 *
 
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.
 
40
 */
 
41
 
 
42
 
 
43
package org.netbeans.modules.i18n;
 
44
 
 
45
 
 
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;
 
54
 
 
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;
 
61
 
 
62
 
 
63
/**
 
64
 * Panel which provides GUI for i18n action.
 
65
 * Customizes {@code I18nString} object and is used by {@code I18nSupport} for i18n-izing 
 
66
 * one source.
 
67
 *
 
68
 * @author  Peter Zavadsky
 
69
 */
 
70
public class I18nPanel extends JPanel {
 
71
 
 
72
    /** {@code I18nString} cusomized in this panel. */
 
73
    private I18nString i18nString;
 
74
    
 
75
    /** Helper bundle used for i18n-zing strings in this source.  */
 
76
    private ResourceBundle bundle;
 
77
 
 
78
    /** Helper property change support. */
 
79
    private PropertyChangeListener propListener;
 
80
    
 
81
    /** Generated serial version ID. */
 
82
    static final long serialVersionUID =-6982482491017390786L;
 
83
 
 
84
    private Project project;
 
85
 
 
86
    private FileObject file;
 
87
    
 
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;
 
95
 
 
96
 
 
97
    
 
98
    /** 
 
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.
 
102
     *
 
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
 
106
     */
 
107
    public I18nPanel(PropertyPanel propertyPanel, Project project, FileObject file) {
 
108
        this(propertyPanel, true, project, file);
 
109
    }
 
110
 
 
111
    /**
 
112
     * Creates i18n panel.
 
113
     *
 
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
 
118
     */
 
119
    public I18nPanel(PropertyPanel propertyPanel, boolean withButtons, Project project, FileObject file) {
 
120
        this.project = project;
 
121
        this.file = file;
 
122
        this.propertyPanel = propertyPanel;
 
123
        this.propertyPanel.setFile(file);
 
124
        this.propertyPanel.setEnabled(project != null);
 
125
 
 
126
        // Init bundle.
 
127
        bundle = I18nUtil.getBundle();
 
128
        
 
129
        initComponents();
 
130
        myInitComponents();
 
131
        initAccessibility();        
 
132
        
 
133
        if (!withButtons) {
 
134
            remove(buttonsPanel);
 
135
        }
 
136
        
 
137
        // create empty panel
 
138
        emptyPanel = new EmptyPropertyPanel();
 
139
        contentsShown = true;
 
140
 
 
141
        showBundleMessage("TXT_SearchingForStrings");                   //NOI18N
 
142
    }
 
143
 
 
144
 
 
145
    private boolean contentsShown;
 
146
 
 
147
    public void showBundleMessage(String bundleKey) {
 
148
 
 
149
        emptyPanel.setBundleText(bundleKey);
 
150
        if (contentsShown) {
 
151
            contentsPanelPlaceholder.remove(propertyPanel);
 
152
            contentsPanelPlaceholder.add(emptyPanel);
 
153
            contentsPanelPlaceholder.validate();
 
154
            contentsPanelPlaceholder.repaint();
 
155
            contentsShown = false;
 
156
        }
 
157
        buttonsEnableDisable();
 
158
    }
 
159
 
 
160
    public void showPropertyPanel() {
 
161
        if (!contentsShown) {
 
162
            contentsPanelPlaceholder.remove(emptyPanel);
 
163
            contentsPanelPlaceholder.add(propertyPanel);
 
164
            contentsPanelPlaceholder.validate();
 
165
            contentsPanelPlaceholder.repaint();
 
166
            contentsShown = true;
 
167
        }
 
168
        buttonsEnableDisable();        
 
169
    }
 
170
 
 
171
 
 
172
    
 
173
    /**
 
174
     * Reset associated project to a new value
 
175
     */
 
176
//    public void setProject(Project project) {
 
177
////        ((ResourcePanel)resourcePanel).setProject(project);
 
178
//        propertyPanel.setEnabled(project != null);
 
179
//
 
180
//    }
 
181
//
 
182
//    public Project getProject() { 
 
183
//        return ((ResourcePanel)resourcePanel).getProject();
 
184
//    }
 
185
    
 
186
    /**
 
187
     * Sets the file associated with this panel -- the one, which
 
188
     * is localized
 
189
     */ 
 
190
    public void setFile(FileObject file) {
 
191
//        ((ResourcePanel)resourcePanel).setFile(file);
 
192
        propertyPanel.setFile(file);
 
193
    }
 
194
    
 
195
    /**
 
196
     * Gets the file associated with this panel -- the one, which
 
197
     * is localized
 
198
     */ 
 
199
    public FileObject getFile() {
 
200
//        return ((ResourcePanel)resourcePanel).getFile();
 
201
        return propertyPanel.getFile();
 
202
    }
 
203
 
 
204
    
 
205
    /** Overrides superclass method to set default button. */
 
206
    @Override
 
207
    public void addNotify() {
 
208
        super.addNotify();
 
209
        
 
210
        if (SwingUtilities.isDescendingFrom(replaceButton, this)) {
 
211
            getRootPane().setDefaultButton(replaceButton);
 
212
        }
 
213
    }
 
214
    
 
215
    /** Getter for <code>i18nString</code>. */
 
216
    public I18nString getI18nString() {
 
217
        return i18nString;
 
218
    }
 
219
    
 
220
    /** Setter for i18nString property. */
 
221
    public void setI18nString(I18nString i18nString) {
 
222
        this.i18nString = i18nString;
 
223
 
 
224
        propertyPanel.setI18nString(i18nString);
 
225
//        ((ResourcePanel)resourcePanel).setI18nString(i18nString);        
 
226
        
 
227
        showPropertyPanel();
 
228
    }
 
229
 
 
230
    /** Replace button accessor. */
 
231
    JButton getReplaceButton() {
 
232
        return replaceButton;
 
233
    }
 
234
    
 
235
    /** Skip button accessor. */
 
236
    JButton getSkipButton() {
 
237
        return skipButton;
 
238
    }
 
239
 
 
240
    /** Info button accessor. */
 
241
    JButton getInfoButton() {
 
242
        return infoButton;
 
243
    }
 
244
    
 
245
    /** Cancel/Close button accessor. */
 
246
    JButton getCancelButton() {
 
247
        return cancelButton;
 
248
    }
 
249
    
 
250
    /** Enables/disables buttons based on the contents of the dialog. */
 
251
    private void buttonsEnableDisable() {
 
252
        if (contentsShown) {
 
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);
 
260
        } else {
 
261
            enableButtons(CANCEL_BUTTON | HELP_BUTTON);
 
262
        }
 
263
    }
 
264
 
 
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);
 
270
            
 
271
            FileObject folder = cp.findResource(cp.getResourceName(fo.getParent()));
 
272
            
 
273
            while (folder != null && cp.contains(folder)) {
 
274
                
 
275
                String rn = cp.getResourceName(folder) + "/Bundle.properties"; // NOI18N
 
276
                
 
277
                FileObject peer = cp.findResource(rn);
 
278
                if (peer != null) {
 
279
                    try {
 
280
                        DataObject peerDataObject = DataObject.find(peer);
 
281
//                        ((ResourcePanel)resourcePanel).setResource(peerDataObject);
 
282
                        propertyPanel.setResource(peerDataObject);
 
283
                        return;
 
284
                    } catch (IOException ex) {
 
285
                        // no default resource
 
286
                    }
 
287
                }
 
288
                folder = folder.getParent();
 
289
            }            
 
290
        }        
 
291
    }
 
292
    
 
293
    /** Creates <code>ResourcePanel</code>. */
 
294
//    private JPanel createResourcePanel() {
 
295
//        return new ResourcePanel(project, file);
 
296
//    }
 
297
    
 
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
 
311
    }
 
312
    
 
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.
 
317
     */
 
318
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
 
319
    private void initComponents() {
 
320
        java.awt.GridBagConstraints gridBagConstraints;
 
321
 
 
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();
 
330
 
 
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);
 
339
 
 
340
        buttonsPanel.setLayout(new java.awt.GridLayout(1, 5, 5, 0));
 
341
 
 
342
        org.openide.awt.Mnemonics.setLocalizedText(replaceButton, bundle.getString("CTL_ReplaceButton")); // NOI18N
 
343
        buttonsPanel.add(replaceButton);
 
344
 
 
345
        org.openide.awt.Mnemonics.setLocalizedText(skipButton, bundle.getString("CTL_SkipButton")); // NOI18N
 
346
        buttonsPanel.add(skipButton);
 
347
 
 
348
        org.openide.awt.Mnemonics.setLocalizedText(infoButton, bundle.getString("CTL_InfoButton")); // NOI18N
 
349
        buttonsPanel.add(infoButton);
 
350
 
 
351
        org.openide.awt.Mnemonics.setLocalizedText(cancelButton, bundle.getString("CTL_CloseButton")); // NOI18N
 
352
        buttonsPanel.add(cancelButton);
 
353
 
 
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);
 
358
            }
 
359
        });
 
360
        buttonsPanel.add(helpButton);
 
361
 
 
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);
 
370
 
 
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
 
381
 
 
382
    private void myInitComponents() {
 
383
//        resourcePanel = createResourcePanel();
 
384
//        contentsPanel = new JPanel();
 
385
//        contentsPanel.setLayout(new BoxLayout(contentsPanel, BoxLayout.Y_AXIS));
 
386
 
 
387
//        contentsPanel.add(resourcePanel);
 
388
//        contentsPanel.add(propertyPanel);
 
389
        contentsShown = true;
 
390
        contentsPanelPlaceholder.add(propertyPanel);
 
391
        
 
392
      
 
393
        propertyPanel.addPropertyChangeListener(
 
394
                PropertyPanel.PROP_STRING, 
 
395
                new PropertyChangeListener() {
 
396
                    public void propertyChange(PropertyChangeEvent evt) {
 
397
                        buttonsEnableDisable();
 
398
                    }
 
399
                });
 
400
        
 
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();
 
409
                            }
 
410
                        }
 
411
                    }
 
412
//                },resourcePanel
 
413
//               )
 
414
        );
 
415
        
 
416
    }
 
417
 
 
418
  private void helpButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_helpButtonActionPerformed
 
419
      HelpCtx help = new HelpCtx(I18nUtil.HELP_ID_AUTOINSERT);
 
420
      
 
421
      String sysprop = System.getProperty("org.openide.actions.HelpAction.DEBUG"); // NOI18N
 
422
      
 
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
 
425
      }
 
426
      Help helpSystem = Lookup.getDefault().lookup(Help.class);
 
427
      helpSystem.showHelp(help);
 
428
  }//GEN-LAST:event_helpButtonActionPerformed
 
429
 
 
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);               
 
436
    }
 
437
 
 
438
        
 
439
 
 
440
 
 
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
 
451
 
 
452
    private EmptyPropertyPanel emptyPanel;
 
453
//    private javax.swing.JPanel resourcePanel;
 
454
    private PropertyPanel propertyPanel;
 
455
//    private javax.swing.JPanel contentsPanel;
 
456
 
 
457
}