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.Collection;
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.xml.axi.AXIType;
036: import org.netbeans.modules.xml.axi.Attribute;
037: import org.netbeans.modules.xml.axi.Element;
038: import org.netbeans.modules.xml.axi.datatype.NumberBase;
039: import org.netbeans.modules.xml.schema.model.Attribute.Use;
040: import org.netbeans.modules.xslt.mapper.model.nodes.NodeFactory;
041: import org.netbeans.modules.xslt.mapper.model.nodes.TreeNode;
042: import org.netbeans.modules.xslt.mapper.model.nodes.actions.ActionConst;
043: import org.netbeans.modules.xslt.mapper.model.nodes.actions.AddNestedRulesGroup;
044: import org.netbeans.modules.xslt.mapper.model.nodes.actions.AddPredicateAction;
045: import org.netbeans.modules.xslt.mapper.model.nodes.actions.AddThisAxiComponentAction;
046: import org.netbeans.modules.xslt.mapper.model.nodes.visitor.NodeVisitor;
047: import org.netbeans.modules.xslt.mapper.view.PredicateManager;
048: import org.netbeans.modules.xslt.mapper.view.XsltMapper;
049: import org.openide.util.NbBundle;
050:
051: /**
052: *
053: * @author Alexey
054: */
055: public class SchemaNode extends TreeNode implements TooltipTextProvider {
056:
057: // private transient Boolean isSourceViewNode = null;
058:
059: /** Creates a new instance of PlaceholderNode */
060: public SchemaNode(AXIComponent component, XsltMapper mapper) {
061: super (component, mapper);
062: }
063:
064: protected SchemaNode(Object dataObject, XsltMapper mapper) {
065: super (dataObject, mapper);
066: }
067:
068: public void accept(NodeVisitor visitor) {
069: visitor.visit(this );
070: }
071:
072: public AXIComponent getType() {
073: return (AXIComponent) getDataObject();
074: }
075:
076: public boolean isMappable() {
077: return true;
078: }
079:
080: public String toString() {
081: String name = ((AXIType) getType()).getName();
082: return ((getType() instanceof Attribute) ? "@" : "") + name;
083: }
084:
085: protected List<TreeNode> loadChildren() {
086: final ArrayList<TreeNode> result = new ArrayList<TreeNode>();
087:
088: AXIComponent axic = getType();
089: if (axic instanceof Element) {
090: new AXIUtils.ElementVisitor() {
091: public void visit(AXIComponent c) {
092: TreeNode newNode = (TreeNode) NodeFactory
093: .createNode(c, getMapper());
094: if (newNode != null) {
095: newNode.setParent(SchemaNode.this );
096: result.add(newNode);
097: }
098: //
099: // Load predicated nodes for source view
100: if (isSourceViewNode()) {
101: XsltMapper mapper = getMapper();
102: PredicateManager pManager = mapper
103: .getPredicateManager();
104: Collection<PredicatedSchemaNode> newPNodes = pManager
105: .createPredicatedNodes(newNode);
106: for (PredicatedSchemaNode newPNode : newPNodes) {
107: newPNode.setParent(SchemaNode.this );
108: result.add(newPNode);
109: }
110: }
111: }
112: }.visitSubelements((Element) axic);
113: }
114: return result;
115:
116: }
117:
118: public Image getIcon() {
119: AXIComponent axiComponent = getType();
120: if (axiComponent instanceof Element) {
121: BadgeModificator mult = AxiomUtils
122: .getElementBadge((Element) axiComponent);
123: return NodeType.ELEMENT.getImage(mult);
124: } else if (axiComponent instanceof Attribute) {
125: Use attrUse = ((Attribute) axiComponent).getUse();
126: if (attrUse == Use.OPTIONAL) {
127: return NodeType.ATTRIBUTE
128: .getImage(BadgeModificator.OPTIONAL);
129: }
130: return NodeType.ATTRIBUTE.getImage(BadgeModificator.SINGLE);
131: } else {
132: return null;
133: }
134: }
135:
136: public String getName() {
137: AXIComponent axiComponent = getType();
138: //
139: String result = null;
140: if (axiComponent instanceof Element) {
141: result = ((Element) axiComponent).getName();
142: } else if (axiComponent instanceof Attribute) {
143: result = ((Attribute) axiComponent).getName();
144: }
145: //
146: return result;
147: }
148:
149: public String getName(boolean selected) {
150: String result = getName();
151: //
152: if (!selected && !isSourceViewNode()) {
153: result = SoaUiUtil.getFormattedHtmlString(true,
154: new SoaUiUtil.TextChunk(result,
155: SoaUiUtil.INACTIVE_BLUE));
156: }
157: //
158: return result;
159: }
160:
161: public String getTooltipText() {
162: AXIComponent axiComponent = getType();
163: //
164: if (axiComponent instanceof Element) {
165: return AxiomUtils.getElementTooltip((Element) axiComponent);
166: } else if (axiComponent instanceof Attribute) {
167: return AxiomUtils
168: .getAttributeTooltip((Attribute) axiComponent);
169: }
170: //
171: return null;
172: }
173:
174: public JPopupMenu constructPopupMenu() {
175: JPopupMenu rootMenu = null;
176: Action newAction;
177: //
178: if (isSourceViewNode()) {
179: // Construct the popup menu for source tree
180: //
181: AXIComponent sc = getType();
182: if (sc instanceof Element) {
183: String max = ((Element) sc).getMaxOccurs();
184: boolean isRepeating = false;
185: if (NumberBase.UNBOUNDED_STRING.equals(max)) {
186: isRepeating = true;
187: } else {
188: try {
189: int maxInt = Integer.parseInt(max);
190: if (maxInt > 1) {
191: isRepeating = true;
192: }
193: } catch (NumberFormatException ex) {
194: // DO NOTHING HERE
195: }
196: }
197: //
198: if (isRepeating) {
199: newAction = new AddPredicateAction(getMapper(),
200: this );
201: rootMenu = new JPopupMenu();
202: rootMenu.add(newAction);
203: }
204: }
205: //
206: } else {
207: // Construct the popup menu for target tree
208: String localizedName = NbBundle.getMessage(
209: ActionConst.class, ActionConst.ADD_MENU);
210: JMenu addMenu = new JMenu(localizedName);
211: //
212: newAction = new AddThisAxiComponentAction(getMapper(), this );
213: addMenu.add(newAction);
214: //
215: AddNestedRulesGroup nestedRules = new AddNestedRulesGroup(
216: getMapper(), this );
217: Action[] addNestedRuleArr = nestedRules.getActions();
218: //
219: if (addNestedRuleArr != null && addNestedRuleArr.length > 0) {
220: addMenu.add(new JSeparator());
221: }
222: //
223: if (addNestedRuleArr != null) {
224: for (Action action : addNestedRuleArr) {
225: addMenu.add(action);
226: }
227: }
228: // Add menu is added only if it's not empty
229: if (addMenu.getMenuComponentCount() != 0) {
230: rootMenu = new JPopupMenu();
231: rootMenu.add(addMenu);
232: }
233: }
234: //
235: return rootMenu;
236: }
237:
238: }
|