com.visural.stereotyped.components.MultiNodeOperationHelper.java Source code

Java tutorial

Introduction

Here is the source code for com.visural.stereotyped.components.MultiNodeOperationHelper.java

Source

/*
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (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.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 *
 * The Original Code is "stereotyped" (http://code.google.com/p/stereotyped).
 *
 * The Initial Developer of the Original Code is Richard Nichols.
 * Portions created by Richard Nichols are Copyright (C) 2010
 * Richard Nichols. All Rights Reserved.
 *
 * Alternatively, the contents of this file may be used under the terms
 * of the GNU Public License (the  "GPL"), in which case the
 * provisions of GPL License are applicable instead of those
 * above.  If you wish to allow use of your version of this file only
 * under the terms of the GPL and not to allow others to use
 * your version of this file under the MPL, indicate your decision by
 * deleting  the provisions above and replace  them with the notice and
 * other provisions required by the GPL.  If you do not delete
 * the provisions above, a recipient may use your version of this file
 * under either the MPL or the GPL License.
 */
package com.visural.stereotyped.components;

import com.google.common.collect.Sets;
import com.visural.stereotyped.core.Component;
import com.visural.stereotyped.core.HasUuid;
import com.visural.stereotyped.core.OperationPointer;
import com.visural.stereotyped.core.Slot;
import com.visural.stereotyped.core.Stereotype;
import com.visural.stereotyped.ui.model.OperableTreeNode;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 *
 * @author Richard Nichols
 */
public class MultiNodeOperationHelper {

    public static List<OperationPointer> getOperationsForOTN(Set<OperableTreeNode> nodes) {
        Set<HasUuid> hu = Sets.newHashSet();
        for (OperableTreeNode otn : nodes) {
            hu.add((HasUuid) otn.getComponentOrSlot());
        }
        return getOperationsFor(hu);
    }

    public static List<OperationPointer> getOperationsFor(Set<HasUuid> nodes) {
        List<OperationPointer> result = new ArrayList<OperationPointer>();
        MultiCopyOperation copy = new MultiCopyOperation();
        if (copy.isApplicableTo(nodes)) {
            result.add(new OperationPointer(copy, nodes));
        }

        MultiCutOperation cut = new MultiCutOperation();
        if (cut.isApplicableTo(nodes)) {
            result.add(new OperationPointer(cut, nodes));
        }

        MultiGroupOperation group = new MultiGroupOperation();
        if (group.isApplicableTo(nodes)) {
            result.add(new OperationPointer(group, nodes));
        }

        MultiMoveUpOperation moveup = new MultiMoveUpOperation();
        if (moveup.isApplicableTo(nodes)) {
            result.add(new OperationPointer(moveup, nodes));
        }

        MultiMoveDownOperation movedown = new MultiMoveDownOperation();
        if (movedown.isApplicableTo(nodes)) {
            result.add(new OperationPointer(movedown, nodes));
        }

        MultiDeleteOperation delete = new MultiDeleteOperation();
        if (delete.isApplicableTo(nodes)) {
            result.add(new OperationPointer(delete, nodes));
        }

        return result;
    }

    public static Slot getSameSlotForComponents(Set<HasUuid> nodes) {
        Slot slot = null;
        for (HasUuid node : nodes) {
            if (Component.class.isAssignableFrom(node.getClass())) {
                Component com = (Component) node;
                if (slot == null) {
                    slot = com.getParentSlot();
                }
                if (!com.getParentSlot().getUuid().equals(slot.getUuid())) {
                    return null;
                }
            } else {
                return null;
            }
        }
        return slot;
    }

    public static List<HasUuid> getOrderedSet(Set<HasUuid> nodes) {
        if (nodes == null || nodes.isEmpty()) {
            return new ArrayList();
        }
        List<HasUuid> list = new ArrayList<HasUuid>();
        recurseOrder(getStereotype(nodes), list, nodes);
        return list;
    }

    private static Stereotype getStereotype(Set<HasUuid> nodes) {
        if (nodes == null || nodes.isEmpty()) {
            return null;
        }
        HasUuid first = nodes.iterator().next();
        if (Component.class.isAssignableFrom(first.getClass())) {
            Component c = (Component) first;
            return c.getStereotype();
        } else {
            Slot s = (Slot) first;
            return s.getStereotype();
        }
    }

    private static void recurseOrder(HasUuid node, List<HasUuid> list, Set<HasUuid> nodes) {
        if (node == null) {
            return;
        }
        if (nodes.contains(node)) {
            list.add(node);
        }
        if (Component.class.isAssignableFrom(node.getClass())) {
            Component c = (Component) node;
            if (c.getSlots() != null) {
                for (Slot slot : c.getSlots()) {
                    recurseOrder(slot, list, nodes);
                }
            }
        } else {
            Slot s = (Slot) node;
            if (s.getContent() != null) {
                for (Component com : s.getContent()) {
                    recurseOrder(com, list, nodes);
                }
            }
        }
    }

    public static boolean isAllComponents(Set<HasUuid> nodes) {
        for (HasUuid node : nodes) {
            if (!Component.class.isAssignableFrom(node.getClass())) {
                return false;
            }
        }
        return true;
    }
}