KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > project > JavaTargetChooserPanelGUI


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-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.java.project;
21
22 import java.awt.Component JavaDoc;
23 import java.awt.Dimension JavaDoc;
24 import java.awt.event.ActionListener JavaDoc;
25 import java.io.File JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import javax.swing.ComboBoxModel JavaDoc;
30 import javax.swing.DefaultComboBoxModel JavaDoc;
31 import javax.swing.DefaultListCellRenderer JavaDoc;
32 import javax.swing.JComboBox JavaDoc;
33 import javax.swing.JList JavaDoc;
34 import javax.swing.ListModel JavaDoc;
35 import javax.swing.SwingUtilities JavaDoc;
36 import javax.swing.event.ChangeEvent JavaDoc;
37 import javax.swing.event.ChangeListener JavaDoc;
38 import javax.swing.event.DocumentListener JavaDoc;
39 import org.netbeans.api.project.Project;
40 import org.netbeans.api.project.ProjectUtils;
41 import org.netbeans.api.project.SourceGroup;
42 import org.netbeans.spi.java.project.support.ui.PackageView;
43 import org.openide.filesystems.FileObject;
44 import org.openide.filesystems.FileUtil;
45 import org.openide.awt.Mnemonics;
46 import org.openide.loaders.DataObject;
47 import org.openide.loaders.DataObjectNotFoundException;
48 import org.openide.util.NbBundle;
49 import org.openide.util.RequestProcessor;
50
51 /**
52  * Permits user to select a package to place a Java class (or other resource) into.
53  * @author Petr Hrebejk, Jesse Glick
54  */

