Bug 297957 - [API][Printing] Allow copy of PrinterData object
Summary: [API][Printing] Allow copy of PrinterData object
Status: NEW
Alias: None
Product: Platform
Classification: Eclipse Project
Component: SWT (show other bugs)
Version: 3.6   Edit
Hardware: PC All
: P3 enhancement (vote)
Target Milestone: ---   Edit
Assignee: Platform-SWT-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords: api
Depends on:
Blocks: 297964 315932
  Show dependency tree
 
Reported: 2009-12-16 09:06 EST by Deepak Azad CLA
Modified: 2017-07-11 08:39 EDT (History)
4 users (show)

See Also:
daniel_megert: review+


Attachments
patch (2.18 KB, patch)
2010-06-09 12:56 EDT, Carolyn MacLeod CLA
daniel_megert: review+
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Deepak Azad CLA 2009-12-16 09:06:18 EST
Build Identifier: I20091210-1301 - 3.6 M4

We need to be able to copy a org.eclipse.swt.printing.PrinterData object. So PrinterData class should either offer clone() or PrinterData(PrinterData).
Another option would be to make PrinterData.otherData as public.

We need this in org.eclipse.jface.text.TextViewer.print(StyledTextPrintOptions), so that user selected printer options can be remembered for the session(267427).


Reproducible: Always
Comment 1 Dani Megert CLA 2010-06-08 11:45:12 EDT
I would like to get one or the other suggested API for 3.6.1 because otherwise we have to disable printing in parallel in order to fix bug 315932.
Comment 2 Carolyn MacLeod CLA 2010-06-08 12:03:47 EDT
We cannot add new API in a point release. There are a lot of PrinterData API changes coming up for 3.7. I am marking this bug for consideration for 3.7 because it makes sense to add this type of API at the same time as we make the other changes.

In order to fix 315932, you only need to persist the public fields of PrinterData, one of which is 'scope', which will keep the 'SELECTION' choice of the user.

Alternatively - is it not possible to simply pass the original PrinterData object for the printing in parallel? Why do you need to clone it?
Comment 3 Dani Megert CLA 2010-06-08 12:26:59 EDT
>In order to fix 315932, you only need to persist the public fields of
>PrinterData, one of which is 'scope', which will keep the 'SELECTION' choice of
>the user.
Doesn't 'otherData' contain printer specific options that would get lost? At least that's what we saw when testing the fix for bug 267427.

>Alternatively - is it not possible to simply pass the original PrinterData
>object for the printing in parallel? Why do you need to clone it?
In order to remember the last used print dialog settings (as requested by you in bug 267427) we have to remember the last used PrinterData but we have to reset some fields like start/end, copy count or selection - most apps do that. Now, given that the latest remembered data is also used for printing in the background thread, there's always an issue.

Another idea I tried was to set the data into the dialog and then override the values with e.g. PrintDialog.setScope(int) but that doesn't work for two reasons:
1) This modifies the printer data that I've set before instead of using an
   internal copy.
2) PrintDialog.setCopyCount(int) is missing.


>We cannot add new API in a point release. 
The PMC can approve exceptions.
Comment 4 Carolyn MacLeod CLA 2010-06-08 14:57:06 EDT
> Doesn't 'otherData' contain printer specific options that would get lost?
Ah, you are correct. I would be fixing one bug and breaking another.  :(

> given that the latest remembered data is also used for printing in the
> background thread
Can you use the original PrinterData for the printing scenario given in bug 315932 comment 0, and then save it and clear those fields? Or just clear the fields right before opening the dialog again? Or something along those lines?
Comment 5 Deepak Azad CLA 2010-06-08 15:13:35 EDT
(In reply to comment #4)
> Can you use the original PrinterData for the printing scenario given in bug #
> 315932 comment 0, and then save it and clear those fields? Or just clear the
> fields right before opening the dialog again? Or something along those lines?
That is the problem, how to 'save' it? :) 

We are forced to use the 'same' PrinterData object as it cannot be copied.
Comment 6 Dani Megert CLA 2010-06-09 03:43:43 EDT
> That is the problem, how to 'save' it? :) 
> We are forced to use the 'same' PrinterData object as it cannot be copied.
Exactly.
Comment 7 Carolyn MacLeod CLA 2010-06-09 11:21:04 EDT
By 'save', I meant save in global variable, as in:
	fgPrinterData= data;

But instead of clearing the fields right away, I think you should clear them before opening the dialog. (i.e. not right after saving the data).

For example:

	final PrintDialog dialog= new PrintDialog(shell, SWT.PRIMARY_MODAL);
	fgPrinterData.startPage= 1;
	fgPrinterData.endPage= 1;
	fgPrinterData.scope= PrinterData.ALL_PAGES;
	fgPrinterData.copyCount= 1;
	dialog.setPrinterData(fgPrinterData);

