Search in sources :

Example 31 with HashPrintRequestAttributeSet

use of javax.print.attribute.HashPrintRequestAttributeSet in project jdk8u_jdk by JetBrains.

the class PageDlgStackOverflowTest method main.

public static void main(String[] args) {
    PrinterJob job = PrinterJob.getPrinterJob();
    if (job == null) {
        return;
    }
    PrintRequestAttributeSet pSet = new HashPrintRequestAttributeSet();
    pSet.add(DialogTypeSelection.NATIVE);
    job.printDialog(pSet);
    try {
        job.pageDialog(pSet);
    } catch (StackOverflowError e) {
        throw new RuntimeException("StackOverflowError is thrown");
    }
}
Also used : HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet) PrinterJob(java.awt.print.PrinterJob) PrintRequestAttributeSet(javax.print.attribute.PrintRequestAttributeSet) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet)

Example 32 with HashPrintRequestAttributeSet

use of javax.print.attribute.HashPrintRequestAttributeSet in project megameklab by MegaMek.

the class UnitPrintManager method printAllUnits.

public static boolean printAllUnits(Vector<Entity> loadedUnits, boolean singlePrint) {
    Book book = new Book();
    List<Infantry> infList = new ArrayList<>();
    List<BattleArmor> baList = new ArrayList<>();
    List<Protomech> protoList = new ArrayList<>();
    List<Entity> unprintable = new ArrayList<>();
    HashPrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
    aset.add(MediaSizeName.NA_LETTER);
    aset.add(new MediaPrintableArea(0, 0, 8.5f, 11, MediaPrintableArea.INCH));
    PrinterJob masterPrintJob = PrinterJob.getPrinterJob();
    if (!masterPrintJob.printDialog(aset)) {
        return true;
    }
    PageFormat pageFormat = new PageFormat();
    pageFormat = masterPrintJob.getPageFormat(null);
    Paper p = pageFormat.getPaper();
    p.setImageableArea(0, 0, p.getWidth(), p.getHeight());
    pageFormat.setPaper(p);
    Tank tank1 = null;
    Tank wige1 = null;
    Tank dualTurret1 = null;
    for (Entity unit : loadedUnits) {
        if (unit instanceof Mech) {
            UnitUtil.removeOneShotAmmo(unit);
            UnitUtil.expandUnitMounts((Mech) unit);
            book.append(new PrintMech((Mech) unit, book.getNumberOfPages()), pageFormat);
        } else if ((unit instanceof LargeSupportTank) || ((unit instanceof Tank) && (unit.getMovementMode() != EntityMovementMode.VTOL) && ((Tank) unit).isSuperHeavy())) {
            book.append(new PrintLargeSupportVehicle((Tank) unit), pageFormat);
        } else if (unit instanceof VTOL) {
            book.append(new PrintVTOL((VTOL) unit), pageFormat);
        } else if (unit.getMovementMode() == EntityMovementMode.WIGE) {
            if (singlePrint) {
                book.append(new PrintVehicle((Tank) unit, null), pageFormat);
            } else if (null != wige1) {
                book.append(new PrintVehicle(wige1, (Tank) unit), pageFormat);
                wige1 = null;
            } else {
                wige1 = (Tank) unit;
            }
        } else if ((unit instanceof Tank) && ((unit.getMovementMode() == EntityMovementMode.NAVAL) || (unit.getMovementMode() == EntityMovementMode.SUBMARINE) || (unit.getMovementMode() == EntityMovementMode.HYDROFOIL))) {
            unprintable.add(unit);
        // book.append(new PrintNavalVehicle((Tank) unit), pageFormat);
        } else if (unit instanceof Tank) {
            if (!((Tank) unit).hasNoDualTurret()) {
                if (singlePrint) {
                    book.append(new PrintDualTurretVehicle((Tank) unit, null), pageFormat);
                } else if (null != dualTurret1) {
                    book.append(new PrintDualTurretVehicle(dualTurret1, (Tank) unit), pageFormat);
                    dualTurret1 = null;
                } else {
                    dualTurret1 = (Tank) unit;
                }
            } else {
                if (singlePrint) {
                    book.append(new PrintVehicle((Tank) unit, null), pageFormat);
                } else if (null != tank1) {
                    book.append(new PrintVehicle(tank1, (Tank) unit), pageFormat);
                    tank1 = null;
                } else {
                    tank1 = (Tank) unit;
                }
            }
        } else if (unit.hasETypeFlag(Entity.ETYPE_AERO) && !unit.hasETypeFlag(Entity.ETYPE_JUMPSHIP)) {
            if (unit instanceof Dropship) {
                if (unit.getMovementMode() == EntityMovementMode.AERODYNE) {
                    book.append(new PrintAerodyne((Dropship) unit), pageFormat);
                } else {
                    book.append(new PrintSpheroid((Dropship) unit), pageFormat);
                }
            } else if (unit instanceof FixedWingSupport) {
                book.append(new PrintFixedWingSupport((FixedWingSupport) unit), pageFormat);
            } else if (unit instanceof ConvFighter) {
                book.append(new PrintConventionalFighter((ConvFighter) unit), pageFormat);
            } else if (unit instanceof SmallCraft) {
                if (unit.getMovementMode() == EntityMovementMode.AERODYNE) {
                    book.append(new PrintSmallCraftAerodyne((SmallCraft) unit), pageFormat);
                } else {
                    book.append(new PrintSmallCraftSpheroid((SmallCraft) unit), pageFormat);
                }
            } else {
                book.append(new PrintAero((Aero) unit), pageFormat);
            }
        } else if (unit instanceof BattleArmor) {
            baList.add((BattleArmor) unit);
            if (singlePrint || baList.size() > 4) {
                book.append(new PrintBattleArmor(baList), pageFormat);
                baList = new ArrayList<>();
            }
        } else if (unit instanceof Infantry) {
            infList.add((Infantry) unit);
            if (singlePrint || infList.size() > 3) {
                book.append(new PrintInfantry(infList), pageFormat);
                infList = new ArrayList<>();
            }
        } else if (unit instanceof Protomech) {
            protoList.add((Protomech) unit);
            if (singlePrint || protoList.size() > 4) {
                book.append(new PrintProtomech(protoList), pageFormat);
                protoList = new ArrayList<>();
            }
        } else {
            // TODO: show a message dialog that lists the unprintable units
            unprintable.add(unit);
        }
    }
    if (unprintable.size() > 0) {
        JOptionPane.showMessageDialog(null, "Printing is not currently supported for the following units:\n" + unprintable.stream().map(en -> en.getChassis() + " " + en.getModel()).collect(Collectors.joining("\n")));
    }
    if (null != wige1) {
        book.append(new PrintVehicle(wige1, null), pageFormat);
    }
    if (null != tank1) {
        book.append(new PrintVehicle(tank1, null), pageFormat);
    }
    if (null != dualTurret1) {
        book.append(new PrintDualTurretVehicle(dualTurret1, null), pageFormat);
    }
    if (baList.size() > 0) {
        book.append(new PrintBattleArmor(baList), pageFormat);
    }
    if (infList.size() > 0) {
        book.append(new PrintInfantry(infList), pageFormat);
    }
    if (protoList.size() > 0) {
        book.append(new PrintProtomech(protoList), pageFormat);
    }
    masterPrintJob.setPageable(book);
    if (loadedUnits.size() > 1) {
        masterPrintJob.setJobName(loadedUnits.get(0).getShortNameRaw() + " etc");
    } else if (loadedUnits.size() > 0) {
        masterPrintJob.setJobName(loadedUnits.get(0).getShortNameRaw());
    }
    PrintTask task = new PrintTask(masterPrintJob, aset);
    task.execute();
    return true;
}
Also used : PrintAero(megameklab.com.ui.Aero.Printing.PrintAero) Dropship(megamek.common.Dropship) Aero(megamek.common.Aero) PrintLargeSupportVehicle(megameklab.com.ui.Vehicle.Printing.PrintLargeSupportVehicle) PrintSmallCraftSpheroid(megameklab.com.ui.Aero.Printing.PrintSmallCraftSpheroid) PrintBattleArmor(megameklab.com.ui.BattleArmor.Printing.PrintBattleArmor) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet) PrinterJob(java.awt.print.PrinterJob) PrintTask(megameklab.com.printing.PrintTask) Vector(java.util.Vector) BattleArmor(megamek.common.BattleArmor) PrintMech(megameklab.com.printing.PrintMech) PrintInfantry(megameklab.com.ui.Infantry.Printing.PrintInfantry) UnitLoadingDialog(megamek.client.ui.swing.UnitLoadingDialog) EntityListFile(megamek.common.EntityListFile) SmallCraft(megamek.common.SmallCraft) PageFormat(java.awt.print.PageFormat) JFileChooser(javax.swing.JFileChooser) JFrame(javax.swing.JFrame) Infantry(megamek.common.Infantry) KeyStroke(javax.swing.KeyStroke) MechFileParser(megamek.common.MechFileParser) Frame(java.awt.Frame) PrintFixedWingSupport(megameklab.com.ui.Aero.Printing.PrintFixedWingSupport) Mech(megamek.common.Mech) VTOL(megamek.common.VTOL) JMenu(javax.swing.JMenu) KeyEvent(java.awt.event.KeyEvent) PrintAerodyne(megameklab.com.ui.Dropship.Printing.PrintAerodyne) Collectors(java.util.stream.Collectors) FixedWingSupport(megamek.common.FixedWingSupport) Book(java.awt.print.Book) Protomech(megamek.common.Protomech) PrintVehicle(megameklab.com.ui.Vehicle.Printing.PrintVehicle) List(java.util.List) LargeSupportTank(megamek.common.LargeSupportTank) Tank(megamek.common.Tank) Entity(megamek.common.Entity) MediaSizeName(javax.print.attribute.standard.MediaSizeName) ActionListener(java.awt.event.ActionListener) FileNameExtensionFilter(javax.swing.filechooser.FileNameExtensionFilter) PrintSpheroid(megameklab.com.ui.Dropship.Printing.PrintSpheroid) Paper(java.awt.print.Paper) PrintDualTurretVehicle(megameklab.com.ui.Vehicle.Printing.PrintDualTurretVehicle) ArrayList(java.util.ArrayList) JMenuItem(javax.swing.JMenuItem) UnitPrintQueueDialog(megameklab.com.ui.dialog.UnitPrintQueueDialog) PrintSmallCraftAerodyne(megameklab.com.ui.Aero.Printing.PrintSmallCraftAerodyne) UnitSelectorDialog(megamek.client.ui.swing.UnitSelectorDialog) PrintConventionalFighter(megameklab.com.ui.Aero.Printing.PrintConventionalFighter) ConvFighter(megamek.common.ConvFighter) PrintVTOL(megameklab.com.ui.Vehicle.Printing.PrintVTOL) EntityMovementMode(megamek.common.EntityMovementMode) JOptionPane(javax.swing.JOptionPane) ActionEvent(java.awt.event.ActionEvent) MediaPrintableArea(javax.print.attribute.standard.MediaPrintableArea) File(java.io.File) PrintProtomech(megameklab.com.ui.ProtoMek.Printing.PrintProtomech) Entity(megamek.common.Entity) PrintSmallCraftSpheroid(megameklab.com.ui.Aero.Printing.PrintSmallCraftSpheroid) PrintAero(megameklab.com.ui.Aero.Printing.PrintAero) PrintFixedWingSupport(megameklab.com.ui.Aero.Printing.PrintFixedWingSupport) PrintDualTurretVehicle(megameklab.com.ui.Vehicle.Printing.PrintDualTurretVehicle) ArrayList(java.util.ArrayList) PrinterJob(java.awt.print.PrinterJob) MediaPrintableArea(javax.print.attribute.standard.MediaPrintableArea) PageFormat(java.awt.print.PageFormat) Book(java.awt.print.Book) PrintProtomech(megameklab.com.ui.ProtoMek.Printing.PrintProtomech) PrintVehicle(megameklab.com.ui.Vehicle.Printing.PrintVehicle) PrintAerodyne(megameklab.com.ui.Dropship.Printing.PrintAerodyne) PrintBattleArmor(megameklab.com.ui.BattleArmor.Printing.PrintBattleArmor) BattleArmor(megamek.common.BattleArmor) PrintFixedWingSupport(megameklab.com.ui.Aero.Printing.PrintFixedWingSupport) FixedWingSupport(megamek.common.FixedWingSupport) PrintTask(megameklab.com.printing.PrintTask) PrintLargeSupportVehicle(megameklab.com.ui.Vehicle.Printing.PrintLargeSupportVehicle) LargeSupportTank(megamek.common.LargeSupportTank) Dropship(megamek.common.Dropship) ConvFighter(megamek.common.ConvFighter) PrintSmallCraftAerodyne(megameklab.com.ui.Aero.Printing.PrintSmallCraftAerodyne) LargeSupportTank(megamek.common.LargeSupportTank) Tank(megamek.common.Tank) SmallCraft(megamek.common.SmallCraft) PrintInfantry(megameklab.com.ui.Infantry.Printing.PrintInfantry) Infantry(megamek.common.Infantry) PrintInfantry(megameklab.com.ui.Infantry.Printing.PrintInfantry) VTOL(megamek.common.VTOL) PrintVTOL(megameklab.com.ui.Vehicle.Printing.PrintVTOL) Protomech(megamek.common.Protomech) PrintProtomech(megameklab.com.ui.ProtoMek.Printing.PrintProtomech) Paper(java.awt.print.Paper) PrintMech(megameklab.com.printing.PrintMech) Mech(megamek.common.Mech) PrintMech(megameklab.com.printing.PrintMech) PrintSpheroid(megameklab.com.ui.Dropship.Printing.PrintSpheroid) PrintBattleArmor(megameklab.com.ui.BattleArmor.Printing.PrintBattleArmor) PrintConventionalFighter(megameklab.com.ui.Aero.Printing.PrintConventionalFighter) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet) PrintVTOL(megameklab.com.ui.Vehicle.Printing.PrintVTOL)

