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: /*
043: * QuerySubstitutionGroupsReader.java
044: *
045: * Created on October 26, 2005, 11:27 AM
046: *
047: * To change this template, choose Tools | Template Manager
048: * and open the template in the editor.
049: */
050:
051: package org.netbeans.modules.xml.schema.refactoring.query.readers;
052:
053: import java.awt.Color;
054: import java.text.MessageFormat;
055: import java.util.ArrayList;
056: import java.util.Collection;
057: import java.util.HashMap;
058: import java.util.List;
059: import java.util.Map;
060: import java.util.Set;
061: import org.netbeans.modules.xml.nbprefuse.AnalysisConstants;
062: import org.netbeans.modules.xml.refactoring.spi.AnalysisUtilities;
063: import org.netbeans.modules.xml.schema.model.GlobalElement;
064: import org.netbeans.modules.xml.schema.model.SchemaModel;
065: import org.netbeans.modules.xml.schema.refactoring.SchemaUIHelper;
066: import org.netbeans.modules.xml.xam.Component;
067: import org.openide.ErrorManager;
068: import org.openide.awt.StatusDisplayer;
069: import org.openide.util.NbBundle;
070: import org.netbeans.modules.xml.xam.dom.NamedComponentReference;
071: import prefuse.data.Edge;
072: import prefuse.data.Graph;
073: import prefuse.data.Node;
074:
075: /**
076: *
077: * @author Jeri Lockhart
078: */
079: public class QuerySubstitutionGroupsReader {
080:
081: private SchemaModel model;
082: // private SchemaComponent query;
083:
084: // private QuerySchemaComponentNode headElementNode;
085: // Map - Key is HeadElement, Value is List of Global elements in Subst Grp
086: private Map<GlobalElement, List<GlobalElement>> sGroups;
087:
088: public static final prefuse.data.Schema FIND_USAGES_NODES_SCHEMA = new prefuse.data.Schema(); // prefuse graph schema
089: static {
090: FIND_USAGES_NODES_SCHEMA.addColumn(AnalysisConstants.LABEL,
091: String.class, AnalysisConstants.EMPTY_STRING);
092: FIND_USAGES_NODES_SCHEMA.addColumn(AnalysisConstants.ID,
093: String.class, AnalysisConstants.EMPTY_STRING);
094: FIND_USAGES_NODES_SCHEMA.addColumn(
095: AnalysisConstants.COMPONENT_TYPE_NAME, String.class,
096: AnalysisConstants.EMPTY_STRING);
097: FIND_USAGES_NODES_SCHEMA.addColumn(
098: AnalysisConstants.ELEMENT_TYPE, String.class,
099: AnalysisConstants.EMPTY_STRING); // type of a GE, LE, GA, or LA
100: FIND_USAGES_NODES_SCHEMA.addColumn(
101: AnalysisConstants.XML_FILENAME, String.class,
102: AnalysisConstants.EMPTY_STRING);
103: FIND_USAGES_NODES_SCHEMA.addColumn(AnalysisConstants.TOOLTIP,
104: String.class, AnalysisConstants.EMPTY_STRING); // name
105: // FIND_USAGES_NODES_SCHEMA.addColumn(AnalysisConstants.SCHEMA_COMPONENT, SchemaComponent.class); // Global Complex Type, etc
106: FIND_USAGES_NODES_SCHEMA.addColumn(
107: AnalysisConstants.XAM_COMPONENT, Component.class); // Global Complex Type, etc
108: FIND_USAGES_NODES_SCHEMA.addColumn(
109: AnalysisConstants.IS_PRIMITIVE, boolean.class, false); // is builtin type
110: FIND_USAGES_NODES_SCHEMA.addColumn(
111: AnalysisConstants.IS_QUERY_NODE, boolean.class, false);
112: FIND_USAGES_NODES_SCHEMA.addColumn(
113: AnalysisConstants.IS_USAGE_NODE, boolean.class, false);
114: FIND_USAGES_NODES_SCHEMA
115: .addColumn(AnalysisConstants.IS_HEAD_ELEMENT,
116: boolean.class, false);
117: FIND_USAGES_NODES_SCHEMA.addColumn(
118: AnalysisConstants.OPENIDE_NODE,
119: org.openide.nodes.Node.class);
120: }
121:
122: public static final prefuse.data.Schema FIND_USAGES_EDGES_SCHEMA = new prefuse.data.Schema(); // prefuse graph schema
123: static {
124: FIND_USAGES_EDGES_SCHEMA.addColumn(AnalysisConstants.LABEL,
125: String.class, AnalysisConstants.EMPTY_STRING);
126: FIND_USAGES_EDGES_SCHEMA.addColumn(AnalysisConstants.EDGE_TYPE,
127: String.class, AnalysisConstants.EMPTY_STRING); // "file-edge-type" "generalization", "reference" "composition"
128: FIND_USAGES_EDGES_SCHEMA.addColumn(AnalysisConstants.TOOLTIP,
129: String.class, AnalysisConstants.EMPTY_STRING);
130: }
131:
132: /**
133: * Creates a new instance of QuerySubstitutionGroupsReader
134: */
135: public QuerySubstitutionGroupsReader(SchemaModel model) {
136: this .model = model;
137: }
138:
139: public Graph loadGraph() {
140: Graph graph = new Graph(true); // isDirected
141: graph.getNodeTable().addColumns(FIND_USAGES_NODES_SCHEMA);
142: graph.getEdgeTable().addColumns(FIND_USAGES_EDGES_SCHEMA);
143: // get global elements
144: // if GE has SubstitutionGroup (SG)
145: // create new group entry in map
146: // add GE to list
147: sGroups = new HashMap<GlobalElement, List<GlobalElement>>();
148: Collection<GlobalElement> ges = model.getSchema().getElements();
149: for (GlobalElement ge : ges) {
150: NamedComponentReference<GlobalElement> headElemRef = ge
151: .getSubstitutionGroup();
152: // TODO add stats to status area about broken refs
153: // ref can be broken
154: // headElemRef.isBroken()
155: // headElemRef.get() == null
156: if (headElemRef != null) {
157: GlobalElement headElem = headElemRef.get();
158: if (headElem != null) {
159: List<GlobalElement> members = sGroups.get(headElem);
160: if (members == null) {
161: members = new ArrayList<GlobalElement>();
162: sGroups.put(headElem, members);
163: }
164: members.add(ge);
165: } else {
166: ErrorManager.getDefault().log(
167: ErrorManager.ERROR,
168: "QuerySubstitutionGroupsReader.loadGraph ignoring GE w/ invalid substGrp ref "
169: + ge.getName());
170: }
171: }
172: }
173: int numGroups = sGroups.size();
174:
175: if (numGroups == 1) {
176: StatusDisplayer.getDefault().setStatusText(
177: NbBundle.getMessage(
178: QuerySubstitutionGroupsReader.class,
179: "LBL_SubstitutionGroup_Found"));
180: } else {
181: StatusDisplayer.getDefault().setStatusText(
182: MessageFormat.format(NbBundle.getMessage(
183: QuerySubstitutionGroupsReader.class,
184: "LBL_SubstitutionGroups_Found"),
185: new Object[] { numGroups, }));
186: }
187: // build graph
188: Set<GlobalElement> headElems = sGroups.keySet();
189: for (GlobalElement head : headElems) {
190: List<GlobalElement> geList = sGroups.get(head);
191: // this shouldn't happen
192: assert geList != null : "Error is finding substitution group for "
193: + head;
194: Node headNode = createHeadElementNode(graph, head);
195: for (GlobalElement ge : geList) {
196: Node elemNode = createNode(graph, ge);
197:
198: // elemNode.setString(AnalysisConstants.TOOLTIP,
199: // MessageFormat.format(
200: // NbBundle.getMessage(QuerySubstitutionGroupsReader.class,
201: // "LBL_Substitutable_Element"), // e.g., "Substituable for xxx"
202: // new Object[] {headNode.getString(AnalysisConstants.LABEL)}));
203:
204: AnalysisUtilities.ToolTipLine topLine = null;
205: topLine = new AnalysisUtilities.ToolTipLine(
206: (MessageFormat
207: .format(
208: NbBundle
209: .getMessage(
210: QuerySubstitutionGroupsReader.class,
211: "LBL_Substitutable_Element"), // e.g., "Substituable for xxx"
212: new Object[] { headNode
213: .getString(AnalysisConstants.LABEL) })),
214: 100,
215: Color.BLACK.getRGB(),
216: AnalysisUtilities.ToolTipLine.HorizontalAlignment.CENTER);
217: String compType = elemNode
218: .getString(AnalysisConstants.COMPONENT_TYPE_NAME);
219: AnalysisUtilities.ToolTipLine typeLine = new AnalysisUtilities.ToolTipLine(
220: compType,
221: 100,
222: Color.BLACK.getRGB(),
223: AnalysisUtilities.ToolTipLine.HorizontalAlignment.CENTER);
224: String toolTip = AnalysisUtilities
225: .createHTMLToolTip(new AnalysisUtilities.ToolTipLine[] {
226: topLine, typeLine });
227: elemNode.setString(AnalysisConstants.TOOLTIP, toolTip);
228:
229: Edge edge = createEdge(graph, elemNode, headNode);
230: }
231: }
232: // GraphUtilities.dumpGraph(graph);
233: return graph;
234: }
235:
236: private Node createHeadElementNode(Graph graph, GlobalElement head) {
237: Node headElementNode = createNode(graph, head);
238: headElementNode.setBoolean(AnalysisConstants.IS_HEAD_ELEMENT,
239: true);
240:
241: // headElementNode.setAttribute(AnalysisConstants.TOOLTIP,
242: // NbBundle.getMessage(QuerySubstitutionGroupsReader.class,
243: // "LBL_Head_Element"));
244:
245: AnalysisUtilities.ToolTipLine topLine = new AnalysisUtilities.ToolTipLine(
246: (NbBundle.getMessage(
247: QuerySubstitutionGroupsReader.class,
248: "LBL_Head_Element")),
249: 100,
250: Color.BLACK.getRGB(),
251: AnalysisUtilities.ToolTipLine.HorizontalAlignment.CENTER);
252:
253: AnalysisUtilities.ToolTipLine typeLine = new AnalysisUtilities.ToolTipLine(
254: NbBundle.getMessage(
255: QuerySubstitutionGroupsReader.class,
256: "LBL_Global_Element"),
257: 100,
258: Color.BLACK.getRGB(),
259: AnalysisUtilities.ToolTipLine.HorizontalAlignment.CENTER);
260:
261: String toolTip = AnalysisUtilities
262: .createHTMLToolTip(new AnalysisUtilities.ToolTipLine[] {
263: topLine, typeLine });
264: headElementNode.setString(AnalysisConstants.TOOLTIP, toolTip);
265:
266: return headElementNode;
267:
268: }
269:
270: private Edge createEdge(Graph graph, Node elemNode, Node headNode) {
271: //
272: // Edge edge = graph.addEdge(queryNode, fileNode);
273: Edge edge = graph.addEdge(elemNode, headNode);
274: edge.setString(AnalysisConstants.EDGE_TYPE,
275: AnalysisConstants.GENERALIZATION);
276: return edge;
277: }
278:
279: private Node createNode(Graph graph, GlobalElement ge) {
280: Node n = graph.addNode();
281:
282: System.out
283: .println("QuerySubstitutionGroupsReader:: createNode");
284: //Check with Name if I can assume the ui helper is SchemaUIHelper???
285: SchemaUIHelper uiHleper = new SchemaUIHelper();
286: org.openide.nodes.Node displayNode = uiHleper
287: .getDisplayNode(ge);
288: n.set(AnalysisConstants.OPENIDE_NODE, displayNode);
289: n.set(AnalysisConstants.XAM_COMPONENT, ge);
290: n.setString(AnalysisConstants.COMPONENT_TYPE_NAME, displayNode
291: .getShortDescription());
292: n.setString(AnalysisConstants.LABEL, displayNode.getName());
293: n.setString(AnalysisConstants.ELEMENT_TYPE, displayNode
294: .getDisplayName());
295:
296: return n;
297: }
298:
299: }
|