1 19 20 package org.netbeans.beaninfo.editors; 21 22 import java.awt.*; 23 import java.beans.*; 24 import java.io.File ; 25 import java.io.IOException ; 26 import java.util.*; 27 import java.util.logging.Level ; 28 import java.util.logging.Logger ; 29 import javax.swing.*; 30 import javax.swing.border.*; 31 import javax.swing.filechooser.FileSystemView ; 32 import javax.swing.filechooser.FileView ; 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 47 public class DataObjectListView extends DataObjectPanel implements PropertyChangeListener { 48 49 final static int DEFAULT_INSET = 10; 50 51 private JFileChooser chooser; 52 53 File rootFile; 54 55 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 72 @SuppressWarnings ("deprecation") 73 private void completeInitialization() { 74 if (initialized) { 75 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 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 chooser = new NodeFileChooser(rootFile, new NodeFileSystemView()); 148 FileEditor.hackFileChooser(chooser); 149 if (description != null) { 151 setDescription(description); 152 } else { 153 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 if (dObj != null) { 164 String 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 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 path = new StringBuilder (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 237 static String 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 256 public void setDescription(String desc) { 257 this.description = desc; 258 getAccessibleContext().setAccessibleDescription(desc); 259 if (chooser != null) { 260 chooser.getAccessibleContext().setAccessibleDescription(desc); 261 } 262 } 263 264 266 private Node findNode (String path) { 267 Node n = filteredRootNode; 269 String p = path; 270 String 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 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 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 if (!fileName.equals(getFileName(n))) { 308 return null; 309 } 310 311 return n; 312 } 313 314 318 private Node createNode (String path) { 319 Node n = filteredRootNode; 321 Node parent = null; 322 String p = path; 323 String 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 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 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 n = new FakeNode(Children.LEAF); 363 n.setDisplayName(fileName.replace('#',File.separatorChar)); 364 } 365 366 370 371 return n; 372 } 373 374 378 public DataObject getDataObject() { 379 DataObject retValue = null; 380 if (!multiSelection) { 381 File 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 396 public Node getNode() { 397 Node retValue = null; 398 if (!multiSelection) { 399 File f = chooser.getSelectedFile(); 400 if (f instanceof NodeFile) { 401 retValue = ((NodeFile) f).getNode(); 402 } 403 } 404 return retValue; 405 } 406 407 411 public DataObject [] getDataObjects () { 412 DataObject [] retValue = null; 413 if (multiSelection) { 414 File [] 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 432 public Node [] getNodes () { 433 Node [] retValue = null; 434 if (multiSelection) { 435 File [] 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 451 public Object getPropertyValue() throws IllegalStateException { 452 if (multiSelection) { 453 return getDataObjects(); 454 } else { 455 return getDataObject(); 456 } 457 } 458 459 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 [] selFiles = (File []) chooser.getSelectedFiles(); 464 if (selFiles == null) { 465 return; 466 } 467 468 if ((selFiles.length == 0) && (chooser.getSelectedFile() != null)) { 469 selFiles = new File [] { 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 nodes[i] = ((NodeFile) selFiles[i]).getNode(); 477 } else { 478 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 522 private static class FakeNode extends AbstractNode { 523 524 public FakeNode (Children children) { 525 super(children); 526 } 527 } 528 529 532 private class NodeFile extends File { 533 private Node n; 534 535 NodeFile (String path, Node n) { 536 super(path); 537 this.n = n; 538 } 539 540 NodeFile (File parent, String 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 dest) { 554 DataObject dObj = (DataObject) n.getCookie(DataObject.class); 555 if (dObj != null) { 556 try { 557 dObj.rename(dest.getName()); 558 } catch (IOException 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 [] 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 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 getName () { 580 if (n != null) { 581 return n.getDisplayName(); 582 } else { 583 return super.getName(); 584 } 585 } 586 587 public File getParentFile () { 588 String 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 p = getPath(); 617 int ind = p.indexOf(File.separatorChar); 618 if (ind != -1) { 619 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 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 return true; 653 } 654 } 655 656 public boolean isFile () { 657 if (n == null) { 658 return true; 659 } 660 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 return false; 679 } 680 } 681 682 public Icon getIcon () { 683 return new ImageIcon(n.getIcon(BeanInfo.ICON_COLOR_16x16)); 684 } 685 686 public String getAbsolutePath() { 687 return getPath(); 688 } 689 690 public File getAbsoluteFile() { 691 return new NodeFile(getAbsolutePath(), n); 692 } 693 694 public String getCanonicalPath() throws IOException { 695 return getPath(); 696 } 697 698 public File getCanonicalFile() throws IOException { 699 return new NodeFile(getCanonicalPath(), n); 700 } 701 702 public Node getNode () { 703 return n; 704 } 705 706 } 707 708 711 private class NodeFileView extends FileView { 712 713 NodeFileView () { 714 super(); 715 } 716 717 public String getName (File f) { 718 if (f instanceof NodeFile) { 719 return f.getName(); 720 } else { 721 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 f) { 732 if (f instanceof NodeFile) { 733 return ((NodeFile) f).getIcon(); 734 } else { 735 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 749 private class NodeFileSystemView extends FileSystemView { 750 private final String newFolderString = 751 UIManager.getString("FileChooser.other.newFolder"); 753 NodeFileSystemView () { 754 super(); 755 } 756 757 760 public boolean isRoot(File f) { 761 return rootFile.equals(f); 762 } 763 764 767 public File createNewFolder(File containingDir) throws IOException { 768 String 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 createFileObject(File dir, String filename) { 793 filename = filename.replace(File.separatorChar,'#'); 794 String 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 806 public File createFileObject(String path) { 807 Node n = findNode(path); 809 if (n == null) { 810 n = createNode(path); 811 } 812 return new NodeFile(path, n); 813 } 814 815 818 public boolean isHiddenFile(File f) { 819 return false; 820 } 821 822 826 public File [] getRoots() { 827 return new NodeFile [] { (NodeFile) rootFile }; 828 } 829 830 public File getHomeDirectory() { 831 return rootFile; 832 } 833 834 public File [] getFiles (File 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 getParentDirectory (File dir) { 843 if (dir != null) { 844 File f = createFileObject(dir.getPath()); 845 return f.getParentFile(); 846 } 847 return null; 848 } 849 850 public String getSystemDisplayName (File f) { 851 return f.getName(); 852 } 853 854 } 855 856 860 public class NodeFileChooser extends JFileChooser { 861 862 NodeFileChooser (File currentDirectory, FileSystemView fsv) { 863 super(currentDirectory, fsv); 864 } 865 866 protected void setup(FileSystemView view) { 867 setFileView(new DataObjectListView.NodeFileView()); 872 super.setup(view); 873 } 874 875 public void setCurrentDirectory (File 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 |