Example 33 with HashPrintRequestAttributeSet

use of javax.print.attribute.HashPrintRequestAttributeSet in project tray by qzind.

the class PrintDirect method print.

@Override
public void print(PrintOutput output, PrintOptions options) throws PrintException {
    PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
    attributes.add(new JobName(options.getRawOptions().getJobName(Constants.RAW_PRINT), Locale.getDefault()));
    for (int i = 0; i < prints.size(); i++) {
        DocPrintJob printJob = output.getPrintService().createPrintJob();
        InputStream stream = null;
        try {
            switch(formats.get(i)) {
                case BASE64:
                    stream = new Base64InputStream(new ByteArrayInputStream(prints.get(i).getBytes("UTF-8")));
                    break;
                case FILE:
                    stream = new DataInputStream(new URL(prints.get(i)).openStream());
                    break;
                case PLAIN:
                default:
                    stream = new ByteArrayInputStream(prints.get(i).getBytes("UTF-8"));
                    break;
            }
            SimpleDoc doc = new SimpleDoc(stream, DocFlavor.INPUT_STREAM.AUTOSENSE, null);
            waitForPrint(printJob, doc, attributes);
        } catch (IOException e) {
            throw new PrintException(e);
        } finally {
            if (stream != null) {
                try {
                    stream.close();
                } catch (Exception ignore) {
                }
            }
        }
    }
}
Also used : DataInputStream(java.io.DataInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) Base64InputStream(org.apache.commons.codec.binary.Base64InputStream) InputStream(java.io.InputStream) JobName(javax.print.attribute.standard.JobName) DocPrintJob(javax.print.DocPrintJob) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) URL(java.net.URL) IOException(java.io.IOException) PrintException(javax.print.PrintException) JSONException(org.codehaus.jettison.json.JSONException) PrintRequestAttributeSet(javax.print.attribute.PrintRequestAttributeSet) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet) PrintException(javax.print.PrintException) SimpleDoc(javax.print.SimpleDoc) ByteArrayInputStream(java.io.ByteArrayInputStream) Base64InputStream(org.apache.commons.codec.binary.Base64InputStream) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet)

