KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > beaninfo > editors > DataFolderPanel


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.beaninfo.editors;
21
22 import java.awt.event.KeyEvent JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.lang.ref.*;
25 import java.util.Arrays JavaDoc;
26 import java.util.StringTokenizer JavaDoc;
27 import java.beans.*;
28 import java.io.File JavaDoc;
29
30 import javax.swing.event.DocumentListener JavaDoc;
31 import javax.swing.event.ChangeListener JavaDoc;
32 import javax.swing.event.ChangeEvent JavaDoc;
33 import javax.swing.KeyStroke JavaDoc;
34 import javax.swing.SwingUtilities JavaDoc;
35 import org.openide.explorer.propertysheet.PropertyEnv;
36 import org.openide.loaders.*;
37 import org.openide.nodes.*;
38 import org.openide.explorer.ExplorerManager;
39 import org.openide.filesystems.*;
40 import org.openide.util.Exceptions;
41 import org.openide.util.HelpCtx;
42 import org.openide.util.NbCollections;
43 import org.openide.windows.TopComponent;
44 /**
45  * A panel for selecting an existing data folder.
46  * @author Jaroslav Tulach, David Strupl
47  * @version
48  */

49 class DataFolderPanel extends TopComponent implements
50                     DocumentListener JavaDoc, DataFilter,
51                     PropertyChangeListener, VetoableChangeListener {
52
53     /** prefered dimmension of the panels */
54     static java.awt.Dimension JavaDoc PREF_DIM = new java.awt.Dimension JavaDoc (450, 250);
55                     
56     /** format to for default package */
57     /** listener to changes in the panel */
58     private ChangeListener JavaDoc listener;
59
60     /** file system reference */
61     Reference<FileSystem> system = new WeakReference<FileSystem> (null);
62
63     /** root node */
64     private Node rootNode;
65
66     /** last DataFolder object that can be returned */
67     private DataFolder df;
68
69     /** */
70     private DataFolderEditor editor;
71     
72     private static final String JavaDoc PATH_TOKEN_DELIMITER = "/" + java.io.File.separatorChar; // NOI18N
73

74     private String JavaDoc last_suggestion = "";
75     
76     public DataFolderPanel(DataFolderEditor ed) {
77         this();
78         editor = ed;
79
80         editor.env.setState(PropertyEnv.STATE_NEEDS_VALIDATION);
81         editor.env.addPropertyChangeListener(this);
82     }
83     
84     /** Creates new form DataFolderPanel */
85     public DataFolderPanel() {
86         initComponents ();
87
88         setName (getString("LAB_TargetLocationPanelName"));
89
90         setBorder (new javax.swing.border.EmptyBorder JavaDoc(new java.awt.Insets JavaDoc(12, 12, 0, 11)));
91         /*
92         packagesPanel.setBorder (new javax.swing.border.CompoundBorder(
93                                      new javax.swing.border.TitledBorder(getString("LAB_SelectPackageBorder")),
94                                      new javax.swing.border.EmptyBorder(new java.awt.Insets(8, 8, 8, 8)))
95                                 );
96          */

97
98         rootNode = createPackagesNode ();
99         
100         beanTreeView.setRootVisible (false);
101
102         packagesPanel.getExplorerManager ().setRootContext (rootNode);
103         packagesPanel.getExplorerManager ().addPropertyChangeListener (this);
104         packagesPanel.getExplorerManager ().addVetoableChangeListener (this);
105
106         // registers itself to listen to changes in the content of document
107
packageName.getDocument().addDocumentListener(this);
108         packageName.unregisterKeyboardAction(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0));
109         
110         beanTreeView.getAccessibleContext().setAccessibleDescription(getString("ACSD_DataFolderTree"));
111         packageName.getAccessibleContext().setAccessibleDescription(getString("ACSD_package"));
112         directoryName.getAccessibleContext().setAccessibleDescription(getString("ACSD_directory"));
113         createButton.getAccessibleContext().setAccessibleDescription(getString("ACSD_Create"));
114         getAccessibleContext().setAccessibleDescription(getString("ACSD_DataFolderPanel"));
115     }
116
117     /** Preffered size */
118     public java.awt.Dimension JavaDoc getPreferredSize() {
119         return PREF_DIM;
120     }
121
122     /** Request focus.
123     */

