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: package org.netbeans.modules.bpel.editors.api.utils;
042:
043: import javax.swing.Icon;
044: import java.beans.BeanInfo;
045: import javax.swing.ImageIcon;
046: import org.openide.nodes.Node;
047: import org.openide.util.Lookup;
048: import org.openide.util.lookup.Lookups;
049: import org.netbeans.modules.xml.schema.model.SchemaComponent;
050: import org.netbeans.modules.xml.wsdl.model.WSDLComponent;
051: import org.netbeans.modules.bpel.editors.api.nodes.NodeType;
052: import org.netbeans.modules.bpel.model.api.BooleanExpr;
053: import org.netbeans.modules.bpel.model.api.BpelEntity;
054: import org.netbeans.modules.xml.xam.Component;
055: import org.netbeans.modules.xml.xam.Named;
056: import org.netbeans.modules.xml.xam.dom.DocumentComponent;
057: import org.netbeans.modules.xml.schema.ui.nodes.categorized.CategorizedSchemaNodeFactory;
058: import org.netbeans.modules.xml.wsdl.ui.view.treeeditor.NodesFactory;
059:
060: /**
061: *
062: * @author Vitaly Bychkov
063: * @version 1.0
064: */
065: public class RefactorUtil {
066: public static final int MAX_SIMPLE_NAME_LENGTH = 50;
067: public static final String ENTITY_SEPARATOR = "."; // NOI18N
068:
069: private RefactorUtil() {
070: }
071:
072: public static String getName(Component component) {
073: String name = null;
074:
075: if (component instanceof Named) {
076: name = ((Named) component).getName();
077: } else if (component instanceof BooleanExpr) {
078: name = ((BooleanExpr) component).getContent();
079: name = name == null ? null : name.trim();
080: if (name != null && name.length() > MAX_SIMPLE_NAME_LENGTH) {
081: name = name.substring(0, MAX_SIMPLE_NAME_LENGTH);
082: }
083: } else if (component instanceof BpelEntity) {
084: org.netbeans.modules.bpel.editors.api.nodes.NodeType bpelNodeType = org.netbeans.modules.bpel.editors.api.utils.Util
085: .getBasicNodeType((BpelEntity) component);
086:
087: if (bpelNodeType != null
088: && !NodeType.UNKNOWN_TYPE.equals(bpelNodeType)) {
089: name = bpelNodeType.getDisplayName();
090: }
091: }
092:
093: if (name == null && component instanceof DocumentComponent) {
094: name = Util.getTagName((DocumentComponent) component);
095: }
096:
097: return name == null ? "" : name;
098: }
099:
100: public static String getHtmlName(Component component) {
101: String htmlName = null;
102: NodeType nodeType = getBpelNodeType(component);
103:
104: HtmlNameManager[] nameManagers = HtmlNameManager.HTML_NAME_MANAGERS;
105: for (HtmlNameManager htmlNameManager : nameManagers) {
106: if (htmlNameManager.accept(nodeType, component)) {
107: htmlName = htmlNameManager.getHtmlName(nodeType,
108: component);
109: }
110: }
111:
112: htmlName = htmlName == null ? "" : htmlName;
113:
114: return removeHtmlHeader(htmlName);
115: }
116:
117: public static Icon getIcon(Component component) {
118: // vlv
119: Node node = getNode(component);
120:
121: if (node != null) {
122: return new ImageIcon(node
123: .getIcon(BeanInfo.ICON_COLOR_16x16));
124: }
125: Icon icon = null;
126:
127: if (component instanceof BpelEntity) {
128: org.netbeans.modules.bpel.editors.api.nodes.NodeType bpelNodeType = org.netbeans.modules.bpel.editors.api.utils.Util
129: .getBasicNodeType((BpelEntity) component);
130: if (bpelNodeType != null
131: && !org.netbeans.modules.bpel.editors.api.nodes.NodeType.UNKNOWN_TYPE
132: .equals(bpelNodeType)) {
133: icon = bpelNodeType.getIcon();
134: }
135: }
136:
137: icon = icon != null ? icon
138: : org.netbeans.modules.bpel.editors.api.nodes.NodeType.DEFAULT_BPEL_ENTITY_NODE
139: .getIcon();
140:
141: return icon;
142: }
143:
144: // vlv
145: public static String getToolTip(Component component) {
146: String type = getType(component);
147:
148: if (type != null) {
149: return "<html>" + type + " <b>" + getName(component)
150: + "</b></html>";
151: }
152: return getName(component);
153: }
154:
155: // vlv
156: public static String getType(Component component) {
157: String type = null;
158:
159: if (component instanceof BpelEntity) {
160: type = ((BpelEntity) component).getElementType().getName();
161: } else if (component instanceof SchemaComponent) {
162: type = ((SchemaComponent) component).getComponentType()
163: .getName();
164: } else {
165: type = component.getClass().getName();
166: }
167: int k = type.lastIndexOf("."); // NOI18N
168:
169: if (k == -1) {
170: return type;
171: }
172: return type.substring(k + 1);
173: }
174:
175: // vlv
176: private static Node getNode(Component component) {
177: if (component instanceof SchemaComponent) {
178: SchemaComponent schemaComponent = (SchemaComponent) component;
179: CategorizedSchemaNodeFactory factory = new CategorizedSchemaNodeFactory(
180: schemaComponent.getModel(), Lookups
181: .singleton(schemaComponent));
182: return factory.createNode(schemaComponent);
183: }
184: if (component instanceof WSDLComponent) {
185: return NodesFactory.getInstance().create(component);
186: }
187: return null;
188: }
189:
190: private static String removeHtmlHeader(String htmlString) {
191: if (htmlString == null) {
192: return htmlString;
193: }
194:
195: String htmlStart = "<html>"; // NOI18N
196: String htmlEnd = "</html>"; // NOI18N
197:
198: if (htmlString.matches(htmlStart + ".*" + htmlEnd)) {
199: htmlString = htmlString.substring(htmlStart.length() - 1,
200: htmlString.length() - htmlEnd.length() + 1);
201: }
202:
203: return htmlString;
204: }
205:
206: private static NodeType getBpelNodeType(Component component) {
207: if (!(component instanceof BpelEntity)) {
208: return null;
209: }
210:
211: return org.netbeans.modules.bpel.editors.api.utils.Util
212: .getBasicNodeType((BpelEntity) component);
213: }
214:
215: public static String getUsageContextPath(String suffix,
216: BpelEntity entity, Class<? extends BpelEntity> filter) {
217: String resultStr = getUsageContextPath(entity, filter);
218: if (resultStr != null) {
219: suffix = suffix == null ? "" : ENTITY_SEPARATOR + suffix; // NOI18N
220: resultStr += suffix;
221: } else {
222: resultStr = suffix;
223: }
224: return resultStr;
225: }
226:
227: public static String getUsageContextPath(BpelEntity entity,
228: Class<? extends BpelEntity> filter) {
229: assert entity != null;
230: StringBuffer path = new StringBuffer(getName(entity));
231: BpelEntity tmpEntity = entity;
232: while ((tmpEntity = tmpEntity.getParent()) != null) {
233: if (tmpEntity.getElementType() == filter) {
234: continue;
235: }
236:
237: String tmpEntityName = getName(tmpEntity);
238: if (tmpEntityName != null && tmpEntityName.length() > 0) {
239: path.insert(0, ENTITY_SEPARATOR).insert(0,
240: tmpEntityName);
241: }
242: }
243:
244: return path.toString();
245: }
246: }
|