Example 34 with HashPrintRequestAttributeSet

use of javax.print.attribute.HashPrintRequestAttributeSet in project tray by qzind.

the class PrintPixel method applyDefaultSettings.

protected PrintRequestAttributeSet applyDefaultSettings(PrintOptions.Pixel pxlOpts, PageFormat page) {
    PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
    // apply general attributes
    if (pxlOpts.getColorType() != null) {
        attributes.add(pxlOpts.getColorType().getChromatic());
    }
    if (pxlOpts.isDuplex()) {
        attributes.add(Sides.DUPLEX);
    }
    if (pxlOpts.getOrientation() != null) {
        attributes.add(pxlOpts.getOrientation().getAsAttribute());
    }
    // TODO - set paper thickness
    // TODO - set printer tray
    // Java prints using inches at 72dpi
    final float DENSITY = (float) pxlOpts.getDensity() * pxlOpts.getUnits().as1Inch();
    final float CONVERT = pxlOpts.getUnits().toInches() * 72f;
    log.trace("DPI: {}\tCNV: {}", DENSITY, CONVERT);
    if (DENSITY > 0) {
        attributes.add(new PrinterResolution((int) DENSITY, (int) DENSITY, ResolutionSyntax.DPI));
    }
    // apply sizing and margins
    Paper paper = page.getPaper();
    float pageX = 0f;
    float pageY = 0f;
    float pageW = (float) page.getWidth() / CONVERT;
    float pageH = (float) page.getHeight() / CONVERT;
    // page size
    if (pxlOpts.getSize() != null && pxlOpts.getSize().getWidth() > 0 && pxlOpts.getSize().getHeight() > 0) {
        pageW = (float) pxlOpts.getSize().getWidth();
        pageH = (float) pxlOpts.getSize().getHeight();
        paper.setSize(pageW * CONVERT, pageH * CONVERT);
    }
    // margins
    if (pxlOpts.getMargins() != null) {
        pageX += pxlOpts.getMargins().left();
        pageY += pxlOpts.getMargins().top();
        pageW -= (pxlOpts.getMargins().right() + pxlOpts.getMargins().left());
        pageH -= (pxlOpts.getMargins().bottom() + pxlOpts.getMargins().top());
    }
    log.trace("Drawable area: {},{}:{},{}", pageX, pageY, pageW, pageH);
    if (pageW > 0 && pageH > 0) {
        attributes.add(new MediaPrintableArea(pageX, pageY, pageW, pageH, pxlOpts.getUnits().getMediaSizeUnits()));
        paper.setImageableArea(pageX * CONVERT, pageY * CONVERT, pageW * CONVERT, pageH * CONVERT);
        page.setPaper(paper);
    } else {
        log.warn("Could not apply custom size, using printer default");
        attributes.add(new MediaPrintableArea(0, 0, (float) page.getWidth() / 72f, (float) page.getHeight() / 72f, PrintOptions.Unit.INCH.getMediaSizeUnits()));
    }
    log.trace("{}", Arrays.toString(attributes.toArray()));
    return attributes;
}
Also used : Paper(java.awt.print.Paper) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet) PrintRequestAttributeSet(javax.print.attribute.PrintRequestAttributeSet) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet)

