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: package org.netbeans.modules.xslt.mapper.model.targettree;
020:
021: import java.awt.Image;
022: import java.util.ArrayList;
023: import java.util.List;
024: import javax.swing.Action;
025: import javax.swing.JPopupMenu;
026: import org.netbeans.modules.soa.ui.SoaUiUtil;
027: import org.netbeans.modules.soa.ui.TooltipTextProvider;
028: import org.netbeans.modules.soa.ui.axinodes.AxiomUtils;
029: import org.netbeans.modules.soa.ui.axinodes.NodeType;
030: import org.netbeans.modules.soa.ui.axinodes.NodeType.BadgeModificator;
031: import org.netbeans.modules.xml.axi.AXIComponent;
032: import org.netbeans.modules.xml.axi.Element;
033: import org.netbeans.modules.xml.schema.model.Attribute.Use;
034: import org.netbeans.modules.xslt.mapper.model.nodes.NodeFactory;
035: import org.netbeans.modules.xslt.mapper.model.nodes.TreeNode;
036: import org.netbeans.modules.xslt.mapper.model.nodes.actions.DeleteAction;
037: import org.netbeans.modules.xslt.mapper.model.nodes.visitor.NodeVisitor;
038: import org.netbeans.modules.xslt.mapper.view.GetExpressionVisitor;
039: import org.netbeans.modules.xslt.mapper.view.XsltMapper;
040: import org.netbeans.modules.xslt.model.Attribute;
041: import org.netbeans.modules.xslt.model.AttributeValueTemplate;
042: import org.netbeans.modules.xslt.model.XslComponent;
043:
044: /**
045: *
046: * @author Alexey
047: */
048: public class AttributeDeclarationNode extends DeclarationNode implements
049: TooltipTextProvider {
050:
051: /** Creates a new instance of AttributeDeclarationNode */
052: public AttributeDeclarationNode(XslComponent component,
053: XsltMapper mapper) {
054: super (component, mapper);
055: }
056:
057: /**
058: * attribute element has no children
059: * @returns empty list
060: **/
061: protected List<TreeNode> loadChildren() {
062: Attribute myself = (Attribute) getDataObject();
063:
064: if (GetExpressionVisitor.isValueOfContainer(myself) != null) {
065: //do not show child elements, if value-of element
066: //is the the only eleemnt inside this container
067: return new ArrayList<TreeNode>();
068: }
069:
070: List<XslComponent> children = myself.getChildren();
071: final List<TreeNode> result = new ArrayList<TreeNode>(children
072: .size());
073:
074: for (XslComponent c : children) {
075: TreeNode newNode = (TreeNode) NodeFactory.createNode(c,
076: getMapper());
077:
078: if (newNode != null) {
079: newNode.setParent(this );
080: result.add(newNode);
081: }
082: }
083: return result;
084:
085: }
086:
087: public AXIComponent getType() {
088: Element parent_type = (Element) getParent().getType();
089: XslComponent component = getComponent();
090:
091: if (parent_type != null) {
092: for (AXIComponent c : parent_type.getAttributes()) {
093: if (AXIUtils.isSameSchemaType(component, c)) {
094: return c;
095: }
096: }
097: }
098: return null;
099: }
100:
101: public Image getIcon() {
102: AXIComponent axiComponent = getType();
103: if (axiComponent instanceof org.netbeans.modules.xml.axi.Attribute) {
104: Use attrUse = ((org.netbeans.modules.xml.axi.Attribute) axiComponent)
105: .getUse();
106: if (attrUse == Use.OPTIONAL) {
107: return NodeType.ATTRIBUTE
108: .getImage(BadgeModificator.OPTIONAL);
109: } else {
110: return NodeType.ATTRIBUTE
111: .getImage(BadgeModificator.SINGLE);
112: }
113: }
114: //
115: return NodeType.ATTRIBUTE.getImage(BadgeModificator.SINGLE);
116: }
117:
118: public void accept(NodeVisitor visitor) {
119: visitor.visit(this );
120: }
121:
122: public String getName() {
123: AXIComponent axiComponent = getType();
124: if (axiComponent instanceof org.netbeans.modules.xml.axi.Attribute) {
125: return ((org.netbeans.modules.xml.axi.Attribute) axiComponent)
126: .getName();
127: } else {
128: Attribute myself = (Attribute) getDataObject();
129: return myself.getName().toString();
130: }
131: }
132:
133: public String getName(boolean selected) {
134: AXIComponent axiComponent = getType();
135: if (selected) {
136: return getName();
137: } else if (axiComponent instanceof org.netbeans.modules.xml.axi.Attribute) {
138: return getName();
139: } else {
140: String name = getName();
141: return SoaUiUtil
142: .getFormattedHtmlString(true,
143: new SoaUiUtil.TextChunk(name,
144: SoaUiUtil.MISTAKE_RED));
145: }
146: }
147:
148: public String toString() {
149:
150: Attribute component = (Attribute) getComponent();
151:
152: AttributeValueTemplate atv = component.getName();
153:
154: if (atv != null && atv.getQName() != null) {
155: return atv.getQName().getLocalPart();
156: }
157:
158: org.netbeans.modules.xml.axi.Attribute attribute = (org.netbeans.modules.xml.axi.Attribute) getType();
159: if (attribute != null && attribute.getName() != null) {
160: return attribute.getName();
161: }
162:
163: return super .toString();
164: }
165:
166: public String getTooltipText() {
167: AXIComponent axiComponent = getType();
168: if (axiComponent instanceof org.netbeans.modules.xml.axi.Attribute) {
169: return AxiomUtils
170: .getAttributeTooltip((org.netbeans.modules.xml.axi.Attribute) axiComponent);
171: } else {
172: Attribute myself = (Attribute) getDataObject();
173: String name = myself.getName().toString();
174: return SoaUiUtil
175: .getFormattedHtmlString(true,
176: new SoaUiUtil.TextChunk(name,
177: SoaUiUtil.MISTAKE_RED));
178: }
179: }
180:
181: public JPopupMenu constructPopupMenu() {
182: JPopupMenu rootMenu = new JPopupMenu();
183: Action newAction = new DeleteAction(getMapper(), this );
184: rootMenu.add(newAction);
185: //
186: return rootMenu;
187: }
188:
189: }
|