001    /******************************************************************************
002     * Copyright (C) MActor Developers. All rights reserved.                        *
003     * ---------------------------------------------------------------------------*
004     * This file is part of MActor.                                               *
005     *                                                                            *
006     * MActor is free software; you can redistribute it and/or modify             *
007     * it under the terms of the GNU General Public License as published by       *
008     * the Free Software Foundation; either version 2 of the License, or          *
009     * (at your option) any later version.                                        *
010     *                                                                            *
011     * MActor is distributed in the hope that it will be useful,                  *
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
014     * GNU General Public License for more details.                               *
015     *                                                                            *
016     * You should have received a copy of the GNU General Public License          *
017     * along with MActor; if not, write to the Free Software                      *
018     * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA *
019     ******************************************************************************/
020    package org.mactor.ui.gui.project;
021    
022    import java.util.HashMap;
023    import java.util.HashSet;
024    import java.util.Map;
025    import java.util.Set;
026    
027    public class DropAcceptRules {
028            private static Map<ProjectNodeType, Set<ProjectNodeType>> rulesMap = buildAcceptInfoMap();
029            private static Map<ProjectNodeType, Set<ProjectNodeType>> buildAcceptInfoMap() {
030                    Map<ProjectNodeType, Set<ProjectNodeType>> map = new HashMap<ProjectNodeType, Set<ProjectNodeType>>();
031                    // Global config
032                    map.put(ProjectNodeType.PROJECT_GLOBAL_CONFIG, toSet(new ProjectNodeType[] { ProjectNodeType.G_GLOBAL_CONFIG }));
033                    map.put(ProjectNodeType.G_GLOBAL_CONFIG, toSet(new ProjectNodeType[] { ProjectNodeType.G_GROUP, ProjectNodeType.VALUE }));
034                    map.put(ProjectNodeType.G_GROUP, toSet(new ProjectNodeType[] { ProjectNodeType.VALUE }));
035                    // Message broker config
036                    map.put(ProjectNodeType.PROJECT_MESSAGE_BROKER_CONFIG, toSet(new ProjectNodeType[] { ProjectNodeType.MBC_MESSAGE_BROKERS }));
037                    map.put(ProjectNodeType.MBC_MESSAGE_BROKERS, toSet(new ProjectNodeType[] { ProjectNodeType.MBC_MESSAGE_BROKER }));
038                    map.put(ProjectNodeType.MBC_MESSAGE_BROKER, toSet(new ProjectNodeType[] { ProjectNodeType.MBC_CHANNEL, ProjectNodeType.VALUE }));
039                    map.put(ProjectNodeType.MBC_CHANNEL, toSet(new ProjectNodeType[] { ProjectNodeType.VALUE }));
040                    // Test
041                    map.put(ProjectNodeType.PROJECT_TEST, toSet(new ProjectNodeType[] { ProjectNodeType.T_TEST }));
042                    Set<ProjectNodeType> testContainerNode = toSet(new ProjectNodeType[] { ProjectNodeType.T_ACTION, ProjectNodeType.T_CONDITION, ProjectNodeType.T_EVENT_CHOICE, ProjectNodeType.T_LOOP,
043                                    ProjectNodeType.T_MESSAGE_PUBLISH, ProjectNodeType.T_MESSAGE_RECEIVE, ProjectNodeType.T_MESSAGE_SUBSCRIBE, ProjectNodeType.T_VALUE });
044                    map.put(ProjectNodeType.T_TEST, testContainerNode);
045                    map.put(ProjectNodeType.T_CONDITION, testContainerNode);
046                    // map.put(ProjectNodeType.T_EVENT_CHOICE_BRANCH, testContainerNode);
047                    map.put(ProjectNodeType.T_LOOP, testContainerNode);
048                    Set<ProjectNodeType> mrSet = new HashSet<ProjectNodeType>(testContainerNode);
049                    mrSet.add(ProjectNodeType.T_MESSAGE_RESPOND);
050                    map.put(ProjectNodeType.T_MESSAGE_RECEIVE, mrSet);
051                    // map.put(ProjectNodeType.T_EVENT_CHOICE, toSet(new ProjectNodeType[] {
052                    // ProjectNodeType.T_EVENT_CHOICE_BRANCH }));
053                    // Mock Battery
054                    map.put(ProjectNodeType.PROJECT_MOCK_BATTERY, toSet(new ProjectNodeType[] { ProjectNodeType.TM_MOCK_BATTERY }));
055                    map.put(ProjectNodeType.TM_MOCK_BATTERY, toSet(new ProjectNodeType[] { ProjectNodeType.TM_TEST }));
056                    // Test Data
057                    map.put(ProjectNodeType.PROJECT_TEST_RUN, toSet(new ProjectNodeType[] { ProjectNodeType.TR_TEST_RUN }));
058                    map.put(ProjectNodeType.TR_TEST_RUN, toSet(new ProjectNodeType[] {}));
059                    return map;
060            }
061            private static Set<ProjectNodeType> toSet(ProjectNodeType[] types) {
062                    Set<ProjectNodeType> s = new HashSet<ProjectNodeType>();
063                    for (ProjectNodeType t : types)
064                            s.add(t);
065                    return s;
066            }
067            public static DropAcceptInfo getDropAcceptInfo(ProjectTreeNode node, ProjectNodeType nodeTypeToDrop) {
068                    DropAcceptInfo dai = new DropAcceptInfo();
069                    if (nodeTypeToDrop != null && node != null) {
070                            if (node.getParentNode() != null) {
071                                    dai.acceptCopyAside = rulesMap.containsKey(node.getParentNode().getNodeType()) && rulesMap.get(node.getParentNode().getNodeType()).contains(nodeTypeToDrop);
072                                    // special rule for file nodes:
073                                    dai.acceptMoveAside = dai.acceptCopyAside && !(node.getParentNode() instanceof OrganizationProjectTreeNode);
074                            }
075                            dai.acceptCopyInto = rulesMap.containsKey(node.getNodeType()) && rulesMap.get(node.getNodeType()).contains(nodeTypeToDrop);
076                            dai.acceptMoveInto = dai.acceptCopyInto && !(node instanceof OrganizationProjectTreeNode);
077                    }
078                    return dai;
079            }
080            public static DropAcceptInfo getDropAcceptInfo(ProjectTreeNode node, ProjectTreeNode nodeToDrop) {
081                    DropAcceptInfo dai = new DropAcceptInfo();
082                    if (nodeToDrop != null && node != null) {
083                            if (node.getParentNode() != null) {
084                                    dai.acceptCopyAside = rulesMap.containsKey(node.getParentNode().getNodeType()) && rulesMap.get(node.getParentNode().getNodeType()).contains(nodeToDrop.getNodeType());
085                                    // special rule for file nodes:
086                                    dai.acceptMoveAside = dai.acceptCopyAside && !(node.getParentNode() instanceof OrganizationProjectTreeNode);
087                            }
088                            dai.acceptCopyInto = rulesMap.containsKey(node.getNodeType()) && rulesMap.get(node.getNodeType()).contains(nodeToDrop.getNodeType());
089                            dai.acceptMoveInto = dai.acceptCopyInto && !(node instanceof OrganizationProjectTreeNode);
090                            if (dai.acceptCopyInto || dai.acceptCopyAside) {
091                                    if (isRecursive(node, nodeToDrop)) {
092                                            dai.acceptCopyInto = false;
093                                            dai.acceptCopyAside = false;
094                                            dai.acceptMoveInto = false;
095                                            dai.acceptMoveAside = false;
096                                    }
097                            }
098                    }
099                    return dai;
100            }
101            private static boolean isRecursive(ProjectTreeNode node, ProjectTreeNode nodeToDrop) {
102                    if (node == nodeToDrop)
103                            return true;
104                    ProjectTreeNode parent = node.parentNode;
105                    while (parent != null && !(parent instanceof OrganizationProjectTreeNode)) {
106                            if (parent == nodeToDrop)
107                                    return true;
108                            parent = parent.getParentNode();
109                    }
110                    return false;
111            }
112            public static class DropAcceptInfo {
113                    private boolean acceptCopyInto;
114                    private boolean acceptCopyAside;
115                    private boolean acceptMoveInto;
116                    private boolean acceptMoveAside;
117                    public boolean isSomethingAccepted() {
118                            return acceptCopyInto || acceptCopyAside || acceptMoveInto || acceptMoveAside;
119                    }
120                    public boolean isCopyAccepted() {
121                            return acceptCopyAside || acceptCopyInto;
122                    }
123                    public boolean isMoveAccepted() {
124                            return acceptMoveAside || acceptMoveInto;
125                    }
126                    public boolean isCopyIntoAccepted() {
127                            return acceptCopyInto;
128                    }
129                    public boolean isMoveIntoAccepted() {
130                            return acceptMoveInto;
131                    }
132                    public boolean isCopyAsideAccepted() {
133                            return acceptCopyAside;
134                    }
135                    public boolean isMoveAsideAccepted() {
136                            return acceptMoveAside;
137                    }
138            }
139    }