001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.xml.schema.refactoring;
043:
044: import java.util.ArrayList;
045: import java.util.List;
046: import javax.swing.Action;
047: import org.netbeans.modules.xml.schema.model.Redefine;
048: import org.netbeans.modules.xml.schema.model.SchemaComponent;
049: import org.netbeans.modules.xml.schema.refactoring.ui.DisplayInfoVisitor;
050: import org.netbeans.modules.xml.schema.refactoring.ui.DisplayInfoVisitor.DisplayInfo;
051: import org.netbeans.modules.xml.schema.refactoring.ui.QueryUtilities;
052: import org.netbeans.modules.xml.schema.ui.nodes.SchemaNodeFactory;
053: import org.netbeans.modules.xml.schema.ui.nodes.categorized.CategorizedSchemaNodeFactory;
054: import org.netbeans.modules.xml.xam.Component;
055: import org.netbeans.modules.xml.xam.Named;
056: import org.netbeans.modules.xml.xam.ui.actions.GoToAction;
057: import org.netbeans.modules.xml.xam.ui.actions.ShowSourceAction;
058: import org.openide.nodes.Children;
059: import org.openide.nodes.FilterNode;
060: import org.openide.nodes.Node;
061: import org.openide.util.Lookup;
062: import org.openide.util.actions.SystemAction;
063: import org.openide.util.lookup.Lookups;
064:
065: /**
066: *
067: * @author Jeri Lockhart
068: */
069: public class SchemaUIHelper {
070:
071: /**
072: * Return UI relevant path from root. Specific implementation should
073: * override.
074: * For SchemaComponent, return only Named components,
075: * except the Redefine component
076: */
077: /* public List<Component> getRelevantPathFromRoot(Usage item) {
078: List<Component> path = item.getPathFromRoot();
079: ArrayList<Component> relevantPath = null;
080: if (path != null && path.size()>0){
081: relevantPath = new ArrayList<Component>();
082: for (Component c:path){
083: if (c instanceof Named || c instanceof Redefine){
084: relevantPath.add(c);
085: }
086: }
087: }
088: return relevantPath;
089: }*/
090:
091: /**
092: * Returns specific node for displaying the component in a preview window.
093: *
094: * The Node should return the following information that will be used
095: * in the refactoring UI:
096: *
097: * getActions(boolean) -
098: * other Actions for the Component, preferably navigational actions
099: * Minimally, getActions() should return a Go To Source Action, which
100: * will open the source (text) view with the cursor at the Component line
101: * The Actions should also implement org.openide.util.actions.Presenter.
102: * When the action is invoked from a prefuse graph node,
103: * actionPerformed(ActionEvent) is called with the Component as the source
104: * in the ActionEvent.
105: *
106: * getDisplayName() -
107: * a String that will be used as the label on the Component's explorer and
108: * graph nodes.
109: *
110: * getHtmlDisplayName() -
111: * For the usage component, a one line code snippet with the name
112: * of the query component bolded. The Html display name is used in the
113: * Find Usages explorer and the refactoring preview explorer on the
114: * usage node.
115: * The string should be formatted to use < and > for
116: * the XML tags, and < and > for the HTML tags. In the following example,
117: * Find Usages was run on a schema global type named "POSLogCurrencyCode".
118: * The Node represents a schema local element that uses POSLogCurrencyCode.
119: * getHtmlDisplayName() returns the first line of the local element.
120: * The text "POSLogCurrencyCode" in the snippet will be bolded because it is
121: * the name of the query Component.
122: *
123: * <xs:element name="CurrencyCode" type="<b>POSLogCurrencyCode</b>" minOccurs="0"/>
124: *
125: * getIcon() -
126: * an Image for the icon on the Components explorer and graph nodes.
127: *
128: * getPreferredAction() -
129: * the Action which navigates to the primary view of the Component
130: */
131: public Node getDisplayNode(Component component) {
132: assert component instanceof SchemaComponent : "This UIHelper handles SchemaComponents only";
133: SchemaComponent sc = SchemaComponent.class.cast(component);
134:
135: return createNode(sc);
136: }
137:
138: private Node createNode(final SchemaComponent sc) {
139: CategorizedSchemaNodeFactory nodeFactory = new CategorizedSchemaNodeFactory(
140: sc.getModel(), Lookups.singleton(sc));
141: return new FilteredSchemaNode(nodeFactory.createNode(sc), sc);
142: }
143:
144: public class FilteredSchemaNode extends FilterNode {
145: private final SchemaComponent sc;
146:
147: FilteredSchemaNode(Node n, SchemaComponent sc) {
148: super (n, Children.LEAF);
149: disableDelegation(DELEGATE_GET_SHORT_DESCRIPTION
150: | DELEGATE_SET_SHORT_DESCRIPTION
151: | DELEGATE_GET_DISPLAY_NAME
152: | DELEGATE_GET_CONTEXT_ACTIONS
153: | DELEGATE_GET_ACTIONS | DELEGATE_DESTROY);
154: this .sc = sc;
155:
156: DisplayInfoVisitor div = new DisplayInfoVisitor();
157: DisplayInfo dInfo = div.getDisplayInfo(sc);
158: this .setShortDescription(dInfo.getCompType()); // Component type
159: this .setDisplayName(dInfo.getElementType()); // Element Type
160: }
161:
162: /**
163: * XML code snippet
164: *
165: */
166: public String getHtmlDisplayName() {
167: return QueryUtilities.getTextForSchemaComponent(sc);
168: }
169:
170: public Action[] getActions(boolean b) {
171: return ACTIONS;
172: }
173:
174: public Action getPreferredAction() {
175: return SystemAction.get(ShowSourceAction.class);
176: }
177:
178: }
179:
180: private static final SystemAction[] ACTIONS = new SystemAction[] { SystemAction
181: .get(GoToAction.class) };
182:
183: }
|