Skip to content

Commit

Permalink
Add OutputBin support on Linux
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderScherbatiy committed May 21, 2024
1 parent 96e1f53 commit 3855d0a
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 42 deletions.
21 changes: 0 additions & 21 deletions src/java.desktop/macosx/classes/sun/lwawt/macosx/CPrinterJob.java
Original file line number Diff line number Diff line change
Expand Up @@ -214,27 +214,6 @@ protected void setAttributes(PrintRequestAttributeSet attributes) throws Printer
}
}

private String getOutputBinValue(Attribute attr) {
if (attr instanceof CustomOutputBin customOutputBin) {
return customOutputBin.getChoiceName();
} else if (attr instanceof OutputBin outputBin) {
String name = outputBin.toString().replace('-', ' ');
PrintService ps = getPrintService();
if (ps == null) {
return name;
}
OutputBin[] supportedBins = (OutputBin[]) ps.getSupportedAttributeValues(OutputBin.class, null, null);
for (OutputBin bin : supportedBins) {
CustomOutputBin customBin = (CustomOutputBin) bin;
if (customBin.getCustomName().equalsIgnoreCase(name)) {
return customBin.getChoiceName();
}
}
return name;
}
return null;
}

private void setPageRangeAttribute(int from, int to, boolean isRangeSet) {
if (attributes != null) {
// since native Print use zero-based page indices,
Expand Down
16 changes: 13 additions & 3 deletions src/java.desktop/share/classes/sun/print/PSPrinterJob.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import javax.print.attribute.standard.Destination;
import javax.print.attribute.standard.DialogTypeSelection;
import javax.print.attribute.standard.JobName;
import javax.print.attribute.standard.OutputBin;
import javax.print.attribute.standard.Sides;

import java.io.BufferedOutputStream;
Expand Down Expand Up @@ -491,14 +492,19 @@ protected void setAttributes(PrintRequestAttributeSet attributes)
if (attributes == null) {
return; // now always use attributes, so this shouldn't happen.
}
mOptions = "";
Attribute attr = attributes.get(Media.class);
if (attr instanceof CustomMediaTray) {
CustomMediaTray customTray = (CustomMediaTray)attr;
String choice = customTray.getChoiceName();
if (choice != null) {
mOptions = " InputSlot="+ choice;
mOptions += " InputSlot="+ choice;
}
}
String outputBin = getOutputBinValue(outputBinAttr);
if (outputBin != null) {
mOptions += " output-bin=" + outputBin;
}
}

/**
Expand Down Expand Up @@ -1643,7 +1649,9 @@ private String[] printExecCmd(String printer, String options,
execCmd[n++] = "-o job-sheets=standard";
}
if ((pFlags & OPTIONS) != 0) {
execCmd[n++] = "-o" + options;
for(String option: options.trim().split(" ")) {
execCmd[n++] = "-o " + option;
}
}
} else {
ncomps+=1; //add 1 arg for lp
Expand All @@ -1666,7 +1674,9 @@ private String[] printExecCmd(String printer, String options,
execCmd[n++] = "-o job-sheets=standard";
}
if ((pFlags & OPTIONS) != 0) {
execCmd[n++] = "-o" + options;
for(String option: options.trim().split(" ")) {
execCmd[n++] = "-o " + option;
}
}
}
execCmd[n++] = spoolFile;
Expand Down
21 changes: 21 additions & 0 deletions src/java.desktop/share/classes/sun/print/RasterPrinterJob.java
Original file line number Diff line number Diff line change
Expand Up @@ -2625,4 +2625,25 @@ private void setParentWindowID(PrintRequestAttributeSet attrs) {
parentWindowID = DialogOwnerAccessor.getID(onTop);
}
}

protected String getOutputBinValue(Attribute attr) {
if (attr instanceof CustomOutputBin customOutputBin) {
return customOutputBin.getChoiceName();
} else if (attr instanceof OutputBin outputBin) {
String name = outputBin.toString().replace('-', ' ');
PrintService ps = getPrintService();
if (ps == null) {
return name;
}
OutputBin[] supportedBins = (OutputBin[]) ps.getSupportedAttributeValues(OutputBin.class, null, null);
for (OutputBin bin : supportedBins) {
CustomOutputBin customBin = (CustomOutputBin) bin;
if (customBin.getCustomName().equalsIgnoreCase(name)) {
return customBin.getChoiceName();
}
}
return name;
}
return null;
}
}
6 changes: 4 additions & 2 deletions src/java.desktop/share/classes/sun/print/ServiceDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -2913,10 +2913,12 @@ public void updateInfo() {
break;
}
}
} else if (outputBins.length == 1) {
cbOutput.setSelectedIndex(0);
}
}

outputEnabled = cbOutput.getItemCount() != 0;
outputEnabled = outputBins.length > 1;
}
}

cbOutput.setEnabled(outputEnabled);
Expand Down
43 changes: 42 additions & 1 deletion src/java.desktop/unix/classes/sun/print/IPPPrintService.java
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,7 @@ private void initAttributes() {
if ((urlConnection = getIPPConnection(myURL)) == null) {
mediaSizeNames = new MediaSizeName[0];
mediaTrays = new MediaTray[0];
outputBins = new OutputBin[0];
debug_println(debugPrefix+"initAttributes, NULL urlConnection ");
init = true;
return;
Expand All @@ -463,7 +464,9 @@ private void initAttributes() {
cps = new CUPSPrinter(printer);
mediaSizeNames = cps.getMediaSizeNames();
mediaTrays = cps.getMediaTrays();
outputBins = cps.getOutputBins();
outputBins = PrintServiceLookupProvider.isMac()
? cps.getOutputBins()
: getSupportedOutputBins();
customMediaSizeNames = cps.getCustomMediaSizeNames();
defaultMediaIndex = cps.getDefaultMediaIndex();
rawResolutions = cps.getRawResolutions();
Expand Down Expand Up @@ -497,6 +500,11 @@ private void initAttributes() {
mediaTrays = new MediaTray[trayList.size()];
mediaTrays = trayList.toArray(mediaTrays);
}

if (outputBins == null) {
outputBins = getSupportedOutputBins();
}

urlConnection.disconnect();

init = true;
Expand Down Expand Up @@ -1059,6 +1067,25 @@ private Media[] getSupportedMedia() {
return new Media[0];
}

private OutputBin[] getSupportedOutputBins() {
if ((getAttMap != null) && getAttMap.containsKey("output-bin-supported")) {

AttributeClass attribClass = getAttMap.get("output-bin-supported");

if (attribClass != null) {
String[] values = attribClass.getArrayOfStringValues();
if (values == null || values.length == 0) {
return null;
}
OutputBin[] outputBinNames = new OutputBin[values.length];
for (int i = 0; i < values.length; i++) {
outputBinNames[i] = CustomOutputBin.createOutputBin(values[i], values[i]);
}
return outputBinNames;
}
}
return null;
}

public synchronized Class<?>[] getSupportedAttributeCategories() {
if (supportedCats != null) {
Expand All @@ -1076,6 +1103,11 @@ public synchronized Class<?>[] getSupportedAttributeCategories() {
(PrintRequestAttribute)printReqAttribDefault[i];
if (getAttMap != null &&
getAttMap.containsKey(pra.getName()+"-supported")) {

if (pra == OutputBin.TOP && (outputBins == null || outputBins.length == 0)) {
continue;
}

catList.add(pra.getCategory());
}
}
Expand Down Expand Up @@ -1154,6 +1186,11 @@ public synchronized Class<?>[] getSupportedAttributeCategories() {
return true;
}

if (category == OutputBin.class
&& (outputBins == null || outputBins.length == 0)) {
return false;
}

for (int i=0;i<supportedCats.length;i++) {
if (category == supportedCats[i]) {
return true;
Expand Down Expand Up @@ -1652,6 +1689,10 @@ public boolean isAttributeValueSupported(Attribute attr,
} else {
return new PrinterResolution(300, 300, PrinterResolution.DPI);
}
} else if (category == OutputBin.class) {
if (attribClass != null) {
return CustomOutputBin.createOutputBin(attribClass.getStringValue(), attribClass.getStringValue());
}
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* @test
* @bug JDK-8314070
* @key printer
* @requires (os.family == "mac")
* @requires (os.family == "linux" | os.family == "mac")
* @summary javax.print: Support IPP output-bin attribute extension
* @run main/manual OutputBinAttributePrintDialogTest COMMON
* @run main/manual OutputBinAttributePrintDialogTest NATIVE
Expand All @@ -48,6 +48,7 @@
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
Expand All @@ -70,21 +71,23 @@ public static void main(String[] args) throws Exception {

final DialogTypeSelection dialogTypeSelection = getDialogTypeSelection(args[0]);

OutputBin[] supportedOutputBins = getSupportedOutputBinttributes();
if (supportedOutputBins.length < 1) {
return;
if (dialogTypeSelection == DialogTypeSelection.NATIVE) {
String os = System.getProperty("os.name").toLowerCase();
if (os.startsWith("linux")) {
// The Linux native dialog is the same as the native
return;
}
}

// Test only the first and the last output bins to reduce number of tests
final Set<OutputBin> outputBins = new HashSet<>();
outputBins.add(supportedOutputBins[0]);
outputBins.add(supportedOutputBins[supportedOutputBins.length - 1]);

final OutputBin[] supportedOutputBins = getSupportedOutputBinttributes();
if (supportedOutputBins.length < 2) {
return;
}

SwingUtilities.invokeLater(() -> {
testTotalCount = outputBins.size();
for (OutputBin outputBin : outputBins) {
testPrint(dialogTypeSelection, outputBin, outputBins);
testTotalCount = supportedOutputBins.length;
for (OutputBin outputBin : supportedOutputBins) {
testPrint(dialogTypeSelection, outputBin, supportedOutputBins);
}
testFinished = true;
});
Expand Down Expand Up @@ -196,13 +199,13 @@ private static void runPrint(DialogTypeSelection dialogTypeSelection, OutputBin
}
}

private static void testPrint(DialogTypeSelection dialogTypeSelection, OutputBin outputBin, Set<OutputBin> supportedOutputBins) {
private static void testPrint(DialogTypeSelection dialogTypeSelection, OutputBin outputBin, OutputBin[] supportedOutputBins) {

System.out.printf("Test dialog: %s%n", dialogTypeSelection);

String[] instructions = {
"Up to " + testTotalCount + " tests will run and it will test all output bins:",
supportedOutputBins.toString(),
Arrays.toString(supportedOutputBins),
"supported by the printer.",
"",
"The test is " + (testCount + 1) + " from " + testTotalCount + ".",
Expand Down
2 changes: 1 addition & 1 deletion test/jdk/javax/print/attribute/OutputBinAttributeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* @test
* @bug JDK-8314070
* @key printer
* @requires (os.family == "mac")
* @requires (os.family == "linux" | os.family == "mac")
* @summary javax.print: Support IPP output-bin attribute extension
* @run main/manual OutputBinAttributeTest
*/
Expand Down

0 comments on commit 3855d0a

Please sign in to comment.