Example 35 with HashPrintRequestAttributeSet

use of javax.print.attribute.HashPrintRequestAttributeSet in project tray by qzind.

the class PrintRaw method printToPrinter.

/**
 * Constructs a {@code SimpleDoc} with the {@code commands} byte array.
 */
private void printToPrinter(PrintService service, byte[] cmds, PrintOptions.Raw rawOpts) throws PrintException {
    if (service == null) {
        throw new NullPrintServiceException("Service cannot be null");
    }
    if (cmds == null || cmds.length == 0) {
        throw new NullCommandException("No commands found to send to the printer");
    }
    SimpleDoc doc = new SimpleDoc(cmds, DocFlavor.BYTE_ARRAY.AUTOSENSE, null);
    PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
    attributes.add(new JobName(rawOpts.getJobName(Constants.RAW_PRINT), Locale.getDefault()));
    DocPrintJob printJob = service.createPrintJob();
    waitForPrint(printJob, doc, attributes);
}
Also used : JobName(javax.print.attribute.standard.JobName) NullCommandException(qz.exception.NullCommandException) NullPrintServiceException(qz.exception.NullPrintServiceException) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet) PrintRequestAttributeSet(javax.print.attribute.PrintRequestAttributeSet)

Aggregations

HashPrintRequestAttributeSet (javax.print.attribute.HashPrintRequestAttributeSet)40 PrintRequestAttributeSet (javax.print.attribute.PrintRequestAttributeSet)28 PrinterJob (java.awt.print.PrinterJob)16 PrintService (javax.print.PrintService)13 IOException (java.io.IOException)12 JobName (javax.print.attribute.standard.JobName)12 PrinterException (java.awt.print.PrinterException)10 File (java.io.File)9 DocFlavor (javax.print.DocFlavor)9 Copies (javax.print.attribute.standard.Copies)9 DocPrintJob (javax.print.DocPrintJob)8 Attribute (javax.print.attribute.Attribute)7 SimpleDoc (javax.print.SimpleDoc)6 PageFormat (java.awt.print.PageFormat)5 PrintException (javax.print.PrintException)5 PrintRequestAttribute (javax.print.attribute.PrintRequestAttribute)5 PrintServiceAttributeSet (javax.print.attribute.PrintServiceAttributeSet)4 Destination (javax.print.attribute.standard.Destination)4 java.awt.print (java.awt.print)3 Printable (java.awt.print.Printable)3