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.actions;
021
022 import org.mactor.framework.MactorException;
023 import org.mactor.ui.gui.ResourceUtil;
024 import org.mactor.ui.gui.project.DropAcceptRules;
025 import org.mactor.ui.gui.project.GuiAction;
026 import org.mactor.ui.gui.project.ProjectController;
027 import org.mactor.ui.gui.project.ProjectNodeType;
028 import org.mactor.ui.gui.project.ProjectTreeNode;
029 import org.mactor.ui.gui.project.ProjectTreeNodeBuilder;
030 import org.mactor.ui.gui.project.DropAcceptRules.DropAcceptInfo;
031
032 public class CreateEntityAction implements GuiAction {
033 public boolean isPermitted(ProjectTreeNode selectedTreeNode, ProjectController projectController, String[] parameters) {
034 if (parameters == null || parameters.length == 0)
035 return false;
036 ProjectNodeType nodeType = ProjectNodeType.valueOf(parameters[0]);
037 DropAcceptInfo dropAcceptInfo = DropAcceptRules.getDropAcceptInfo(selectedTreeNode, nodeType);
038 return dropAcceptInfo.isCopyAccepted() || dropAcceptInfo.isCopyAsideAccepted();
039 }
040 public void perform(ProjectTreeNode selectedTreeNode, ProjectController projectController, String[] parameters) throws MactorException {
041 if (parameters == null || parameters.length == 0)
042 throw new MactorException("Node type not specified");
043 ProjectNodeType nodeType = ProjectNodeType.valueOf(parameters[0]);
044 if (nodeType == null)
045 throw new MactorException("Unsupported node type '" + parameters[0] + "'");
046 String template = null;
047 if (parameters.length > 1)
048 template = parameters[1];
049 else
050 template = parameters[0];
051 ProjectTreeNode node = ProjectTreeNodeBuilder.createNewNode(nodeType, getTemplateContent(template));
052 ProjectTreeNode targetNode = selectedTreeNode;
053 DropAcceptInfo dropAcceptInfo = DropAcceptRules.getDropAcceptInfo(targetNode, node);
054 if (dropAcceptInfo.isCopyIntoAccepted())
055 projectController.insertNodeIntoNode(selectedTreeNode, node);
056 else if (dropAcceptInfo.isCopyAsideAccepted())
057 projectController.insertNodeAfterNode(selectedTreeNode, node);
058 else
059 throw new MactorException("Illegal request");
060 }
061 public static String getTemplateContent(String templateName) {
062 try {
063 return ResourceUtil.readResourceTextContent("entity_action_templates" + "/" + templateName);
064 } catch (MactorException e) {
065 e.printStackTrace();
066 }
067 return null;
068 }
069 }