I think that will avoid threading issues and fix bug 315932 (plus if there are any other bugs open regarding copy count or start/end page user settings not being used).
Comment 8 Dani Megert CLA 2010-06-09 11:32:50 EDT
>I think that will avoid threading issues
No, it will not: the saved fgPrinterData is from the previous run, so if the printing thread is not fast then changing the values in fgPrinterData will affect that result because the printer data is not copied and accessed in the thread.
Comment 9 Dani Megert CLA 2010-06-09 11:35:53 EDT
Having said that: a non-API fix would be that SWT ensures that the printing thread doesn't access the given printer data i.e. only works on a copy, see org.eclipse.swt.custom.StyledText.Printing.init().
Comment 10 Carolyn MacLeod CLA 2010-06-09 12:13:16 EDT
The user would have to request a second print job before the first print job was initialized (in StyledText.Printing.init()).

But one print job would always work - which I think is better than the current situation where printing portions of a file will never work.

By the way, I notice I forgot to protect against null, so the code should be:
    final PrintDialog dialog= new PrintDialog(shell, SWT.PRIMARY_MODAL);
    if (fgPrinterData != null) {
        fgPrinterData.startPage= 1;
        fgPrinterData.endPage= 1;
        fgPrinterData.scope= PrinterData.ALL_PAGES;
        fgPrinterData.copyCount= 1;
    }
    dialog.setPrinterData(fgPrinterData);


So all I am saying is that I think this code is better than what is there. (It actually feels like the right place to reset the fields, even if you were able to save a copy). I do agree that if a user prints something, and then decides immediately to print something else before the first print job has been completely initialized, then their selections for the first print job will be clobbered. I guess I don't think this is too likely. At least, not as likely as clobbering them for sure every time. <g>

So I will talk to SSQ about new API, but I don't think we have ever done it in a point release before, and I think the suggested work-around will make the bug happen much less often. I do see, however, that the requested API would have prevented the problem in the first place.
Comment 11 Carolyn MacLeod CLA 2010-06-09 12:13:59 EDT
Just noticed your comment 9. Let me see if I can make that work.
Comment 12 Dani Megert CLA 2010-06-09 12:15:45 EDT
>The user would have to request a second print job before the first print job
>was initialized (in StyledText.Printing.init()).
>But one print job would always work 
Nope, because the init() happens in the non-UI thread. If the user prints a second time then it can affect the previous thread.
Comment 13 Carolyn MacLeod CLA 2010-06-09 12:25:41 EDT
>>But one print job would always work 
>Nope, because the init() happens in the non-UI thread. If the user prints a
>second time then it can affect the previous thread.
But that's exactly what I said - one print job is ok, but two in quick succession might have problems.

Anyhow, I can't really implement comment 9 reasonably because StyledText and PrinterData are in different packages. I would have to create a public-internal way to get at the otherData, and that seems pointless because we are planning to give an API way to copy the PrinterData anyhow.

I will talk to SSQ.
Comment 14 Dani Megert CLA 2010-06-09 12:36:50 EDT
>Anyhow, I can't really implement comment 9 reasonably because StyledText and
>PrinterData are in different packages. 
Looks like today is the day we disagree ;-)

In org.eclipse.swt.custom.StyledText.Printing.Printing(...) - which runs in the UI thread - all data is already copied out of the printer data and stored in fields of  except for the scope. Doing the same for scope is all that needs to be done.
Comment 15 Carolyn MacLeod CLA 2010-06-09 12:44:05 EDT
Oh, of course. I don't need to copy otherData because you're not going to clobber it <grin>. I'll get back to you in a few minutes.
Comment 16 Carolyn MacLeod CLA 2010-06-09 12:56:30 EDT
Created attachment 171551 [details]
patch

Easy fix. Tested 4 or 5 times, with various option combinations.
Comment 17 Dani Megert CLA 2010-06-09 12:58:31 EDT
Comment on attachment 171551 [details]
patch

That's what I had in mind ;-)

Thanks!
Comment 18 Dani Megert CLA 2010-06-09 12:59:48 EDT
I draw back the API request since I no longer need it with the given fix.
Comment 19 Carolyn MacLeod CLA 2010-06-09 13:12:27 EDT
Thanks for the idea, Dani (and for persevering <g>).

We should untangle the bugs. We have:
1) This bug, which requests new API to copy PrinterData for someday
   I think I will keep this bug open, because the request makes sense.
   We should do it for 3.7, when we do the other PrinterData changes.

2) Bug 297964, which reminds you to use any new API we create
   I think you probably want to keep this open, because (as we have seen <g>)
   there can be issues...

3) Bug 315932, which is the real user's bug that this patch actually fixes
   I think I should attach the patch to that bug, and make sure the target
   milestone is 3.6.1 and assign to SWT, and then have that bug reviewed.
   I'm not sure what the 3.6.1 reviewer requirements are (+1 or +2?), but
   I know you'll give +1, and I should really ask Felipe (StyledText owner)
   to review as well, so he can give another +1 if we need it.

Sound good?
Comment 20 Dani Megert CLA 2010-06-09 13:19:50 EDT
>Sound good?
I agree completely this time :-)
Comment 21 Dani Megert CLA 2010-06-09 13:20:54 EDT
Moved bug 315932 to SWT with target milestone set to '3.6.1'.
Comment 22 Carolyn MacLeod CLA 2010-06-11 12:22:06 EDT
Before fixing this, read bug 315932 comment 10 and 11. Make sure that the implementation and the doc are consistent and clear for all methods in org.eclipse.swt.printing that take or return a PrinterData object. This is not currently the case.