KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
23 import java.beans.*;
24 import java.io.File JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.util.*;
27 import java.util.logging.Level JavaDoc;
28 import java.util.logging.Logger JavaDoc;
29 import javax.swing.*;
30 import javax.swing.border.*;
31 import javax.swing.filechooser.FileSystemView JavaDoc;
32 import javax.swing.filechooser.FileView JavaDoc;
33 import org.netbeans.beaninfo.editors.DataObjectPanel.FilteredChildren;
34 import org.openide.explorer.propertysheet.PropertyEnv;
35 import org.openide.filesystems.FileObject;
36 import org.openide.loaders.*;
37 import org.openide.nodes.*;
38 import org.openide.util.*;
39
40 /**
41  * Component that displays an explorer that displays only certain
42  * nodes. Similar to the node selector (retrieved from the TopManager)
43  * but arranged a bit differently, plus allows the user to set the
44  * currently selected node.
45  * @author Joe Warzecha
46  */

47 public class DataObjectListView extends DataObjectPanel implements PropertyChangeListener {
48     
49     final static int DEFAULT_INSET = 10;
50     
51     private JFileChooser chooser;
52     
53     File JavaDoc rootFile;
54     
55     /** We must keep created filtered root node because rootNode can be reset any time
56      * eg. when setOkButtonEnabled() is called and we need filtered root node to traverse
57      * node hierarchy up. */

58     Node filteredRootNode;
59     
60     public DataObjectListView (PropertyEditorSupport my, PropertyEnv env) {
61         super(my, env);
62     }
63     
64     public void addNotify() {
65         completeInitialization();
66         super.addNotify();
67     }
68     
69     private boolean initialized=false;
70     
71     /** Called from addNotify. */
72     @SuppressWarnings JavaDoc("deprecation")
73     private void completeInitialization() {
74         if (initialized) {
75             //Do not re-initialize if the dialog has already been used,
76
//otherwise we will end up listening to the wrong thing and
77
//the OK button will never be enabled
78
return;
79         }
80         if (insets != null) {
81             setBorder(new EmptyBorder(insets));
82         } else {
83             setBorder(new EmptyBorder(12, 12, 0, 11));
84         }
85         setLayout(new BorderLayout(0, 2));
86         
87         //TODO Is it possible to set any label for JFileChooser?
88
/*if (subTitle != null) {
89             JLabel l = new JLabel(subTitle);
90             l.setLabelFor(reposTree);
91             add(l, BorderLayout.NORTH);
92         }*/

93         
94         filteredRootNode = rootNode;
95         if (filteredRootNode == null) {
96             if (dataFilter != null) {
97                 if (folderFilter != null) {
98                     DataFilter dFilter = new DataFilter() {
99                         public boolean acceptDataObject(DataObject obj) {
100                             if (folderFilter.acceptDataObject(obj)) {
101                                 return true;
102                             }
103                             return dataFilter.acceptDataObject(obj);
104                         }
105                     };
106                     filteredRootNode = RepositoryNodeFactory.getDefault().repository(dFilter);
107                 } else {
108                     filteredRootNode = RepositoryNodeFactory.getDefault().repository(dataFilter);
109                 }
110             } else {
111                 if (folderFilter != null) {
112                     filteredRootNode = RepositoryNodeFactory.getDefault().repository(folderFilter);
113                 } else {
114                     filteredRootNode = RepositoryNodeFactory.getDefault().repository(DataFilter.ALL);
115                 }
116             }
117         }
118
119         if (nodeFilter != null) {
120             FilteredChildren children =
121                 new FilteredChildren(filteredRootNode, nodeFilter, dataFilter);
122             FilterNode n = new FilterNode(filteredRootNode, children);
123             filteredRootNode = n;
124         }
125         
126         if (rootObject != null) {
127             Node n = findNodeForObj(filteredRootNode, rootObject);
128             if (n != null) {
129                 NodeAcceptor naccep = nodeFilter;
130                 if (naccep == null) {
131                     naccep = new NodeAcceptor() {
132                         public boolean acceptNodes(Node [] nodes) {
133                             return false;
134                         }
135                     };
136                 }
137                 FilteredChildren children =
138                     new FilteredChildren(n, naccep, dataFilter);
139                 FilterNode filtNode = new FilterNode(n, children);
140                 filteredRootNode = filtNode;
141             }
142         }
143         
144         rootFile = new NodeFile(getFileName(filteredRootNode), filteredRootNode);
145         
146         //Create instance AFTER root file is set!!!
147
chooser = new NodeFileChooser(rootFile, new NodeFileSystemView());
148         FileEditor.hackFileChooser(chooser);
149         //We must initialize it after JFileChooser is created
150
if (description != null) {
151             setDescription(description);
152         } else {
153             //Set default value
154
setDescription(NbBundle.getMessage(DataObjectListView.class, "ACSD_DataObjectPanel"));
155         }
156         
157         chooser.setControlButtonsAreShown(false);
158         chooser.setMultiSelectionEnabled(multiSelection);
159         chooser.setFileSelectionMode(selectionMode);
160         chooser.addPropertyChangeListener(this);
161         
162         //set initial selection
163
if (dObj != null) {
164             String JavaDoc path = findPathTo(filteredRootNode, dObj);
165             chooser.setCurrentDirectory(new NodeFile(path, dObj.getNodeDelegate()));
166         }
167         
168         add(chooser, BorderLayout.CENTER);
169
170         if (multiSelection) {
171             DataObject [] dObjArr = getDataObjects();
172             if ((dataFilter != null) && (dObjArr != null)) {
173                 boolean b = false;
174                 for (int i = 0; i < dObjArr.length; i++) {
175                     if (dataFilter.acceptDataObject(dObjArr[i])) {
176                         b = true;
177                         break;
178                     }
179                 }
180                 setOkButtonEnabled(b);
181             } else {
182                 setOkButtonEnabled(dObjArr != null);
183             }
184         } else {
185             if ((dataFilter != null) && (getDataObject() != null)) {
186                 setOkButtonEnabled(
187                     dataFilter.acceptDataObject(getDataObject()));
188             } else {
189                 setOkButtonEnabled(getDataObject() != null);
190             }
191         }
192         initialized=true;
193     }
194     
195     private static String JavaDoc findPathTo(Node rootNode, DataObject dobj) {
196         Stack<DataObject> st = new Stack<DataObject>();
197         DataObject o = dobj;
198
199         while (o != null) {
200             st.push(o);
201             o = o.getFolder();
202         }
203         
204         Children children = rootNode.getChildren();
205         Node n = null;
206         while (n == null && !st.isEmpty()) {
207             o = st.pop();
208             n = children.findChild(o.getNodeDelegate().getName());
209             
210             if (n == null) {
211                 Node [] nodes = children.getNodes(true);
212                 for (int i = 0; (i < nodes.length) && (n == null); i++) {
213                     DataObject oo = (DataObject) nodes [i].getCookie(DataObject.class);
214                     if ((oo != null) && oo == o) {
215                         n = nodes [i];
216                     }
217                 }
218             }
219         }
220         StringBuilder JavaDoc path = new StringBuilder JavaDoc(getFileName(rootNode));
221         if (n != null) {
222             path.append( File.separator ).append( getFileName(n) );
223
224             while (!st.isEmpty()) {
225                 Node nn = st.pop().getNodeDelegate();
226                 path.append( File.separator ).append( getFileName(nn));
227             }
228         }
229         
230         return path.toString();
231     }
232     
233     /**
234      * Tries to retrieve unique file name from Node -> DataObject -> PrimaryFile
235      * if possible. Used to set file name for NodeFile representing node in JFileChooser.
236      */

237     static String JavaDoc getFileName (Node n) {
238         DataObject dObj = (DataObject) n.getCookie(DataObject.class);
239         if (dObj != null) {
240             FileObject pf = dObj.getPrimaryFile();
241             if (pf.isRoot()) {
242                 return n.getDisplayName();
243             } else {
244                 return pf.getNameExt();
245             }
246         } else {
247             return n.getDisplayName();
248         }
249     }
250     
251     /**
252      * Sets description of the panel.
253      *
254      * @param desc Desciption of the panel.
255      */

256     public void setDescription(String JavaDoc desc) {
257         this.description = desc;
258         getAccessibleContext().setAccessibleDescription(desc);
259         if (chooser != null) {
260             chooser.getAccessibleContext().setAccessibleDescription(desc);
261         }
262     }
263     
264     /** Finds node by path from root node. It can return null if node is not found.
265      */

266     private Node findNode (String JavaDoc path) {
267         //Find node corresponding to given path
268
Node n = filteredRootNode;
269         String JavaDoc p = path;
270         String JavaDoc fileName;
271         int ind = p.indexOf(File.separatorChar);
272         if (ind != -1) {
273             fileName = p.substring(0, ind);
274             p = p.substring(ind + 1);
275         } else {
276             fileName = p;
277         }
278         fileName = fileName.replace('#',File.separatorChar);
279         
280         //Root node must correspond to root file
281
/*if (!fileName.equals(n.getDisplayName())) {
282             System.out.println("########### ERROR folder name and node display name does not match #########");
283             System.out.println("fileName:" + fileName
284             + " nodeName:" + n.getDisplayName());
285         }*/

286
287         while (ind != -1) {
288             Node [] nodes = n.getChildren().getNodes(true);
289             ind = p.indexOf(File.separatorChar);
290             if (ind != -1) {
291                 fileName = p.substring(0, ind);
292                 p = p.substring(ind + 1);
293             } else {
294                 fileName = p;
295             }
296             fileName = fileName.replace('#',File.separatorChar);
297             //Find node with the same name
298
for (int i = 0; i < nodes.length; i++) {
299                 if (fileName.equals(getFileName(nodes[i]))) {
300                     n = nodes[i];
301                     break;
302                 }
303             }
304         }
305         
306         //Check if node was found
307
if (!fileName.equals(getFileName(n))) {
308             return null;
309         }
310         
311         return n;
312     }
313     
314     /** Creates node by path from root node. It either returns existing node as
315      * findNode does or creates instance of FakeNode. Created node is NOT added
316      * to node hierarchy.
317      */

318     private Node createNode (String JavaDoc path) {
319         //Find node corresponding to given path
320
Node n = filteredRootNode;
321         Node parent = null;
322         String JavaDoc p = path;
323         String JavaDoc fileName;
324         int ind = p.indexOf(File.separatorChar);
325         if (ind != -1) {
326             fileName = p.substring(0, ind);
327             p = p.substring(ind + 1);
328         } else {
329             fileName = p;
330         }
331         fileName = fileName.replace('#',File.separatorChar);
332         
333         //Root node must correspond to root file
334
/*if (!fileName.equals(n.getDisplayName())) {
335             System.out.println("########### ERROR folder name and node display name does not match #########");
336             System.out.println("fileName:" + fileName
337             + " nodeName:" + n.getDisplayName());
338         }*/

339
340         while (ind != -1) {
341             Node [] nodes = n.getChildren().getNodes(true);
342             parent = n;
343             ind = p.indexOf(File.separatorChar);
344             if (ind != -1) {
345                 fileName = p.substring(0, ind);
346                 p = p.substring(ind + 1);
347             } else {
348                 fileName = p;
349             }
350             fileName = fileName.replace('#',File.separatorChar);
351             //Find node with the same name
352
for (int i = 0; i < nodes.length; i++) {
353                 if (fileName.equals(getFileName(nodes[i]))) {
354                     n = nodes[i];
355                     break;
356                 }
357             }
358         }
359         
360         if (!fileName.equals(getFileName(n))) {
361             //Create new node
362
n = new FakeNode(Children.LEAF);
363             n.setDisplayName(fileName.replace('#',File.separatorChar));
364         }
365         
366         /*if (parent != null) {
367             System.out.println("ADD NODE TO PARENT");
368             parent.getChildren().add(new Node [] { n });
369         }*/

370         
371         return n;
372     }
373     
374     /**
375      * Return the currently selected DataObject.
376      * @return The currently selected DataObject or null if there is no node seleted
377      */

378     public DataObject getDataObject() {
379         DataObject retValue = null;
380         if (!multiSelection) {
381             File JavaDoc f = chooser.getSelectedFile();
382             if (f instanceof NodeFile) {
383                 Node n = ((NodeFile) f).getNode();
384                 if (n != null) {
385                     retValue = (DataObject) n.getCookie(DataObject.class);
386                 }
387             }
388         }
389         return retValue;
390     }
391     
392     /**
393      * Return the currently selected Node.
394      * @return The currently selected Node or null if there is no node seleted
395      */

396     public Node getNode() {
397         Node retValue = null;
398         if (!multiSelection) {
399             File JavaDoc f = chooser.getSelectedFile();
400             if (f instanceof NodeFile) {
401                 retValue = ((NodeFile) f).getNode();
402             }
403         }
404         return retValue;
405     }
406     
407     /**
408      * Return the currently selected DataObject.
409      * @return The currently selected DataObject or null if there is no node seleted
410      */

411     public DataObject [] getDataObjects () {
412         DataObject [] retValue = null;
413         if (multiSelection) {
414             File JavaDoc [] f = chooser.getSelectedFiles();
415             retValue = new DataObject [f.length];
416             for (int i = 0; i < f.length; i++) {
417                 if (f[i] instanceof NodeFile) {
418                     Node n = ((NodeFile) f[i]).getNode();
419                     if (n != null) {
420                         retValue[i] = (DataObject) n.getCookie(DataObject.class);
421                     }
422                 }
423             }
424         }
425         return retValue;
426     }
427     
428     /**
429      * Return the currently selected Node.
430      * @return The currently selected Node or null if there is no node seleted
431      */

432     public Node [] getNodes () {
433         Node [] retValue = null;
434         if (multiSelection) {
435             File JavaDoc [] f = chooser.getSelectedFiles();
436             retValue = new Node [f.length];
437             for (int i = 0; i < f.length; i++) {
438                 if (f[i] instanceof NodeFile) {
439                     retValue[i] = ((NodeFile) f[i]).getNode();
440                 }
441             }
442         }
443         return retValue;
444     }
445     
446     /** Get the customized property value.
447      * @return the property value
448      * @exception InvalidStateException when the custom property editor does not contain a valid property value
449      * (and thus it should not be set)
450      */

451     public Object JavaDoc getPropertyValue() throws IllegalStateException JavaDoc {
452         if (multiSelection) {
453             return getDataObjects();
454         } else {
455             return getDataObject();
456         }
457     }
458     
459     /** Property change listaner attached to the JFileChooser chooser. */
460     public void propertyChange(PropertyChangeEvent e) {
461         if (JFileChooser.SELECTED_FILES_CHANGED_PROPERTY.equals(e.getPropertyName()) ||
462             JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals(e.getPropertyName())) {
463             File JavaDoc[] selFiles = (File JavaDoc[]) chooser.getSelectedFiles();
464             if (selFiles == null) {
465                 return;
466             }
467
468             if ((selFiles.length == 0) && (chooser.getSelectedFile() != null)) {
469                 selFiles = new File JavaDoc[] { chooser.getSelectedFile() };
470             }
471                         
472             Node [] nodes = new Node [selFiles.length];
473             for (int i = 0; i < selFiles.length; i++) {
474                 if (selFiles[i] instanceof NodeFile) {
475                     //Get node directly
476
nodes[i] = ((NodeFile) selFiles[i]).getNode();
477                 } else {
478                     //Try to find node by path
479
nodes[i] = findNode(selFiles[i].getPath());
480                 }
481             }
482             
483             ArrayList<DataObject> dObjList = new ArrayList<DataObject>(selFiles.length);
484             for (int i = 0; i < nodes.length; i++) {
485                 if (nodes[i] != null) {
486                     DataObject dObj = (DataObject) nodes[i].getCookie(DataObject.class);
487                     if (dObj != null) {
488                         if (dataFilter != null) {
489                             if (dataFilter.acceptDataObject(dObj)) {
490                                 dObjList.add(dObj);
491                             }
492                         } else {
493                             dObjList.add(dObj);
494                         }
495                     }
496                 }
497             }
498             
499             DataObject [] dObjArray = dObjList.toArray(new DataObject[dObjList.size()]);
500             boolean enableOK = false;
501             if (dObjArray.length > 0) {
502                 enableOK = true;
503             } else {
504                 enableOK = false;
505             }
506             if (multiSelection) {
507                 myEditor.setValue(dObjArray);
508             } else {
509                 if (dObjArray.length > 0) {
510                     myEditor.setValue(dObjArray[0]);
511                 } else {
512                     myEditor.setValue(null);
513                 }
514             }
515             setOkButtonEnabled(enableOK);
516         }
517     }
518     
519     /** Fake node used to create NodeFile for nonexisting node. JFileChooser calls
520      * FileSystemView.createFileObject() when renaming existing file.
521      */

522     private static class FakeNode extends AbstractNode {
523         
524         public FakeNode (Children children) {
525             super(children);
526         }
527     }
528     
529     /** Used by JFileChooser to display File instances from our fake
530      * file system representing node hierarchy.
531      */

532     private class NodeFile extends File JavaDoc {
533         private Node n;
534         
535         NodeFile (String JavaDoc path, Node n) {
536             super(path);
537             this.n = n;
538         }
539         
540         NodeFile (File JavaDoc parent, String JavaDoc child, Node n) {
541             super(parent,child);
542             this.n = n;
543         }
544         
545         public boolean canRead() {
546             return true;
547         }
548         
549         public boolean canWrite() {
550             return true;
551         }
552         
553         public boolean renameTo (File JavaDoc dest) {
554             DataObject dObj = (DataObject) n.getCookie(DataObject.class);
555             if (dObj != null) {
556                 try {
557                     dObj.rename(dest.getName());
558                 } catch (IOException JavaDoc exc) {
559                     Logger.getLogger(DataObjectListView.class.getName()).log(Level.WARNING, null, exc);
560                     return false;
561                 }
562                 return true;
563             } else {
564                 return false;
565             }
566         }
567         
568         public File JavaDoc[] listFiles () {
569             Node [] nodes = n.getChildren().getNodes(true);
570             NodeFile [] files = new NodeFile[nodes.length];
571             for (int i = 0; i < nodes.length; i++) {
572                 String JavaDoc name = getFileName(nodes[i]);
573                 name = name.replace(File.separatorChar,'#');
574                 files[i] = new NodeFile(getPath() + File.separator + name, nodes[i]);
575             }
576             return files;
577         }
578         
579         public String JavaDoc getName () {
580             if (n != null) {
581                 return n.getDisplayName();
582             } else {
583                 return super.getName();
584             }
585         }
586         
587         public File JavaDoc getParentFile () {
588             String JavaDoc p = getParent();
589             if (p == null) {
590                 return null;
591             }
592             if (n == null) {
593                 return null;
594             }
595             Node parent = findNode(p);
596             if (parent == null) {
597                 return null;
598             }
599             return new NodeFile(p, parent);
600         }
601         
602         public boolean exists () {
603             Node n = findNode(getPath());
604             if (n != null) {
605                 if (n instanceof FakeNode) {
606                     return false;
607                 } else {
608                     return true;
609                 }
610             } else {
611                 return false;
612             }
613         }
614         
615         public boolean isAbsolute () {
616             String JavaDoc p = getPath();
617             int ind = p.indexOf(File.separatorChar);
618             if (ind != -1) {
619                 //Get root of path
620
p = p.substring(0, ind);
621             }
622             p = p.replace('#',File.separatorChar);
623             if (p.equals(getFileName(filteredRootNode))) {
624                 return true;
625             } else {
626                 return false;
627             }
628         }
629         
630         public boolean isDirectory () {
631             if (n == null) {
632                 return false;
633             }
634             /*Node [] nodes = n.getChildren().getNodes(true);
635             System.out.println("NodeFile.isDirectory sz:" + nodes.length);
636             if (nodes.length > 0) {
637                 System.out.println("NodeFile.isDirectory LEAVE 2 true f:" + this);
638                 return true;
639             } else {
640                 System.out.println("NodeFile.isDirectory LEAVE 2 false f:" + this);
641                 return false;
642             }*/

643             DataObject dObj = (DataObject) n.getCookie(DataObject.class);
644             if (dObj != null) {
645                 if (dObj instanceof DataFolder) {
646                     return true;
647                 } else {
648                     return false;
649                 }
650             } else {
651                 //Always root??
652
return true;
653             }
654         }
655         
656         public boolean isFile () {
657             if (n == null) {
658                 return true;
659             }
660             /*Node [] nodes = n.getChildren().getNodes(true);
661             System.out.println("NodeFile.isFile sz:" + nodes.length);
662             if (nodes.length > 0) {
663                 System.out.println("NodeFile.isFile LEAVE 2 false f:" + this);
664                 return false;
665             } else {
666                 System.out.println("NodeFile.isFile LEAVE 3 true f:" + this);
667                 return true;
668             }*/

669             DataObject dObj = (DataObject) n.getCookie(DataObject.class);
670             if (dObj != null) {
671                 if (dObj instanceof DataFolder) {
672                     return false;
673                 } else {
674                     return true;
675                 }
676             } else {
677                 //Always root??
678
return false;
679             }
680         }
681         
682         public Icon getIcon () {
683             return new ImageIcon(n.getIcon(BeanInfo.ICON_COLOR_16x16));
684         }
685         
686         public String JavaDoc getAbsolutePath() {
687             return getPath();
688         }
689         
690         public File JavaDoc getAbsoluteFile() {
691             return new NodeFile(getAbsolutePath(), n);
692         }
693         
694         public String JavaDoc getCanonicalPath() throws IOException JavaDoc {
695             return getPath();
696         }
697         
698         public File JavaDoc getCanonicalFile() throws IOException JavaDoc {
699             return new NodeFile(getCanonicalPath(), n);
700         }
701         
702         public Node getNode () {
703             return n;
704         }
705         
706     }
707     
708     /** Used by JFileChooser to display File instances from our fake
709      * file system representing node hierarchy.
710      */

711     private class NodeFileView extends FileView JavaDoc {
712         
713         NodeFileView () {
714             super();
715         }
716         
717         public String JavaDoc getName (File JavaDoc f) {
718             if (f instanceof NodeFile) {
719                 return f.getName();
720             } else {
721                 //Try to locate corresponding node by path
722
Node n = findNode(f.getPath());
723                 if (n != null) {
724                     return n.getDisplayName();
725                 } else {
726                     return null;
727                 }
728             }
729         }
730         
731         public Icon getIcon (File JavaDoc f) {
732             if (f instanceof NodeFile) {
733                 return ((NodeFile) f).getIcon();
734             } else {
735                 //Try to locate corresponding node by path
736
Node n = findNode(f.getPath());
737                 if (n != null) {
738                     return new ImageIcon(n.getIcon(BeanInfo.ICON_COLOR_16x16));
739                 } else {
740                     return null;
741                 }
742             }
743         }
744     }
745     
746     /** Used by JFileChooser to display File instances from our fake
747      * file system representing node hierarchy.
748      */

749     private class NodeFileSystemView extends FileSystemView JavaDoc {
750         private final String JavaDoc newFolderString =
751                 UIManager.getString("FileChooser.other.newFolder"); // NOI18N
752

753         NodeFileSystemView () {
754             super();
755         }
756         
757         /**
758          * Determines if the given file is a root partition or drive.
759          */

760         public boolean isRoot(File JavaDoc f) {
761             return rootFile.equals(f);
762         }
763         
764         /** Creates a new folder with a default folder name.
765          *
766          */

767         public File JavaDoc createNewFolder(File JavaDoc containingDir) throws IOException JavaDoc {
768             String JavaDoc path = containingDir.getPath() + File.separator + newFolderString;
769             Node n = findNode(path);
770             if (n != null) {
771                 return new NodeFile(path, n);
772             } else {
773                 Node parent = findNode(containingDir.getPath());
774                 if (parent == null) {
775                     return null;
776                 }
777                 DataObject dObj = (DataObject) parent.getCookie(DataObject.class);
778                 if (dObj != null) {
779                     if (dObj instanceof DataFolder) {
780                         DataFolder.create((DataFolder) dObj, newFolderString);
781                     } else {
782                         return null;
783                     }
784                 } else {
785                     return null;
786                 }
787                 n = createNode(path);
788                 return new NodeFile(path, n);
789             }
790         }
791         
792         public File JavaDoc createFileObject(File JavaDoc dir, String JavaDoc filename) {
793             filename = filename.replace(File.separatorChar,'#');
794             //Find node corresponding to given path
795
String JavaDoc path = dir.getPath() + File.separator + filename;
796             Node n = findNode(path);
797             if (n == null) {
798                 n = createNode(path);
799             }
800             return new NodeFile(path, n);
801         }
802
803         /**
804          * Returns a File object constructed from the given path string.
805          */

806         public File JavaDoc createFileObject(String JavaDoc path) {
807             //Find node corresponding to given path
808
Node n = findNode(path);
809             if (n == null) {
810                 n = createNode(path);
811             }
812             return new NodeFile(path, n);
813         }
814         
815         /**
816          * Returns whether a file is hidden or not.
817          */

818         public boolean isHiddenFile(File JavaDoc f) {
819             return false;
820         }
821         
822         /**
823          * Returns all root partitians on this system. For example, on Windows,
824          * this would be the A: through Z: drives.
825          */

826         public File JavaDoc[] getRoots() {
827             return new NodeFile [] { (NodeFile) rootFile };
828         }
829         
830         public File JavaDoc getHomeDirectory() {
831             return rootFile;
832         }
833         
834         public File JavaDoc[] getFiles (File JavaDoc dir, boolean useFileHiding) {
835             if (dir instanceof NodeFile) {
836                 return dir.listFiles();
837             } else {
838                 return super.getFiles(dir, useFileHiding);
839             }
840         }
841         
842         public File JavaDoc getParentDirectory (File JavaDoc dir) {
843             if (dir != null) {
844                 File JavaDoc f = createFileObject(dir.getPath());
845                 return f.getParentFile();
846             }
847             return null;
848         }
849         
850         public String JavaDoc getSystemDisplayName (File JavaDoc f) {
851             return f.getName();
852         }
853         
854     }
855     
856     /** Extended JFileChooser. We have to overwrite some methods because
857      * UI implementation creates its own instances of java.io.File -> it causes
858      * trouble with our fake filesystem for example java.io.File.exists() returns
859      * false when our fake path is provided. */

860     public class NodeFileChooser extends JFileChooser {
861         
862         NodeFileChooser (File JavaDoc currentDirectory, FileSystemView JavaDoc fsv) {
863             super(currentDirectory, fsv);
864         }
865         
866         protected void setup(FileSystemView JavaDoc view) {
867             // MCF Bug 4972092
868
// If the FileView is not set, the calls to getIcon will trigger
869
// null pointer exceptions. And getIcon is called indirectly from
870
// the super version of JFileChooser.setup
871
setFileView(new DataObjectListView.NodeFileView());
872             super.setup(view);
873         }
874         
875         public void setCurrentDirectory (File JavaDoc dir) {
876             if ((dir != null) && !(dir instanceof NodeFile)) {
877                 Node n = findNode(dir.getPath());
878                 dir = new NodeFile(dir.getPath(), n);
879             }
880             super.setCurrentDirectory(dir);
881         }
882         
883     }
884 }
885
Popular Tags