All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.alee.demo.content.data.tree.WebAsyncTreeExample Maven / Gradle / Ivy

There is a newer version: 1.2.8
Show newest version
/*
 * This file is part of WebLookAndFeel library.
 *
 * WebLookAndFeel library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * WebLookAndFeel library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with WebLookAndFeel library.  If not, see .
 */

package com.alee.demo.content.data.tree;

import com.alee.api.annotations.NotNull;
import com.alee.demo.api.example.*;
import com.alee.demo.api.example.wiki.WebLafWikiPage;
import com.alee.demo.api.example.wiki.WikiPage;
import com.alee.demo.content.SampleData;
import com.alee.demo.content.data.tree.model.SampleNode;
import com.alee.demo.content.data.tree.model.SampleObjectType;
import com.alee.demo.content.data.tree.model.SampleTreeCellEditor;
import com.alee.extended.tree.*;
import com.alee.laf.scroll.WebScrollPane;
import com.alee.managers.style.StyleId;
import com.alee.utils.CollectionUtils;

import javax.swing.*;
import java.util.List;

/**
 * @author Mikle Garin
 */
public class WebAsyncTreeExample extends AbstractStylePreviewExample
{
    @NotNull
    @Override
    public String getId ()
    {
        return "asynctree";
    }

    @NotNull
    @Override
    protected String getStyleFileName ()
    {
        return "tree";
    }

    @NotNull
    @Override
    public FeatureType getFeatureType ()
    {
        return FeatureType.extended;
    }

    @NotNull
    @Override
    public WikiPage getWikiPage ()
    {
        return new WebLafWikiPage ( "How to use WebAsyncTree" );
    }

    @NotNull
    @Override
    protected List createPreviews ()
    {
        return CollectionUtils.asList (
                new BasicTree ( StyleId.asynctree ),
                new EditableTree ( StyleId.asynctree ),
                new DragAndDropTree ( StyleId.asynctree )
        );
    }

    /**
     * Basic asynchronous tree preview.
     */
    protected class BasicTree extends AbstractStylePreview
    {
        /**
         * Constructs new style preview.
         *
         * @param styleId preview {@link StyleId}
         */
        public BasicTree ( final StyleId styleId )
        {
            super ( WebAsyncTreeExample.this, "basic", FeatureState.updated, styleId );
        }

        @NotNull
        @Override
        protected List createPreviewElements ()
        {
            final AsyncTreeDataProvider dataProvider = SampleData.createDelayingAsyncDataProvider ();
            final WebAsyncTree tree = new WebAsyncTree ( getStyleId (), dataProvider );
            tree.setVisibleRowCount ( 8 );
            return CollectionUtils.asList ( new WebScrollPane ( tree ).setPreferredWidth ( 200 ) );
        }
    }

    /**
     * Editable asynchronous tree preview.
     */
    protected class EditableTree extends AbstractStylePreview
    {
        /**
         * Constructs new style preview.
         *
         * @param styleId preview {@link StyleId}
         */
        public EditableTree ( final StyleId styleId )
        {
            super ( WebAsyncTreeExample.this, "editable", FeatureState.updated, styleId );
        }

        @NotNull
        @Override
        protected List createPreviewElements ()
        {
            final AsyncTreeDataProvider dataProvider = SampleData.createDelayingAsyncDataProvider ();
            final WebAsyncTree tree = new WebAsyncTree ( getStyleId (), dataProvider, new SampleTreeCellEditor () );
            tree.setVisibleRowCount ( 8 );
            return CollectionUtils.asList ( new WebScrollPane ( tree ).setPreferredWidth ( 200 ) );
        }
    }

    /**
     * Drag and drop tree preview.
     */
    protected class DragAndDropTree extends AbstractStylePreview
    {
        /**
         * Constructs new style preview.
         *
         * @param styleId preview {@link StyleId}
         */
        public DragAndDropTree ( final StyleId styleId )
        {
            super ( WebAsyncTreeExample.this, "dragndrop", FeatureState.updated, styleId );
        }

        @NotNull
        @Override
        protected List createPreviewElements ()
        {
            final AsyncTreeDataProvider leftDataProvider = SampleData.createDelayingAsyncDataProvider ();
            final WebAsyncTree left = new WebAsyncTree ( getStyleId (), leftDataProvider, new SampleTreeCellEditor () );
            left.setVisibleRowCount ( 8 );
            left.setDragEnabled ( true );
            left.setDropMode ( DropMode.ON_OR_INSERT );
            left.setTransferHandler ( createTransferHandler () );
            final WebScrollPane leftScroll = new WebScrollPane ( left ).setPreferredWidth ( 200 );

            final AsyncTreeDataProvider rightDataProvider = SampleData.createDelayingAsyncDataProvider ();
            final WebAsyncTree right = new WebAsyncTree ( getStyleId (), rightDataProvider, new SampleTreeCellEditor () );
            right.setVisibleRowCount ( 8 );
            right.setDragEnabled ( true );
            right.setDropMode ( DropMode.ON_OR_INSERT );
            right.setTransferHandler ( createTransferHandler () );
            final WebScrollPane rightScroll = new WebScrollPane ( right ).setPreferredWidth ( 200 );

            return CollectionUtils.asList ( leftScroll, rightScroll );
        }
    }

    /**
     * Returns sample tree transfer handler.
     * It will provide base functionality of DnD for our sample tree.
     *
     * @return sample extended tree transfer handler
     */
    protected static AsyncTreeTransferHandler, AsyncTreeModel> createTransferHandler ()
    {
        return new AsyncTreeTransferHandler, AsyncTreeModel> ()
        {
            /**
             * Forcing this {@link TransferHandler} to move nodes.
             */
            @Override
            public int getSourceActions ( @NotNull final JComponent c )
            {
                return MOVE;
            }

            /**
             * Blocks drop on {@link SampleObjectType#leaf} nodes.
             */
            @NotNull
            @Override
            protected List createDropHandlers ()
            {
                return CollectionUtils.asList ( new NodesDropHandler, AsyncTreeModel> ()
                {
                    @Override
                    protected boolean canDrop ( @NotNull final TransferSupport support, @NotNull final WebAsyncTree tree,
                                                @NotNull final AsyncTreeModel model, @NotNull final SampleNode destination,
                                                final int dropIndex, @NotNull final List nodes )
                    {
                        return destination.getUserObject ().getType () != SampleObjectType.leaf;
                    }
                } );
            }

            /**
             * We do not need to copy children as {@link AsyncTreeDataProvider} will do that instead.
             * We only need to provide a copy of the specified node here.
             */
            @NotNull
            @Override
            protected SampleNode copy ( @NotNull final WebAsyncTree tree, @NotNull final AsyncTreeModel model, @NotNull
            final SampleNode node )
            {
                return node.clone ();
            }

            /**
             * Blocks root element drag.
             */
            @Override
            protected boolean canBeDragged ( @NotNull final WebAsyncTree tree, @NotNull final AsyncTreeModel model,
                                             @NotNull final List nodes )
            {
                boolean allowed = true;
                for ( final SampleNode node : nodes )
                {
                    if ( node.getUserObject ().getType () == SampleObjectType.root )
                    {
                        allowed = false;
                        break;
                    }
                }
                return allowed;
            }
        };
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy