Path: blob/master/test/jdk/javax/swing/JTable/PrintManualTest_FitWidthMultiple.java
41152 views
/*1* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22/*23* @test24* @bug 817034925* @summary Verify if printed content is within border and all columns are26* printed for PrintMode.FIT_WIDTH27* @run main/manual PrintManualTest_FitWidthMultiple28*/2930import java.awt.BorderLayout;31import java.awt.event.ActionEvent;32import java.awt.event.ActionListener;33import java.awt.event.WindowAdapter;34import java.awt.event.WindowEvent;35import java.text.MessageFormat;36import java.util.concurrent.CountDownLatch;37import java.util.concurrent.TimeUnit;38import javax.print.attribute.HashPrintRequestAttributeSet;39import javax.print.attribute.PrintRequestAttributeSet;40import javax.swing.AbstractAction;41import javax.swing.JButton;42import javax.swing.JComponent;43import javax.swing.JFrame;44import javax.swing.JPanel;45import javax.swing.JScrollPane;46import javax.swing.JTable;47import javax.swing.JTextArea;48import javax.swing.KeyStroke;49import javax.swing.SwingUtilities;50import javax.swing.table.AbstractTableModel;51import javax.swing.table.TableModel;5253public class PrintManualTest_FitWidthMultiple extends JTable implements Runnable {5455static boolean testPassed;56static JFrame fr = null;57static JFrame instructFrame = null;58private final CountDownLatch latch;5960public PrintManualTest_FitWidthMultiple(CountDownLatch latch){61this.latch = latch;62}6364@Override65public void run() {66try {67createUIandTest();68} catch (Exception ex) {69dispose();70latch.countDown();71throw new RuntimeException(ex.getMessage());72}73}7475private void createUIandTest() throws Exception {76/*Message Format Header and Footer */77final MessageFormat header=new MessageFormat("JTable Printing Header {0}");78final MessageFormat footer = new MessageFormat("JTable Printing Footer {0}");7980SwingUtilities.invokeAndWait(new Runnable() {81@Override82public void run() {83/* Instructions Section */84String info =85" \nThis test case brings up JTable with more Columns and Rows \n"+86"Press the Print Button. It Prints in PRINT_MODE_FIT_WIDTH \n" +87"It Pops up the Print Dialog. Check if Job/Print Attributes in the\n" +88"Print Dialog are configurable. Default Print out will be in Landscape \n"+89"The Print out should have JTable Centered on the Print out with thin borders \n"+90"Prints out with Header and Footer. \n"+91"The JTable should have all columns printed within border";9293instructFrame=new JFrame("PrintManualTest_NormalSingle");94JPanel panel=new JPanel(new BorderLayout());95JButton button1 = new JButton("Pass");96JButton button2 = new JButton("Fail");97button1.addActionListener((e) -> {98testPassed = true;99dispose();100latch.countDown();101});102button2.addActionListener((e) -> {103testPassed = false;104dispose();105latch.countDown();106});107JPanel btnpanel1 = new JPanel();108btnpanel1.add(button1);109btnpanel1.add(button2);110panel.add(addInfo(info),BorderLayout.CENTER);111panel.add(btnpanel1, BorderLayout.SOUTH);112instructFrame.getContentPane().add(panel);113instructFrame.setBounds(600,100,350,350);114115/* Print Button */116final JButton printButton=new JButton("Print");117118/* Table Model */119final TableModel datamodel=new AbstractTableModel(){120@Override121public int getColumnCount() { return 50;}122@Override123public int getRowCount() { return 50; }124@Override125public Object getValueAt(int row, int column){ return new Integer(row*column);}126};127128/* Constructing the JTable */129final JTable table=new JTable(datamodel);130131/* Putting the JTable in ScrollPane and Frame Container */132JScrollPane scrollpane=new JScrollPane(table);133fr = new JFrame("PrintManualTest_FitWidthMultiple");134fr.getContentPane().add(scrollpane);135136/* Light Weight Panel for holding Print and other buttons */137JPanel btnpanel=new JPanel();138btnpanel.add(printButton);139fr.getContentPane().add(btnpanel,BorderLayout.SOUTH);140fr.setBounds(0,0,400,400);141fr.setSize(500,500);142143/* Binding the KeyStroke to Print Button Action */144fr.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("ctrl P"), "printButton");145fr.getRootPane().getActionMap().put("printButton", new AbstractAction(){146@Override147public void actionPerformed(ActionEvent e){148printButton.doClick();149}150});151152/* Container and Component Listeners */153fr.addWindowListener(new WindowAdapter() {154@Override155public void windowClosing(WindowEvent e) {156dispose();157if (testPassed == false) {158throw new RuntimeException(" User has not executed the test");159}160}161});162163final PrintRequestAttributeSet prattr=new HashPrintRequestAttributeSet();164prattr.add(javax.print.attribute.standard.OrientationRequested.LANDSCAPE);165166printButton.addActionListener(new ActionListener(){167@Override168public void actionPerformed(ActionEvent ae){169try{170table.print(JTable.PrintMode.FIT_WIDTH, header,footer,true,prattr,true);171} catch(Exception e){}172}173});174instructFrame.setVisible(true);175fr.setVisible(true);176}177});178}179180public void dispose() {181instructFrame.dispose();182fr.dispose();183}184185public JScrollPane addInfo(String info) {186JTextArea jta = new JTextArea(info,8,20);187jta.setEditable(false);188jta.setLineWrap(true);189JScrollPane sp = new JScrollPane(jta);190return sp;191192}193194/* Main Method */195196public static void main(String[] argv) throws Exception {197final CountDownLatch latch = new CountDownLatch(1);198PrintManualTest_FitWidthMultiple test = new PrintManualTest_FitWidthMultiple(latch);199Thread T1 = new Thread(test);200T1.start();201202// wait for latch to complete203boolean ret = false;204try {205ret = latch.await(60, TimeUnit.SECONDS);206} catch (InterruptedException ie) {207throw ie;208}209if (!ret) {210test.dispose();211throw new RuntimeException(" User has not executed the test");212}213if (test.testPassed == false) {214throw new RuntimeException("printed contents is beyond borders");215}216}217}218219220