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

com.alee.laf.tree.TreeStateConverter Maven / Gradle / Ivy

The 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.laf.tree;

import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.converters.reflection.ReflectionConverter;
import com.thoughtworks.xstream.converters.reflection.ReflectionProvider;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.mapper.Mapper;

import java.util.Map;

/**
 * Custom converter for {@link TreeState} class.
 *
 * @author Mikle Garin
 */
public class TreeStateConverter extends ReflectionConverter
{
    /**
     * Constructs new {@link TreeStateConverter} with the specified mapper and reflection provider.
     *
     * @param mapper             {@link Mapper} implementation
     * @param reflectionProvider {@link ReflectionProvider} implementation
     */
    public TreeStateConverter ( final Mapper mapper, final ReflectionProvider reflectionProvider )
    {
        super ( mapper, reflectionProvider );
    }

    @Override
    public boolean canConvert ( final Class type )
    {
        return type.equals ( TreeState.class );
    }

    @Override
    public void marshal ( final Object source, final HierarchicalStreamWriter writer, final MarshallingContext context )
    {
        final TreeState treeState = ( TreeState ) source;
        for ( final Map.Entry entry : treeState.states ().entrySet () )
        {
            final String nodeId = entry.getKey ();
            final NodeState nodeState = entry.getValue ();
            writer.startNode ( "node" );
            writer.addAttribute ( "id", nodeId );
            writer.addAttribute ( "expanded", "" + nodeState.isExpanded () );
            writer.addAttribute ( "selected", "" + nodeState.isSelected () );
            writer.endNode ();
        }
    }

    @Override
    public Object unmarshal ( final HierarchicalStreamReader reader, final UnmarshallingContext context )
    {
        final TreeState state = new TreeState ();
        while ( reader.hasMoreChildren () )
        {
            reader.moveDown ();
            final String nodeId = reader.getAttribute ( "id" );
            final String expandedAttribue = reader.getAttribute ( "expanded" );
            final boolean expanded = Boolean.parseBoolean ( expandedAttribue != null ? expandedAttribue : "false" );
            final String selectedAttribue = reader.getAttribute ( "selected" );
            final boolean selected = Boolean.parseBoolean ( selectedAttribue != null ? selectedAttribue : "false" );
            state.addState ( nodeId, new NodeState ( expanded, selected ) );
            reader.moveUp ();
        }
        return state;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy