⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 smarttree.java

📁 JAVA开源LDAP浏览器jxplorer的源码!
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
     * Sets numOfResults.
     *
     * @param numOfResults a holder for the number of results returned by a search.
     */

    public void setNumOfResults(int numOfResults)
    {
        this.numOfResults = numOfResults;
    }

    /**
     *    By default the tree can handle dataQueries of type LIST, COPY, MODIFY, and READENTRY.
     *    This method allows these capabilities to be modified (for example to include SEARCH).
     *    @see com.ca.directory.jxplorer.DataQuery .
     */
/*
    public void setCapabilities(int cap)
    {
        treeCapabilities = cap;
    }
*/
    //
    //
    //  Graphicsy overhead / user i/o functions...
    //
    //


    /**
     * Sets up a listener to monitor whether the user has
     * finished editing a tree cell...
     */

    protected void setTreeCellEditorListener()
    {

        // We are unable to distinguish between keyboard 'esc'
        // and mouse clicking outside the cell, we'll pretend
        // the user wants their changes to go through.
        // XXX may still need to work out some way to distinguish
        // XXX between different cancel modes...
        
        CellEditorListener cl = new CellEditorListener()
        {
            public void editingCanceled(ChangeEvent e)
            {
                changeDN();
            }

            public void editingStopped(ChangeEvent e)
            {
                changeDN();
            }

            /**
             * This method is called when the user has changed the name of an
             * entry directly, using a tree cell editor or the multi-valued
             * RDN editor.
             */

            protected void changeDN()
            {
                // o.k., we're not connected to anything...
                if (isActive() == false)
                    return;

                RDN rdn = (RDN) treeEditor.getCellEditorValue();

                DN newDN = new DN(currentDN);

                newDN.setRDN(rdn, newDN.size() - 1);

                // check if anything actually changed...
                if (currentDN.toString().equals(newDN.toString()))
                    return;

                //TE: Bug 3172 - if the name exists in the tree, don't attempt a rename...

                if (treeModel.exists(newDN))
                {
                    new CBErrorWin(owner, "The name you are trying to use already exists - " +
                            "please choose another name or delete the original entry.",
                            "Name already exists");
                    refresh(currentDN.parentDN());
                    return;
                }

                // modify entry will sort out all the yucky details for us...
                treeDataSource.modifyEntry(new DXEntry(currentDN), new DXEntry(newDN));
            }
        };

        treeEditor.addCellEditorListener(cl);
    }

    /**
     * sets up the mouse listener to monitor mouse clicks.  At
     * the moment, the sole use of this is to check whether the
     * popup menu has been triggered.
     */

    protected void setTreeMouseListener()
    {
        MouseListener ml = new MouseAdapter()
        {
            public void mousePressed(MouseEvent e)
            {
                if (!doPopupStuff(e)) super.mousePressed(e);
            }

            public void mouseReleased(MouseEvent e)
            {
                if (!doPopupStuff(e)) super.mouseReleased(e);
            }

            public boolean doPopupStuff(MouseEvent e)
            {
                if (isActive() == false) return false; // o.k., we're not connected to anything...

                if (e.isPopupTrigger() == false) return false;

                TreePath path = getPathForLocation(e.getX(), e.getY());
                if (path == null)
                {
                    return false;
                }

                setSelectionPath(path); // make sure highlighting stays around

                // this probably isn't necessary, but just to make sure that currentDN is set

                DN thisDN = treeModel.getDNForPath(path);
                if (thisDN.equals(currentDN) == false)
                {
                    currentDN = thisDN;
                }

                if (treeDataSource != null)
                {
                    popupTreeTool.setModifiable(treeDataSource.isModifiable());  // whether the user can change anything...

                    // XXX el hack - check to see if entry has a *special* popup tool to use instead...
                    if (getSelectedNode().getPopupMenu() != null)
                        getSelectedNode().getPopupMenu().show(SmartTree.this, e.getX(), e.getY());
                    else
                    {	//TE: this should be improved...
                        Toolkit toolKit = Toolkit.getDefaultToolkit();

                        popupTreeTool.show(SmartTree.this, e.getX(), e.getY());	//TE: displays the popup menu.

                        if ((int) popupTreeTool.getLocationOnScreen().getY() > toolKit.getScreenSize().height - (popupTreeTool.getHeight() + 30))	//TE: if the popup menu extends off the bottom of the screen...
                        {
                            popupTreeTool.show(SmartTree.this, e.getX(), e.getY() - popupTreeTool.getHeight());	//TE: ...reposition it so that the menu ascends from the node rather than descends!
                        }
                    }
                }
                return true;
            }
        };
        addMouseListener(ml);
    }

    /**
     * null implementation to satisfy @TreeExpansionListener interface
     *
     * @param e tree event, implicitly specifying the expanding node.
     */

    public void treeCollapsed(TreeExpansionEvent e)
    {
    }

    /**
     * The user has asked the tree to expand.  Check the node,
     * and if it is a null placeholder, read the node properly from the
     * directory before expanding and displaying.
     *
     * @param e tree event, implicitly specifying the expanding node.
     */

    public void treeExpanded(TreeExpansionEvent e)
    {
        if (isActive() == false) return; // o.k., we're not connected to anything...
        SmartNode current = (SmartNode) e.getPath().getLastPathComponent();


        try
        {
            if (((SmartNode) current.getFirstChild()).isDummy() == true)
            {
                treeDataSource.getChildren(treeModel.getDNForNode(current));
            }
            else if (current.isAlwaysRefresh())
            {
                treeDataSource.getChildren(treeModel.getDNForNode(current));
            }
        }
        catch (java.util.NoSuchElementException err)
        {
        }  // why would it be trying to expand anyway?
    }

    /**
     * a node value has changed, so redisplay it...
     *
     * @param e tree event, implicitly specifying the changed node.
     */

    public void valueChanged(TreeSelectionEvent e)
    {
        if (isActive() == false) return; // o.k., we're not connected to anything...

        if (getSelectionPath() == null)
            return;

        if (e.isAddedPath() == false) // deletion occured
        {
            setSelectionPath(null);			  // clear the 'currently selected' data object in popupTreeTool
            displayReadNodeResult(null);          // clear the editor
        }
        else // addition occured
        {
            DN addedDN = treeModel.getDNForPath(getSelectionPath());

            if (addedDN.equals(currentDN) == false)
            {
                treeDataSource.getEntry(treeModel.getDNForNode(getSelectedNode()));
            }
        }
    }


    /**
     * Returns whether the tree is active - i.e. has a valid data source,
     * which is active, and the
     * tree has it's root set.
     */

    protected boolean isActive()
    {
        if (treeDataSource == null) return false;
        if (treeDataSource.isActive() == false) return false;
        if (rootSet == false) return false;
        return true;
    }

    /**
     * This is the data listener interface - this method is called when a data query is finished
     * by a Broker.  The tree listens to these results, and adjusts itself to reflect successfull
     * directory operations.
     */

    public void dataReady(DataQuery result)
    {
        int type = result.getType();

        if (result.hasException())
        {
            String exception = result.getException().toString();	//TE: quick solution to bug 561...if dsa is offine keep the tree but set everything else to disconnected mode.
            if (exception.indexOf("Socket closed") > -1)
                if (owner instanceof JXplorer)
                    ((JXplorer) owner).setDisconnectView();

            CBUtility.error("Unable to perform " + result.getTypeString() + " operation.", result.getException());

            if (type == DataQuery.LIST)        // clean up failed list result...
            {
                SmartNode node = treeModel.getNodeForDN(result.requestDN());
                if (!node.isAlwaysRefresh()) // XXX Hack to avoid losing tree when get error reading non-existant base DN node.
                {
                    node.removeAllChildren();
                    treeModel.nodeStructureChanged(node);
                }
            }

            return;
        }
        else
        {
            switch (type)
            {
                case DataQuery.LIST:
                    displayExpandedNodeResult(result);
                    break;

                case DataQuery.COPY:
                    displayCopyResult(result);
                    break;

                case DataQuery.MODIFY:
                    displayModifyResult(result);
                    break;

                case DataQuery.SEARCH:
                    displaySearchResult(result);
                    break;

                case DataQuery.READENTRY:
                    displayReadNodeResult(result);
                    break;
            }

            if (result.hasException())
            {
                CBUtility.error("Exception occurred during tree display of " + result.getTypeString() + ".\n\n(Error caught by display tree)", result.getException());
                return;
            }
        }
    }


    public void validate()
    {
        super.validate();
    }

    //	********************
    //
    //  *** DRAG 'N DROP ***
    //
    //  ********************


    protected void setupDragAndDrop()
    {
// XXX Disable Drag and Drop on Solaris.  Doesn't work worth a damn, and
// XXX has some *VERY* strange behaviour

        if (JXplorer.isSolaris()) return;

// Disable Drag and Drop if the user has set the option to do so... ('true' is default though).
        if (!JXplorer.getProperty("option.drag.and.drop").equals("true"))
            return;

        /*  Custom dragsource object: needed to handle DnD in a JTree.
          *  This is pretty ugly. I had to overide (labotimize) the updateCurrentCursor
         *  method to get the cursor to update properly.
         */

        dragSource = new DragSource()
        {
            protected DragSourceContext createDragSourceContext
                    (DragSourceContextPeer dscp, DragGestureEvent dgl, Cursor dragCursor,
                     Image dragImage, Point imageOffset, Transferable t,
                     DragSourceListener dsl)
            {
                return new DragSourceContext(dscp, dgl, dragCursor, dragImage, imageOffset, t, dsl)
                {
                    protected void updateCurrentCursor(int dropOp, int targetAct, int status)
                    {
                    }
                };
            }
        };


        DragGestureRecognizer dgr = dragSource.createDefaultDragGestureRecognizer(this,
                DnDConstants.ACTION_COPY_OR_MOVE, this);


        /*
* Eliminates right mouse clicks as valid actions - useful especially
          * if you implement a JPopupMenu for the JTree
         */
//?		dgr.setSourceActions(dgr.getSourceActions() & ~InputEvent.BUTTON3_MASK);

        dgr.setSourceActions(dgr.getSourceActions() + InputEvent.BUTTON1_MASK);

        /* First argument:  Component to associate the target with
         * Second argument: DropTargetListener
        */
        new DropTarget(this, this);

    }

    /**
     * DragGestureListener interface method
     */
    public void dragGestureRecognized(DragGestureEvent e)
    {
        //Get the selected node
        SmartNode dragNode = getSelectedNode();
        if (dragNode != null)
        {
            dragging = true;

            //Get the Transferable Object
            Transferable transferab

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -