Java tutorial
/* * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ /** * MetaHeadline.java * Copyright (C) 2017 University of Waikato, Hamilton, New Zealand */ package adams.flow.transformer.pdfproclet; import adams.core.base.BaseString; import adams.core.base.BaseText; import adams.core.io.PdfFont; import com.itextpdf.text.Paragraph; import java.awt.Color; import java.io.File; /** <!-- globalinfo-start --> * Adds a simple headline before applying the base proclet. * <br><br> <!-- globalinfo-end --> * <!-- options-start --> * <pre>-logging-level <OFF|SEVERE|WARNING|INFO|CONFIG|FINE|FINER|FINEST> (property: loggingLevel) * The logging level for outputting errors and debugging output. * default: WARNING * </pre> * * <pre>-regexp-filename <adams.core.base.BaseRegExp> (property: regExpFilename) * The regular expression that the filename must match. * default: .* * </pre> * * <pre>-proclet <adams.flow.transformer.pdfproclet.PdfProclet> (property: proclet) * The base PDF proclet to use. * default: adams.flow.transformer.pdfproclet.PlainText * </pre> * * <pre>-page-break-before <boolean> (property: pageBreakBefore) * If true, then a page-break is added before the content of the file is inserted. * default: false * </pre> * * <pre>-page-break-after <boolean> (property: pageBreakAfter) * If true, then a page-break is added after the content of the file is inserted. * default: false * </pre> * * <pre>-num-files <int> (property: numFilesPerPage) * The number of files to put on a page before adding an automatic page break; * use -1 for unlimited. * default: -1 * minimum: -1 * </pre> * * <pre>-headline <adams.core.base.BaseText> (property: headline) * The headline to add, can be multi-line. * default: Fill in headline * </pre> * * <pre>-font-headline <adams.core.io.PdfFont> (property: fontHeadline) * The font to use for the headline. * default: Helvetica-Bold-14 * </pre> * * <pre>-color-headline <java.awt.Color> (property: colorHeadline) * The color to use for the headline. * default: #000000 * </pre> * * <pre>-extension <adams.core.base.BaseString> [-extension ...] (property: extensions) * The file extension(s) that the processor will be used for. * default: * * </pre> * * <pre>-first-page-only <boolean> (property: firstPageOnly) * If set to true, then the headline is only added to the first page. * default: false * </pre> * <!-- options-end --> * * @author fracpete (fracpete at waikato dot ac dot nz) * @version $Revision$ */ public class MetaHeadline extends AbstractMetaPdfProclet implements PdfProcletWithPageBreaks, PdfProcletWithVariableFileExtension { /** for serialization. */ private static final long serialVersionUID = 3962046484864891107L; /** whether to add a page-break before adding the file. */ protected boolean m_PageBreakBefore; /** whether to add a page-break after adding the file. */ protected boolean m_PageBreakAfter; /** the number of files per page. */ protected int m_NumFilesPerPage; /** the headline to add. */ protected BaseText m_Headline; /** the font for the headline. */ protected PdfFont m_FontHeadline; /** the color for the headline. */ protected Color m_ColorHeadline; /** the file extensions. */ protected BaseString[] m_Extensions; /** whether to add it only on the first page. */ protected boolean m_FirstPageOnly; /** * Returns a short description of the writer. * * @return a description of the writer */ public String globalInfo() { return "Adds a simple headline before applying the base proclet."; } /** * Adds options to the internal list of options. */ public void defineOptions() { super.defineOptions(); m_OptionManager.add("page-break-before", "pageBreakBefore", false); m_OptionManager.add("page-break-after", "pageBreakAfter", false); m_OptionManager.add("num-files", "numFilesPerPage", -1, -1, null); m_OptionManager.add("headline", "headline", new BaseText("Fill in headline")); m_OptionManager.add("font-headline", "fontHeadline", new PdfFont(PdfFont.HELVETICA, PdfFont.BOLD, 14.0f)); m_OptionManager.add("color-headline", "colorHeadline", Color.BLACK); m_OptionManager.add("extension", "extensions", new BaseString[] { new BaseString(MATCH_ALL_EXTENSION) }); m_OptionManager.add("first-page-only", "firstPageOnly", false); } /** * Whether to add a page break before the file is inserted. * * @param value if true then a page-break is added before the file * is inserted */ public void setPageBreakBefore(boolean value) { m_PageBreakBefore = value; reset(); } /** * Returns whether a page break is added before the file is inserted. * * @return true if a page break is added before the file is * inserted */ public boolean getPageBreakBefore() { return m_PageBreakBefore; } /** * Returns the tip text for this property. * * @return tip text for this property suitable for * displaying in the GUI or for listing the options. */ public String pageBreakBeforeTipText() { return "If true, then a page-break is added before the content of the file is inserted."; } /** * Whether to add a page break after the file is inserted. * * @param value if true then a page-break is added after the file * is inserted */ public void setPageBreakAfter(boolean value) { m_PageBreakAfter = value; reset(); } /** * Returns whether a page break is added after the file is inserted. * * @return true if a page break is added after the file is * inserted */ public boolean getPageBreakAfter() { return m_PageBreakAfter; } /** * Returns the tip text for this property. * * @return tip text for this property suitable for * displaying in the GUI or for listing the options. */ public String pageBreakAfterTipText() { return "If true, then a page-break is added after the content of the file is inserted."; } /** * Sets the number of files per page. * * @param value the number of files */ public void setNumFilesPerPage(int value) { if (getOptionManager().isValid("numFilesPerPage", value)) { m_NumFilesPerPage = value; reset(); } } /** * Returns the number of files to put on a single page. * * @return the number of files */ public int getNumFilesPerPage() { return m_NumFilesPerPage; } /** * Returns the tip text for this property. * * @return tip text for this property suitable for * displaying in the GUI or for listing the options. */ public String numFilesPerPageTipText() { return "The number of files to put on a page before adding an automatic page break; use -1 for unlimited."; } /** * Sets the headline to add. * * @param value the headline */ public void setHeadline(BaseText value) { m_Headline = value; reset(); } /** * Returns the headline to add. * * @return the headline */ public BaseText getHeadline() { return m_Headline; } /** * Returns the tip text for this property. * * @return tip text for this property suitable for * displaying in the GUI or for listing the options. */ public String headlineTipText() { return "The headline to add, can be multi-line."; } /** * Sets the font to use for the headline. * * @param value the font */ public void setFontHeadline(PdfFont value) { m_FontHeadline = value; reset(); } /** * Returns the font to use for the headline. * * @return the font */ public PdfFont getFontHeadline() { return m_FontHeadline; } /** * Returns the tip text for this property. * * @return tip text for this property suitable for * displaying in the GUI or for listing the options. */ public String fontHeadlineTipText() { return "The font to use for the headline."; } /** * Sets the color to use for the headline. * * @param value the color */ public void setColorHeadline(Color value) { m_ColorHeadline = value; reset(); } /** * Returns the color to use for the headline. * * @return the color */ public Color getColorHeadline() { return m_ColorHeadline; } /** * Returns the tip text for this property. * * @return tip text for this property suitable for * displaying in the GUI or for listing the options. */ public String colorHeadlineTipText() { return "The color to use for the headline."; } /** * Returns the extensions that the processor can process. * * @return the extensions (no dot) */ public BaseString[] getExtensions() { return m_Extensions; } /** * Sets the extensions that the processor can process. * * @param value the extensions (no dot) */ public void setExtensions(BaseString[] value) { m_Extensions = value; reset(); } /** * Returns the tip text for this property. * * @return tip text for this property suitable for * displaying in the GUI or for listing the options. */ public String extensionsTipText() { return "The file extension(s) that the processor will be used for."; } /** * Sets the whether to add the headline only on the first page. * * @param value if true then the headline is only added on the first page */ public void setFirstPageOnly(boolean value) { m_FirstPageOnly = value; reset(); } /** * Returns the whether to add the headline only on the first page. * * @return true if the headline is only on the first page */ public boolean getFirstPageOnly() { return m_FirstPageOnly; } /** * Returns the tip text for this property. * * @return tip text for this property suitable for * displaying in the GUI or for listing the options. */ public String firstPageOnlyTipText() { return "If set to true, then the headline is only added to the first page."; } /** * Whether the processor can handle this particular file. * * @param generator the context * @param file the file to check * @return true if the file can be handled */ public boolean canProcess(PDFGenerator generator, File file) { if (m_FirstPageOnly) { if (generator.getState().numTotalFiles() == 0) return super.canProcess(generator, file); else return false; } else { return super.canProcess(generator, file); } } /** * For pre-processing the document. * * @param generator the context * @param file the file to add * @return true if successfully added * @throws Exception if something goes wrong */ protected boolean preProcess(PDFGenerator generator, File file) throws Exception { boolean result; result = super.preProcess(generator, file); if (result) { if (m_PageBreakBefore) result = generator.newPage(); } return result; } /** * The actual processing of the document. * * @param generator the context * @param file the file to add * @return true if successfully added * @throws Exception if something goes wrong */ protected boolean doProcess(PDFGenerator generator, File file) throws Exception { boolean result; result = addElement(generator, new Paragraph(m_Headline.getValue(), m_FontHeadline.toFont(m_ColorHeadline))); if (result) result = m_Proclet.process(generator, file); return result; } /** * For post-processing the document. * * @param generator the context * @param file the file to add * @return true if successfully added * @throws Exception if something goes wrong */ protected boolean postProcess(PDFGenerator generator, File file) throws Exception { boolean result; result = super.postProcess(generator, file); if (result) { if (m_PageBreakAfter || (generator.getState().numCurrentFiles() == m_NumFilesPerPage)) { result = generator.getDocument().newPage(); generator.getState().resetCurrentFiles(); } } return result; } /** * Whether the processor can handle this particular object. * * @param generator the context * @param obj the object to check * @return true if the object can be handled */ public boolean canProcess(PDFGenerator generator, Object obj) { return true; } /** * For pre-processing the document. * * @param generator the context * @param obj the object to add * @return true if successfully added * @throws Exception if something goes wrong */ protected boolean preProcess(PDFGenerator generator, Object obj) throws Exception { boolean result; result = super.preProcess(generator, obj); if (result) { if (m_PageBreakBefore) result = generator.newPage(); } return result; } /** * The actual processing of the document. * * @param generator the context * @param obj the object to add * @return true if successfully added * @throws Exception if something goes wrong */ protected boolean doProcess(PDFGenerator generator, Object obj) throws Exception { boolean result; result = addElement(generator, new Paragraph(m_Headline.getValue(), m_FontHeadline.toFont(m_ColorHeadline))); if (result && m_Proclet.canProcess(generator, obj)) result = m_Proclet.process(generator, obj); return result; } /** * For post-processing the document. * * @param generator the context * @param obj the object to add * @return true if successfully added * @throws Exception if something goes wrong */ protected boolean postProcess(PDFGenerator generator, Object obj) throws Exception { boolean result; result = super.postProcess(generator, obj); if (result) { if (m_PageBreakAfter || (generator.getState().numCurrentFiles() == m_NumFilesPerPage)) { result = generator.getDocument().newPage(); generator.getState().resetCurrentFiles(); } } return result; } }