1
2
3
4
5
6
7
8
9
10
11 package de.jaret.util.ui.table.print;
12
13 import org.eclipse.jface.dialogs.Dialog;
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.custom.CCombo;
16 import org.eclipse.swt.events.SelectionEvent;
17 import org.eclipse.swt.events.SelectionListener;
18 import org.eclipse.swt.graphics.Point;
19 import org.eclipse.swt.layout.GridData;
20 import org.eclipse.swt.layout.GridLayout;
21 import org.eclipse.swt.layout.RowLayout;
22 import org.eclipse.swt.printing.PrintDialog;
23 import org.eclipse.swt.printing.Printer;
24 import org.eclipse.swt.printing.PrinterData;
25 import org.eclipse.swt.widgets.Button;
26 import org.eclipse.swt.widgets.Composite;
27 import org.eclipse.swt.widgets.Control;
28 import org.eclipse.swt.widgets.Display;
29 import org.eclipse.swt.widgets.Label;
30 import org.eclipse.swt.widgets.Scale;
31 import org.eclipse.swt.widgets.Shell;
32
33 import de.jaret.util.ui.table.JaretTablePrinter;
34
35 /***
36 * Simple print dialog for a jaret table.
37 *
38 * @author Peter Kliem
39 * @version $Id: JaretTablePrintDialog.java 179 2007-01-07 17:37:50Z olk $
40 */
41 public class JaretTablePrintDialog extends Dialog {
42 protected static PrinterData _printerData;
43 protected int _pIdx = -1;
44 protected String[] _printers;
45 protected PrinterData[] _pdatas;
46 protected CCombo _printerCombo;
47 protected JaretTablePrintConfiguration _configuration;
48 protected Button _repeatHeader;
49 protected Label _pagesLabel;
50 protected JaretTablePrinter _tablePrinter;
51
52 public JaretTablePrintDialog(Shell parentShell, String printerName, JaretTablePrinter tablePrinter,
53 JaretTablePrintConfiguration printConfiguration) {
54 super(parentShell);
55 _tablePrinter = tablePrinter;
56 _configuration = printConfiguration;
57 if (_configuration == null) {
58 _configuration = new JaretTablePrintConfiguration("table", false, 1.0);
59 }
60 if (printerName == null && _printerData != null) {
61 printerName = _printerData.name;
62 }
63
64 _pdatas = Printer.getPrinterList();
65 _printers = new String[_pdatas.length];
66 int stdIdx = -1;
67 for (int i = 0; i < _pdatas.length; i++) {
68 PrinterData pd = _pdatas[i];
69 _printers[i] = pd.name;
70 if (printerName != null && pd.name.equals(printerName)) {
71 _pIdx = i;
72 }
73 if (pd.name.equals(Printer.getDefaultPrinterData().name)) {
74 stdIdx = i;
75 }
76 }
77 if (_pIdx == -1) {
78 _printerData = Printer.getDefaultPrinterData();
79 _pIdx = stdIdx;
80 } else {
81 _printerData = _pdatas[_pIdx];
82 }
83 }
84
85 public void setRowLimit(int limit) {
86 _configuration.setRowLimit(limit);
87 }
88
89 public void setColLimit(int limit) {
90 _configuration.setColLimit(limit);
91 }
92
93 public JaretTablePrintConfiguration getConfiguration() {
94 return _configuration;
95 }
96
97 @Override
98 protected void configureShell(Shell newShell) {
99 super.configureShell(newShell);
100 newShell.setText("Print");
101 }
102
103 @Override
104 protected Control createDialogArea(Composite parent) {
105 Composite dialogArea = new Composite(parent, SWT.NULL);
106
107 GridData gd1 = new GridData(GridData.FILL_BOTH);
108 dialogArea.setLayoutData(gd1);
109 GridLayout gl = new GridLayout();
110 gl.numColumns = 1;
111 dialogArea.setLayout(gl);
112
113 createPrinterSelection(dialogArea);
114
115 Composite parameterArea = new Composite(dialogArea, SWT.NULL);
116 GridData gd = new GridData(GridData.FILL_BOTH);
117 parameterArea.setLayoutData(gd);
118 createParameterArea(parameterArea);
119 return dialogArea;
120 }
121
122 @Override
123 protected void okPressed() {
124 _printerData = _pdatas[_printerCombo.getSelectionIndex()];
125 super.okPressed();
126 }
127
128 private void createPrinterSelection(Composite parent) {
129 Composite area = new Composite(parent, SWT.NULL);
130 area.setLayout(new RowLayout());
131
132
133 _printerCombo = new CCombo(area, SWT.BORDER | SWT.READ_ONLY);
134 _printerCombo.setItems(_printers);
135 _printerCombo.select(_pIdx);
136
137 Button select = new Button(area, SWT.PUSH);
138 select.setText("Configure");
139 select.addSelectionListener(new SelectionListener() {
140
141 public void widgetSelected(SelectionEvent e) {
142 PrintDialog pd = new PrintDialog(Display.getCurrent().getActiveShell());
143 PrinterData pdata = pd.open();
144 if (pdata != null) {
145 _printerData = pdata;
146 select(_printerData);
147 }
148 }
149
150 private void select(PrinterData printerData) {
151 for (int i = 0; i < _pdatas.length; i++) {
152 PrinterData pd = _pdatas[i];
153 if (pd.name.equals(printerData.name)) {
154 _printerCombo.select(i);
155 break;
156 }
157 }
158
159 }
160
161 public void widgetDefaultSelected(SelectionEvent e) {
162 }
163
164 });
165
166 }
167
168 protected void createParameterArea(Composite parent) {
169 GridLayout gl = new GridLayout();
170 gl.numColumns = 2;
171 parent.setLayout(gl);
172
173 _repeatHeader = new Button(parent, SWT.CHECK);
174 _repeatHeader.setSelection(_configuration.getRepeatHeader());
175 _repeatHeader.setText("Repeat header");
176 GridData gd = new GridData(GridData.FILL_HORIZONTAL);
177 gd.horizontalSpan = 2;
178 _repeatHeader.setLayoutData(gd);
179
180 final Label scaleText = new Label(parent, SWT.RIGHT);
181 scaleText.setText(getScaleText());
182 gd = new GridData(GridData.FILL_HORIZONTAL);
183 gd.horizontalSpan = 2;
184 scaleText.setLayoutData(gd);
185
186 final Scale scale = new Scale(parent, SWT.HORIZONTAL);
187 gd = new GridData(GridData.FILL_HORIZONTAL);
188 gd.horizontalSpan = 2;
189 scale.setLayoutData(gd);
190 scale.setMaximum(1000);
191 scale.setMinimum(10);
192 scale.setSelection((int) (_configuration.getScale() * 100));
193 scale.addSelectionListener(new SelectionListener() {
194 public void widgetSelected(SelectionEvent ev) {
195 int val = scale.getSelection();
196 double s = (double) val / 100.0;
197 _configuration.setScale(s);
198 scaleText.setText(getScaleText());
199 updateConf();
200 }
201
202 public void widgetDefaultSelected(SelectionEvent arg0) {
203 }
204 });
205
206 _pagesLabel = new Label(parent, SWT.RIGHT);
207 gd = new GridData(GridData.FILL_HORIZONTAL);
208 gd.horizontalSpan = 2;
209 _pagesLabel.setLayoutData(gd);
210 _printerData = _pdatas[_printerCombo.getSelectionIndex()];
211 Printer printer = new Printer(_printerData);
212 _tablePrinter.setPrinter(printer);
213 Point pages = _tablePrinter.calculatePageCount(_configuration);
214 printer.dispose();
215 _pagesLabel.setText(getPagesText(pages));
216
217 }
218
219 private String getScaleText() {
220 int pc = (int) (_configuration.getScale() * 100);
221 return Integer.toString(pc) + "%";
222 }
223
224 private String getPagesText(Point pages) {
225 return "X: " + pages.x + " Y: " + pages.y + " (" + pages.x * pages.y + " pages)";
226 }
227
228 private void updateConf() {
229 _configuration.setRepeatHeader(_repeatHeader.getSelection());
230 Printer printer = new Printer(_printerData);
231 _tablePrinter.setPrinter(printer);
232 Point pages = _tablePrinter.calculatePageCount(_configuration);
233 printer.dispose();
234 _pagesLabel.setText(getPagesText(pages));
235 }
236
237 public PrinterData getPrinterData() {
238 return _printerData;
239 }
240
241 }