KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > schema > refactoring > SchemaUIHelper


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.schema.refactoring;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24 import javax.swing.Action JavaDoc;
25 import org.netbeans.modules.xml.refactoring.Usage;
26 import org.netbeans.modules.xml.refactoring.spi.UIHelper;
27 import org.netbeans.modules.xml.schema.model.Redefine;
28 import org.netbeans.modules.xml.schema.model.SchemaComponent;
29 import org.netbeans.modules.xml.schema.refactoring.ui.DisplayInfoVisitor;
30 import org.netbeans.modules.xml.schema.refactoring.ui.DisplayInfoVisitor.DisplayInfo;
31 import org.netbeans.modules.xml.schema.refactoring.ui.QueryUtilities;
32 import org.netbeans.modules.xml.schema.ui.nodes.SchemaNodeFactory;
33 import org.netbeans.modules.xml.schema.ui.nodes.categorized.CategorizedSchemaNodeFactory;
34 import org.netbeans.modules.xml.xam.Component;
35 import org.netbeans.modules.xml.xam.Named;
36 import org.netbeans.modules.xml.xam.ui.actions.GoToAction;
37 import org.netbeans.modules.xml.xam.ui.actions.ShowSourceAction;
38 import org.openide.nodes.Children;
39 import org.openide.nodes.FilterNode;
40 import org.openide.nodes.Node;
41 import org.openide.util.Lookup;
42 import org.openide.util.actions.SystemAction;
43 import org.openide.util.lookup.Lookups;
44
45 /**
46  *
47  * @author Jeri Lockhart
48  */

49 public class SchemaUIHelper extends UIHelper{
50     
51     /**
52      * Return UI relevant path from root. Specific implementation should
53      * override.
54      * For SchemaComponent, return only Named components,
55      * except the Redefine component
56      */

57     public List JavaDoc<Component> getRelevantPathFromRoot(Usage item) {
58         List JavaDoc<Component> path = item.getPathFromRoot();
59         ArrayList JavaDoc<Component> relevantPath = null;
60         if (path != null && path.size()>0){
61             relevantPath = new ArrayList JavaDoc<Component>();
62             for (Component c:path){
63                 if (c instanceof Named || c instanceof Redefine){
64                     relevantPath.add(c);
65                 }
66             }
67         }
68         return relevantPath;
69     }
70
71     /**
72      * Returns specific node for displaying the component in a preview window.
73      *
74      * The Node should return the following information that will be used
75      * in the refactoring UI:
76      *
77      * getActions(boolean) -
78      * other Actions for the Component, preferably navigational actions
79      * Minimally, getActions() should return a Go To Source Action, which
80      * will open the source (text) view with the cursor at the Component line
81      * The Actions should also implement org.openide.util.actions.Presenter.
82      * When the action is invoked from a prefuse graph node,
83      * actionPerformed(ActionEvent) is called with the Component as the source
84      * in the ActionEvent.
85      *
86      * getDisplayName() -
87      * a String that will be used as the label on the Component's explorer and
88      * graph nodes.
89      *
90      * getHtmlDisplayName() -
91      * For the usage component, a one line code snippet with the name
92      * of the query component bolded. The Html display name is used in the
93      * Find Usages explorer and the refactoring preview explorer on the
94      * usage node.
95      * The string should be formatted to use &lt and &gt for
96      * the XML tags, and < and > for the HTML tags. In the following example,
97      * Find Usages was run on a schema global type named "POSLogCurrencyCode".
98      * The Node represents a schema local element that uses POSLogCurrencyCode.
99      * getHtmlDisplayName() returns the first line of the local element.
100      * The text "POSLogCurrencyCode" in the snippet will be bolded because it is
101      * the name of the query Component.
102      *
103      * &lt;xs:element name="CurrencyCode" type="<b>POSLogCurrencyCode</b>" minOccurs="0"/&gt;
104      *
105      * getIcon() -
106      * an Image for the icon on the Components explorer and graph nodes.
107      *
108      * getPreferredAction() -
109      * the Action which navigates to the primary view of the Component
110      */

111     public Node getDisplayNode(Component component) {
112         assert component instanceof SchemaComponent:"This UIHelper handles SchemaComponents only";
113     SchemaComponent sc = SchemaComponent.class.cast(component);
114     
115         return createNode(sc);
116     }
117     
118     private Node createNode(final SchemaComponent sc) {
119     CategorizedSchemaNodeFactory nodeFactory =
120         new CategorizedSchemaNodeFactory(sc.getModel(), Lookups.singleton(sc));
121     return new FilteredSchemaNode(nodeFactory.createNode(sc),sc);
122     }
123     
124     public class FilteredSchemaNode extends FilterNode {
125     private final SchemaComponent sc;
126     
127     FilteredSchemaNode(Node n, SchemaComponent sc) {
128         super(n,Children.LEAF);
129         disableDelegation(DELEGATE_GET_SHORT_DESCRIPTION |
130                 DELEGATE_SET_SHORT_DESCRIPTION |
131                 DELEGATE_GET_DISPLAY_NAME |
132                 DELEGATE_GET_CONTEXT_ACTIONS |
133                 DELEGATE_GET_ACTIONS |
134                 DELEGATE_DESTROY);
135         this.sc = sc;
136           
137         DisplayInfoVisitor div = new DisplayInfoVisitor();
138             DisplayInfo dInfo = div.getDisplayInfo(sc);
139             this.setShortDescription(dInfo.getCompType()); // Component type
140
this.setDisplayName(dInfo.getElementType()); // Element Type
141
}
142     
143     /**
144          * XML code snippet
145          *
146          */

147         public String JavaDoc getHtmlDisplayName() {
148             return QueryUtilities.getTextForSchemaComponent(sc);
149         }
150     
151     public Action JavaDoc[] getActions(boolean b) {
152             return ACTIONS;
153         }
154
155         public Action JavaDoc getPreferredAction() {
156             return SystemAction.get(ShowSourceAction.class);
157         }
158
159     }
160     
161     private static final SystemAction[] ACTIONS =
162         new SystemAction[] {
163         SystemAction.get(GoToAction.class)
164     };
165     
166 }
167
Popular Tags