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.wsdl.refactoring;
043:
044: import java.awt.Image;
045: import java.util.ArrayList;
046: import java.util.List;
047:
048: import javax.swing.Action;
049:
050: import org.netbeans.modules.xml.schema.model.SchemaComponent;
051: import org.netbeans.modules.xml.schema.model.SchemaModel;
052: import org.netbeans.modules.xml.wsdl.model.Definitions;
053: import org.netbeans.modules.xml.wsdl.model.WSDLComponent;
054: import org.netbeans.modules.xml.wsdl.model.WSDLModel;
055: import org.netbeans.modules.xml.wsdl.ui.netbeans.module.WSDLDataObject;
056: import org.netbeans.modules.xml.wsdl.ui.view.treeeditor.NodesFactory;
057: import org.netbeans.modules.xml.xam.Component;
058: import org.netbeans.modules.xml.xam.Model;
059: import org.netbeans.modules.xml.xam.ModelSource;
060: import org.netbeans.modules.xml.xam.dom.DocumentModel;
061: import org.netbeans.modules.xml.xam.ui.actions.GoToAction;
062: import org.openide.filesystems.FileObject;
063: import org.openide.loaders.DataObject;
064: import org.openide.nodes.AbstractNode;
065: import org.openide.nodes.Children;
066: import org.openide.nodes.FilterNode;
067: import org.openide.nodes.Node;
068: import org.openide.util.actions.SystemAction;
069:
070: /**
071: *
072: * @author Jeri Lockhart
073: */
074: public class WSDLUIHelper {
075:
076: private static final SystemAction[] ACTIONS = new SystemAction[] { SystemAction
077: .get(GoToAction.class) };
078:
079: private WSDLHelperNode displayNode;
080:
081: /**
082: * Creates a new instance of WSDLUIHelper
083: */
084: public WSDLUIHelper() {
085: super ();
086: }
087:
088: /**
089: * Return UI relevant path from root. Specific implementation should
090: * override.
091: */
092:
093: public List<Component> getRelevantPathFromRoot(Component component) {
094: ArrayList<Component> pathFromRoot = new ArrayList<Component>();
095: Component dc = component;
096: pathFromRoot.add(dc);
097: while (dc.getParent() != null) {
098: dc = dc.getParent();
099: if (!(dc instanceof Definitions)) {
100: pathFromRoot.add(0, dc);
101: }
102:
103: }
104: return pathFromRoot;
105: }
106:
107: /**
108: * Returns specific node for displaying the component in a preview window.
109: *
110: * The Node should return the following information that will be used
111: * in the refactoring UI:
112: *
113: * getActions(boolean) -
114: * other Actions for the Component, preferably navigational actions
115: * Minimally, getActions() should return a Go To Source Action, which
116: * will open the source (text) view with the cursor at the Component line
117: * The Actions should also implement org.openide.util.actions.Presenter.
118: * When the action is invoked from a prefuse graph node,
119: * actionPerformed(ActionEvent) is called with the Component as the source
120: * in the ActionEvent.
121: *
122: * getDisplayName() -
123: * a String that will be used as the label on the Component's explorer and
124: * graph nodes.
125: *
126: * getHtmlDisplayName() -
127: * For the usage component, a one line code snippet with the name
128: * of the query component bolded. The Html display name is used in the
129: * Find Usages explorer and the refactoring preview explorer on the
130: * usage node.
131: * The string should be formatted to use < and > for
132: * the XML tags, and < and > for the HTML tags. In the following example,
133: * Find Usages was run on a schema global type named "POSLogCurrencyCode".
134: * The Node represents a schema local element that uses POSLogCurrencyCode.
135: * getHtmlDisplayName() returns the first line of the local element.
136: * The text "POSLogCurrencyCode" in the snippet will be bolded because it is
137: * the name of the query Component.
138: *
139: * <xs:element name="CurrencyCode" type="<b>POSLogCurrencyCode</b>" minOccurs="0"/>
140: *
141: * getIcon() -
142: * an Image for the icon on the Components explorer and graph nodes.
143: *
144: * getPreferredAction() -
145: * the Action which navigates to the primary view of the Component
146: */
147:
148: public Node getDisplayNode(Component component) {
149: if (!(component instanceof WSDLComponent)
150: && !(component instanceof SchemaComponent)) {
151: return null;
152: }
153:
154: if (component instanceof SchemaComponent) {
155: SchemaComponent sc = (SchemaComponent) component;
156: SchemaModel sm = sc.getModel();
157: assert sm != null : "Given a dead component";
158: // return null;
159: // UIHelper delegate = RefactoringManager.getInstance().getTargetComponentUIHelper(sm);
160: // if (delegate != null) {
161: // return delegate.getDisplayNode(component);
162: // }
163: }
164:
165: Model model = component.getModel();
166: try {
167: if (model instanceof WSDLModel) {
168: ModelSource ms = model.getModelSource();
169: FileObject fo = (FileObject) ms.getLookup().lookup(
170: FileObject.class);
171: if (fo != null) {
172: DataObject dObj = DataObject.find(fo);
173: if (dObj != null && dObj instanceof WSDLDataObject) {
174: Node node = NodesFactory.getInstance().create(
175: component);
176: if (node != null) {
177: Node filterNode = new FilterNode(node) {
178: @Override
179: public SystemAction[] getActions(
180: boolean val) {
181: return ACTIONS;
182: }
183:
184: @Override
185: public Action getPreferredAction() {
186: return ACTIONS[0];
187: }
188: };
189:
190: return filterNode;
191: }
192: }
193: }
194: }
195: } catch (Exception ex) {
196: ex.printStackTrace();
197: }
198:
199: if (displayNode == null) {
200: displayNode = new WSDLHelperNode(component);
201: }
202:
203: return displayNode;
204: }
205:
206: public Node getDisplayNode(Model model) {
207: if (model instanceof DocumentModel) {
208: return getDisplayNode(((DocumentModel) model)
209: .getRootComponent());
210: }
211: return null;
212: }
213:
214: public class WSDLHelperNode extends AbstractNode {
215: private Component comp;
216:
217: public WSDLHelperNode(Component component) {
218: super (Children.LEAF);
219: this .comp = component;
220: initialize();
221: }
222:
223: public void initialize() {
224: this .setName("Temporary Name"); // Name
225: }
226:
227: @Override
228: public Image getIcon(int i) {
229: return super .getIcon(i);
230: }
231:
232: /**
233: * XML code snippet
234: *
235: */
236: @Override
237: public String getHtmlDisplayName() {
238: return "Temporary <b>Name</b>";
239: }
240:
241: @Override
242: public Action[] getActions(boolean b) {
243: return ACTIONS;
244: }
245:
246: @Override
247: public Action getPreferredAction() {
248: return ACTIONS[0];
249: }
250:
251: }
252:
253: }
|