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-2006 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.documentation;
042:
043: import java.awt.GraphicsConfiguration;
044: import java.awt.GraphicsDevice;
045: import java.awt.GraphicsEnvironment;
046: import java.awt.Image;
047: import java.awt.Transparency;
048: import java.awt.image.BufferedImage;
049: import java.io.File;
050: import java.util.Date;
051: import java.util.Iterator;
052: import java.util.List;
053: import java.util.Map;
054: import javax.swing.Icon;
055: import javax.swing.ImageIcon;
056: import javax.swing.JComponent;
057:
058: import javax.xml.namespace.QName;
059: import org.openide.ErrorManager;
060: import org.openide.filesystems.FileUtil;
061:
062: import org.netbeans.modules.xml.xam.Named;
063: import org.netbeans.modules.xml.xam.dom.AbstractDocumentComponent;
064:
065: import org.netbeans.modules.bpel.model.api.BpelEntity;
066: import org.netbeans.modules.bpel.model.api.CorrelationSet;
067: import org.netbeans.modules.bpel.model.api.CorrelationSetContainer;
068: import org.netbeans.modules.bpel.model.api.CorrelationContainer;
069: import org.netbeans.modules.bpel.model.api.Documentation;
070: import org.netbeans.modules.bpel.model.api.ExtensibleElements;
071: import org.netbeans.modules.bpel.model.api.Import;
072: import org.netbeans.modules.bpel.model.api.PartnerLink;
073: import org.netbeans.modules.bpel.model.api.PartnerLinkContainer;
074: import org.netbeans.modules.bpel.model.api.PatternedCorrelationContainer;
075: import org.netbeans.modules.bpel.model.api.Process;
076: import org.netbeans.modules.bpel.model.api.Variable;
077: import org.netbeans.modules.bpel.model.api.VariableContainer;
078:
079: import org.netbeans.modules.reportgenerator.api.Report;
080: import org.netbeans.modules.reportgenerator.api.ReportAttribute;
081: import org.netbeans.modules.reportgenerator.api.ReportBody;
082: import org.netbeans.modules.reportgenerator.spi.ReportCookie;
083: import org.netbeans.modules.reportgenerator.api.ReportElement;
084: import org.netbeans.modules.reportgenerator.api.ReportElementFactory;
085: import org.netbeans.modules.reportgenerator.api.ReportException;
086: import org.netbeans.modules.reportgenerator.api.ReportSection;
087:
088: import org.netbeans.modules.bpel.core.BPELDataObject;
089: import org.netbeans.modules.bpel.core.helper.api.CoreUtil;
090: import org.netbeans.modules.bpel.editors.api.utils.RefactorUtil;
091: import static org.netbeans.modules.soa.ui.util.UI.*;
092:
093: /**
094: * @author Vladimir Yaroslavskiy
095: * @version 2007.11.06
096: */
097: public class DocumentationCookie implements ReportCookie {
098:
099: public DocumentationCookie(BPELDataObject dataObject,
100: JComponent canvas) {
101: myDataObject = dataObject;
102: myCanvas = canvas;
103: }
104:
105: public Report generateReport() {
106: try {
107: myFactory = ReportElementFactory.getDefault();
108:
109: if (myFactory == null) {
110: return null;
111: }
112: return createReport(CoreUtil.getBpelModel(myDataObject)
113: .getProcess(), myFactory.createReport());
114: } catch (ReportException e) {
115: ErrorManager.getDefault().notify(e);
116: }
117: return null;
118: }
119:
120: private Report createReport(Process process, Report report)
121: throws ReportException {
122: if (process == null) {
123: return report;
124: }
125: // description
126: report.setName(i18n("LBL_Report_of", process.getName())); // NOI18N
127: String description = process.getDocumentation();
128:
129: if (description == null) {
130: description = i18n("LBL_Report_for", // NOI18N
131: myDataObject.getPrimaryFile().getNameExt());
132: }
133: report.setDescription(description);
134:
135: // image
136: report.setOverViewImage(createImage());
137:
138: // summary
139: ReportAttribute attribute;
140: File file = FileUtil.toFile(myDataObject.getPrimaryFile());
141:
142: attribute = myFactory.createReportAttribute();
143: attribute.setName(i18n("LBL_Name")); // NOI18N
144: attribute.setValue(myDataObject.getName());
145: report.addAttribute(attribute);
146:
147: attribute = myFactory.createReportAttribute();
148: attribute.setName(i18n("LBL_Location")); // NOI18N
149: attribute.setValue(file.getAbsolutePath());
150: report.addAttribute(attribute);
151:
152: attribute = myFactory.createReportAttribute();
153: attribute.setName(i18n("LBL_Size")); // NOI18N
154: attribute.setValue(i18n("LBL_Bytes", Long.toString(file
155: .length()))); // NOI18N
156: report.addAttribute(attribute);
157:
158: attribute = myFactory.createReportAttribute();
159: attribute.setName(i18n("LBL_Created")); // NOI18N
160: attribute.setValue(new Date(System.currentTimeMillis()));
161: report.addAttribute(attribute);
162:
163: attribute = myFactory.createReportAttribute();
164: attribute.setName(i18n("LBL_Last_Modified")); // NOI18N
165: attribute.setValue(new Date(file.lastModified()));
166: report.addAttribute(attribute);
167:
168: // body
169: ReportBody body = myFactory.createReportBody();
170: report.setBody(body);
171: ReportSection section;
172:
173: // import
174: section = createSection("LBL_Import", "LBL_Section_Import"); // NOI18N
175: fillImport(process, body, section);
176:
177: // partner link
178: section = createSection("LBL_Partner_Link",
179: "LBL_Section_Partner_Link"); // NOI18N
180: fillPartnerLink(process, body, section);
181:
182: // variable
183: section = createSection("LBL_Variable", "LBL_Section_Variable"); // NOI18N
184: fillVariable(process, body, section);
185:
186: // correlation set
187: section = createSection("LBL_Correlation_Set",
188: "LBL_Section_Correlation_Set");//NOI18N
189: fillCorrelationSet(process, body, section);
190:
191: // elements
192: section = createSection("LBL_Element", "LBL_Section_Element"); // NOI18N
193: body.addReportSection(section);
194: travelElement(process, section);
195:
196: return report;
197: }
198:
199: private ReportSection createSection(String name, String description) {
200: ReportSection section = myFactory.createReportSection();
201: section.setName(i18n(name));
202: section.setDescription(i18n(description));
203: return section;
204: }
205:
206: private void fillImport(Process process, ReportBody body,
207: ReportSection section) {
208: Import[] imports = process.getImports();
209:
210: if (imports == null || imports.length == 0) {
211: return;
212: }
213: for (Import imp : imports) {
214: fillElement(imp, section);
215: }
216: body.addReportSection(section);
217: }
218:
219: private void fillPartnerLink(Process process, ReportBody body,
220: ReportSection section) {
221: PartnerLinkContainer container = process
222: .getPartnerLinkContainer();
223:
224: if (container == null) {
225: return;
226: }
227: PartnerLink[] partners = container.getPartnerLinks();
228:
229: if (partners == null || partners.length == 0) {
230: return;
231: }
232: for (PartnerLink partner : partners) {
233: fillElement(partner, section);
234: }
235: body.addReportSection(section);
236: }
237:
238: private void fillVariable(Process process, ReportBody body,
239: ReportSection section) {
240: VariableContainer container = process.getVariableContainer();
241:
242: if (container == null) {
243: return;
244: }
245: Variable[] variables = container.getVariables();
246:
247: if (variables == null || variables.length == 0) {
248: return;
249: }
250: for (Variable variable : variables) {
251: fillElement(variable, section);
252: }
253: body.addReportSection(section);
254: }
255:
256: private void fillCorrelationSet(Process process, ReportBody body,
257: ReportSection section) {
258: CorrelationSetContainer container = process
259: .getCorrelationSetContainer();
260:
261: if (container == null) {
262: return;
263: }
264: CorrelationSet[] correlations = container.getCorrelationSets();
265:
266: if (correlations == null || correlations.length == 0) {
267: return;
268: }
269: for (CorrelationSet correlation : correlations) {
270: fillElement(correlation, section);
271: }
272: body.addReportSection(section);
273: }
274:
275: private void travelElement(Object object, ReportSection section) {
276: if (!(object instanceof BpelEntity)) {
277: return;
278: }
279: BpelEntity entity = (BpelEntity) object;
280:
281: if (skipElement(entity)) {
282: return;
283: }
284: if (!(object instanceof Process)) {
285: fillElement(entity, section);
286: }
287: List children = entity.getChildren();
288:
289: for (Object child : children) {
290: travelElement(child, section);
291: }
292: }
293:
294: private boolean skipElement(BpelEntity entity) {
295: return entity instanceof Import
296: || entity instanceof CorrelationSet
297: || entity instanceof CorrelationSetContainer
298: || entity instanceof CorrelationContainer
299: || entity instanceof PartnerLink
300: || entity instanceof PartnerLinkContainer
301: || entity instanceof Variable
302: || entity instanceof VariableContainer
303: || entity instanceof Documentation
304: || entity instanceof PatternedCorrelationContainer;
305: }
306:
307: private void fillElement(BpelEntity entity, ReportSection section) {
308: ReportElement element = myFactory.createReportElement();
309: Icon icon = RefactorUtil.getIcon(entity);
310:
311: if (icon instanceof ImageIcon) {
312: element.setImage(((ImageIcon) icon).getImage());
313: }
314: element.setName(getDisplayName(entity));
315:
316: if (entity instanceof ExtensibleElements) {
317: String documentation = ((ExtensibleElements) entity)
318: .getDocumentation();
319:
320: if (documentation != null) {
321: element.setDescription(documentation);
322: }
323: }
324: fillAttributes(entity, element);
325: section.addReportElement(element);
326: }
327:
328: private void fillAttributes(BpelEntity entity, ReportElement element) {
329: AbstractDocumentComponent component = (AbstractDocumentComponent) entity;
330: Map<QName, String> map = component.getAttributeMap();
331: Iterator<QName> iterator = map.keySet().iterator();
332:
333: while (iterator.hasNext()) {
334: QName name = iterator.next();
335: String value = map.get(name);
336:
337: if (value == null || value.length() == 0) {
338: continue;
339: }
340: ReportAttribute attribute = myFactory
341: .createReportAttribute();
342: attribute.setName(name.toString());
343: attribute.setValue(value);
344: element.addAttribute(attribute);
345: }
346: }
347:
348: private Image createImage() {
349: GraphicsEnvironment environment = GraphicsEnvironment
350: .getLocalGraphicsEnvironment();
351: GraphicsDevice device = environment.getDefaultScreenDevice();
352: GraphicsConfiguration configuration = device
353: .getDefaultConfiguration();
354:
355: BufferedImage image = configuration.createCompatibleImage(
356: myCanvas.getWidth(), myCanvas.getHeight(),
357: Transparency.BITMASK);
358: myCanvas.print(image.createGraphics());
359:
360: return image;
361: }
362:
363: private String getDisplayName(BpelEntity entity) {
364: String type = RefactorUtil.getType(entity);
365: String name = getName(entity);
366:
367: if (name != null) {
368: type += " '" + ((Named) entity).getName() + "'"; // NOI18N
369: }
370: return type;
371: }
372:
373: private String getName(BpelEntity entity) {
374: if (!(entity instanceof Named)) {
375: return null;
376: }
377: return ((Named) entity).getName();
378: }
379:
380: private String i18n(String key) {
381: return org.netbeans.modules.soa.ui.util.UI.i18n(
382: DocumentationCookie.class, key);
383: }
384:
385: private String i18n(String key, String param) {
386: return org.netbeans.modules.soa.ui.util.UI.i18n(
387: DocumentationCookie.class, key, param);
388: }
389:
390: private JComponent myCanvas;
391: private BPELDataObject myDataObject;
392: private ReportElementFactory myFactory;
393: }
|