55 public class JavaTargetChooserPanelGUI extends javax.swing.JPanel JavaDoc implements ActionListener JavaDoc, DocumentListener JavaDoc {
56   
57     private static final String JavaDoc DEFAULT_NEW_PACKAGE_NAME =
58         NbBundle.getMessage( JavaTargetChooserPanelGUI.class, "LBL_JavaTargetChooserPanelGUI_DefaultNewPackageName" ); // NOI18N
59
private static final String JavaDoc NEW_CLASS_PREFIX =
60         NbBundle.getMessage( JavaTargetChooserPanelGUI.class, "LBL_JavaTargetChooserPanelGUI_NewJavaClassPrefix" ); // NOI18N
61

62     /** preferred dimension of the panel */
63     private static final Dimension JavaDoc PREF_DIM = new Dimension JavaDoc(500, 340);
64     
65     private Project project;
66     private String JavaDoc expectedExtension;
67     private final List JavaDoc<ChangeListener JavaDoc> listeners = new ArrayList JavaDoc<ChangeListener JavaDoc>();
68     private int type;
69     private SourceGroup groups[];
70     private boolean ignoreRootCombo;
71     
72     /** Creates new form SimpleTargetChooserGUI */
73     public JavaTargetChooserPanelGUI( Project p, SourceGroup[] groups, Component JavaDoc bottomPanel, int type ) {
74         this.type = type;
75         this.project = p;
76         this.groups = groups;
77         
78         initComponents();
79                 
80         if ( type == NewJavaFileWizardIterator.TYPE_PACKAGE ) {
81             packageComboBox.setVisible( false );
82             packageLabel.setVisible( false );
83             Mnemonics.setLocalizedText (fileLabel, NbBundle.getMessage (JavaTargetChooserPanelGUI.class, "LBL_JavaTargetChooserPanelGUI_CreatedFolder_Label")); // NOI18N
84
Mnemonics.setLocalizedText (documentNameLabel, NbBundle.getMessage (JavaTargetChooserPanelGUI.class, "LBL_JavaTargetChooserPanelGUI_PackageName_Label")); // NOI18N
85
documentNameTextField.getDocument().addDocumentListener( this );
86         }
87         else if ( type == NewJavaFileWizardIterator.TYPE_PKG_INFO ) {
88             documentNameTextField.setEditable (false);
89         }
90         else {
91             packageComboBox.getEditor().addActionListener( this );
92             documentNameTextField.getDocument().addDocumentListener( this );
93         }
94         
95                 
96         if ( bottomPanel != null ) {
97             bottomPanelContainer.add( bottomPanel, java.awt.BorderLayout.CENTER );
98         }
99                 
100         //initValues( project, null, null );
101

102
103         // Not very nice
104
Component JavaDoc packageEditor = packageComboBox.getEditor().getEditorComponent();
105         if ( packageEditor instanceof javax.swing.JTextField JavaDoc ) {
106             ((javax.swing.JTextField JavaDoc)packageEditor).getDocument().addDocumentListener( this );
107         }
108         else {
109             packageComboBox.addActionListener( this );
110         }
111         
112         rootComboBox.setRenderer(new GroupListCellRenderer());
113         packageComboBox.setRenderer(PackageView.listRenderer());
114         rootComboBox.addActionListener( this );
115         
116         setPreferredSize( PREF_DIM );
117         setName( NbBundle.getBundle (JavaTargetChooserPanelGUI.class).getString ("LBL_JavaTargetChooserPanelGUI_Name") ); // NOI18N
118
}
119             
120     public void addNotify () {
121         Dimension JavaDoc panel2Size = this.jPanel2.getPreferredSize();
122         Dimension JavaDoc bottomPanelSize = this.bottomPanelContainer.getPreferredSize ();
123         Dimension JavaDoc splitterSize = this.targetSeparator.getPreferredSize();
124         int vmax = panel2Size.height + bottomPanelSize.height + splitterSize.height + 12; //Insets=12
125
//Update only height, keep the wizard width
126
if (vmax > PREF_DIM.height) {
127             this.setPreferredSize (new Dimension JavaDoc (PREF_DIM.width,vmax));
128         }
129         super.addNotify();
130     }
131     
132     public void initValues( FileObject template, FileObject preselectedFolder ) {
133         assert project != null : "Project must be specified."; // NOI18N
134
// Show name of the project
135
projectTextField.setText( ProjectUtils.getInformation(project).getDisplayName() );
136         assert template != null;
137         
138         String JavaDoc displayName = null;
139         try {
140             DataObject templateDo = DataObject.find (template);
141             displayName = templateDo.getNodeDelegate ().getDisplayName ();
142         } catch (DataObjectNotFoundException ex) {
143             displayName = template.getName ();
144         }
145         
146         putClientProperty ("NewFileWizard_Title", displayName);// NOI18N
147
// Setup comboboxes
148
rootComboBox.setModel(new DefaultComboBoxModel JavaDoc(groups));
149         SourceGroup preselectedGroup = getPreselectedGroup( preselectedFolder );
150         ignoreRootCombo = true;
151         rootComboBox.setSelectedItem( preselectedGroup );
152         ignoreRootCombo = false;
153         Object JavaDoc preselectedPackage = getPreselectedPackage(preselectedGroup, preselectedFolder, packageComboBox.getModel());
154         if ( type == NewJavaFileWizardIterator.TYPE_PACKAGE ) {
155             String JavaDoc docName = preselectedPackage == null || preselectedPackage.toString().length() == 0 ?
156                 DEFAULT_NEW_PACKAGE_NAME :
157                 preselectedPackage.toString() + "." + DEFAULT_NEW_PACKAGE_NAME;
158
159             documentNameTextField.setText( docName );
160             int docNameLen = docName.length();
161             int defPackageNameLen = DEFAULT_NEW_PACKAGE_NAME.length();
162
163             documentNameTextField.setSelectionEnd( docNameLen - 1 );
164             documentNameTextField.setSelectionStart( docNameLen - defPackageNameLen );
165         } else {
166             if (preselectedPackage != null) {
167                 // packageComboBox.setSelectedItem( preselectedPackage );
168
packageComboBox.getEditor().setItem( preselectedPackage );
169             }
170             if (template != null) {
171                 if ( documentNameTextField.getText().trim().length() == 0 ) { // To preserve the class name on back in the wiazard
172
if (this.type == NewJavaFileWizardIterator.TYPE_PKG_INFO) {
173                         documentNameTextField.setText (template.getName ());
174                     }
175                     else {
176                         //Ordinary file
177
documentNameTextField.setText (NEW_CLASS_PREFIX + template.getName ());
178                         documentNameTextField.selectAll ();
179                     }
180                 }
181             }
182             updatePackages( false );
183         }
184         // Determine the extension
185
String JavaDoc ext = template == null ? "" : template.getExt(); // NOI18N
186
expectedExtension = ext.length() == 0 ? "" : "." + ext; // NOI18N
187

188         updateText();
189         
190     }
191         
192     public FileObject getRootFolder() {
193         return ((SourceGroup) rootComboBox.getSelectedItem()).getRootFolder();
194     }
195     
196     public String JavaDoc getPackageFileName() {
197         
198         if ( type == NewJavaFileWizardIterator.TYPE_PACKAGE ) {
199             return ""; // NOI18N
200
}
201         
202         String JavaDoc packageName = packageComboBox.getEditor().getItem().toString();
203         return packageName.replace( '.', '/' ); // NOI18N
204
}
205     
206     /**
207      * Name of selected package, or "" for default package.
208      */

209     String JavaDoc getPackageName() {
210         if ( type == NewJavaFileWizardIterator.TYPE_PACKAGE ) {
211             return ""; // NOI18N
212
}
213         return packageComboBox.getEditor().getItem().toString();
214     }
215     
216     public String JavaDoc getTargetName() {
217         String JavaDoc text = documentNameTextField.getText().trim();
218         
219         if ( text.length() == 0 ) {
220             return null;
221         }
222         else {
223             return text;
224         }
225
226     }
227     
228     public void addChangeListener(ChangeListener JavaDoc l) {
229         listeners.add(l);
230     }
231     
232     public void removeChangeListener(ChangeListener JavaDoc l) {
233         listeners.remove(l);
234     }
235     
236     private void fireChange() {
237         ChangeEvent JavaDoc e = new ChangeEvent JavaDoc(this);
238         for (ChangeListener JavaDoc l : listeners) {
239             l.stateChanged(e);
240         }
241     }
242         
243     /** This method is called from within the constructor to
244      * initialize the form.
245      * WARNING: Do NOT modify this code. The content of this method is
246      * always regenerated by the Form Editor.
247      */

248     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
249
private void initComponents() {
250         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
251
252         targetSeparator = new javax.swing.JSeparator JavaDoc();
253         bottomPanelContainer = new javax.swing.JPanel JavaDoc();
254         jPanel2 = new javax.swing.JPanel JavaDoc();
255         jPanel1 = new javax.swing.JPanel JavaDoc();
256         documentNameLabel = new javax.swing.JLabel JavaDoc();
257         documentNameTextField = new javax.swing.JTextField JavaDoc();
258         jLabel5 = new javax.swing.JLabel JavaDoc();
259         projectTextField = new javax.swing.JTextField JavaDoc();
260         jLabel1 = new javax.swing.JLabel JavaDoc();
261         rootComboBox = new javax.swing.JComboBox JavaDoc();
262         packageLabel = new javax.swing.JLabel JavaDoc();
263         packageComboBox = new javax.swing.JComboBox JavaDoc();
264         fileLabel = new javax.swing.JLabel JavaDoc();
265         fileTextField = new javax.swing.JTextField JavaDoc();
266
267         setLayout(new java.awt.GridBagLayout JavaDoc());
268         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
269         gridBagConstraints.gridx = 0;
270         gridBagConstraints.gridy = 1;
271         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
272         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
273         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 12, 0);
274         add(targetSeparator, gridBagConstraints);
275
276         bottomPanelContainer.setLayout(new java.awt.BorderLayout JavaDoc());
277         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
278         gridBagConstraints.gridx = 0;
279         gridBagConstraints.gridy = 2;
280         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
281         gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
282         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
283         gridBagConstraints.weighty = 1.0;
284         add(bottomPanelContainer, gridBagConstraints);
285
286         jPanel2.setLayout(new java.awt.GridBagLayout JavaDoc());
287
288         jPanel1.setLayout(new java.awt.GridBagLayout JavaDoc());
289
290         documentNameLabel.setLabelFor(documentNameTextField);
291         org.openide.awt.Mnemonics.setLocalizedText(documentNameLabel, org.openide.util.NbBundle.getMessage(JavaTargetChooserPanelGUI.class, "LBL_JavaTargetChooserPanelGUI_ClassName_Label")); // NOI18N
292
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
293         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
294         jPanel1.add(documentNameLabel, gridBagConstraints);
295         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
296         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
297         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
298         gridBagConstraints.weightx = 1.0;
299         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 6, 0, 0);
300         jPanel1.add(documentNameTextField, gridBagConstraints);
301         java.util.ResourceBundle JavaDoc bundle = java.util.ResourceBundle.getBundle("org/netbeans/modules/java/project/Bundle"); // NOI18N
302
documentNameTextField.getAccessibleContext().setAccessibleDescription(bundle.getString("AD_documentNameTextField")); // NOI18N
303