124     public void requestFocus () {
125         // TODO: set the focus
126
// used to be :
127
// className.requestFocus();
128
// className.selectAll ();
129
}
130
131     /** Creates node that displays all packages.
132     */

133     private Node createPackagesNode () {
134         Node topNode = RepositoryNodeFactory.getDefault().repository(this);
135         Node [] nodes = topNode.getChildren ().getNodes (true);
136         assert nodes != null && nodes.length == 1 : "Only one subnode " + topNode + " found, but was " + Arrays.asList (nodes);
137         return nodes [0];
138     }
139
140     /** This method is called from within the constructor to
141      * initialize the form.
142      * WARNING: Do NOT modify this code. The content of this method is
143      * always regenerated by the FormEditor.
144      */

145     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
146
private void initComponents() {
147         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
148
149         packagesPanel = new org.netbeans.beaninfo.ExplorerPanel();
150         beanTreeView = new org.openide.explorer.view.BeanTreeView();
151         descriptionLabel = new javax.swing.JLabel JavaDoc();
152         packageLabel = new javax.swing.JLabel JavaDoc();
153         packageName = new javax.swing.JTextField JavaDoc();
154         dirLabel = new javax.swing.JLabel JavaDoc();
155         directoryName = new javax.swing.JTextField JavaDoc();
156         createButton = new javax.swing.JButton JavaDoc();
157
158         setLayout(new java.awt.BorderLayout JavaDoc());
159
160         packagesPanel.setLayout(new java.awt.GridBagLayout JavaDoc());
161
162         beanTreeView.setDefaultActionAllowed(false);
163         beanTreeView.setPopupAllowed(false);
164         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
165         gridBagConstraints.gridx = 0;
166         gridBagConstraints.gridy = 1;
167         gridBagConstraints.gridwidth = 3;
168         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
169         gridBagConstraints.weightx = 1.0;
170         gridBagConstraints.weighty = 1.0;
171         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 5, 0);
172         packagesPanel.add(beanTreeView, gridBagConstraints);
173
174         descriptionLabel.setLabelFor(beanTreeView);
175         org.openide.awt.Mnemonics.setLocalizedText(descriptionLabel, org.openide.util.NbBundle.getBundle(DataFolderPanel.class).getString("LAB_TargetLocationDescription")); // NOI18N
176
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
177         gridBagConstraints.gridx = 0;
178         gridBagConstraints.gridy = 0;
179         gridBagConstraints.gridwidth = 3;
180         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
181         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 2, 0);
182         packagesPanel.add(descriptionLabel, gridBagConstraints);
183
184         packageLabel.setLabelFor(packageName);
185         org.openide.awt.Mnemonics.setLocalizedText(packageLabel, org.openide.util.NbBundle.getBundle(DataFolderPanel.class).getString("LAB_package")); // NOI18N
186
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
187         gridBagConstraints.gridx = 0;
188         gridBagConstraints.gridy = 2;
189         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
190         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 5, 12);
191         packagesPanel.add(packageLabel, gridBagConstraints);
192         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
193         gridBagConstraints.gridx = 1;
194         gridBagConstraints.gridy = 2;
195         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
196         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
197         gridBagConstraints.weightx = 1.0;
198         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 5, 5);
199         packagesPanel.add(packageName, gridBagConstraints);
200
201         org.openide.awt.Mnemonics.setLocalizedText(dirLabel, org.openide.util.NbBundle.getBundle(DataFolderPanel.class).getString("LAB_directory")); // NOI18N
202
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
203         gridBagConstraints.gridx = 0;
204         gridBagConstraints.gridy = 3;
205         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
206         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 0, 12);
207         packagesPanel.add(dirLabel, gridBagConstraints);
208
209         directoryName.setEnabled(false);
210         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
211         gridBagConstraints.gridx = 1;
212         gridBagConstraints.gridy = 3;
213         gridBagConstraints.gridwidth = 2;
214         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
215         packagesPanel.add(directoryName, gridBagConstraints);
216
217         org.openide.awt.Mnemonics.setLocalizedText(createButton, org.openide.util.NbBundle.getBundle(DataFolderPanel.class).getString("CTL_Create")); // NOI18N
218
createButton.setEnabled(false);
219         createButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
220             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
221                 createButtonActionPerformed(evt);
222             }
223         });
224         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
225         gridBagConstraints.gridx = 2;
226         gridBagConstraints.gridy = 2;
227         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
228         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 5, 0);
229         packagesPanel.add(createButton, gridBagConstraints);
230
231         add(packagesPanel, java.awt.BorderLayout.CENTER);
232     }// </editor-fold>//GEN-END:initComponents
233

