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.HashMap;
024: import java.util.List;
025: import javax.swing.Action;
026: import javax.swing.JMenu;
027: import javax.swing.JPopupMenu;
028: import javax.swing.JSeparator;
029: import org.netbeans.modules.soa.ui.SoaUiUtil;
030: import org.netbeans.modules.soa.ui.TooltipTextProvider;
031: import org.netbeans.modules.soa.ui.axinodes.AxiomUtils;
032: import org.netbeans.modules.soa.ui.axinodes.NodeType;
033: import org.netbeans.modules.soa.ui.axinodes.NodeType.BadgeModificator;
034: import org.netbeans.modules.xml.axi.AXIComponent;
035: import org.netbeans.modules.xslt.mapper.model.nodes.NodeFactory;
036: import org.netbeans.modules.xslt.mapper.model.nodes.TreeNode;
037: import org.netbeans.modules.xslt.mapper.model.nodes.actions.ActionConst;
038: import org.netbeans.modules.xslt.mapper.model.nodes.actions.ActionGroupConstructor;
039: import org.netbeans.modules.xslt.mapper.model.nodes.actions.AddNestedAxiGroup;
040: import org.netbeans.modules.xslt.mapper.model.nodes.actions.AddNestedRulesGroup;
041: import org.netbeans.modules.xslt.mapper.model.nodes.actions.DeleteAction;
042: import org.netbeans.modules.xslt.mapper.model.nodes.visitor.NodeVisitor;
043: import org.netbeans.modules.xslt.mapper.view.GetExpressionVisitor;
044: import org.netbeans.modules.xslt.mapper.view.XsltMapper;
045: import org.netbeans.modules.xslt.model.Element;
046: import org.netbeans.modules.xslt.model.LiteralResultElement;
047: import org.netbeans.modules.xslt.model.XslComponent;
048: import org.openide.util.NbBundle;
049:
050: /**
051: *
052: * @author Alexey
053: */
054: public class ElementDeclarationNode extends DeclarationNode implements
055: TooltipTextProvider {
056:
057: public ElementDeclarationNode(XslComponent component,
058: XsltMapper mapper) {
059: super (component, mapper);
060: }
061:
062: protected List<TreeNode> loadChildren() {
063: XslComponent myself = (XslComponent) getDataObject();
064:
065: List<AXIComponent> childTypes = AXIUtils
066: .getChildTypes(getType());
067: List<AXIComponent> usedTypes = new ArrayList<AXIComponent>();
068: List<TreeNode> xslNodes = new ArrayList<TreeNode>();
069:
070: //dont show child XSL components if the only child of current eleemnt is "value-of" element
071: if (GetExpressionVisitor.isValueOfContainer(myself) == null) {
072:
073: List<XslComponent> children = myself.getChildren();
074: for (XslComponent c : children) {
075: TreeNode newNode = (TreeNode) NodeFactory.createNode(c,
076: getMapper());
077:
078: if (newNode == null) {
079: continue;
080: }
081:
082: newNode.setParent(this );
083: xslNodes.add(newNode);
084: usedTypes.add(newNode.getType());
085: }
086: }
087: TreeNode current = null;
088: int lastPos = 0;
089: List<TreeNode> results = new ArrayList<TreeNode>();
090:
091: for (TreeNode xsl_tn : xslNodes) {
092: AXIComponent type = xsl_tn.getType();
093: int pos0 = childTypes.indexOf(type);
094: if (pos0 > 0) {
095: for (int n = lastPos; n < pos0; n++) {
096: AXIComponent t = childTypes.get(n);
097: if (!usedTypes.contains(t)) {
098: results.add(createSchemaNode(t));
099: }
100: }
101: lastPos = pos0 + 1;
102: }
103: results.add(xsl_tn);
104: }
105:
106: //add the remaining schema nodes
107: for (int n = lastPos; n < childTypes.size(); n++) {
108: AXIComponent t = childTypes.get(n);
109:
110: if (!usedTypes.contains(t)) {
111: results.add(createSchemaNode(t));
112: }
113: }
114: return results;
115: }
116:
117: public AXIComponent getType() {
118: AXIComponent type = AXIUtils.getType(getComponent(),
119: getMapper());
120: if (type == null || type.getModel() == null) {
121: return null;
122: }
123: return type;
124: }
125:
126: public void accept(NodeVisitor visitor) {
127: visitor.visit(this );
128: }
129:
130: public String toString() {
131: XslComponent comp = getComponent();
132: if (comp instanceof Element) {
133: return ((Element) comp).getName().getQName().getLocalPart();
134: } else if (comp instanceof LiteralResultElement) {
135: return ((LiteralResultElement) comp).getQName()
136: .getLocalPart();
137: }
138: return comp.toString();
139: }
140:
141: public Image getIcon() {
142: AXIComponent axiComponent = getType();
143: if (axiComponent instanceof org.netbeans.modules.xml.axi.Element) {
144: BadgeModificator bm = AxiomUtils
145: .getElementBadge((org.netbeans.modules.xml.axi.Element) axiComponent);
146: return NodeType.ELEMENT.getImage(bm);
147: }
148: //
149: return NodeType.ELEMENT.getImage(BadgeModificator.SINGLE);
150: }
151:
152: public String getName() {
153: AXIComponent axiComponent = getType();
154: if (axiComponent instanceof org.netbeans.modules.xml.axi.Element) {
155: return ((org.netbeans.modules.xml.axi.Element) axiComponent)
156: .getName();
157: }
158: return toString();
159: }
160:
161: public String getName(boolean selected) {
162: AXIComponent axiComponent = getType();
163:
164: if (selected) {
165: return getName();
166: } else if (axiComponent instanceof org.netbeans.modules.xml.axi.Element) {
167: return getName();
168: } else {
169: return SoaUiUtil.getFormattedHtmlString(true,
170: new SoaUiUtil.TextChunk(getName(),
171: SoaUiUtil.MISTAKE_RED));
172: }
173: }
174:
175: public String getTooltipText() {
176: AXIComponent axiComponent = getType();
177: if (axiComponent instanceof org.netbeans.modules.xml.axi.Element) {
178: return AxiomUtils
179: .getElementTooltip((org.netbeans.modules.xml.axi.Element) axiComponent);
180: } else {
181: return SoaUiUtil.getFormattedHtmlString(true,
182: new SoaUiUtil.TextChunk(toString(),
183: SoaUiUtil.MISTAKE_RED));
184: }
185: }
186:
187: public JPopupMenu constructPopupMenu() {
188: JPopupMenu rootMenu = new JPopupMenu();
189: //
190: String localizedName = NbBundle.getMessage(ActionConst.class,
191: ActionConst.ADD_MENU);
192: JMenu addMenu = new JMenu(localizedName);
193: //
194: ActionGroupConstructor nestedAxi = new AddNestedAxiGroup(
195: getMapper(), this );
196: Action[] addNestedAxiArr = nestedAxi.getActions();
197: //
198: AddNestedRulesGroup nestedRules = new AddNestedRulesGroup(
199: getMapper(), this );
200: Action[] addNestedRuleArr = nestedRules.getActions();
201: //
202: if (addNestedAxiArr != null) {
203: for (Action action : addNestedAxiArr) {
204: addMenu.add(action);
205: }
206: }
207: //
208: if (addNestedAxiArr != null && addNestedAxiArr.length > 0
209: && addNestedRuleArr != null
210: && addNestedRuleArr.length > 0) {
211: addMenu.add(new JSeparator());
212: }
213: //
214: if (addNestedRuleArr != null) {
215: for (Action action : addNestedRuleArr) {
216: addMenu.add(action);
217: }
218: }
219: // Add menu is added only if it's not empty
220: if (addMenu.getMenuComponentCount() != 0) {
221: rootMenu.add(addMenu);
222: }
223: //
224: Action newAction = new DeleteAction(getMapper(), this );
225: rootMenu.add(newAction);
226: //
227: return rootMenu;
228: }
229:
230: private TreeNode createSchemaNode(AXIComponent c) {
231: TreeNode newNode = (TreeNode) NodeFactory.createNode(c,
232: getMapper());
233: if (newNode != null) {
234: newNode.setParent(ElementDeclarationNode.this);
235: }
236: return newNode;
237: }
238: }
|