Author: dcosse Date: 2014-06-27 18:43:13 +0200 (Fri, 27 Jun 2014) New Revision: 283 Url: http://forge.codelutin.com/projects/faxtomail/repository/revisions/283 Log: Ajout de la possibilit?\195?\169 d'imprimer depuis la liste Added: trunk/faxtomail-ui-swing/src/main/java/com/franciaflex/faxtomail/ui/swing/content/print/ComputeDemandeContent.java Modified: trunk/faxtomail-ui-swing/src/main/java/com/franciaflex/faxtomail/ui/swing/content/demande/DemandeListUIHandler.java trunk/faxtomail-ui-swing/src/main/java/com/franciaflex/faxtomail/ui/swing/content/print/AttachmentToPrintChooserUIHandler.java Modified: trunk/faxtomail-ui-swing/src/main/java/com/franciaflex/faxtomail/ui/swing/content/demande/DemandeListUIHandler.java =================================================================== --- trunk/faxtomail-ui-swing/src/main/java/com/franciaflex/faxtomail/ui/swing/content/demande/DemandeListUIHandler.java 2014-06-27 16:29:09 UTC (rev 282) +++ trunk/faxtomail-ui-swing/src/main/java/com/franciaflex/faxtomail/ui/swing/content/demande/DemandeListUIHandler.java 2014-06-27 16:43:13 UTC (rev 283) @@ -28,6 +28,7 @@ import com.ezware.oxbow.swingbits.table.filter.TableRowFilterSupport; import com.franciaflex.faxtomail.persistence.entities.Attachment; import com.franciaflex.faxtomail.persistence.entities.AttachmentFile; +import com.franciaflex.faxtomail.persistence.entities.AttachmentFileImpl; import com.franciaflex.faxtomail.persistence.entities.DemandStatus; import com.franciaflex.faxtomail.persistence.entities.Email; import com.franciaflex.faxtomail.persistence.entities.FaxToMailUser; @@ -40,6 +41,7 @@ import com.franciaflex.faxtomail.ui.swing.actions.ComputeQuantitiesByRangeAction; import com.franciaflex.faxtomail.ui.swing.actions.LoadFolderEmailsAction; import com.franciaflex.faxtomail.ui.swing.actions.SaveDemandeFromListAction; +import com.franciaflex.faxtomail.ui.swing.content.print.ComputeDemandeContent; import com.franciaflex.faxtomail.ui.swing.content.reply.ReplyFormUI; import com.franciaflex.faxtomail.ui.swing.content.reply.ReplyFormUIModel; import com.franciaflex.faxtomail.ui.swing.util.AbstractFaxToMailDemandListHandler; @@ -47,7 +49,6 @@ import com.franciaflex.faxtomail.ui.swing.util.FaxToMailUIUtil; import com.franciaflex.faxtomail.ui.swing.util.FolderTreeNode; import jaxx.runtime.validator.swing.SwingValidator; -import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.jdesktop.swingx.JXTable; @@ -69,6 +70,8 @@ import java.awt.event.MouseEvent; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; +import java.text.DateFormat; +import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Collection; import java.util.Date; @@ -89,6 +92,10 @@ /** Logger. */ private static final Log log = LogFactory.getLog(DemandeListUIHandler.class); + protected Map<String, String> pdfFieldValues; + + protected DateFormat dayFormat = new SimpleDateFormat("dd/MM/yyyy"); + public final PropertyChangeListener selectedDemandeChangeListener = new PropertyChangeListener() { @Override public void propertyChange(PropertyChangeEvent evt) { @@ -469,6 +476,14 @@ if (take) { demandeUIModel.setTakenBy(currentUser); } + + final AttachmentFile demandFile = new AttachmentFileImpl(); + demandFile.setFilename("demand_details"); + ComputeDemandeContent generateDemandeContent = new ComputeDemandeContent(); + byte[] demandContent = generateDemandeContent.getDocumentContent(demandeUIModel); + demandFile.setContent(demandContent); + FaxToMailUIUtil.print(demandFile, true); + for (Attachment attachment : demandeUIModel.getAttachment()) { // force lasy loading Modified: trunk/faxtomail-ui-swing/src/main/java/com/franciaflex/faxtomail/ui/swing/content/print/AttachmentToPrintChooserUIHandler.java =================================================================== --- trunk/faxtomail-ui-swing/src/main/java/com/franciaflex/faxtomail/ui/swing/content/print/AttachmentToPrintChooserUIHandler.java 2014-06-27 16:29:09 UTC (rev 282) +++ trunk/faxtomail-ui-swing/src/main/java/com/franciaflex/faxtomail/ui/swing/content/print/AttachmentToPrintChooserUIHandler.java 2014-06-27 16:43:13 UTC (rev 283) @@ -31,29 +31,17 @@ import com.franciaflex.faxtomail.ui.swing.util.AbstractFaxToMailUIHandler; import com.franciaflex.faxtomail.ui.swing.util.Cancelable; import com.franciaflex.faxtomail.ui.swing.util.FaxToMailUIUtil; -import com.google.common.base.Joiner; import com.google.common.collect.Lists; -import com.google.common.collect.Maps; -import com.google.common.io.Closeables; import jaxx.runtime.JAXXUtil; import jaxx.runtime.validator.swing.SwingValidator; import org.apache.commons.lang3.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.apache.pdfbox.examples.fdf.PrintFields; -import org.apache.pdfbox.examples.fdf.SetField; -import org.apache.pdfbox.pdmodel.PDDocument; import javax.swing.*; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.text.DateFormat; -import java.text.SimpleDateFormat; import java.util.List; -import java.util.Map; import static org.nuiton.i18n.I18n.t; @@ -66,9 +54,7 @@ private static final Log log = LogFactory.getLog(AttachmentToPrintChooserUIHandler.class); - protected Map<String, String> pdfFieldValues; - protected DateFormat dayFormat = new SimpleDateFormat("dd/MM/yyyy"); @Override @@ -84,7 +70,8 @@ // add demand details final AttachmentFile demandFile = new AttachmentFileImpl(); demandFile.setFilename("demand_details"); - byte[] demandContent = getDocumentContent(); + ComputeDemandeContent generateDemandeContent = new ComputeDemandeContent(); + byte[] demandContent = generateDemandeContent.getDocumentContent(demand); demandFile.setContent(demandContent); getModel().addAttachmentToPrint(demandFile); @@ -188,85 +175,5 @@ } } - public byte[] getDocumentContent() { - pdfFieldValues = Maps.newHashMap(); - List<InputStream> streams = Lists.newArrayList(); - PDDocument resultDocument; - byte[] result = null; - - try { - - InputStream emptyPage = getClass().getResourceAsStream("/pdf/demande.pdf"); - streams.add(emptyPage); - resultDocument = PDDocument.load(emptyPage); - SetField fields = new SetField(); - if (log.isTraceEnabled()) { - log.trace("fields in document are:"); - PrintFields printFields = new PrintFields(); - printFields.printFields(resultDocument); - } - - loadDocumentDedails(); - - - if (log.isDebugEnabled()) { - log.debug("will fill form document:\n" + pdfFieldValues); - } - - for (Map.Entry<String, String> field : pdfFieldValues.entrySet()) { - String fieldName = field.getKey(); - String fieldValue = field.getValue(); - fields.setField(resultDocument, fieldName, fieldValue); - } - - - ByteArrayOutputStream out = new ByteArrayOutputStream(); - resultDocument.save(out); - resultDocument.close(); - - result = out.toByteArray(); - - - } catch (Exception e) { - if (log.isErrorEnabled()) { - log.error("", e); - } - } finally { - for (InputStream is : streams) { - try { - Closeables.close(is, false); - } catch (IOException e) { - if (log.isErrorEnabled()) { - log.error("An exception occurred", e); - } - } - } - } - return result; - } - - protected void loadDocumentDedails() { - DemandeUIModel demand = getModel().getDemand(); - setField("demand", demand.getObject()); - setField("receivedDate", dayFormat.format(demand.getReceptionDate())); - setField("sender", demand.getSender()); - setField("object", demand.getObject()); - setField("clientCode", demand.getClientCode()); - setField("demandType", (demand.getDemandType() == null) ? "" : demand.getDemandType().getLabel()); - setField("priority", (demand.getPriority() == null) ? "" : demand.getPriority().getLabel()); - setField("projectReference", demand.getProjectReference()); - setField("companyReference", demand.getCompanyReference()); - setField("etatAttente", (demand.getEtatAttente() == null) ? "" : demand.getEtatAttente().getLabel()); - setField("status", (demand.getDemandStatus() == null) ? "" : demand.getDemandStatus().getLabel()); - setField("takenBy", (demand.getTakenBy() == null) ? "" : (demand.getTakenBy().getLogin() + " " + demand.getTakenBy().getFirstName() + " " + demand.getTakenBy().getLastName())); - } - - protected void setField(String fieldName, String... value) { - if (value == null) { - pdfFieldValues.put(fieldName, ""); - } else { - pdfFieldValues.put(fieldName, Joiner.on(" ").skipNulls().join(value)); - } - } } Added: trunk/faxtomail-ui-swing/src/main/java/com/franciaflex/faxtomail/ui/swing/content/print/ComputeDemandeContent.java =================================================================== --- trunk/faxtomail-ui-swing/src/main/java/com/franciaflex/faxtomail/ui/swing/content/print/ComputeDemandeContent.java (rev 0) +++ trunk/faxtomail-ui-swing/src/main/java/com/franciaflex/faxtomail/ui/swing/content/print/ComputeDemandeContent.java 2014-06-27 16:43:13 UTC (rev 283) @@ -0,0 +1,112 @@ +package com.franciaflex.faxtomail.ui.swing.content.print; + +import com.franciaflex.faxtomail.ui.swing.content.demande.DemandeUIModel; +import com.google.common.base.Joiner; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import com.google.common.io.Closeables; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.pdfbox.examples.fdf.PrintFields; +import org.apache.pdfbox.examples.fdf.SetField; +import org.apache.pdfbox.pdmodel.PDDocument; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.List; +import java.util.Map; + +/** + * Created by davidcosse on 27/06/14. + */ +public class ComputeDemandeContent { + + private static final Log log = LogFactory.getLog(ComputeDemandeContent.class); + protected Map<String, String> pdfFieldValues; + protected DateFormat dayFormat = new SimpleDateFormat("dd/MM/yyyy"); + + + public byte[] getDocumentContent(DemandeUIModel demand) { + pdfFieldValues = Maps.newHashMap(); + List<InputStream> streams = Lists.newArrayList(); + PDDocument resultDocument; + + byte[] result = null; + + try { + + InputStream emptyPage = getClass().getResourceAsStream("/pdf/demande.pdf"); + streams.add(emptyPage); + resultDocument = PDDocument.load(emptyPage); + SetField fields = new SetField(); + if (log.isTraceEnabled()) { + log.trace("fields in document are:"); + PrintFields printFields = new PrintFields(); + printFields.printFields(resultDocument); + } + + loadDocumentDedails(demand); + + + if (log.isDebugEnabled()) { + log.debug("will fill form document:\n" + pdfFieldValues); + } + + for (Map.Entry<String, String> field : pdfFieldValues.entrySet()) { + String fieldName = field.getKey(); + String fieldValue = field.getValue(); + fields.setField(resultDocument, fieldName, fieldValue); + } + + + ByteArrayOutputStream out = new ByteArrayOutputStream(); + resultDocument.save(out); + resultDocument.close(); + + result = out.toByteArray(); + + + } catch (Exception e) { + if (log.isErrorEnabled()) { + log.error("", e); + } + } finally { + for (InputStream is : streams) { + try { + Closeables.close(is, false); + } catch (IOException e) { + if (log.isErrorEnabled()) { + log.error("An exception occurred", e); + } + } + } + } + return result; + } + + protected void loadDocumentDedails(DemandeUIModel demand) { + setField("demand", demand.getObject()); + setField("receivedDate", dayFormat.format(demand.getReceptionDate())); + setField("sender", demand.getSender()); + setField("object", demand.getObject()); + setField("clientCode", demand.getClientCode()); + setField("demandType", (demand.getDemandType() == null) ? "" : demand.getDemandType().getLabel()); + setField("priority", (demand.getPriority() == null) ? "" : demand.getPriority().getLabel()); + setField("projectReference", demand.getProjectReference()); + setField("companyReference", demand.getCompanyReference()); + setField("etatAttente", (demand.getEtatAttente() == null) ? "" : demand.getEtatAttente().getLabel()); + setField("status", (demand.getDemandStatus() == null) ? "" : demand.getDemandStatus().getLabel()); + setField("takenBy", (demand.getTakenBy() == null) ? "" : (demand.getTakenBy().getLogin() + " " + demand.getTakenBy().getFirstName() + " " + demand.getTakenBy().getLastName())); + } + + protected void setField(String fieldName, String... value) { + if (value == null) { + pdfFieldValues.put(fieldName, ""); + } else { + pdfFieldValues.put(fieldName, Joiner.on(" ").skipNulls().join(value)); + } + } +}