private void updateTree() { if (myFileSystemTree != null) { Disposer.dispose(myFileSystemTree); myFileSystemTree = null; } myFileSystemTree = new FileSystemTreeImpl( null, new FileChooserDescriptor(true, true, true, true, true, false)); AbstractTreeUi ui = myFileSystemTree.getTreeBuilder().getUi(); String path = myModelRoot.getPath() == null ? "" : myModelRoot.getPath(); VirtualFile virtualFile = VirtualFileManager.getInstance() .findFileByUrl(VirtualFileManager.constructUrl("file", path)); if (myModelRoot.getModule() != null && (virtualFile == null || path.isEmpty())) { if (myModelRoot.getModule() instanceof AbstractModule) { virtualFile = VirtualFileManager.getInstance() .findFileByUrl( VirtualFileManager.constructUrl( "file", ((AbstractModule) myModelRoot.getModule()) .getModuleSourceDir() .getPath())); } } if (virtualFile != null) myFileSystemTree.select(virtualFile, null); myFileSystemTree.addListener( new Listener() { @Override public void selectionChanged(List<VirtualFile> selection) { if (selection.size() > 0) { myModelRoot.setPath(FileUtil.getCanonicalPath(selection.get(0).getPath())); myEventDispatcher.getMulticaster().fireDataChanged(); } } }, SModelRootEntry.this); Disposer.register(SModelRootEntry.this, myFileSystemTree); myTreePanel.removeAll(); myTreePanel.add(ui.getTree(), BorderLayout.CENTER); ui.scrollSelectionToVisible(null, true); }
public void update() { if (myFileSystemTree != null) { myFileSystemTree.updateTree(); final DefaultTreeModel model = (DefaultTreeModel) myTree.getModel(); final int visibleRowCount = myTree.getVisibleRowCount(); for (int row = 0; row < visibleRowCount; row++) { final TreePath pathForRow = myTree.getPathForRow(row); if (pathForRow != null) { final TreeNode node = (TreeNode) pathForRow.getLastPathComponent(); if (node != null) { model.nodeChanged(node); } } } } }
@Override protected JComponent createCenterPanel() { final FileChooserDescriptor descriptor = FileChooserDescriptorFactory.createAllButJarContentsDescriptor(); calculateRoots(); final ArrayList<VirtualFile> list = new ArrayList<VirtualFile>(myRoots); final Comparator<VirtualFile> comparator = new Comparator<VirtualFile>() { @Override public int compare(VirtualFile o1, VirtualFile o2) { final boolean isDir1 = o1.isDirectory(); final boolean isDir2 = o2.isDirectory(); if (isDir1 != isDir2) return isDir1 ? -1 : 1; final String module1 = myModulesSet.get(o1); final String path1 = module1 != null ? module1 : o1.getPath(); final String module2 = myModulesSet.get(o2); final String path2 = module2 != null ? module2 : o2.getPath(); return path1.compareToIgnoreCase(path2); } }; descriptor.setRoots(list); myTree = new Tree(); myTree.setMinimumSize(new Dimension(200, 200)); myTree.setBorder(BORDER); myTree.setShowsRootHandles(true); myTree.setRootVisible(true); myTree.getExpandableItemsHandler().setEnabled(false); final MyCheckboxTreeCellRenderer cellRenderer = new MyCheckboxTreeCellRenderer( mySelectionManager, myModulesSet, myProject, myTree, myRoots); final FileSystemTreeImpl fileSystemTree = new FileSystemTreeImpl( myProject, descriptor, myTree, cellRenderer, null, new Convertor<TreePath, String>() { @Override public String convert(TreePath o) { final DefaultMutableTreeNode lastPathComponent = ((DefaultMutableTreeNode) o.getLastPathComponent()); final Object uo = lastPathComponent.getUserObject(); if (uo instanceof FileNodeDescriptor) { final VirtualFile file = ((FileNodeDescriptor) uo).getElement().getFile(); final String module = myModulesSet.get(file); if (module != null) return module; return file == null ? "" : file.getName(); } return o.toString(); } }); final AbstractTreeUi ui = fileSystemTree.getTreeBuilder().getUi(); ui.setNodeDescriptorComparator( new Comparator<NodeDescriptor>() { @Override public int compare(NodeDescriptor o1, NodeDescriptor o2) { if (o1 instanceof FileNodeDescriptor && o2 instanceof FileNodeDescriptor) { final VirtualFile f1 = ((FileNodeDescriptor) o1).getElement().getFile(); final VirtualFile f2 = ((FileNodeDescriptor) o2).getElement().getFile(); return comparator.compare(f1, f2); } return o1.getIndex() - o2.getIndex(); } }); myRoot = (DefaultMutableTreeNode) myTree.getModel().getRoot(); new ClickListener() { @Override public boolean onClick(@NotNull MouseEvent e, int clickCount) { int row = myTree.getRowForLocation(e.getX(), e.getY()); if (row < 0) return false; final Object o = myTree.getPathForRow(row).getLastPathComponent(); if (myRoot == o || getFile(o) == null) return false; Rectangle rowBounds = myTree.getRowBounds(row); cellRenderer.setBounds(rowBounds); Rectangle checkBounds = cellRenderer.myCheckbox.getBounds(); checkBounds.setLocation(rowBounds.getLocation()); if (checkBounds.height == 0) checkBounds.height = rowBounds.height; if (checkBounds.contains(e.getPoint())) { mySelectionManager.toggleSelection((DefaultMutableTreeNode) o); myTree.revalidate(); myTree.repaint(); } return true; } }.installOn(myTree); myTree.addKeyListener( new KeyAdapter() { public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_SPACE) { TreePath[] paths = myTree.getSelectionPaths(); if (paths == null) return; for (TreePath path : paths) { if (path == null) continue; final Object o = path.getLastPathComponent(); if (myRoot == o || getFile(o) == null) return; mySelectionManager.toggleSelection((DefaultMutableTreeNode) o); } myTree.revalidate(); myTree.repaint(); e.consume(); } } }); JBPanel panel = new JBPanel(new BorderLayout()); panel.add(new JBScrollPane(fileSystemTree.getTree()), BorderLayout.CENTER); mySelectedLabel = new JLabel(""); mySelectedLabel.setBorder(BorderFactory.createEmptyBorder(2, 0, 2, 0)); panel.add(mySelectedLabel, BorderLayout.SOUTH); mySelectionManager.setSelectionChangeListener( new PlusMinus<VirtualFile>() { @Override public void plus(VirtualFile virtualFile) { mySelectedFiles.add(virtualFile); recalculateErrorText(); } private void recalculateErrorText() { checkEmpty(); if (mySelectionManager.canAddSelection()) { mySelectedLabel.setText(""); } else { mySelectedLabel.setText(CAN_NOT_ADD_TEXT); } mySelectedLabel.revalidate(); } @Override public void minus(VirtualFile virtualFile) { mySelectedFiles.remove(virtualFile); recalculateErrorText(); } }); return panel; }
public void select(VirtualFile file) { if (myFileSystemTree != null) { myFileSystemTree.select(file, null); } }
/** @param contentEntryEditor : null means to clear the editor */ public void setContentEntryEditor(final ContentEntryEditor contentEntryEditor) { if (myContentEntryEditor != null && myContentEntryEditor.equals(contentEntryEditor)) { return; } if (myFileSystemTree != null) { Disposer.dispose(myFileSystemTree); myFileSystemTree = null; } if (myContentEntryEditor != null) { myContentEntryEditor.removeContentEntryEditorListener(myContentEntryEditorListener); myContentEntryEditor = null; } if (contentEntryEditor == null) { ((DefaultTreeModel) myTree.getModel()).setRoot(EMPTY_TREE_ROOT); myTreePanel.setVisible(false); if (myFileSystemTree != null) { Disposer.dispose(myFileSystemTree); } return; } myTreePanel.setVisible(true); myContentEntryEditor = contentEntryEditor; myContentEntryEditor.addContentEntryEditorListener(myContentEntryEditorListener); final ContentEntry entry = contentEntryEditor.getContentEntry(); assert entry != null : contentEntryEditor; final VirtualFile file = entry.getFile(); myDescriptor.setRoots(file); if (file == null) { final String path = VfsUtilCore.urlToPath(entry.getUrl()); myDescriptor.setTitle(FileUtil.toSystemDependentName(path)); } final Runnable init = new Runnable() { @Override public void run() { //noinspection ConstantConditions myFileSystemTree.updateTree(); myFileSystemTree.select(file, null); } }; myFileSystemTree = new FileSystemTreeImpl( myProject, myDescriptor, myTree, getContentEntryCellRenderer(), init, null) { @Override protected AbstractTreeBuilder createTreeBuilder( JTree tree, DefaultTreeModel treeModel, AbstractTreeStructure treeStructure, Comparator<NodeDescriptor> comparator, FileChooserDescriptor descriptor, final Runnable onInitialized) { return new MyFileTreeBuilder( tree, treeModel, treeStructure, comparator, descriptor, onInitialized); } }; myFileSystemTree.showHiddens(true); Disposer.register(myProject, myFileSystemTree); final NewFolderAction newFolderAction = new MyNewFolderAction(); final DefaultActionGroup mousePopupGroup = new DefaultActionGroup(); mousePopupGroup.add(myEditingActionsGroup); mousePopupGroup.addSeparator(); mousePopupGroup.add(newFolderAction); myFileSystemTree.registerMouseListener(mousePopupGroup); }