234   private void createButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_createButtonActionPerformed
235
try {
236             // create the folder
237
final DataFolder newDf = (DataFolder)getPropertyValue();
238             // TODO: this line does not work - because the Node is not there yet
239
setTargetFolder(newDf);
240             updateDirectory ();
241             updatePropertyEditor();
242             enableCreateButton();
243         } catch (IllegalStateException JavaDoc ex) {
244               throw new RuntimeException JavaDoc(ex.getMessage());
245         }
246   }//GEN-LAST:event_createButtonActionPerformed
247

248
249     // Variables declaration - do not modify//GEN-BEGIN:variables
250
private org.openide.explorer.view.BeanTreeView beanTreeView;
251     private javax.swing.JButton JavaDoc createButton;
252     private javax.swing.JLabel JavaDoc descriptionLabel;
253     private javax.swing.JLabel JavaDoc dirLabel;
254     private javax.swing.JTextField JavaDoc directoryName;
255     private javax.swing.JLabel JavaDoc packageLabel;
256     private javax.swing.JTextField JavaDoc packageName;
257     private org.netbeans.beaninfo.ExplorerPanel packagesPanel;
258     // End of variables declaration//GEN-END:variables
259

260     //
261
// Filter to accept only folders
262
//
263

264     /** Should the data object be displayed or not?
265     * @param obj the data object
266     * @return <CODE>true</CODE> if the object should be displayed,
267     * <CODE>false</CODE> otherwise
268     */

269     public boolean acceptDataObject(DataObject obj) {
270         return obj instanceof DataFolder;
271     }
272
273     /** Allow only simple selection.
274     */

275     public void vetoableChange(PropertyChangeEvent ev)
276     throws PropertyVetoException {
277         if (ExplorerManager.PROP_SELECTED_NODES.equals (ev.getPropertyName ())) {
278             Node[] arr = (Node[])ev.getNewValue();
279
280             if (arr.length > 1) {
281                 throw new PropertyVetoException ("Only single selection allowed", ev); // NOI18N
282
}
283         }
284     }
285
286     /** Changes in selected node in packages.
287     */

288     public void propertyChange (PropertyChangeEvent ev) {
289         if (ExplorerManager.PROP_SELECTED_NODES.equals (ev.getPropertyName ())) {
290             Node[] arr = packagesPanel.getExplorerManager ().getSelectedNodes ();
291             if (!isVisible()) {
292                 // in the case we are not shown don't update the panel's state
293
return;
294             }
295             if (arr.length == 1) {
296                 if (!isValid ()) {
297                     setTargetFolder (null);
298                     implSetDataFolder (null);
299                     return ;
300                 }
301                 DataFolder df = (DataFolder)arr[0].getCookie (DataFolder.class);
302                 if (df != null) {
303                     setTargetFolder (df);
304                     updatePropertyEditor();
305                     enableCreateButton();
306                     return;
307                 }
308             }
309             setTargetFolder (null);
310             implSetDataFolder (null);
311         }
312
313
314         if (PropertyEnv.PROP_STATE.equals(ev.getPropertyName()) && ev.getNewValue() == PropertyEnv.STATE_VALID) {
315             editor.setValue(getPropertyValue());
316         }
317
318     }
319
320     /** Fires info to listener.
321     */

