Example usage for javax.swing.event TreeWillExpandListener TreeWillExpandListener

List of usage examples for javax.swing.event TreeWillExpandListener TreeWillExpandListener

Introduction

In this page you can find the example usage for javax.swing.event TreeWillExpandListener TreeWillExpandListener.

Prototype

TreeWillExpandListener

Source Link

Usage

From source file:VASSAL.launch.ModuleManagerWindow.java

protected void buildTree() {
    recentModuleConfig = new StringArrayConfigurer("RecentModules", null);
    Prefs.getGlobalPrefs().addOption(null, recentModuleConfig);
    final List<String> missingModules = new ArrayList<String>();
    final List<ModuleInfo> moduleList = new ArrayList<ModuleInfo>();
    for (String s : recentModuleConfig.getStringArray()) {
        final ModuleInfo module = new ModuleInfo(s);
        if (module.getFile().exists() && module.isValid()) {
            moduleList.add(module);/*from w  w  w .  j ava2s.c  o m*/
        } else {
            missingModules.add(s);
        }
    }

    for (String s : missingModules) {
        logger.info(Resources.getString("ModuleManager.removing_module", s));
        moduleList.remove(s);
        recentModuleConfig.removeValue(s);
    }

    Collections.sort(moduleList, new Comparator<ModuleInfo>() {
        public int compare(ModuleInfo f1, ModuleInfo f2) {
            return f1.compareTo(f2);
        }
    });

    rootNode = new MyTreeNode(new RootInfo());

    for (ModuleInfo moduleInfo : moduleList) {
        final MyTreeNode moduleNode = new MyTreeNode(moduleInfo);
        for (ExtensionInfo ext : moduleInfo.getExtensions()) {
            final MyTreeNode extensionNode = new MyTreeNode(ext);
            moduleNode.add(extensionNode);
        }

        final ArrayList<File> missingFolders = new ArrayList<File>();

        for (File f : moduleInfo.getFolders()) {
            if (f.exists() && f.isDirectory()) {
                final GameFolderInfo folderInfo = new GameFolderInfo(f, moduleInfo);
                final MyTreeNode folderNode = new MyTreeNode(folderInfo);
                moduleNode.add(folderNode);
                final ArrayList<File> l = new ArrayList<File>();

                final File[] files = f.listFiles();
                if (files == null)
                    continue;

                for (File f1 : files) {
                    if (f1.isFile()) {
                        l.add(f1);
                    }
                }
                Collections.sort(l);

                for (File f2 : l) {
                    final SaveFileInfo fileInfo = new SaveFileInfo(f2, folderInfo);
                    if (fileInfo.isValid() && fileInfo.belongsToModule()) {
                        final MyTreeNode fileNode = new MyTreeNode(fileInfo);
                        folderNode.add(fileNode);
                    }
                }
            } else {
                missingFolders.add(f);
            }
        }

        for (File mf : missingFolders) {
            logger.info(Resources.getString("ModuleManager.removing_folder", mf.getPath()));
            moduleInfo.removeFolder(mf);
        }

        rootNode.add(moduleNode);
    }

    updateModuleList();

    treeModel = new MyTreeTableModel(rootNode);
    tree = new MyTree(treeModel);

    tree.setRootVisible(false);
    tree.setEditable(false);

    tree.setTreeCellRenderer(new MyTreeCellRenderer());

    tree.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            if (e.getClickCount() == 2) {
                final TreePath path = tree.getPathForLocation(e.getPoint().x, e.getPoint().y);

                // do nothing if not on a node, or if this node was expanded
                // or collapsed during the past doubleClickInterval milliseconds
                if (path == null || (lastExpansionPath == path
                        && e.getWhen() - lastExpansionTime <= doubleClickInterval))
                    return;

                selectedNode = (MyTreeNode) path.getLastPathComponent();

                final int row = tree.getRowForPath(path);
                if (row < 0)
                    return;

                final AbstractInfo target = (AbstractInfo) selectedNode.getUserObject();

                // launch module or load save, otherwise expand or collapse node
                if (target instanceof ModuleInfo) {
                    final ModuleInfo modInfo = (ModuleInfo) target;
                    if (modInfo.isModuleTooNew()) {
                        ErrorDialog.show("Error.module_too_new", modInfo.getFile().getPath(),
                                modInfo.getVassalVersion(), Info.getVersion());
                        return;
                    } else {
                        ((ModuleInfo) target).play();
                    }
                } else if (target instanceof SaveFileInfo) {
                    ((SaveFileInfo) target).play();
                } else if (tree.isExpanded(row)) {
                    tree.collapseRow(row);
                } else {
                    tree.expandRow(row);
                }
            }
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            final TreePath path = tree.getPathForLocation(e.getPoint().x, e.getPoint().y);
            if (path == null)
                return;

            selectedNode = (MyTreeNode) path.getLastPathComponent();

            if (e.isMetaDown()) {
                final int row = tree.getRowForPath(path);
                if (row >= 0) {
                    tree.clearSelection();
                    tree.addRowSelectionInterval(row, row);
                    final AbstractInfo target = (AbstractInfo) selectedNode.getUserObject();
                    target.buildPopup(row).show(tree, e.getX(), e.getY());
                }
            }
        }
    });

    // We capture the time and location of clicks which would cause
    // expansion in order to distinguish these from clicks which
    // might launch a module or game.
    tree.addTreeWillExpandListener(new TreeWillExpandListener() {
        public void treeWillCollapse(TreeExpansionEvent e) {
            lastExpansionTime = System.currentTimeMillis();
            lastExpansionPath = e.getPath();
        }

        public void treeWillExpand(TreeExpansionEvent e) {
            lastExpansionTime = System.currentTimeMillis();
            lastExpansionPath = e.getPath();
        }
    });

    // This ensures that double-clicks always start the module but
    // doesn't prevent single-clicks on the handles from working.
    tree.setToggleClickCount(3);

    tree.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    tree.addTreeSelectionListener(new TreeSelectionListener() {
        public void valueChanged(TreeSelectionEvent e) {
            final MyTreeNode node = (MyTreeNode) e.getPath().getLastPathComponent();
            final AbstractInfo target = node.getNodeInfo();
            if (target instanceof ModuleInfo) {
                setSelectedModule(target.getFile());
            } else {
                if (node.getParent() != null) {
                    setSelectedModule(node.getParentModuleFile());
                }
            }
        }
    });

    // FIXME: Width handling needs improvement. Also save in prefs
    tree.getColumnModel().getColumn(KEY_COLUMN).setMinWidth(250);

    tree.getColumnModel().getColumn(VERSION_COLUMN).setCellRenderer(new VersionCellRenderer());
    tree.getColumnModel().getColumn(VERSION_COLUMN).setMinWidth(100);

    tree.getColumnModel().getColumn(VASSAL_COLUMN).setCellRenderer(new VersionCellRenderer());
    tree.getColumnModel().getColumn(VASSAL_COLUMN).setMinWidth(100);

    tree.getColumnModel().getColumn(SPARE_COLUMN).setMinWidth(10);
    tree.getColumnModel().getColumn(SPARE_COLUMN).setPreferredWidth(600);

    // FIXME: How to set alignment of individual header components?
    tree.getTableHeader().setAlignmentX(JComponent.CENTER_ALIGNMENT);

}