package com.customfit.ctg.view.print; import com.customfit.ctg.controller.*; import com.customfit.ctg.view.*; import java.awt.print.*; import javax.swing.*; /** * A Print Preview frame to contain Printable objects. * * @author Drew */ public class PrintPreviewPanel extends SubPanel { Printable printComponent; JPanel tempPanel; /** Creates new form PrintPanel */ public PrintPreviewPanel(AbstractPrintPanel p) { initComponents(); this.printComponent = p; this.printPanel.setViewportView(p); } /** * Extends SubPanel functionality by returning the title as being the * same as on the panel itself. * @return The title in the panel. */ @Override public String getTitle() { //set title return "Print Preview"; } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { jScrollPaneOuter = new javax.swing.JScrollPane(); jDesktopPane1 = new javax.swing.JDesktopPane(); printPanel = new javax.swing.JScrollPane(); jToolBar1 = new javax.swing.JToolBar(); printButton = new javax.swing.JButton(); jSeparator1 = new javax.swing.JToolBar.Separator(); cancelButton = new javax.swing.JButton(); jScrollPaneOuter.setBackground(java.awt.SystemColor.controlDkShadow); jScrollPaneOuter.setBorder(null); jDesktopPane1.setBackground(java.awt.SystemColor.controlDkShadow); jDesktopPane1.addComponentListener(new java.awt.event.ComponentAdapter() { public void componentResized(java.awt.event.ComponentEvent evt) { jDesktopPane1ComponentResized(evt); } }); printPanel.setBorder(null); printPanel.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS); printPanel.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); printPanel.setBounds(0, 0, 420, 310); jDesktopPane1.add(printPanel, javax.swing.JLayeredPane.DEFAULT_LAYER); jScrollPaneOuter.setViewportView(jDesktopPane1); jToolBar1.setRollover(true); printButton.setText("Print"); printButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { printButtonActionPerformed(evt); } }); jToolBar1.add(printButton); jToolBar1.add(jSeparator1); cancelButton.setText("Close"); cancelButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { cancelButtonActionPerformed(evt); } }); jToolBar1.add(cancelButton); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); this.setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 410, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap()) .addComponent(jScrollPaneOuter, javax.swing.GroupLayout.DEFAULT_SIZE, 420, Short.MAX_VALUE) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jScrollPaneOuter, javax.swing.GroupLayout.DEFAULT_SIZE, 310, Short.MAX_VALUE)) ); }// </editor-fold>//GEN-END:initComponents private void printButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_printButtonActionPerformed PrinterJob job = PrinterJob.getPrinterJob(); job.setPrintable(this.printComponent); if(job.printDialog()){ try { job.print(); } catch(PrinterException pe){} Application.getMainFrame().goBack(); } }//GEN-LAST:event_printButtonActionPerformed private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed Application.getMainFrame().goBack(); this.setSize(WIDTH, WIDTH); }//GEN-LAST:event_cancelButtonActionPerformed private void jDesktopPane1ComponentResized(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_jDesktopPane1ComponentResized //make the scrollable panel with the print panel inside //80% of the height of the outer panel int previewPaneHeight = new Double(new Integer(this.jScrollPaneOuter.getVisibleRect().height).doubleValue() - 20.0).intValue(); //then the proportion of height int previewPaneWidth = new Double((new Integer(previewPaneHeight)).doubleValue() * 8.5 / 11.0).intValue(); if (previewPaneWidth > this.jScrollPaneOuter.getWidth()) { //if this happens, try swapping the order //80% of the width of the outer panel previewPaneWidth = new Double(new Integer(this.jScrollPaneOuter.getVisibleRect().width).doubleValue() - 20.0).intValue(); //then the proportion of width previewPaneHeight = new Double((new Integer(previewPaneHeight)).doubleValue() / 11.0 * 8.5).intValue(); } this.printPanel.setSize(previewPaneWidth, previewPaneHeight); //now figure out the X and Y positions int previewPanelX = (new Double(new Integer(this.jScrollPaneOuter.getVisibleRect().width).doubleValue()).intValue() - previewPaneWidth) / 2; int previewPanelY = (new Double(new Integer(this.jScrollPaneOuter.getVisibleRect().height).doubleValue()).intValue() - previewPaneHeight) / 2; this.printPanel.setLocation(previewPanelX, previewPanelY); System.out.println("W=" + previewPaneWidth + ";H=" + previewPaneHeight + ";X=" + previewPanelX + ";Y=" + previewPanelY); }//GEN-LAST:event_jDesktopPane1ComponentResized // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton cancelButton; private javax.swing.JDesktopPane jDesktopPane1; private javax.swing.JScrollPane jScrollPaneOuter; private javax.swing.JToolBar.Separator jSeparator1; private javax.swing.JToolBar jToolBar1; private javax.swing.JButton printButton; private javax.swing.JScrollPane printPanel; // End of variables declaration//GEN-END:variables // @Override // public void setSize(int width, int height) // { // int newHeight = new Double((new Integer(width).doubleValue() / 8.5) * 11.0).intValue(); // super.setSize(width, newHeight); // //do what you should //// super.setSize(width, height); // //but also set the size for our printable area so that it's in // //proportion to the paper (8.5x11) // int printHeight = this.jPanelOuter.getVisibleRect().height; // int printWidth = new Double((new Integer(printHeight)).doubleValue() / 8.5 * 11.0).intValue(); // this.printPanel.setSize(printWidth, printHeight); // } // // @Override // public void setSize(Dimension dimension) // { // setSize(dimension.width, dimension.height); // } }