322     private void fireStateChanged () {
323         if (listener != null) {
324             listener.stateChanged (new ChangeEvent JavaDoc (this));
325         }
326     }
327
328     //
329
// Modification of package name
330
//
331

332     public void changedUpdate(final javax.swing.event.DocumentEvent JavaDoc p1) {
333         if (p1.getDocument () == packageName.getDocument ()) {
334             SwingUtilities.invokeLater (new Runnable JavaDoc () {
335                                             public void run () {
336                                                 String JavaDoc text = packageName.getText ();
337                                                 if (text != null) {
338                                                     if (isValid()) {
339                                                         setTargetFolder (text, false);
340                                                         updatePropertyEditor();
341                                                     }
342                                                     updateDirectory ();
343                                                 }
344                                                 enableCreateButton();
345                                             }
346                                         });
347             return;
348         }
349     }
350
351     public void removeUpdate(final javax.swing.event.DocumentEvent JavaDoc p1) {
352         // when deleted => do no looking for folder
353
// changedUpdate (p1);
354
if (p1.getDocument () == packageName.getDocument ()) {
355             SwingUtilities.invokeLater(new Runnable JavaDoc () {
356                                             public void run () {
357                                                 if (packageName.getText ().length () == 0) {
358                                                     FileSystem fs = system.get ();
359                                                     if (fs != null) {
360                                                         DataFolder df = DataFolder.findFolder (fs.getRoot ());
361                                                         setTargetFolder (df);
362                                                         packageName.selectAll ();
363                                                     }
364                                                 }
365                                                 String JavaDoc text = packageName.getText ();
366                                                 if (text != null) {
367                                                     if (isValid()) {
368                                                         setTargetFolder (text, true);
369                                                         updatePropertyEditor();
370                                                    }
371                                                    updateDirectory ();
372                                                }
373                                                enableCreateButton();
374                                             }
375                                         });
376         }
377     }
378     
379     public void insertUpdate(final javax.swing.event.DocumentEvent JavaDoc p1) {
380         changedUpdate (p1);
381     }
382
383
384     /** Help for this panel.
385     * @return the help or <code>null</code> if no help is supplied
386     */

387     public org.openide.util.HelpCtx getHelp () {
388         return new HelpCtx (DataFolderPanel.class);
389     }
390
391     /** Test whether the panel is finished and it is safe to proceed to the next one.
392     * If the panel is valid, the "Next" (or "Finish") button will be enabled.
393     * @return <code>true</code> if the user has entered satisfactory information
394     */

395     public boolean isValid () {
396         String JavaDoc text = packageName.getText ();
397         if (text.length () == 0) {
398             Node[] arr = packagesPanel.getExplorerManager ().getSelectedNodes ();
399             if (arr.length == 1 && arr[0] == rootNode) {
400                 return false;
401             }
402         }
403
404         return true;
405     }
406
407     /** Add a listener to changes of the panel's validity.
408     * @param l the listener to add
409     * @see #isValid
410     */

411     public void addChangeListener (ChangeListener JavaDoc l) {
412         if (listener != null) throw new IllegalStateException JavaDoc ();
413
414         listener = l;
415     }
416
417     /** Remove a listener to changes of the panel's validity.
418     * @param l the listener to remove
419     */

420     public void removeChangeListener (ChangeListener JavaDoc l) {
421         listener = null;
422     }
423
424     /** Computes a suggestion for a given prefix and
425     * a list of file objects.
426     *
427     * @param node the node to start with
428     * @param pref prefix
429     * @param first [0] is the first node that satisfies the suggestion
430     * @return the longest continuation string for all folders that
431     * starts with prefix
432     */