304         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
305         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
306         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
307         gridBagConstraints.weightx = 1.0;
308         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 24, 0);
309         jPanel2.add(jPanel1, gridBagConstraints);
310
311         jLabel5.setLabelFor(projectTextField);
312         org.openide.awt.Mnemonics.setLocalizedText(jLabel5, org.openide.util.NbBundle.getMessage(JavaTargetChooserPanelGUI.class, "LBL_JavaTargetChooserPanelGUI_jLabel5")); // NOI18N
313
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
314         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
315         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 6, 0);
316         jPanel2.add(jLabel5, gridBagConstraints);
317
318         projectTextField.setEditable(false);
319         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
320         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
321         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
322         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 6, 6, 0);
323         jPanel2.add(projectTextField, gridBagConstraints);
324         projectTextField.getAccessibleContext().setAccessibleDescription(bundle.getString("AD_projectTextField")); // NOI18N
325

326         jLabel1.setLabelFor(rootComboBox);
327         org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(JavaTargetChooserPanelGUI.class, "LBL_JavaTargetChooserPanelGUI_jLabel1")); // NOI18N
328
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
329         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
330         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 6, 0);
331         jPanel2.add(jLabel1, gridBagConstraints);
332         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
333         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
334         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
335         gridBagConstraints.weightx = 1.0;
336         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 6, 6, 0);
337         jPanel2.add(rootComboBox, gridBagConstraints);
338         rootComboBox.getAccessibleContext().setAccessibleDescription(bundle.getString("AD_rootComboBox")); // NOI18N
339

