View Javadoc

1   /*
2    *  UnBBayes
3    *  Copyright (C) 2002, 2008 Universidade de Brasilia - http://www.unb.br
4    *
5    *  This file is part of UnBBayes.
6    *
7    *  UnBBayes is free software: you can redistribute it and/or modify
8    *  it under the terms of the GNU General Public License as published by
9    *  the Free Software Foundation, either version 3 of the License, or
10   *  (at your option) any later version.
11   *
12   *  UnBBayes is distributed in the hope that it will be useful,
13   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   *  GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License
18   *  along with UnBBayes.  If not, see <http://www.gnu.org/licenses/>.
19   *
20   */
21  package unbbayes.controller;
22  
23  import java.awt.BorderLayout;
24  import java.awt.Dimension;
25  import java.awt.FlowLayout;
26  import java.awt.GridLayout;
27  import java.awt.event.ActionEvent;
28  import java.awt.event.ActionListener;
29  import java.awt.print.Pageable;
30  import java.util.ResourceBundle;
31  
32  import javax.swing.BorderFactory;
33  import javax.swing.JButton;
34  import javax.swing.JPanel;
35  import javax.swing.JScrollPane;
36  import javax.swing.JTextField;
37  import javax.swing.border.BevelBorder;
38  
39  /**
40   * Creates a pre-visualization of a printing
41   *
42   */
43  public class PrintPreviewer extends JPanel {
44  
45    /** Serialization runtime version number */
46    private static final long serialVersionUID = 0;	
47  	
48    protected Pageable pageable;
49    protected PrintComponent printComponent;
50    protected int pageIndex;
51  
52    protected JScrollPane scrollPane;
53    protected JButton previousButton;
54    protected JButton nextButton;
55    protected JButton sizeButton;
56    protected JTextField scaleText;
57  
58    /** Load resource file from this package */
59    private static ResourceBundle resource = unbbayes.util.ResourceController.newInstance().getBundle(
60  		  unbbayes.controller.resources.ControllerResources.class.getName());
61  
62    public PrintPreviewer(Pageable p, int page) {
63      pageable = p;
64      pageIndex = page;
65      printComponent = new PrintComponent(null, null);
66      printComponent.setBorder(BorderFactory.createBevelBorder(
67          BevelBorder.RAISED));
68      buildLayout();
69      displayPage(pageIndex);
70    }
71  
72    /**
73     * Adds the appropriate components to this panel
74     */
75    protected void buildLayout() {
76      setLayout(new BorderLayout());
77      JPanel panel = new JPanel();
78      panel.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
79      panel.add(printComponent);
80      scrollPane = new JScrollPane(panel);
81      add(scrollPane, BorderLayout.CENTER);
82      add(getBottomPanel(), BorderLayout.SOUTH);
83      addListeners();
84    }
85  
86    /**
87     * Returns a panel that contains the buttons supported by this interface
88     */
89    protected JPanel getBottomPanel() {
90      JPanel outer = new JPanel();
91      outer.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 0));
92      JPanel inner = new JPanel();
93      inner.setLayout(new GridLayout(1, 2, 10, 0));
94      previousButton = new JButton(resource.getString("previewButtonLabel"));
95      inner.add(previousButton);
96      nextButton = new JButton(resource.getString("nextButtonLabel"));
97      inner.add(nextButton);
98      outer.add(inner);
99      scaleText = new JTextField(3);
100     outer.add(scaleText);
101     sizeButton = new JButton(resource.getString("fitToPageButtonLabel"));
102     outer.add(sizeButton);
103     return outer;
104   }
105 
106   /**
107    * Adds listeners to the buttons and the text field
108    */
109   protected void addListeners() {
110     previousButton.addActionListener(new ActionListener() {
111       public void actionPerformed(ActionEvent event) {
112         displayPage(pageIndex - 1);
113       }
114     });
115     nextButton.addActionListener(new ActionListener() {
116       public void actionPerformed(ActionEvent event) {
117         displayPage(pageIndex + 1);
118       }
119     });
120     sizeButton.addActionListener(new ActionListener() {
121       public void actionPerformed(ActionEvent event) {
122         sizeToFit();
123       }
124     });
125     scaleText.addActionListener(new ActionListener() {
126       public void actionPerformed(ActionEvent event) {
127         try {
128           int scale = Integer.parseInt(
129               scaleText.getText());
130           printComponent.setScaleFactor(scale);
131         } catch (NumberFormatException nfe) {};
132       }
133     });
134   }
135 
136   /**
137    * Displays the specified page within the panel
138    */
139   protected void displayPage(int index) {
140     pageIndex = index;
141     printComponent.setPrintable(pageable.getPrintable(pageIndex));
142     printComponent.setPageFormat(pageable.getPageFormat(pageIndex));
143     printComponent.setDisplayPage(index);
144     previousButton.setEnabled(pageIndex > 0);
145     nextButton.setEnabled(pageIndex <
146         (pageable.getNumberOfPages() - 1));
147     repaint();
148   }
149 
150   /**
151    * Determine the largest scale factor that can be used that will
152    * allow the current page to be displayed completely. In other words,
153    * make the page as large as possible without making it necessary for
154    * the user to use scroll bars to view the entire page. This is
155    * accomplished by calculating the ratios that represent the actual
156    * width of the page relative to the available width, and the actual
157    * height of the page to the available height. The smaller of the two
158    * values will dictate how large the ratio can be set while still
159    * allowing the entire page to be displayed.
160    */
161   protected void sizeToFit() {
162     int newScaleFactor;
163     Dimension compSize = printComponent.getSizeWithScale(100d);
164     Dimension viewSize = scrollPane.getSize();
165 
166     int scaleX = (viewSize.width - 25) * 100 / compSize.width;
167     int scaleY = (viewSize.height - 25) * 100 / compSize.height;
168     newScaleFactor = Math.min(scaleX, scaleY);
169 
170     printComponent.setScaleFactor(newScaleFactor);
171     scaleText.setText(Integer.toString(newScaleFactor));
172     repaint();
173   }
174 
175 }