Beispiel #1
0
  public static void exporterPapier(JasperPrint jPrint, Integer nbCopies) {

    // choix de l'imprimante par défaut
    PrintService service = PrintServiceLookup.lookupDefaultPrintService();

    // paramètres d'impression
    PrintRequestAttributeSet printRequestAttributeSetImpression =
        new HashPrintRequestAttributeSet();
    printRequestAttributeSetImpression.add(MediaSizeName.ISO_A4);
    printRequestAttributeSetImpression.add(new Copies(nbCopies));

    // paramètres de l'imprimante
    PrintServiceAttributeSet printServiceAttributeSetImpression =
        new HashPrintServiceAttributeSet();
    printServiceAttributeSetImpression.add(new PrinterName(service.getName(), null));

    // création de l'interface d'impression
    JRPrintServiceExporter imprimanteFacture = new JRPrintServiceExporter();

    // initialisation de l'interface d'impression
    imprimanteFacture.setParameter(JRExporterParameter.JASPER_PRINT, jPrint);
    imprimanteFacture.setParameter(
        JRPrintServiceExporterParameter.PRINT_REQUEST_ATTRIBUTE_SET,
        printRequestAttributeSetImpression);
    imprimanteFacture.setParameter(
        JRPrintServiceExporterParameter.PRINT_SERVICE_ATTRIBUTE_SET,
        printServiceAttributeSetImpression);

    try {
      imprimanteFacture.exportReport();
    } catch (Exception e) {
      throw new RuntimeException("Impossible de générer la Facture", e);
      //  COMMANDE ENREGISTREE MAIS NON IMPRIMEE
    }
  }
  public PrintService[] getPrintServices(DocFlavor flavor, AttributeSet attributes) {

    SecurityManager security = System.getSecurityManager();
    if (security != null) {
      security.checkPrintJobAccess();
    }
    PrintRequestAttributeSet requestSet = null;
    PrintServiceAttributeSet serviceSet = null;

    if (attributes != null && !attributes.isEmpty()) {

      requestSet = new HashPrintRequestAttributeSet();
      serviceSet = new HashPrintServiceAttributeSet();

      Attribute[] attrs = attributes.toArray();
      for (int i = 0; i < attrs.length; i++) {
        if (attrs[i] instanceof PrintRequestAttribute) {
          requestSet.add(attrs[i]);
        } else if (attrs[i] instanceof PrintServiceAttribute) {
          serviceSet.add(attrs[i]);
        }
      }
    }

    /*
     * Special case: If client is asking for a particular printer
     * (by name) then we can save time by getting just that service
     * to check against the rest of the specified attributes.
     */
    PrintService[] services = null;
    if (serviceSet != null && serviceSet.get(PrinterName.class) != null) {
      PrinterName name = (PrinterName) serviceSet.get(PrinterName.class);
      PrintService service = getPrintServiceByName(name.getValue());
      if (service == null || !matchingService(service, serviceSet)) {
        services = new PrintService[0];
      } else {
        services = new PrintService[1];
        services[0] = service;
      }
    } else {
      services = getPrintServices();
    }

    if (services.length == 0) {
      return services;
    } else {
      ArrayList matchingServices = new ArrayList();
      for (int i = 0; i < services.length; i++) {
        try {
          if (services[i].getUnsupportedAttributes(flavor, requestSet) == null) {
            matchingServices.add(services[i]);
          }
        } catch (IllegalArgumentException e) {
        }
      }
      services = new PrintService[matchingServices.size()];
      return (PrintService[]) matchingServices.toArray(services);
    }
  }
Beispiel #3
0
 public void attributeUpdate(PrintServiceAttributeEvent e) {
   System.out.println("In attributeUpdate method");
   System.out.println("PS IS  " + e.getPrintService());
   PrintServiceAttributeSet psas = e.getAttributes();
   Attribute[] nattr = psas.toArray();
   System.out.println("New Attributes set:" + nattr.length);
   for (int i = 0; i < nattr.length; i++) {
     System.out.println("Printer is " + nattr[i]);
   }
 }
Beispiel #4
0
  /* A heuristic is used to calculate sleep time.
   * 10 times the time taken to loop through all the listeners, with
   * a minimum of 15 seconds. Ensures this won't take more than 10%
   * of available time.
   */
  public void run() {

    long minSleepTime = 15000;
    long sleepTime = 2000;
    HashPrintServiceAttributeSet attrs;
    PrintServiceAttributeEvent attrEvent;
    PrintServiceAttributeListener listener;
    PrintServiceAttributeSet psa;

    while (!stop) {
      try {
        Thread.sleep(sleepTime);
      } catch (InterruptedException e) {
      }
      synchronized (this) {
        if (listeners == null) {
          continue;
        }
        long startTime = System.currentTimeMillis();
        if (listeners != null) {
          if (service instanceof AttributeUpdater) {
            psa = ((AttributeUpdater) service).getUpdatedAttributes();
          } else {
            psa = service.getAttributes();
          }
          if (psa != null && !psa.isEmpty()) {
            for (int i = 0; i < listeners.size(); i++) {
              listener = (PrintServiceAttributeListener) listeners.elementAt(i);
              attrs = new HashPrintServiceAttributeSet(psa);
              attrEvent = new PrintServiceAttributeEvent(service, attrs);
              listener.attributeUpdate(attrEvent);
            }
          }
        }
        sleepTime = (System.currentTimeMillis() - startTime) * 10;
        if (sleepTime < minSleepTime) {
          sleepTime = minSleepTime;
        }
      }
    }
  }
 boolean matchingService(PrintService service, PrintServiceAttributeSet serviceSet) {
   if (serviceSet != null) {
     Attribute[] attrs = serviceSet.toArray();
     Attribute serviceAttr;
     for (int i = 0; i < attrs.length; i++) {
       serviceAttr = service.getAttribute(attrs[i].getCategory());
       if (serviceAttr == null || !serviceAttr.equals(attrs[i])) {
         return false;
       }
     }
   }
   return true;
 }