1/* 2 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23/* 24 * @test 25 * @bug 8170349 26 * @summary Verify if printed content is within border and all columns are 27 * printed for PrintMode.FIT_WIDTH 28 * @run main/manual PrintManualTest_FitWidthMultiple 29 */ 30 31import java.awt.BorderLayout; 32import java.awt.event.ActionEvent; 33import java.awt.event.ActionListener; 34import java.awt.event.WindowAdapter; 35import java.awt.event.WindowEvent; 36import java.text.MessageFormat; 37import java.util.concurrent.CountDownLatch; 38import java.util.concurrent.TimeUnit; 39import javax.print.attribute.HashPrintRequestAttributeSet; 40import javax.print.attribute.PrintRequestAttributeSet; 41import javax.swing.AbstractAction; 42import javax.swing.JButton; 43import javax.swing.JComponent; 44import javax.swing.JFrame; 45import javax.swing.JPanel; 46import javax.swing.JScrollPane; 47import javax.swing.JTable; 48import javax.swing.JTextArea; 49import javax.swing.KeyStroke; 50import javax.swing.SwingUtilities; 51import javax.swing.table.AbstractTableModel; 52import javax.swing.table.TableModel; 53 54public class PrintManualTest_FitWidthMultiple extends JTable implements Runnable { 55 56 static boolean testPassed; 57 static JFrame fr = null; 58 static JFrame instructFrame = null; 59 private final CountDownLatch latch; 60 61 public PrintManualTest_FitWidthMultiple(CountDownLatch latch){ 62 this.latch = latch; 63 } 64 65 @Override 66 public void run() { 67 try { 68 createUIandTest(); 69 } catch (Exception ex) { 70 dispose(); 71 latch.countDown(); 72 throw new RuntimeException(ex.getMessage()); 73 } 74 } 75 76 private void createUIandTest() throws Exception { 77 /*Message Format Header and Footer */ 78 final MessageFormat header=new MessageFormat("JTable Printing Header {0}"); 79 final MessageFormat footer = new MessageFormat("JTable Printing Footer {0}"); 80 81 SwingUtilities.invokeAndWait(new Runnable() { 82 @Override 83 public void run() { 84 /* Instructions Section */ 85 String info = 86 " \nThis test case brings up JTable with more Columns and Rows \n"+ 87 "Press the Print Button. It Prints in PRINT_MODE_FIT_WIDTH \n" + 88 "It Pops up the Print Dialog. Check if Job/Print Attributes in the\n" + 89 "Print Dialog are configurable. Default Print out will be in Landscape \n"+ 90 "The Print out should have JTable Centered on the Print out with thin borders \n"+ 91 "Prints out with Header and Footer. \n"+ 92 "The JTable should have all columns printed within border"; 93 94 instructFrame=new JFrame("PrintManualTest_NormalSingle"); 95 JPanel panel=new JPanel(new BorderLayout()); 96 JButton button1 = new JButton("Pass"); 97 JButton button2 = new JButton("Fail"); 98 button1.addActionListener((e) -> { 99 testPassed = true; 100 dispose(); 101 latch.countDown(); 102 }); 103 button2.addActionListener((e) -> { 104 testPassed = false; 105 dispose(); 106 latch.countDown(); 107 }); 108 JPanel btnpanel1 = new JPanel(); 109 btnpanel1.add(button1); 110 btnpanel1.add(button2); 111 panel.add(addInfo(info),BorderLayout.CENTER); 112 panel.add(btnpanel1, BorderLayout.SOUTH); 113 instructFrame.getContentPane().add(panel); 114 instructFrame.setBounds(600,100,350,350); 115 116 /* Print Button */ 117 final JButton printButton=new JButton("Print"); 118 119 /* Table Model */ 120 final TableModel datamodel=new AbstractTableModel(){ 121 @Override 122 public int getColumnCount() { return 50;} 123 @Override 124 public int getRowCount() { return 50; } 125 @Override 126 public Object getValueAt(int row, int column){ return new Integer(row*column);} 127 }; 128 129 /* Constructing the JTable */ 130 final JTable table=new JTable(datamodel); 131 132 /* Putting the JTable in ScrollPane and Frame Container */ 133 JScrollPane scrollpane=new JScrollPane(table); 134 fr = new JFrame("PrintManualTest_FitWidthMultiple"); 135 fr.getContentPane().add(scrollpane); 136 137 /* Light Weight Panel for holding Print and other buttons */ 138 JPanel btnpanel=new JPanel(); 139 btnpanel.add(printButton); 140 fr.getContentPane().add(btnpanel,BorderLayout.SOUTH); 141 fr.setBounds(0,0,400,400); 142 fr.setSize(500,500); 143 144 /* Binding the KeyStroke to Print Button Action */ 145 fr.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("ctrl P"), "printButton"); 146 fr.getRootPane().getActionMap().put("printButton", new AbstractAction(){ 147 @Override 148 public void actionPerformed(ActionEvent e){ 149 printButton.doClick(); 150 } 151 }); 152 153 /* Container and Component Listeners */ 154 fr.addWindowListener(new WindowAdapter() { 155 @Override 156 public void windowClosing(WindowEvent e) { 157 dispose(); 158 if (testPassed == false) { 159 throw new RuntimeException(" User has not executed the test"); 160 } 161 } 162 }); 163 164 final PrintRequestAttributeSet prattr=new HashPrintRequestAttributeSet(); 165 prattr.add(javax.print.attribute.standard.OrientationRequested.LANDSCAPE); 166 167 printButton.addActionListener(new ActionListener(){ 168 @Override 169 public void actionPerformed(ActionEvent ae){ 170 try{ 171 table.print(JTable.PrintMode.FIT_WIDTH, header,footer,true,prattr,true); 172 } catch(Exception e){} 173 } 174 }); 175 instructFrame.setVisible(true); 176 fr.setVisible(true); 177 } 178 }); 179 } 180 181 public void dispose() { 182 instructFrame.dispose(); 183 fr.dispose(); 184 } 185 186 public JScrollPane addInfo(String info) { 187 JTextArea jta = new JTextArea(info,8,20); 188 jta.setEditable(false); 189 jta.setLineWrap(true); 190 JScrollPane sp = new JScrollPane(jta); 191 return sp; 192 193 } 194 195 /* Main Method */ 196 197 public static void main(String[] argv) throws Exception { 198 final CountDownLatch latch = new CountDownLatch(1); 199 PrintManualTest_FitWidthMultiple test = new PrintManualTest_FitWidthMultiple(latch); 200 Thread T1 = new Thread(test); 201 T1.start(); 202 203 // wait for latch to complete 204 boolean ret = false; 205 try { 206 ret = latch.await(60, TimeUnit.SECONDS); 207 } catch (InterruptedException ie) { 208 throw ie; 209 } 210 if (!ret) { 211 test.dispose(); 212 throw new RuntimeException(" User has not executed the test"); 213 } 214 if (test.testPassed == false) { 215 throw new RuntimeException("printed contents is beyond borders"); 216 } 217 } 218} 219