433     private static String JavaDoc computeSuggestion (
434         Node node,
435         String JavaDoc pref,
436         Node[] first
437     ) {
438         Node[] arr = node.getChildren ().getNodes ();
439
440         String JavaDoc match = null;
441
442         for (int i = 0; i < arr.length; i++) {
443             String JavaDoc name = arr[i].getName ();
444             if (name.startsWith (pref)) {
445                 // ok, has the right prefix
446
if (match == null) {
447                     // first match
448
match = name;
449                     if (first != null) {
450                         first[0] = arr[i];
451                     }
452                 } else {
453                     // find common part of the names
454
int indx = pref.length ();
455                     int end = Math.min (name.length (), match.length ());
456                     while (indx < end && match.charAt (indx) == name.charAt (indx)) {
457                         indx++;
458                     }
459                     match = match.substring (0, indx);
460                 }
461             }
462         }
463
464         if (match == null ) { // why? || match.length () == pref.length ()) {
465
return null;
466         } else {
467             return match.substring (pref.length ());
468         }
469     }
470
471
472     /** Presets a target folder.
473     * @param f the folder
474     * @return true if succeeded
475     */

476     boolean setTargetFolder (final DataFolder f) {
477         boolean exact;
478         Node n = null;
479         String JavaDoc name;
480         
481         df = f;
482         
483         if (f != null) {
484             FileObject fo = f.getPrimaryFile ();
485             name = fo.getPath();
486
487             StringTokenizer JavaDoc st = new StringTokenizer JavaDoc (name, PATH_TOKEN_DELIMITER);
488             try {
489                 FileSystem fs = fo.getFileSystem ();
490
491                 if (fo.isRoot ()) {
492                     // bugfix #31645, possibility create new folder under root
493
name = packageName.getText().trim();
494                     // bugfix #32910, possibility create only for single folder
495
boolean withSubfolder = name.indexOf (File.separatorChar) != -1;
496                     if (fo.getFileObject (name) != null || withSubfolder) {
497                         name = ""; // NOI18N
498
}
499                 }
500
501                 system = new WeakReference<FileSystem> (fs);
502
503                 n = NodeOp.findPath(rootNode, NbCollections.checkedEnumerationByFilter(st, String JavaDoc.class, true));
504
505                 exact = true;
506                 
507             } catch (FileStateInvalidException ex) {
508                 // invalid state of file system => back to root
509
n = rootNode;
510                 name = ""; // NOI18N
511
exact = false;
512             } catch (NodeNotFoundException ex) {
513                 n = ex.getClosestNode();
514                 DataFolder df = (DataFolder)n.getCookie (DataFolder.class);
515                 if (df != null) {
516                     name = df.getPrimaryFile ().getPath ();
517                 } else {
518                     name = ""; // NO-I18N // NOI18N
519
}
520                 exact = false;
521             }
522
523
524         } else {
525             // null folder => use root
526
n = rootNode;
527             name = null;
528             exact = true;
529         }
530
531         // remove listener + do change + add listener
532
ExplorerManager em = packagesPanel.getExplorerManager ();
533         em.removePropertyChangeListener (this);
534         packageName.getDocument ().removeDocumentListener (this);
535
536         try {
537             em.setSelectedNodes (new Node[] { n });
538         } catch (PropertyVetoException ex) {
539             throw new InternalError JavaDoc ();
540         }
541
542         packageName.setText (name);
543         updateDirectory ();
544
545         packageName.getDocument ().addDocumentListener (this);
546         em.addPropertyChangeListener (this);
547
548         fireStateChanged ();
549
550         return exact;
551     }
552     
553     /** Getter for target folder. If the folder does not
554     * exists it is created at this point.
555     * @param create true if the target folder should be created.
556     * @return the target folder
557     * @exception IOException if the possible creation of the folder fails
558     */

559     private DataFolder getTargetFolder(boolean create) throws IOException JavaDoc {
560         if (create && isValid()) {
561             FileSystem fs = system.get ();
562             if (fs != null) {
563                 DataFolder folder = DataFolder.findFolder (fs.getRoot ());
564                 String JavaDoc currentName = packageName.getText().replace('\\', '/');
565                 if (currentName.length () > 0) {
566                     folder = DataFolder.create (folder, currentName);
567                 }
568                 df = folder;
569                 return folder;
570             }
571         }
572         return df;
573     }
574
575     /** Presets a target folder.
576     * @param f the name of target folder
577     * @return true if succeeded
578     */

