~ubuntu-branches/ubuntu/oneiric/tuxguitar/oneiric

« back to all changes in this revision

Viewing changes to src/org/herac/tuxguitar/gui/printer/PrintDialog.java

  • Committer: Bazaar Package Importer
  • Author(s): Philippe Coval
  • Date: 2008-06-19 00:30:30 UTC
  • mto: (5.1.2 sid)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20080619003030-h719szrhsngou7c6
Tags: upstream-1.0
ImportĀ upstreamĀ versionĀ 1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package org.herac.tuxguitar.gui.printer;
2
 
 
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;
9
 
 
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;
27
 
 
28
 
public class PrintDialog {
29
 
        private Shell parent;
30
 
        private Shell dialog;
31
 
        private int style;
32
 
        private PrintService[] printServices;
33
 
        private PrinterData printerData;
34
 
        
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;       
47
 
        
48
 
        public PrintDialog (Shell parent) {
49
 
                this (parent,SWT.DIALOG_TRIM |SWT.APPLICATION_MODAL);
50
 
        }
51
 
        
52
 
        public PrintDialog(Shell parent,int style){
53
 
                this.parent = parent;
54
 
                this.style = style;
55
 
                this.findServices();
56
 
        }
57
 
        
58
 
        public void findServices(){
59
 
                this.printServices = PrintServiceLookup.lookupPrintServices(null,null);
60
 
        }
61
 
        
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"));
66
 
                
67
 
                this.initComposites();
68
 
                
69
 
                this.dialog.pack();
70
 
                this.dialog.open();
71
 
 
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);          
75
 
                
76
 
                while (!dialog.isDisposed()) {
77
 
            if (!dialog.getDisplay().readAndDispatch()) {
78
 
                dialog.getDisplay().sleep();
79
 
            }
80
 
 
81
 
        }               
82
 
                return this.printerData;                
83
 
        }
84
 
        
85
 
        private void initComposites(){
86
 
                Composite composite = new Composite(this.dialog,SWT.NONE);
87
 
                composite.setLayout(new GridLayout(1,true));
88
 
                
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());
102
 
        }
103
 
        this.servicesCombo.select(0);
104
 
        this.servicesCombo.addSelectionListener(new SelectionAdapter() {
105
 
                        public void widgetSelected(SelectionEvent e) {
106
 
                                checkServiceValues();
107
 
                        }               
108
 
                });
109
 
        
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));
114
 
                
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));
119
 
                
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));
124
 
                                
125
 
                //print to file
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));
129
 
                                
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();
136
 
                        }               
137
 
                });
138
 
                
139
 
                this.filePath = new Text(services,SWT.BORDER);
140
 
                this.filePath.setLayoutData(new GridData(220,16));
141
 
                
142
 
        GridData chooserData = new GridData(SWT.LEFT,SWT.FILL,true,true);    
143
 
        chooserData.minimumWidth = 80;
144
 
        chooserData.minimumHeight = 25;                                 
145
 
                
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) {
151
 
                                 chooseFile();
152
 
                        }               
153
 
                });
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));
160
 
                
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"));
166
 
 
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();
174
 
                        }               
175
 
                });
176
 
                
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();
182
 
                        }               
183
 
                });
184
 
                
185
 
                this.fromRangePage = new Text(range,SWT.BORDER);
186
 
                this.fromRangePage.setLayoutData(new GridData(25,15));
187
 
                
188
 
                Label rangeToLabel = new Label(range,SWT.NONE);
189
 
                rangeToLabel.setText(TuxGuitar.getProperty("print.range.pages-to"));
190
 
                
191
 
                this.toRangePage = new Text(range,SWT.BORDER);          
192
 
                this.toRangePage.setLayoutData(new GridData(25,15));
193
 
                
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"));
200
 
                
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));        
212
 
        
213
 
        GridData data = new GridData(SWT.FILL,SWT.FILL,true,true);    
214
 
        data.minimumWidth = 80;
215
 
        data.minimumHeight = 25;   
216
 
        
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) {
222
 
                                updatePrinterData();
223
 
                                dialog.dispose();
224
 
                        }
225
 
                });
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) {
231
 
                                dialog.dispose();
232
 
                        }
233
 
                });
234
 
        }
235
 
        
236
 
 
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();
250
 
                }               
251
 
                this.servicesStatus.setText(status);
252
 
                this.servicesType.setText(type);
253
 
                this.servicesInfo.setText(info);
254
 
        }
255
 
        
256
 
        private void checkRangeComposites(){
257
 
                boolean rangePagesSelected = this.rangePages.getSelection();            
258
 
                this.fromRangePage.setEnabled(rangePagesSelected);
259
 
                this.toRangePage.setEnabled(rangePagesSelected);
260
 
        }
261
 
 
262
 
        
263
 
        private void checkPrintToFileComposites(){
264
 
                boolean selected = this.printToFile.getSelection();             
265
 
                this.filePath.setEnabled(selected);
266
 
                this.fileChooser.setEnabled(selected);
267
 
        }
268
 
        
269
 
        private void chooseFile(){
270
 
        String fileName = FileChooser.instance().open(dialog,FileChooser.ALL_FORMAT/*,null*/);
271
 
        if(fileName != null){
272
 
                this.filePath.setText(fileName);
273
 
        }
274
 
        }
275
 
        
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();
281
 
                }
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()){
287
 
                        try{
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){                                
294
 
                        }
295
 
                }
296
 
        }
297
 
}