/*
 * Copyright 2015-2017 Richard Linsdale.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package uk.theretiredprogrammer.photogallery.nodes;

import java.awt.datatransfer.DataFlavor;
import java.io.IOException;
import java.util.List;
import javax.swing.Action;
import uk.theretiredprogrammer.photogallery.dataobjects.*;
import uk.theretiredprogrammer.nbpcglibrary.annotations.UseCommonNodeAction;
import uk.theretiredprogrammer.nbpcglibrary.annotations.UseCommonNodeActions;
import uk.theretiredprogrammer.nbpcglibrary.data.entity.*;
import uk.theretiredprogrammer.nbpcglibrary.node.nodes.*;
import uk.theretiredprogrammer.nbpcglibrary.node.properties.*;
import uk.theretiredprogrammer.nbpcglibrary.api.LogicException;
import org.openide.nodes.PropertySupport;
import uk.theretiredprogrammer.photogallery.dataobjects.Albumgrouppermission.AlbumgrouppermissionField;
import static uk.theretiredprogrammer.photogallery.nodes.GroupNode.GROUPNODE_FLAVOR;

/**
 * Node for AlbumgrouppermissionNode.
 *
 * (Class generated by NetBeans Platform Code Generator tools using script.xml.
 * Do not edit this file. Apply any changes to the definition file and
 * regenerate all files.)
 *
 * @author Richard Linsdale (richard at theretiredprogrammer.uk)
 */
public class AlbumgrouppermissionNode extends TreeNode<Integer, Albumgrouppermission, Album, AlbumgrouppermissionField> {

    /**
     * Data Flavor for Nodes of type AlbumgrouppermissionNode
     */
    public static final DataFlavor ALBUMGROUPPERMISSIONNODE_FLAVOR = new DataFlavor(AlbumgrouppermissionNode.class, "AlbumgrouppermissionNode");
    
    private final AlbumgrouppermissionNodeChildFactory cf;

    /**
     * Constructor
     *
     * @param e the entity associated with this node
     */
    public AlbumgrouppermissionNode(Albumgrouppermission e) {
        this(e, new AlbumgrouppermissionNodeChildFactory(e));
    }
    
    private AlbumgrouppermissionNode(Albumgrouppermission e, AlbumgrouppermissionNodeChildFactory cf) {
        super("AlbumgrouppermissionNode", e, cf, Albumgrouppermission.EM.class,
                new DataFlavor[]{
                    GROUPNODE_FLAVOR,}, CAN_COPY | CAN_DELETE | CAN_CUT);
        this.cf = cf;
    }
    
    @UseCommonNodeActions({
        @UseCommonNodeAction(id = "uk.theretiredprogrammer.nbpcglibrary.node.actions.NodePasteAction",
                node = "AlbumgrouppermissionNode", position = 830),
        @UseCommonNodeAction(id = "uk.theretiredprogrammer.nbpcglibrary.node.actions.NodeCutAction",
                node = "AlbumgrouppermissionNode",
                position = 850, separator = 800),
        @UseCommonNodeAction(id = "uk.theretiredprogrammer.nbpcglibrary.node.actions.NodeCopyAction",
                node = "AlbumgrouppermissionNode", position = 870),
        @UseCommonNodeAction(id = "uk.theretiredprogrammer.nbpcglibrary.node.actions.NodeDeleteAction",
                node = "AlbumgrouppermissionNode", position = 890),})
    private static void useCommonNodeActions() {
    } // a dummy method

    @Override
    protected DataFlavor nodeGetDataFlavor() {
        return ALBUMGROUPPERMISSIONNODE_FLAVOR;        
    }

    /**
     * Get the node display name
     *
     * @return the display name
     */
    @Override
    public String getDisplayName() {
        return getEntity() != null ? getEntity().getDisplayName() : "#undefined#";
    }

    /**
     * Get the node display title
     *
     * @return the display title
     */
    @Override
    public String getDisplayTitle() {
        return getEntity() != null ? getEntity().getDisplayTitle() : "#undefined#";
    }
    
    StringReadonlyProperty permissionproperty;
    StringReadonlyProperty albumproperty;
    StringReadonlyProperty idproperty;
    StringReadonlyProperty createdbyproperty;
    StringReadonlyProperty createdonproperty;
    StringReadonlyProperty updatedbyproperty;
    StringReadonlyProperty updatedonproperty;
    
    @Override
    protected List<PropertySupport.ReadOnly<?>> createPropertyItems(List<PropertySupport.ReadOnly<?>> props) {
        Albumgrouppermission e = getEntity();
        if (e != null) {
            props.add(new StringReadonlyProperty("Node Type", "AlbumgrouppermissionNode"));
            props.add(permissionproperty = new StringReadonlyProperty("Permission", e.formatPermission()));
            props.add(albumproperty = new StringReadonlyProperty("Album", e.formatAlbum()));
            props.add(idproperty = new StringReadonlyProperty("Id", e.formatId()));
            props.add(createdbyproperty = new StringReadonlyProperty("Created by", e.formatCreatedby()));
            props.add(createdonproperty = new StringReadonlyProperty("Created on", e.formatCreatedon()));
            props.add(updatedbyproperty = new StringReadonlyProperty("Updated by", e.formatUpdatedby()));
            props.add(updatedonproperty = new StringReadonlyProperty("Updated on", e.formatUpdatedon()));
        }
        return props;
    }
    
    @Override
    protected void nodeProcessFieldChange(AlbumgrouppermissionField field) {
        Albumgrouppermission e = getEntity();
        if (field == null) {
            if (e != null && idproperty != null) {
                permissionproperty.update(e.formatPermission());
                albumproperty.update(e.formatAlbum());
                idproperty.update(e.formatId());
                createdbyproperty.update(e.formatCreatedby());
                createdonproperty.update(e.formatCreatedon());
                updatedbyproperty.update(e.formatUpdatedby());
                updatedonproperty.update(e.formatUpdatedon());
                propertyChange();
            }
        } else {
            switch (field) {
                case PERMISSION:
                    if (e != null && permissionproperty != null) {
                        permissionproperty.update(e.formatPermission());
                        propertyChange("Permission");
                    }
                    break;
                case ALBUM:
                    if (e != null && albumproperty != null) {
                        albumproperty.update(e.formatAlbum());
                        propertyChange("Album");
                    }
                    break;
            }
        }
    }
    
    @Override
    protected void nodeCutPaste(BasicNode child) throws IOException {
        CoreEntity e = child.getEntity();
        if (e instanceof Group) {
            Group f = (Group) e;
            f.getParent().removeGroup(f);
            getEntity().addGroup(f);
            return;
        }
        addNewChild(e);
    }
    
    @Override
    protected void nodeCopyPaste(BasicNode child) throws IOException {
        CoreEntity e = child.getEntity();
        if (e instanceof Group) {
            Group.getEM().getNew((Group) e, getEntity());
            return;
        }
        addNewChild(e);
    }
    
    private void addNewChild(CoreEntity e) {
        throw new LogicException("Albumgrouppermission Entity does not have a child of the requested class (" + e.getClass().getSimpleName() + ")");
    }
    
    @Override
    protected void nodeDelete() throws IOException {
        getEntity().remove();
    }
    
    @Override
    protected void nodeReorderChildByFlavor(DataFlavor df, int[] perm) {
    }
    
    @Override
    public Action getPreferredAction() {
        return findDefaultAction("AlbumgrouppermissionNode");
    }
    
    @Override
    public Action[] getActions(boolean context) {
        return findActions("AlbumgrouppermissionNode");
    }
}