579     private boolean setTargetFolder (final String JavaDoc f, boolean afterDelete) {
580         Node n = null;
581         NodeNotFoundException closest = null;
582
583         // first of all test the currently selected nod
584
// for location of closest
585
java.util.Collection JavaDoc<Node> selected = new java.util.HashSet JavaDoc<Node> ();
586         Node[] nodes = packagesPanel.getExplorerManager().getSelectedNodes();
587         for ( int i = 0; i < nodes.length; i++ ) {
588             Node n1 = nodes[i];
589             if ( n1.getParentNode() == null ) {
590                 continue;
591             }
592             while ( n1.getParentNode().getParentNode() != null )
593                 n1 = n1.getParentNode();
594             selected.add( n1 );
595         }
596
597         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc (f, PATH_TOKEN_DELIMITER);
598
599         try {
600             n = NodeOp.findPath(rootNode, NbCollections.checkedEnumerationByFilter(st, String JavaDoc.class, true));
601         } catch (NodeNotFoundException ex) {
602             if (!st.hasMoreElements ()) {
603                 // a test for !hasMoreElements is here to be sure that
604
// all tokens has been read, so only the last item
605
// has not been found
606

607                 // check whether we can continue from the nod
608
final String JavaDoc sugg = computeSuggestion (
609                                         ex.getClosestNode (),
610                                         ex.getMissingChildName(),
611                                         null
612                                     );
613
614                 if ( sugg != null ) {
615                     // if we can go on and there has been no suggestion o
616
// this is the current filesystem => go o
617
closest = ex;
618                 }
619             }
620         }
621
622         if (n != null) {
623             // closest node not used
624
closest = null;
625         } else {
626
627             if (closest == null) {
628                 // the node has not been even found
629
return false;
630             }
631
632             // we will select the closest node found - old version
633
n = closest.getClosestNode ();
634
635             // new - try to build shadow nodes hierarchy
636

637         }
638
639         // remove listener + do change + add listener
640
ExplorerManager em = packagesPanel.getExplorerManager ();
641         em.removePropertyChangeListener (this);
642
643         // change the text if we want to add suggestion
644
if (closest != null) {
645             Node[] first = new Node[1];
646             String JavaDoc sugg = computeSuggestion (
647                                     closest.getClosestNode (),
648                                     closest.getMissingChildName(),
649                                     first
650                                 );
651
652             if ( afterDelete && sugg != null && sugg.equals( last_suggestion ) )
653                 sugg = null;
654             
655             last_suggestion = sugg;
656             if (sugg != null) {
657                 packageName.getDocument ().removeDocumentListener (
658                     DataFolderPanel.this
659                 );
660
661                 packageName.setText (f + sugg);
662                 updateDirectory ();
663
664                 javax.swing.text.Caret JavaDoc c = packageName.getCaret ();
665                 c.setDot (f.length () + sugg.length ());
666                 c.moveDot (f.length ());
667
668                 packageName.getDocument ().addDocumentListener (
669                     DataFolderPanel.this
670                 );
671             }
672
673             if (first[0] != null) {
674                 // show the first node that fits
675
n = first[0];
676             }
677         }
678
679
680         // change the node
681
try {
682             em.setSelectedNodes(new Node[] { n });
683             //beanTreeView.selectionChanged(new Node[] { n }, em);
684
} catch (PropertyVetoException ex) {
685             throw new InternalError JavaDoc ();
686         }
687
688         // change the selected filesystem
689
df = (DataFolder)n.getCookie (DataFolder.class);
690         if (df != null) {
691             try {
692                 FileSystem fs = df.getPrimaryFile ().getFileSystem ();
693                 system = new WeakReference<FileSystem> (fs);
694             } catch (FileStateInvalidException ex) {
695             }
696         }
697
698
699         em.addPropertyChangeListener (this);
700
701         fireStateChanged ();
702
703         return closest == null;
704     }
705
706     /** Updates directory name
707     */

708     void updateDirectory () {
709         FileSystem fs = system.get ();
710         if (fs == null) {
711             // No known directory?? Leave it blank.
712
directoryName.setText(""); // NOI18N
713
return;
714         }
715         String JavaDoc name = packageName.getText ();
716         FileObject folder = fs.findResource(name);
717         if (folder != null) {
718             File JavaDoc f = FileUtil.toFile(folder);
719             if (f != null) {
720                 // A folder is selected which exists on disk.
721
directoryName.setText(f.getAbsolutePath());
722             } else {
723                 // A folder is selected which is nowhere on disk (e.g. in a JAR).
724
directoryName.setText(""); // NOI18N
725
}
726         } else {
727             FileObject fo = fs.getRoot();
728             assert fo != null : fs;
729             File JavaDoc f = FileUtil.toFile(fo);
730             if (f != null) {
731                     // The folder does not really exist, but the FS root does
732
// exist on disk. Guess that the resulting file name will
733
// be derived simply from the folder of the root (not always
734
// true, note).
735
File JavaDoc f2 = new File JavaDoc(f, name.replace('/', File.separatorChar)); // NOI18N
736
directoryName.setText(f2.getAbsolutePath());
737                 } else {
738                     // The folder has not been made, and even if it were, the FS
739
// root is not on disk anyway. Leave it blank.
740
directoryName.setText(""); // NOI18N
741
}
742         }
743     }
744     
745     // bugfix #29401, correct notify all changes in data folders
746
private void implSetDataFolder (DataFolder df) {
747         if (editor != null) {
748             if (!isValid ()) {
749                 editor.setDataFolder (null);
750             } else {
751                 FileSystem fs = null;
752                 if (system != null) {
753                     fs = system.get();
754                 }
755                 if (df == null && fs!= null) {
756                     FileObject fo = fs.getRoot();
757                     //issue 34896, for whatever reason the root is sometimes null
758
if (fo != null) {
759                         df = DataFolder.findFolder (fo);
760                     }
761                 }
762                 if (df != null) {
763                     String JavaDoc name = df.getPrimaryFile ().getPath ();
764                     if (name.equals(packageName.getText())) {
765                         editor.setDataFolder (df);
766                     } else {
767                         editor.setDataFolder (null);
768                     }
769                 } else {
770                     editor.setDataFolder(null);
771                 }
772             }
773         }
774     }
775
776     /** Updates associated editor by calling setDataFolder(...) . */
777     void updatePropertyEditor() {
778         try {
779             DataFolder newF = getTargetFolder(false);
780             //fix for issue 31434, DataFolder may be null if
781
//user used the search popup in the target folder tree
782
implSetDataFolder (newF);
783         } catch (IOException JavaDoc ex) {
784              Exceptions.printStackTrace(ex);
785         }
786     }
787     
788     /** Sets the state of the createButton */
789     void enableCreateButton() {
790         String JavaDoc name = null;
791         if (df != null) {
792             name = df.getPrimaryFile ().getPath ();
793         } else {
794             name = ""; // NOI18N
795
}
796         if (name.equals(packageName.getText())) {
797             // nothing to create
798
createButton.setEnabled(false);
799         } else {
800             createButton.setEnabled(isValid());
801         }
802     }
803         
804     
805     /** Get the customized property value.
806      * @return the property value
807      * @exception InvalidStateException when the custom property editor does not contain a valid property value
808      * (and thus it should not be set)
809      */

810     private Object JavaDoc getPropertyValue() throws IllegalStateException JavaDoc {
811         if (isValid()) {
812             try {
813                 df = getTargetFolder(true);
814                 return df;
815             } catch (IOException JavaDoc x) {
816                 Exceptions.printStackTrace(x);
817                 throw new IllegalStateException JavaDoc();
818             }
819         } else {
820             throw new IllegalStateException JavaDoc();
821         }
822     }
823     public static class ShadowDirNode extends AbstractNode {
824         public ShadowDirNode(Children children) {
825             super(children);
826         }
827     }
828
829     public static class ShadowLeafNode extends AbstractNode {
830         public ShadowLeafNode() {
831             super(Children.LEAF);
832         }
833     }
834
835     private static String JavaDoc getString (String JavaDoc s) {
836         return org.openide.util.NbBundle.getBundle (DataFolderPanel.class).getString (s);
837     }
838 }
839
Popular Tags