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

« back to all changes in this revision

Viewing changes to TuxGuitar/src/org/herac/tuxguitar/gui/printer/PrintPreview.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 java.util.List;
 
4
 
 
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;
 
28
 
 
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;
 
35
        
 
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;
 
43
        protected List pages;
 
44
        protected int currentPage;
 
45
        
 
46
        public PrintPreview(List pages,Rectangle bounds){
 
47
                this.pages = pages;
 
48
                this.bounds = bounds;
 
49
        }
 
50
        
 
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"));
 
55
                
 
56
                this.initToolBar();
 
57
                this.initPreviewComposite();
 
58
                this.changePage(0);
 
59
                
 
60
                DialogUtils.openDialog(this.dialog, DialogUtils.OPEN_STYLE_MAXIMIZED | DialogUtils.OPEN_STYLE_WAIT);
 
61
        }
 
62
        
 
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));
 
67
                
 
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);
 
73
                
 
74
                Button close = new Button(composite,SWT.PUSH);
 
75
                close.setLayoutData(getButtonData());
 
76
                
 
77
                this.currentText.addKeyListener(new KeyAdapter() {
 
78
                        public void keyReleased(KeyEvent e) {
 
79
                                if(e.keyCode == KeyBindingConstants.ENTER){
 
80
                                        try{
 
81
                                                Integer number = new Integer(PrintPreview.this.currentText.getText());
 
82
                                                changePage(number.intValue() - 1);
 
83
                                        }catch(NumberFormatException exception){
 
84
                                                changePage(PrintPreview.this.currentPage);
 
85
                                        }
 
86
                                }
 
87
                        }
 
88
                });
 
89
                this.previous.addSelectionListener(new SelectionAdapter() {
 
90
                        public void widgetSelected(SelectionEvent e) {
 
91
                                if(PrintPreview.this.currentPage >= 0){
 
92
                                        changePage(PrintPreview.this.currentPage - 1);
 
93
                                }
 
94
                        }
 
95
                });
 
96
                this.next.addSelectionListener(new SelectionAdapter() {
 
97
                        public void widgetSelected(SelectionEvent e) {
 
98
                                if(PrintPreview.this.currentPage >= 0){
 
99
                                        changePage(PrintPreview.this.currentPage + 1);
 
100
                                }
 
101
                        }
 
102
                });
 
103
                close.addSelectionListener(new SelectionAdapter() {
 
104
                        public void widgetSelected(SelectionEvent e) {
 
105
                                PrintPreview.this.dialog.dispose();
 
106
                        }
 
107
                });
 
108
                maxPages.setText(TuxGuitar.getProperty("print.preview.page-of") + " " + this.pages.size());
 
109
                close.setText(TuxGuitar.getProperty("close"));
 
110
        }
 
111
        
 
112
        private GridData getButtonData(){
 
113
                GridData data = new GridData(SWT.RIGHT, SWT.FILL, true, true);
 
114
                data.minimumWidth = 80;
 
115
                data.minimumHeight = 25;
 
116
                return data;
 
117
        }
 
118
        
 
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){
 
131
                                        updateScroll();
 
132
                                        
 
133
                                        int vScroll = PrintPreview.this.previewComposite.getVerticalBar().getSelection();
 
134
                                        
 
135
                                        TGPainter painter = new TGPainter(e.gc);
 
136
                                        painter.drawImage((Image)PrintPreview.this.pages.get(PrintPreview.this.currentPage),MARGIN_LEFT,MARGIN_TOP - vScroll);
 
137
                                }
 
138
                        }
 
139
                });
 
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();
 
152
                        }
 
153
                });
 
154
        }
 
155
        
 
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));
 
161
        }
 
162
        
 
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 ));
 
172
                        }
 
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();
 
177
                }else{
 
178
                        this.currentText.setEnabled(false);
 
179
                        this.previous.setEnabled(false);
 
180
                        this.next.setEnabled(false);
 
181
                }
 
182
                
 
183
        }
 
184
        
 
185
}