340         packageLabel.setLabelFor(packageComboBox);
341         org.openide.awt.Mnemonics.setLocalizedText(packageLabel, org.openide.util.NbBundle.getMessage(JavaTargetChooserPanelGUI.class, "LBL_JavaTargetChooserPanelGUI_jLabel2")); // NOI18N
342
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
343         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
344         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 6, 0);
345         jPanel2.add(packageLabel, gridBagConstraints);
346
347         packageComboBox.setEditable(true);
348         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
349         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
350         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
351         gridBagConstraints.weightx = 1.0;
352         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 6, 6, 0);
353         jPanel2.add(packageComboBox, gridBagConstraints);
354         packageComboBox.getAccessibleContext().setAccessibleDescription(bundle.getString("AD_packageComboBox")); // NOI18N
355

356         fileLabel.setLabelFor(fileTextField);
357         org.openide.awt.Mnemonics.setLocalizedText(fileLabel, org.openide.util.NbBundle.getMessage(JavaTargetChooserPanelGUI.class, "LBL_JavaTargetChooserPanelGUI_CreatedFile_Label")); // NOI18N
358
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
359         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
360         gridBagConstraints.insets = new java.awt.Insets JavaDoc(6, 0, 12, 0);
361         jPanel2.add(fileLabel, gridBagConstraints);
362
363         fileTextField.setEditable(false);
364         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
365         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
366         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
367         gridBagConstraints.weightx = 1.0;
368         gridBagConstraints.insets = new java.awt.Insets JavaDoc(6, 6, 12, 0);
369         jPanel2.add(fileTextField, gridBagConstraints);
370         fileTextField.getAccessibleContext().setAccessibleDescription(bundle.getString("AD_fileTextField")); // NOI18N
371

