📄 exportertopdf.java
字号:
// "http://ganttproject.sf.net"); addAttribute("xslfo-path", myStylesheet.getUrl().getPath(), attrs); startPrefixedElement("report", attrs, handler); addAttribute("xslfo-path", myStylesheet.getUrl().getPath(), attrs); addAttribute("title", i18n("ganttReport"), attrs); addAttribute("name", i18n("project"), attrs); addAttribute("nameValue", getProject().getProjectName(), attrs); addAttribute("organisation", i18n("organization"), attrs); addAttribute("organisationValue", getProject().getOrganization(), attrs); addAttribute("webLink", i18n("webLink"), attrs); addAttribute("webLinkValue", getProject().getWebLink(), attrs); addAttribute("currentDateTimeValue", df.format(new java.util.Date()), attrs); addAttribute("description", i18n("shortDescription"), attrs); startPrefixedElement("project", attrs, handler); textElement("descriptionValue", attrs, getProject().getDescription(), handler); endPrefixedElement("project", handler); writeCharts(state, handler); writeTasks(getProject().getTaskManager(), handler); writeResources(getProject().getHumanResourceManager(), handler); endPrefixedElement("report", handler); // handler.endPrefixMapping("ganttproject"); endElement("xsl:stylesheet", handler); handler.endDocument(); } private void writeCharts(ExportState state, TransformerHandler handler) throws SAXException { AttributesImpl attrs = new AttributesImpl(); addAttribute("title", i18n("ganttChart"), attrs); addAttribute("src", state.ganttChartImageFile.getAbsolutePath(), attrs); startPrefixedElement("ganttchart", attrs, handler); endPrefixedElement("ganttchart", handler); addAttribute("title", i18n("resourcesChart"), attrs); addAttribute("src", state.resourceChartImageFile.getAbsolutePath(), attrs); startPrefixedElement("resourceschart", attrs, handler); endPrefixedElement("resourceschart", handler); } private void writeTasks(TaskManager taskManager, final TransformerHandler handler) throws ExportException, SAXException { AttributesImpl attrs = new AttributesImpl(); addAttribute("xslfo-path", "", attrs); addAttribute("title", i18n("tasksList"), attrs); addAttribute("name", i18n("name"), attrs); addAttribute("begin", i18n("start"), attrs); addAttribute("end", i18n("end"), attrs); addAttribute("milestone", i18n("meetingPoint"), attrs); addAttribute("progress", "%", attrs); addAttribute("assigned-to", "Res.", attrs); addAttribute("notes", i18n("notes"), attrs); startPrefixedElement("tasks", attrs, handler); TaskVisitor visitor = new TaskVisitor() { AttributesImpl myAttrs = new AttributesImpl(); protected String serializeTask(Task t, int depth) throws Exception { startPrefixedElement("task", myAttrs, handler); textElement("name", myAttrs, t.getName(), handler); textElement("begin", myAttrs, t.getStart().toString(), handler); textElement("end", myAttrs, t.getEnd().toString(), handler); textElement("milestone", myAttrs, Boolean.valueOf( t.isMilestone()).toString(), handler); textElement("progress", myAttrs, String.valueOf(t .getCompletionPercentage()), handler); StringBuffer usersS = new StringBuffer(); ResourceAssignment[] assignments = t.getAssignments(); if (assignments.length > 0) { for (int j = 0; j < assignments.length; j++) { usersS.append(assignments[j].getResource().getName() + "\n"); } } else { usersS.append(" "); } textElement("assigned-to", myAttrs, usersS.toString(), handler); textElement("notes", myAttrs, ((t.getNotes() == null || t .getNotes().length() == 0) ? " " : t.getNotes()), handler); if (t.getColor()!=null) { textElement("color", myAttrs, getHexaColor(t.getColor()), handler); } endPrefixedElement("task", handler); return ""; } }; try { visitor.visit(taskManager); } catch (Exception e) { throw new ExportException("Failed to write tasks", e); } endPrefixedElement("tasks", handler); } private void writeResources(ResourceManager resourceManager, TransformerHandler handler) throws SAXException { AttributesImpl attrs = new AttributesImpl(); addAttribute("title", i18n("resourcesList"), attrs); addAttribute("name", i18n("colName"), attrs); addAttribute("role", i18n("colRole"), attrs); addAttribute("mail", i18n("colMail"), attrs); addAttribute("phone", i18n("colPhone"), attrs); startPrefixedElement("resources", attrs, handler); { List resources = resourceManager.getResources(); // String // []function=RoleManager.Access.getInstance().getRoleNames(); for (int i = 0; i < resources.size(); i++) { HumanResource p = (HumanResource) resources.get(i); startPrefixedElement("resource", attrs, handler); textElement("name", attrs, p.getName(), handler); textElement("role", attrs, p.getRole().getName(), handler); textElement("mail", attrs, p.getMail(), handler); textElement("phone", attrs, p.getPhone(), handler); endPrefixedElement("resource", handler); } } endPrefixedElement("resources", handler); } private Options createOptions() throws ExportException { JDKFontLocator locator = new JDKFontLocator(); FontRecord[] fontRecords = locator.getFontRecords(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); StreamResult output = new StreamResult(outputStream); try { TransformerHandler handler = getTransformerFactory() .newTransformerHandler(); handler.setResult(output); // just for nifty debugging :) // handler.getTransformer().setOutputProperty(OutputKeys.INDENT, // "yes"); createConfiguration(handler, fontRecords); } catch (TransformerConfigurationException e) { throw new ExportException("Failed to create FOP options", e); } catch (SAXException e) { throw new ExportException("Failed to create FOP options", e); } Options result; try { result = new Options(new ByteArrayInputStream(outputStream .toByteArray())); } catch (FOPException e) { throw new ExportException("Failed to create FOP options", e); } return result; } private void createConfiguration(TransformerHandler handler, FontRecord[] fontRecords) throws SAXException { AttributesImpl attrs = new AttributesImpl(); handler.startDocument(); handler.startElement("", "configuration", "configuration", attrs); handler.startElement("", "fonts", "fonts", attrs); for (int i = 0; i < fontRecords.length; i++) { FontRecord nextRecord = fontRecords[i]; attrs.clear(); attrs.addAttribute("", "metrics-file", "metrics-file", "CDATA", nextRecord.getMetricsLocation().toString()); attrs.addAttribute("", "kerning", "kerning", "CDATA", "yes"); attrs.addAttribute("", "embed-file", "embed-file", "CDATA", nextRecord.getFontLocation().getPath()); handler.startElement("", "font", "font", attrs); writeTriplets(handler, nextRecord.getFontTriplets()); handler.endElement("", "font", "font"); } handler.endElement("", "fonts", "fonts"); handler.endElement("", "configuration", "configuration"); handler.endDocument(); } private void writeTriplets(TransformerHandler handler, FontTriplet[] fontTriplets) throws SAXException { AttributesImpl attrs = new AttributesImpl(); for (int i = 0; i < fontTriplets.length; i++) { FontTriplet next = fontTriplets[i]; attrs.clear(); attrs.addAttribute("", "name", "name", "CDATA", next.getName()); attrs.addAttribute("", "style", "style", "CDATA", next.isItalic() ? "italic" : "normal"); attrs.addAttribute("", "weight", "weight", "CDATA", next.isBold() ? "bold" : "normal"); handler.startElement("", "font-triplet", "font-triplet", attrs); handler.endElement("", "font-triplet", "font-triplet"); } } private static class ExportState { final File outputFile; public ExportState(File outputFile) { this.outputFile = outputFile; } Driver driver; File ganttChartImageFile; File resourceChartImageFile; } protected void setSelectedStylesheet(Stylesheet stylesheet) { myStylesheet = (PDFStylesheet) stylesheet; } protected String getStylesheetOptionID() { return "impex.pdf.stylesheet"; } protected Stylesheet[] getStylesheets() { StylesheetFactoryImpl factory = new StylesheetFactoryImpl() { protected Stylesheet newStylesheet(URL resolvedUrl, String localizedName) { return new PDFStylesheetImpl(resolvedUrl, localizedName); } }; return (Stylesheet[]) factory.createStylesheets(PDFStylesheet.class); } private class PDFStylesheetImpl extends StylesheetImpl implements PDFStylesheet { PDFStylesheetImpl(URL stylesheetURL, String localizedName) { super(stylesheetURL, localizedName); } } private static String getHexaColor(java.awt.Color color) { StringBuffer out = new StringBuffer(); out.append("#"); if (color.getRed() <= 15) { out.append("0"); } out.append(Integer.toHexString(color.getRed())); if (color.getGreen() <= 15) { out.append("0"); } out.append(Integer.toHexString(color.getGreen())); if (color.getBlue() <= 15) { out.append("0"); } out.append(Integer.toHexString(color.getBlue())); return out.toString(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -