开发者

Is there a good way to persist printer settings in a Swing app?

We are using the new Java printing API which uses PrinterJob.printDialog(attributes) to display the dialog to the user.

Wanting to save the user's settings for the next time, I wanted to do this:

PrintRequestAttributeSet attributes = loadAttributesFromPreferences();
if (printJob.printDialog(attributes)) {
    // print, and then...

    saveAttributesToPreferences(attributes);
}

However, what I found by doing this is that sometimes (I haven't figured out how, yet) the attributes get some bad data inside, and then when you print, you get a white page of nothing. Then the code saves the poisoned settings into the preferences, and all subsequent print runs get poisoned settings too. Additionally, the entire point of the exercise, making the settings for the new run the same as the user chose for the previous run, is defeated, because the new dialog does not appear to use the old settings.

So I would like to know if there is a proper way to do this. Surely Sun didn't intend that users have to select the printer, page size, orientation and margin settings every time the application starts up.

Edit to show the implementation of the storage methods:

private PrintRequestAttributeSet loadAttributesFromPreferences()
{
    PrintRequestAttributeSet attributes = null;

    byte[] marshaledAttributes = preferences.getByteArray(PRINT_REQUEST_ATTRIBUTES_KEY, null);
    if (marshaledAttributes != null)
    {
        try
        {
            @SuppressWarnings({"IOResourceOpenedButNotSafelyClosed"})
            ObjectInput objectInput = new ObjectInputStream(new ByteArrayInputStream(marshaledAttributes));

            attributes = (PrintRequestAttributeSet) objectInput.readObject();
        }
        catch (IOException e)
        {
            // Can occur due to invalid object data e.g. InvalidClassException, StreamCorruptedException
            Logger.getLogger(getClass()).warn("Error trying to read print attributes from preferences", e);
        }
        catch (ClassNotFoundException e)
        {
            Logger.getLogger(getClass()).warn("Class not found trying to read print attributes from preferences", e);
        }
    }

    if (attributes == null)
    {
        attributes = new HashPrintRequestAttributeSet();
    }

    return attributes;
}

private void saveAttributesToPreferences(PrintRequestAttributeSet attributes)
{
    ByteArrayOutputStream storage = new ByteArrayOutputStream();
    try
    {
        ObjectOutput objectOutput = new ObjectOutputStream(storage);
        try
        {
            objectOutput.writeObject(attributes);
        }
        finally
        {
            objectOutput.close(); // side-effect of flushing the underlying stream
        }
    }
    catch (IOException e)
    {
        throw new IllegalStateException("I/O error writing to a stream going to a byte array", e);
    }

    preferences.putByteArray(PRINT_REQUEST_ATTRIBUTES_KEY, storage.toByteArray());
}

Edit: Okay, it seems like the reason it开发者_JS百科 isn't remembering the printer is that it isn't in the PrintRequestAttributeSet at all. Indeed, the margins and page sizes are remembered, at least until the settings get poisoned at random. But the printer chosen by the user is not here:

[0] = {java.util.HashMap$Entry@9494} class javax.print.attribute.standard.Media -> na-letter
[1] = {java.util.HashMap$Entry@9501} class javax.print.attribute.standard.Copies -> 1
[2] = {java.util.HashMap$Entry@9510} class javax.print.attribute.standard.MediaPrintableArea -> (10.0,10.0)->(195.9,259.4)mm
[3] = {java.util.HashMap$Entry@9519} class javax.print.attribute.standard.OrientationRequested -> portrait


It appears that what you're looking for is the PrintServiceAttributeSet, rather than the PrintRequestAttributeSet.

Take a look at the PrintServiceAttribute interface, and see if the elements you need have been implemented as classes. If not, you can implement your own PrintServiceAttribute class(es).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