372         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
373         gridBagConstraints.gridx = 0;
374         gridBagConstraints.gridy = 0;
375         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
376         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
377         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
378         gridBagConstraints.weightx = 1.0;
379         add(jPanel2, gridBagConstraints);
380
381         getAccessibleContext().setAccessibleDescription(bundle.getString("AD_JavaTargetChooserPanelGUI")); // NOI18N
382
}// </editor-fold>//GEN-END:initComponents
383

384     
385     
386     // Variables declaration - do not modify//GEN-BEGIN:variables
387
private javax.swing.JPanel JavaDoc bottomPanelContainer;
388     private javax.swing.JLabel JavaDoc documentNameLabel;
389     private javax.swing.JTextField JavaDoc documentNameTextField;
390     private javax.swing.JLabel JavaDoc fileLabel;
391     private javax.swing.JTextField JavaDoc fileTextField;
392     private javax.swing.JLabel JavaDoc jLabel1;
393     private javax.swing.JLabel JavaDoc jLabel5;
394     private javax.swing.JPanel JavaDoc jPanel1;
395     private javax.swing.JPanel JavaDoc jPanel2;
396     private javax.swing.JComboBox JavaDoc packageComboBox;
397     private javax.swing.JLabel JavaDoc packageLabel;
398     private javax.swing.JTextField JavaDoc projectTextField;
399     private javax.swing.JComboBox JavaDoc rootComboBox;
400     private javax.swing.JSeparator JavaDoc targetSeparator;
401     // End of variables declaration//GEN-END:variables
402

403     // ActionListener implementation -------------------------------------------
404

405     public void actionPerformed(java.awt.event.ActionEvent JavaDoc e) {
406         if ( rootComboBox == e.getSource() ) {
407             if ( !ignoreRootCombo && type != NewJavaFileWizardIterator.TYPE_PACKAGE ) {
408                 updatePackages( true );
409             }
410             updateText();
411         }
412         else if ( packageComboBox == e.getSource() ) {
413             updateText();
414             fireChange();
415         }
416         else if ( packageComboBox.getEditor() == e.getSource() ) {
417             updateText();
418             fireChange();
419         }
420     }
421     
422     // DocumentListener implementation -----------------------------------------
423

424     public void changedUpdate(javax.swing.event.DocumentEvent JavaDoc e) {
425         updateText();
426         fireChange();
427     }
428     
429     public void insertUpdate(javax.swing.event.DocumentEvent JavaDoc e) {
430         changedUpdate( e );
431     }
432     
433     public void removeUpdate(javax.swing.event.DocumentEvent JavaDoc e) {
434         changedUpdate( e );
435     }
436     
437     // Private methods ---------------------------------------------------------
438

439     private RequestProcessor.Task updatePackagesTask = null;
440     
441     private static final ComboBoxModel JavaDoc WAIT_MODEL = new DefaultComboBoxModel JavaDoc(
442         new String JavaDoc[] {
443             NbBundle.getMessage( JavaTargetChooserPanelGUI.class, "LBL_JavaTargetChooserPanelGUI_PackageName_PleaseWait" ) // NOI18N
444
}
445     );
446     
447     private void updatePackages( final boolean clean ) {
448         WAIT_MODEL.setSelectedItem( packageComboBox.getEditor().getItem() );
449         packageComboBox.setModel( WAIT_MODEL );
450         
451         if ( updatePackagesTask != null ) {
452             updatePackagesTask.cancel();
453         }
454         
455         updatePackagesTask = new RequestProcessor( "ComboUpdatePackages" ).post(
456             new Runnable JavaDoc() {
457             
458                 private ComboBoxModel JavaDoc model;
459             
460                 public void run() {
461                     if ( !SwingUtilities.isEventDispatchThread() ) {
462                         model = PackageView.createListView((SourceGroup) rootComboBox.getSelectedItem());
463                         SwingUtilities.invokeLater( this );
464                     }
465                     else {
466                         if ( !clean ) {
467                             model.setSelectedItem( packageComboBox.getEditor().getItem() );
468                         }
469                         packageComboBox.setModel( model );
470                     }
471                 }
472             }
473         );
474                 
475     }
476         
477     private void updateText() {
478         
479         SourceGroup g = (SourceGroup) rootComboBox.getSelectedItem();
480         FileObject rootFolder = g.getRootFolder();
481         String JavaDoc packageName = getPackageFileName();
482         String JavaDoc documentName = documentNameTextField.getText().trim();
483         if ( type == NewJavaFileWizardIterator.TYPE_PACKAGE ) {
484             documentName = documentName.replace( '.', '/' ); // NOI18N
485
}
486         else if ( documentName.length() > 0 ) {
487             documentName = documentName + expectedExtension;
488         }
489         String JavaDoc createdFileName = FileUtil.getFileDisplayName( rootFolder ) +
490             ( packageName.startsWith("/") || packageName.startsWith( File.separator ) ? "" : "/" ) + // NOI18N
491
packageName +
492             ( packageName.endsWith("/") || packageName.endsWith( File.separator ) || packageName.length() == 0 ? "" : "/" ) + // NOI18N
493
documentName;
494         
495         fileTextField.setText( createdFileName.replace( '/', File.separatorChar ) ); // NOI18N
496
}
497     
498     private SourceGroup getPreselectedGroup(FileObject folder) {
499         for(int i = 0; folder != null && i < groups.length; i++) {
500             FileObject root = groups[i].getRootFolder();
501             if (root.equals(folder) || FileUtil.isParentOf(root, folder)) {
502                 return groups[i];
503             }
504         }
505         return groups[0];
506     }
507     
508     /**
509      * Get a package combo model item for the package the user selected before opening the wizard.
510      * May return null if it cannot find it; or a String instance if there is a well-defined
511      * package but it is not listed among the packages shown in the list model.
512      */

513     private Object JavaDoc getPreselectedPackage(SourceGroup group, FileObject folder, ListModel JavaDoc model) {
514         if ( folder == null ) {
515             return null;
516         }
517         FileObject root = group.getRootFolder();
518         
519         String JavaDoc relPath = FileUtil.getRelativePath( root, folder );
520         
521         if ( relPath == null ) {
522             // Group Root folder is no a parent of the preselected folder
523
// No package should be selected
524
return null;
525         }
526         else {
527             // Find the right item.
528
String JavaDoc name = relPath.replace('/', '.');
529             /*
530             int max = model.getSize();
531             for (int i = 0; i < max; i++) {
532                 Object item = model.getElementAt(i);
533                 if (item.toString().equals(name)) {
534                     return item;
535                 }
536             }
537              */

538             // Didn't find it.
539
// #49954: should nonetheless show something in the combo box.
540
return name;
541         }
542     }
543     
544     // Private innerclasses ----------------------------------------------------
545

546     /**
547      * Displays a {@link SourceGroup} in {@link #rootComboBox}.
548      */

549     private static final class GroupListCellRenderer extends DefaultListCellRenderer JavaDoc/*<SourceGroup>*/ {
550         
551         public GroupListCellRenderer() {}
552         
553         public Component JavaDoc getListCellRendererComponent(JList JavaDoc list, Object JavaDoc value, int index, boolean isSelected, boolean cellHasFocus) {
554             SourceGroup g = (SourceGroup) value;
555             super.getListCellRendererComponent(list, g.getDisplayName(), index, isSelected, cellHasFocus);
556             setIcon(g.getIcon(false));
557             return this;
558         }
559         
560     }
561     
562 }
563
Popular Tags