Skip to content
Snippets Groups Projects
Commit a6b54807 authored by Andrey Ruzhanskiy's avatar Andrey Ruzhanskiy
Browse files

Checked out printer classes

parent c7f6e264
No related branches found
No related tags found
1 merge request!41Feat/print byte dump 56
package de.tudresden.inf.mci.brailleplot.printerbackend; package de.tudresden.inf.mci.brailleplot.printerbackend;
import de.tudresden.inf.mci.brailleplot.brailleparser.AbstractBrailleTableParser; import de.tudresden.inf.mci.brailleplot.brailleparser.AbstractBrailleTableParser;
import de.tudresden.inf.mci.brailleplot.printabledata.MatrixData; import de.tudresden.inf.mci.brailleplot.printabledata.PrintableData;
/** /**
* This class provides an extension point for further implementation * This class provides an extension point for further implementation
* and protocol building for documents that need to be send to the printer. * and protocol building for documents that need to be send to the printer.
* The common Interface is the getDocument() and assemble() method. * The common Interface is the getDocument() and assemble() method.
* Its usable for all braille printers. * Its usable for all braille printers.
* @param <T> Type of MatrixData. * @param <T> Type of Data.
* @author Andrey Ruzhanskiy * @author Andrey Ruzhanskiy
* @version 28.05.2019 * @version 28.05.2019
*/ */
abstract class AbstractDocumentBuilder<T> { abstract class AbstractDocumentBuilder<T extends PrintableData> {
MatrixData<T> mData; T mData;
AbstractBrailleTableParser mParser; AbstractBrailleTableParser mParser;
...@@ -24,6 +25,6 @@ abstract class AbstractDocumentBuilder<T> { ...@@ -24,6 +25,6 @@ abstract class AbstractDocumentBuilder<T> {
* @param data Raw data to be printed without any escapes equences * @param data Raw data to be printed without any escapes equences
* @return Fully build document as byte[] * @return Fully build document as byte[]
*/ */
abstract byte[] assemble(MatrixData<T> data); abstract byte[] assemble(T data);
} }
package de.tudresden.inf.mci.brailleplot.printerbackend; package de.tudresden.inf.mci.brailleplot.printerbackend;
import de.tudresden.inf.mci.brailleplot.printabledata.PrintableData;
/** /**
* Abstract class for Documents, that need special escape sequences for the Index Everest-V4. * Abstract class for Documents, that need special escape sequences for the Index Everest-V4.
* All special documents (i.e. Floating Dot Area) should implement this class. All information taken * All special documents (i.e. Floating Dot Area) should implement this class. All information taken
...@@ -11,7 +13,7 @@ package de.tudresden.inf.mci.brailleplot.printerbackend; ...@@ -11,7 +13,7 @@ package de.tudresden.inf.mci.brailleplot.printerbackend;
*/ */
@SuppressWarnings("checkstyle:MagicNumber") @SuppressWarnings("checkstyle:MagicNumber")
abstract class AbstractIndexV4Builder extends AbstractDocumentBuilder { abstract class AbstractIndexV4Builder<T extends PrintableData> extends AbstractDocumentBuilder<T> {
...@@ -22,6 +24,10 @@ abstract class AbstractIndexV4Builder extends AbstractDocumentBuilder { ...@@ -22,6 +24,10 @@ abstract class AbstractIndexV4Builder extends AbstractDocumentBuilder {
final byte[] mStartTemporaryDoc = new byte[] {0x1B, 0x44}; final byte[] mStartTemporaryDoc = new byte[] {0x1B, 0x44};
final byte[] mStartFloatingMode = new byte[] {0x1B, 0x46};
final byte[] mNewLine = new byte[] {0x0A};
/** /**
* Standard variable name for binding margin (BI). * Standard variable name for binding margin (BI).
*/ */
......
package de.tudresden.inf.mci.brailleplot.printerbackend; package de.tudresden.inf.mci.brailleplot.printerbackend;
import de.tudresden.inf.mci.brailleplot.printabledata.MatrixData; import de.tudresden.inf.mci.brailleplot.point.Point2DValued;
import de.tudresden.inf.mci.brailleplot.printabledata.SimpleFloatingPointDataImpl;
import tec.units.ri.unit.MetricPrefix;
import javax.measure.Quantity;
import javax.measure.quantity.Length;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.Locale;
import java.util.Objects;
import static tec.units.ri.unit.Units.METRE;
/** /**
* Class representing the FloatingDotAre protocol for the braille Index Everest V4 for printing * Class representing the FloatingDotArea protocol for the braille Index Everest V4 for printing
* variable areas on paper via coordinates. * variable areas on paper via coordinates.
* @author Andrey Ruzhanskiy, Leonard Kupper * @author Andrey Ruzhanskiy, Leonard Kupper
* @version 29.05.2019 * @version 29.05.2019
*/ */
class FloatingDotAreaBuilder extends AbstractIndexV4Builder { class FloatingDotAreaBuilder extends AbstractIndexV4Builder<SimpleFloatingPointDataImpl<Boolean>> {
/** /**
* Constructor. Does not have any functionality. Should only be used in {@link PrintDirector} * Constructor. Does not have any functionality. Should only be used in {@link PrintDirector}
*/ */
FloatingDotAreaBuilder() { } FloatingDotAreaBuilder() { }
//private int l;
/** /**
* Currently not implemented. * Currently not implemented.
* @param data Raw data to be printed without any escape sequences * @param data Raw data to be printed via the FloatingDotArea
* @return Exception. * @return Exception.
*/ */
byte[] assemble(final MatrixData data) { @Override
throw new UnsupportedOperationException(); byte[] assemble(final SimpleFloatingPointDataImpl<Boolean> data){
mData = Objects.requireNonNull(data);
Iterator<Point2DValued<Quantity<Length>, Boolean>> iter = mData.getIterator();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
//FileOutputStream stream = null;
/*
try {
stream = new FileOutputStream(String.valueOf(l));
} catch (FileNotFoundException e) {
e.getCause();
}
*/
//l++;
try {
stream.write(mStartFloatingMode);
// Here goes Origo, Width and Height
// End of Origo etc.
stream.write(mSemicolon);
stream.write(mNewLine);
// Start iteration over values
while (iter.hasNext()) {
Point2DValued<Quantity<Length>, Boolean> current = iter.next();
Quantity<Length> x = current.getX().to(MetricPrefix.MILLI(METRE));
Quantity<Length> y = current.getY().to(MetricPrefix.MILLI(METRE));
String xFormated = String.format(Locale.ENGLISH, "%.2f", x.getValue().doubleValue());
String yFormated = String.format(Locale.ENGLISH, "%.2f", x.getValue().doubleValue());
stream.write(xFormated.getBytes());
stream.write(mColon);
stream.write(yFormated.getBytes());
//stream.write(y.floatValue());
if (iter.hasNext()) {
stream.write(mNewLine);
}
}
// End with ;
stream.write(mSemicolon);
} catch (IOException e) {
e.printStackTrace();
}
return stream.toByteArray();
//return null;
} }
} }
...@@ -7,7 +7,7 @@ import de.tudresden.inf.mci.brailleplot.printabledata.MatrixData; ...@@ -7,7 +7,7 @@ import de.tudresden.inf.mci.brailleplot.printabledata.MatrixData;
* Class representing the graphic mode protocol from braille Index Everest D4. * Class representing the graphic mode protocol from braille Index Everest D4.
* @author Andrey Ruzhanskiy * @author Andrey Ruzhanskiy
*/ */
class GraphicPrintBuilder extends AbstractIndexV4Builder { class GraphicPrintBuilder extends AbstractIndexV4Builder<MatrixData> {
/** /**
* Constructor. Does not have any functionality. Should only be used in {@link PrintDirector} * Constructor. Does not have any functionality. Should only be used in {@link PrintDirector}
......
...@@ -16,7 +16,7 @@ import java.util.Objects; ...@@ -16,7 +16,7 @@ import java.util.Objects;
* @version 12.07.2019 * @version 12.07.2019
*/ */
@SuppressWarnings("checkstyle:MagicNumber") @SuppressWarnings("checkstyle:MagicNumber")
class NormalBuilder extends AbstractDocumentBuilder<Boolean> { class NormalBuilder extends AbstractDocumentBuilder<MatrixData<Boolean>> {
/** /**
...@@ -33,7 +33,7 @@ class NormalBuilder extends AbstractDocumentBuilder<Boolean> { ...@@ -33,7 +33,7 @@ class NormalBuilder extends AbstractDocumentBuilder<Boolean> {
* @return the final, printable document. * @return the final, printable document.
*/ */
@Override @Override
byte[] assemble(final MatrixData<Boolean> data) { byte[] assemble(MatrixData<Boolean> data) {
//Check if null object was given. //Check if null object was given.
......
...@@ -2,19 +2,16 @@ package de.tudresden.inf.mci.brailleplot.printerbackend; ...@@ -2,19 +2,16 @@ package de.tudresden.inf.mci.brailleplot.printerbackend;
import de.tudresden.inf.mci.brailleplot.configparser.Printer; import de.tudresden.inf.mci.brailleplot.configparser.Printer;
import de.tudresden.inf.mci.brailleplot.printabledata.MatrixData; import de.tudresden.inf.mci.brailleplot.printabledata.PrintableData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.print.DocFlavor; import javax.print.*;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.Doc;
import javax.print.SimpleDoc;
import javax.print.attribute.HashPrintRequestAttributeSet; import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet; import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.JobName; import javax.print.attribute.standard.JobName;
import javax.print.attribute.standard.PrinterState;
import javax.print.event.PrintJobEvent;
import java.util.Objects; import java.util.Objects;
/** /**
* Implements a variation of the GoF design pattern Builder. This class is used for setting the printer configuration and * Implements a variation of the GoF design pattern Builder. This class is used for setting the printer configuration and
...@@ -29,6 +26,7 @@ public class PrintDirector { ...@@ -29,6 +26,7 @@ public class PrintDirector {
private PrintService mService; private PrintService mService;
private String mPrinterName; private String mPrinterName;
private DocFlavor mDocflavor; private DocFlavor mDocflavor;
private final Logger mLogger = LoggerFactory.getLogger(PrintDirector.class);
private DocPrintJob mPrintJob; private DocPrintJob mPrintJob;
...@@ -44,14 +42,20 @@ public class PrintDirector { ...@@ -44,14 +42,20 @@ public class PrintDirector {
Objects.requireNonNull(printerConfig); Objects.requireNonNull(printerConfig);
this.mPrinter = printerCap; this.mPrinter = printerCap;
mPrinterName = printerConfig.getProperty("name").toString(); mPrinterName = printerConfig.getProperty("name").toString();
mLogger.trace("using following printercapability {}", printerCap.toString()," loaded.");
mLogger.info("using the following printer: {}.", mPrinterName);
switch (mPrinter) { switch (mPrinter) {
case NORMALPRINTER: case NORMALPRINTER:
mBuilder = new NormalBuilder(); break; mBuilder = new NormalBuilder();
mLogger.trace("using NormalBuilder as protocol.");
break;
case INDEX_EVEREST_D_V4_GRAPHIC_PRINTER: case INDEX_EVEREST_D_V4_GRAPHIC_PRINTER:
mBuilder = new GraphicPrintBuilder(); mBuilder = new GraphicPrintBuilder();
mLogger.trace("using Index Everest-D V4 graphic print as protocol.");
break; break;
case INDEX_EVEREST_D_V4_FLOATINGDOT_PRINTER: case INDEX_EVEREST_D_V4_FLOATINGDOT_PRINTER:
mBuilder = new FloatingDotAreaBuilder(); mBuilder = new FloatingDotAreaBuilder();
mLogger.trace("using Index Everest-D V4 floatingdot as protocol.");
break; break;
default: throw new IllegalArgumentException(); default: throw new IllegalArgumentException();
} }
...@@ -61,23 +65,27 @@ public class PrintDirector { ...@@ -61,23 +65,27 @@ public class PrintDirector {
/** /**
* Public method for printing the given document with the given data. * Public method for printing the given document with the given data.
* @param data {@link de.tudresden.inf.mci.brailleplot.printabledata.MatrixData} to be printed. * @param data {@link de.tudresden.inf.mci.brailleplot.printabledata.MatrixData} to be printed.
* @param <T> The type of {@link MatrixData}.
*/ */
// Needed if someone tries to use a normal builder with something that is not a boolean. // Needed if someone tries to use a normal builder with something that is not a boolean.
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T> void print(final MatrixData<T> data) { public void print(final PrintableData data) {
mLogger.info("starting with print process.");
Objects.requireNonNull(data); Objects.requireNonNull(data);
mLogger.info("setting up docflavour and service.");
setUpDoc(); setUpDoc();
setUpService(); setUpService();
byte[] result; byte[] result;
mLogger.info("finished setting up doc and service.");
try { try {
mLogger.trace("assembling the data according to protocol: {}.", mBuilder.getClass().getCanonicalName());
result = mBuilder.assemble(data); result = mBuilder.assemble(data);
} catch (ClassCastException e) { } catch (ClassCastException e) {
throw new IllegalArgumentException(e.getMessage(), e); throw new IllegalArgumentException(e.getMessage(), e);
} }
mLogger.info("finished assembling data..");
print(result); print(result);
} }
...@@ -117,12 +125,24 @@ public class PrintDirector { ...@@ -117,12 +125,24 @@ public class PrintDirector {
Objects.requireNonNull(data); Objects.requireNonNull(data);
Objects.requireNonNull(mService); Objects.requireNonNull(mService);
Objects.requireNonNull(mDocflavor); Objects.requireNonNull(mDocflavor);
mLogger.info("setting up doc, asset and job.");
Doc doc = new SimpleDoc(data, mDocflavor, null); Doc doc = new SimpleDoc(data, mDocflavor, null);
PrintRequestAttributeSet asset = new HashPrintRequestAttributeSet(); PrintRequestAttributeSet asset = new HashPrintRequestAttributeSet();
DocPrintJob job = mService.createPrintJob(); DocPrintJob job = mService.createPrintJob();
mLogger.trace("finished setting up doc, asset and job.");
PrinterState state;
asset.add(new JobName("Braille Printing", null)); asset.add(new JobName("Braille Printing", null));
try { try {
mLogger.trace("adding job to the PrintJobListener.");
PrintJobListener listener = new PrintJobListener();
job.addPrintJobListener(listener);
mLogger.trace("starting printing.");
//PrinterIsAcceptingJobs set = mService.getAttribute(PrinterIsAcceptingJobs.class);
//PrinterStateReasons reasons = mService.getAttribute(PrinterStateReasons.class);
job.print(doc, asset); job.print(doc, asset);
//set = mService.getAttribute(PrinterIsAcceptingJobs.class);
//reasons = mService.getAttribute(PrinterStateReasons.class);
listener.waitForDone();
mPrintJob = job; mPrintJob = job;
} catch (PrintException pe) { } catch (PrintException pe) {
throw new RuntimeException(pe); throw new RuntimeException(pe);
...@@ -141,4 +161,64 @@ public class PrintDirector { ...@@ -141,4 +161,64 @@ public class PrintDirector {
} }
return true; return true;
} }
private class PrintJobListener implements javax.print.event.PrintJobListener {
boolean done = false;
@Override
public void printDataTransferCompleted(PrintJobEvent pje) {
mLogger.info("data transfer to printer complete.");
}
@Override
public void printJobCompleted(PrintJobEvent pje) {
mLogger.info("printjob completed.");
synchronized (PrintJobListener.this) {
done = true;
PrintJobListener.this.notify();
}
}
@Override
public void printJobFailed(PrintJobEvent pje) {
mLogger.info("printjob failed.");
synchronized (PrintJobListener.this) {
done = true;
PrintJobListener.this.notify();
}
}
@Override
public void printJobCanceled(PrintJobEvent pje) {
mLogger.info("printjob was canceled.");
synchronized (PrintJobListener.this) {
done = true;
PrintJobListener.this.notify();
}
}
@Override
public void printJobNoMoreEvents(PrintJobEvent pje) {
mLogger.info("printjob has no more events.");
synchronized (PrintJobListener.this) {
done = true;
}
}
@Override
public void printJobRequiresAttention(PrintJobEvent pje) {
mLogger.info("printjob requires attention.");
PrintJobListener.this.notify();
}
public synchronized void waitForDone() {
try {
while (!done) {
wait();
}
} catch (InterruptedException e) {
return;
}
}
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment