001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019:
020: package org.netbeans.modules.xslt.mapper.model.nodes.actions;
021:
022: import java.awt.event.ActionEvent;
023: import java.lang.reflect.Method;
024: import javax.swing.Icon;
025: import javax.swing.ImageIcon;
026: import org.netbeans.modules.xslt.mapper.model.BranchConstructor;
027: import org.netbeans.modules.xslt.mapper.model.nodes.TreeNode;
028: import org.netbeans.modules.xslt.mapper.model.targettree.SchemaNode;
029: import org.netbeans.modules.xslt.mapper.model.targettree.StylesheetNode;
030: import org.netbeans.modules.xslt.mapper.view.XsltMapper;
031: import org.netbeans.modules.xslt.model.Instruction;
032: import org.netbeans.modules.xslt.model.SequenceConstructor;
033: import org.netbeans.modules.xslt.model.XslComponent;
034: import org.netbeans.modules.xslt.model.XslComponentFactory;
035: import org.netbeans.modules.xslt.model.XslModel;
036: import org.openide.ErrorManager;
037:
038: /**
039: *
040: * @author nk160297
041: */
042: public class AddNestedRule extends XsltNodeAction {
043:
044: private static final long serialVersionUID = 1L;
045:
046: protected SupportedRuleTypes myType;
047:
048: public AddNestedRule(XsltMapper xsltMapper, TreeNode node,
049: Class<? extends Instruction> ruleClass) {
050: this (xsltMapper, node, SupportedRuleTypes
051: .getRuleType(ruleClass));
052: }
053:
054: public AddNestedRule(XsltMapper xsltMapper, TreeNode node,
055: SupportedRuleTypes type) {
056: super (xsltMapper, node);
057: myType = type;
058: postInit();
059: }
060:
061: public String getDisplayName() {
062: return myType.getDisplayName();
063: }
064:
065: public Icon getIcon() {
066: Icon icon = new ImageIcon(myType.getImage());
067: return icon;
068: }
069:
070: public void actionPerformed(ActionEvent e) {
071: XslModel model = myXsltMapper.getContext().getXSLModel();
072: if (model == null) {
073: return;
074: }
075: //
076: try {
077: XslComponent parentComp = null;
078: if (myTreeNode instanceof SchemaNode) {
079: BranchConstructor xbc = new BranchConstructor(
080: (SchemaNode) myTreeNode, getMapper());
081: xbc.exitTranactionOnFinish(false);
082: parentComp = xbc.construct();
083: } else if (myTreeNode instanceof StylesheetNode) {
084: Object dataObject = myTreeNode.getDataObject();
085: if (dataObject != null
086: && dataObject instanceof XslComponent) {
087: parentComp = (XslComponent) dataObject;
088: }
089: }
090: //
091: if (parentComp != null
092: && parentComp instanceof SequenceConstructor) {
093: XslComponentFactory factory = model.getFactory();
094: //
095: Instruction newXslInstruction = null;
096: Method[] methodArr = XslComponentFactory.class
097: .getMethods();
098: for (Method method : methodArr) {
099: Class returnType = method.getReturnType();
100: if (returnType.equals(myType.getInterface())) {
101: try {
102: newXslInstruction = (Instruction) method
103: .invoke(factory);
104: break;
105: } catch (Exception ex) {
106: ErrorManager.getDefault().notify(ex);
107: }
108: }
109: }
110: //
111: assert newXslInstruction != null : "Can't create the rule object"; // NOI18N
112: //
113: // newXslAttribute.setName();
114: //
115: if (!model.isIntransaction()) {
116: model.startTransaction();
117: }
118: //
119: ((SequenceConstructor) parentComp)
120: .appendSequenceChild(newXslInstruction);
121: }
122: } finally {
123: if (model.isIntransaction()) {
124: model.endTransaction();
125: }
126: }
127: }
128:
129: }
|