1
package org.herac.tuxguitar.gui.printer;
5
import org.eclipse.swt.SWT;
6
import org.eclipse.swt.events.KeyAdapter;
7
import org.eclipse.swt.events.KeyEvent;
8
import org.eclipse.swt.events.PaintEvent;
9
import org.eclipse.swt.events.PaintListener;
10
import org.eclipse.swt.events.SelectionAdapter;
11
import org.eclipse.swt.events.SelectionEvent;
12
import org.eclipse.swt.graphics.Image;
13
import org.eclipse.swt.graphics.Rectangle;
14
import org.eclipse.swt.layout.GridData;
15
import org.eclipse.swt.layout.GridLayout;
16
import org.eclipse.swt.widgets.Button;
17
import org.eclipse.swt.widgets.Composite;
18
import org.eclipse.swt.widgets.Event;
19
import org.eclipse.swt.widgets.Label;
20
import org.eclipse.swt.widgets.Listener;
21
import org.eclipse.swt.widgets.ScrollBar;
22
import org.eclipse.swt.widgets.Shell;
23
import org.eclipse.swt.widgets.Text;
24
import org.herac.tuxguitar.gui.TuxGuitar;
25
import org.herac.tuxguitar.gui.editors.TGPainter;
26
import org.herac.tuxguitar.gui.system.keybindings.KeyBindingConstants;
27
import org.herac.tuxguitar.gui.util.DialogUtils;
29
public class PrintPreview{
30
private static final int SCROLL_INCREMENT = 50;
31
private static final int MARGIN_TOP = 20;
32
private static final int MARGIN_BOTTOM = 40;
33
private static final int MARGIN_LEFT = 50;
34
private static final int MARGIN_RIGHT = 20;
36
protected Shell dialog;
37
protected Composite previewComposite;
38
protected Composite pageComposite;
39
protected Text currentText;
40
protected Button previous;
41
protected Button next;
42
protected Rectangle bounds;
44
protected int currentPage;
46
public PrintPreview(List pages,Rectangle bounds){
51
public void showPreview(Shell parent){
52
this.dialog = DialogUtils.newDialog(parent,SWT.SHELL_TRIM | SWT.APPLICATION_MODAL );
53
this.dialog.setLayout(new GridLayout());
54
this.dialog.setText(TuxGuitar.getProperty("print.preview"));
57
this.initPreviewComposite();
60
DialogUtils.openDialog(this.dialog, DialogUtils.OPEN_STYLE_MAXIMIZED | DialogUtils.OPEN_STYLE_WAIT);
63
private void initToolBar(){
64
Composite composite = new Composite(this.dialog,SWT.NONE);
65
composite.setLayout(new GridLayout(5,false));
66
composite.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,false));
68
this.previous = new Button(composite,SWT.ARROW | SWT.LEFT);
69
this.currentText = new Text(composite,SWT.BORDER);
70
this.currentText.setLayoutData(new GridData(25,SWT.DEFAULT));
71
this.next = new Button(composite,SWT.ARROW | SWT.RIGHT);
72
Label maxPages = new Label(composite,SWT.NONE);
74
Button close = new Button(composite,SWT.PUSH);
75
close.setLayoutData(getButtonData());
77
this.currentText.addKeyListener(new KeyAdapter() {
78
public void keyReleased(KeyEvent e) {
79
if(e.keyCode == KeyBindingConstants.ENTER){
81
Integer number = new Integer(PrintPreview.this.currentText.getText());
82
changePage(number.intValue() - 1);
83
}catch(NumberFormatException exception){
84
changePage(PrintPreview.this.currentPage);
89
this.previous.addSelectionListener(new SelectionAdapter() {
90
public void widgetSelected(SelectionEvent e) {
91
if(PrintPreview.this.currentPage >= 0){
92
changePage(PrintPreview.this.currentPage - 1);
96
this.next.addSelectionListener(new SelectionAdapter() {
97
public void widgetSelected(SelectionEvent e) {
98
if(PrintPreview.this.currentPage >= 0){
99
changePage(PrintPreview.this.currentPage + 1);
103
close.addSelectionListener(new SelectionAdapter() {
104
public void widgetSelected(SelectionEvent e) {
105
PrintPreview.this.dialog.dispose();
108
maxPages.setText(TuxGuitar.getProperty("print.preview.page-of") + " " + this.pages.size());
109
close.setText(TuxGuitar.getProperty("close"));
112
private GridData getButtonData(){
113
GridData data = new GridData(SWT.RIGHT, SWT.FILL, true, true);
114
data.minimumWidth = 80;
115
data.minimumHeight = 25;
119
private void initPreviewComposite(){
120
this.previewComposite = new Composite(this.dialog,SWT.BORDER | SWT.V_SCROLL);
121
this.previewComposite.setLayout(new GridLayout());
122
this.previewComposite.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
123
this.previewComposite.setBackground(this.previewComposite.getDisplay().getSystemColor(SWT.COLOR_GRAY));
124
this.previewComposite.setFocus();
125
this.pageComposite = new Composite(this.previewComposite,SWT.BORDER | SWT.DOUBLE_BUFFERED);
126
this.pageComposite.setLayout(new GridLayout());
127
this.pageComposite.setBackground(this.previewComposite.getDisplay().getSystemColor(SWT.COLOR_WHITE));
128
this.pageComposite.addPaintListener(new PaintListener() {
129
public void paintControl(PaintEvent e) {
130
if(PrintPreview.this.currentPage >= 0){
133
int vScroll = PrintPreview.this.previewComposite.getVerticalBar().getSelection();
135
TGPainter painter = new TGPainter(e.gc);
136
painter.drawImage((Image)PrintPreview.this.pages.get(PrintPreview.this.currentPage),MARGIN_LEFT,MARGIN_TOP - vScroll);
140
GridData pageData = new GridData();
141
pageData.horizontalAlignment = SWT.CENTER;
142
pageData.verticalAlignment = SWT.CENTER;
143
pageData.grabExcessHorizontalSpace = true;
144
pageData.grabExcessVerticalSpace = true;
145
pageData.widthHint = (this.bounds.width - this.bounds.x) + (MARGIN_LEFT + MARGIN_RIGHT);
146
pageData.heightHint = (this.bounds.height - this.bounds.y) + (MARGIN_TOP + MARGIN_BOTTOM);
147
this.pageComposite.setLayoutData(pageData);
148
this.previewComposite.getVerticalBar().setIncrement(SCROLL_INCREMENT);
149
this.previewComposite.getVerticalBar().addListener(SWT.Selection, new Listener() {
150
public void handleEvent(Event e) {
151
PrintPreview.this.pageComposite.redraw();
156
protected void updateScroll(){
157
ScrollBar vBar = this.previewComposite.getVerticalBar();
158
Rectangle client = this.pageComposite.getClientArea();
159
vBar.setMaximum((this.bounds.height - this.bounds.y) + (MARGIN_TOP + MARGIN_BOTTOM));
160
vBar.setThumb(Math.min((this.bounds.height - this.bounds.y) + (MARGIN_TOP + MARGIN_BOTTOM), client.height));
163
protected void changePage(int index){
164
if(!this.pages.isEmpty()){
165
int pageCount = this.pages.size();
166
if(index >= 0 && index < pageCount){
167
this.currentPage = index;
168
this.currentText.setText(Integer.toString(index + 1));
169
this.pageComposite.redraw();
170
}else if(this.currentPage >= 0 && this.currentPage < pageCount){
171
this.currentText.setText(Integer.toString(this.currentPage + 1 ));
173
this.previous.setEnabled(this.currentPage > 0);
174
this.next.setEnabled((this.currentPage + 1) < pageCount);
175
this.previewComposite.getVerticalBar().setSelection(0);
176
this.previewComposite.setFocus();
178
this.currentText.setEnabled(false);
179
this.previous.setEnabled(false);
180
this.next.setEnabled(false);