1
package org.herac.tuxguitar.gui.printer;
3
import javax.print.PrintService;
4
import javax.print.PrintServiceLookup;
5
import javax.print.attribute.PrintServiceAttribute;
6
import javax.print.attribute.standard.PrinterInfo;
7
import javax.print.attribute.standard.PrinterIsAcceptingJobs;
8
import javax.print.attribute.standard.PrinterMakeAndModel;
10
import org.eclipse.swt.SWT;
11
import org.eclipse.swt.events.SelectionAdapter;
12
import org.eclipse.swt.events.SelectionEvent;
13
import org.eclipse.swt.layout.FillLayout;
14
import org.eclipse.swt.layout.GridData;
15
import org.eclipse.swt.layout.GridLayout;
16
import org.eclipse.swt.printing.PrinterData;
17
import org.eclipse.swt.widgets.Button;
18
import org.eclipse.swt.widgets.Combo;
19
import org.eclipse.swt.widgets.Composite;
20
import org.eclipse.swt.widgets.Group;
21
import org.eclipse.swt.widgets.Label;
22
import org.eclipse.swt.widgets.Shell;
23
import org.eclipse.swt.widgets.Spinner;
24
import org.eclipse.swt.widgets.Text;
25
import org.herac.tuxguitar.gui.TuxGuitar;
26
import org.herac.tuxguitar.gui.util.FileChooser;
28
public class PrintDialog {
32
private PrintService[] printServices;
33
private PrinterData printerData;
35
private Combo servicesCombo;
36
private Label servicesStatus;
37
private Label servicesType;
38
private Label servicesInfo;
39
private Button printToFile;
40
private Text filePath;
41
private Button fileChooser;
42
private Spinner copiesSpinner;
43
private Button allPages;
44
private Button rangePages;
45
private Text fromRangePage;
46
private Text toRangePage;
48
public PrintDialog (Shell parent) {
49
this (parent,SWT.DIALOG_TRIM |SWT.APPLICATION_MODAL);
52
public PrintDialog(Shell parent,int style){
58
public void findServices(){
59
this.printServices = PrintServiceLookup.lookupPrintServices(null,null);
62
public PrinterData open(){
63
this.dialog = new Shell(this.parent,this.style);
64
this.dialog.setLayout(new FillLayout());
65
this.dialog.setText(TuxGuitar.getProperty("print.dialog"));
67
this.initComposites();
72
int x = parent.getBounds().x + (parent.getBounds().width - dialog.getSize().x) / 2;
73
int y = parent.getBounds().y + (parent.getBounds().height - dialog.getSize().y) / 2;
74
this.dialog.setLocation(x, y);
76
while (!dialog.isDisposed()) {
77
if (!dialog.getDisplay().readAndDispatch()) {
78
dialog.getDisplay().sleep();
82
return this.printerData;
85
private void initComposites(){
86
Composite composite = new Composite(this.dialog,SWT.NONE);
87
composite.setLayout(new GridLayout(1,true));
89
//-------SERVICES--------------------------------------------------
90
Composite servicesComposite = new Composite(composite,SWT.NONE);
91
servicesComposite.setLayout(new GridLayout());
92
servicesComposite.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
93
Group services = new Group(servicesComposite,SWT.NONE);
94
services.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
95
services.setText(TuxGuitar.getProperty("print.service"));
96
services.setLayout(new GridLayout(2,false));
97
Label serviceNameLabel = new Label(services,SWT.NONE);
98
serviceNameLabel.setText(TuxGuitar.getProperty("print.service.name") + ":");
99
this.servicesCombo = new Combo(services,SWT.DROP_DOWN | SWT.READ_ONLY);
100
for (int i = 0; i < this.printServices.length; i++) {
101
this.servicesCombo.add(this.printServices[i].getName());
103
this.servicesCombo.select(0);
104
this.servicesCombo.addSelectionListener(new SelectionAdapter() {
105
public void widgetSelected(SelectionEvent e) {
106
checkServiceValues();
110
Label serviceStatusLabel = new Label(services,SWT.NONE);
111
serviceStatusLabel.setText(TuxGuitar.getProperty("print.service.status") + ":");
112
this.servicesStatus = new Label(services,SWT.NONE);
113
this.servicesStatus.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
115
Label serviceTypeLabel = new Label(services,SWT.NONE);
116
serviceTypeLabel.setText(TuxGuitar.getProperty("print.service.type") + ":");
117
this.servicesType = new Label(services,SWT.NONE);
118
this.servicesType.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
120
Label serviceInfoLabel = new Label(services,SWT.NONE);
121
serviceInfoLabel.setText(TuxGuitar.getProperty("print.service.info") + ":");
122
this.servicesInfo = new Label(services,SWT.NONE);
123
this.servicesInfo.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
126
//Composite fileComposite = new Composite(services,SWT.NONE);
127
//fileComposite.setLayout(new GridLayout(3,false));
128
//fileComposite.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true,2,1));
130
this.printToFile = new Button(services,SWT.CHECK);
131
this.printToFile.setLayoutData(new GridData(SWT.BEGINNING,SWT.CENTER,true,true,2,1));
132
this.printToFile.setText(TuxGuitar.getProperty("print.print-to-file"));
133
this.printToFile.addSelectionListener(new SelectionAdapter() {
134
public void widgetSelected(SelectionEvent e) {
135
checkPrintToFileComposites();
139
this.filePath = new Text(services,SWT.BORDER);
140
this.filePath.setLayoutData(new GridData(220,16));
142
GridData chooserData = new GridData(SWT.LEFT,SWT.FILL,true,true);
143
chooserData.minimumWidth = 80;
144
chooserData.minimumHeight = 25;
146
this.fileChooser = new Button(services,SWT.PUSH);
147
this.fileChooser.setLayoutData(chooserData);
148
this.fileChooser.setText(TuxGuitar.getProperty("print.file-chooser"));
149
this.fileChooser.addSelectionListener(new SelectionAdapter() {
150
public void widgetSelected(SelectionEvent e) {
154
this.checkPrintToFileComposites();
155
this.checkServiceValues();
156
//-------DOCUMENT INFO---------------------------------------------
157
Composite docInfo = new Composite(composite,SWT.NONE);
158
docInfo.setLayout(new GridLayout(2,false));
159
docInfo.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
161
//range--------------------------------------------------
162
Group range = new Group(docInfo, SWT.SHADOW_ETCHED_IN);
163
range.setLayout(new GridLayout(4,false));
164
range.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
165
range.setText(TuxGuitar.getProperty("print.range"));
167
this.allPages = new Button(range,SWT.RADIO);
168
this.allPages.setText(TuxGuitar.getProperty("print.range.all-pages"));
169
this.allPages.setLayoutData(new GridData(SWT.NONE,SWT.NONE,false,false,4,1));
170
this.allPages.setSelection(true);
171
this.allPages.addSelectionListener(new SelectionAdapter() {
172
public void widgetSelected(SelectionEvent e) {
173
checkRangeComposites();
177
this.rangePages = new Button(range,SWT.RADIO);
178
this.rangePages.setText(TuxGuitar.getProperty("print.range.pages"));
179
this.rangePages.addSelectionListener(new SelectionAdapter() {
180
public void widgetSelected(SelectionEvent e) {
181
checkRangeComposites();
185
this.fromRangePage = new Text(range,SWT.BORDER);
186
this.fromRangePage.setLayoutData(new GridData(25,15));
188
Label rangeToLabel = new Label(range,SWT.NONE);
189
rangeToLabel.setText(TuxGuitar.getProperty("print.range.pages-to"));
191
this.toRangePage = new Text(range,SWT.BORDER);
192
this.toRangePage.setLayoutData(new GridData(25,15));
194
this.checkRangeComposites();
195
//copies--------------------------------------------------------------
196
Group copies = new Group(docInfo, SWT.SHADOW_ETCHED_IN);
197
copies.setLayout(new GridLayout(2,false));
198
copies.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
199
copies.setText(TuxGuitar.getProperty("print.copies"));
201
Label copiesLabel = new Label(copies,SWT.NONE);
202
copiesLabel.setLayoutData(new GridData(SWT.NONE,SWT.CENTER,true,true));
203
copiesLabel.setText(TuxGuitar.getProperty("print.copies-number"));
204
this.copiesSpinner = new Spinner(copies,SWT.BORDER);
205
this.copiesSpinner.setLayoutData(new GridData(SWT.NONE,SWT.CENTER,true,true));
206
this.copiesSpinner.setMaximum(100);
207
this.copiesSpinner.setMinimum(1);
208
//-------BUTTONS---------------------------------------------------
209
Composite buttons = new Composite(composite, SWT.NONE);
210
buttons.setLayout(new GridLayout(2,false));
211
buttons.setLayoutData(new GridData(SWT.END,SWT.FILL,true,true));
213
GridData data = new GridData(SWT.FILL,SWT.FILL,true,true);
214
data.minimumWidth = 80;
215
data.minimumHeight = 25;
217
Button buttonOk = new Button(buttons, SWT.PUSH);
218
buttonOk.setLayoutData(data);
219
buttonOk.setText(TuxGuitar.getProperty("print.print"));
220
buttonOk.addSelectionListener(new SelectionAdapter() {
221
public void widgetSelected(SelectionEvent arg0) {
226
Button buttonCancel = new Button(buttons, SWT.PUSH);
227
buttonCancel.setLayoutData(data);
228
buttonCancel.setText(TuxGuitar.getProperty("cancel"));
229
buttonCancel.addSelectionListener(new SelectionAdapter() {
230
public void widgetSelected(SelectionEvent arg0) {
237
private void checkServiceValues(){
238
String status = new String();
239
String type = new String();
240
String info = new String();
241
int serviceSelection = servicesCombo.getSelectionIndex();
242
if(serviceSelection >= 0){
243
PrintService printService = this.printServices[serviceSelection];
244
PrintServiceAttribute statusAtt = printService.getAttribute(PrinterIsAcceptingJobs.class);
245
PrintServiceAttribute typeAtt = printService.getAttribute(PrinterMakeAndModel.class);
246
PrintServiceAttribute infoAtt = printService.getAttribute(PrinterInfo.class);
247
status = (statusAtt == null)?"":statusAtt.toString();
248
type = (typeAtt == null)?"":typeAtt.toString();
249
info = (infoAtt == null)?"":infoAtt.toString();
251
this.servicesStatus.setText(status);
252
this.servicesType.setText(type);
253
this.servicesInfo.setText(info);
256
private void checkRangeComposites(){
257
boolean rangePagesSelected = this.rangePages.getSelection();
258
this.fromRangePage.setEnabled(rangePagesSelected);
259
this.toRangePage.setEnabled(rangePagesSelected);
263
private void checkPrintToFileComposites(){
264
boolean selected = this.printToFile.getSelection();
265
this.filePath.setEnabled(selected);
266
this.fileChooser.setEnabled(selected);
269
private void chooseFile(){
270
String fileName = FileChooser.instance().open(dialog,FileChooser.ALL_FORMAT/*,null*/);
271
if(fileName != null){
272
this.filePath.setText(fileName);
276
private void updatePrinterData(){
277
this.printerData = new PrinterData();
278
int serviceSelection = servicesCombo.getSelectionIndex();
279
if(serviceSelection >= 0){
280
printerData.name = this.printServices[serviceSelection].getName();
282
printerData.copyCount = this.copiesSpinner.getSelection();
283
printerData.printToFile = this.printToFile.getSelection();
284
printerData.fileName = this.filePath.getText();
285
printerData.scope = PrinterData.ALL_PAGES;
286
if(!this.allPages.getSelection()){
288
int startPage = Integer.parseInt(this.fromRangePage.getText());
289
int endPage = Integer.parseInt(this.toRangePage.getText());
290
printerData.startPage = startPage;
291
printerData.endPage = endPage;
292
printerData.scope = PrinterData.PAGE_RANGE;
293
}catch(NumberFormatException e){