1 /* 2 * Copyright (c) 2007, 2017, 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 /** 25 * @test 26 * @bug 4855801 6949753 27 * @requires (os.family == "Windows") | (os.family == "Linux") 28 * @summary Changing margins in the page format does not have any effect 29 * @run main/manual PDialogTest 30 */ 31 32 import java.awt.GridBagConstraints; 33 import java.awt.GridBagLayout; 34 import java.awt.event.ActionEvent; 35 import java.awt.print.PageFormat; 36 import java.awt.print.Paper; 37 import java.awt.print.PrinterException; 38 import java.util.concurrent.CountDownLatch; 39 import java.util.concurrent.TimeUnit; 40 import javax.swing.BorderFactory; 41 import javax.swing.Box; 42 import javax.swing.JButton; 43 import javax.swing.JFrame; 44 import javax.swing.JLabel; 45 import javax.swing.JPanel; 46 import javax.swing.JTextArea; 47 import javax.swing.SwingUtilities; 48 49 public class PDialogTest 50 { 51 public static void main(String args[]) throws Exception { 52 final CountDownLatch latch = new CountDownLatch(1); 53 54 // Move the test UI to a different thread so that the count down latch can work in an independent way. 55 TestUI test = new TestUI(latch); 56 Thread T1 = new Thread(test); 57 T1.start(); 58 59 SwingUtilities.invokeLater(new Runnable() { 60 @Override 61 public void run() { 62 try { 63 do_test(); 64 }catch(Exception e) { 65 66 } 67 } 68 }); 69 70 boolean status = latch.await(1, TimeUnit.MINUTES); 71 if (!status) { 72 throw new RuntimeException("Test timed out."); 73 } 74 75 if (test.testResult == false) { 76 throw new RuntimeException("Test Failed."); 77 } 78 79 SwingUtilities.invokeAndWait(() -> { 80 test.disposeUI(); 81 }); 82 } 83 84 public static void do_test() throws PrinterException { 85 PageFormat pageFormat = new PageFormat(); 86 87 createNewPrintPageSetup(pageFormat); 88 89 setValuesForPrintPageSetup(pageFormat, 2); 90 91 createNewPrintPageSetup(pageFormat); 92 93 setValuesForPrintPageSetup(pageFormat, 3); 94 95 createNewPrintPageSetup(pageFormat); 96 } 97 98 public static void createNewPrintPageSetup(PageFormat pageFormat) 99 throws PrinterException { 100 101 // Display printer default setup dialog 102 java.awt.print.PrinterJob.getPrinterJob().pageDialog(pageFormat); 103 } 104 105 public static void setValuesForPrintPageSetup(PageFormat pageFormat, 106 int marginValue) throws PrinterException { 107 Paper paper = new Paper(); 108 109 double paperHeight = paper.getHeight(); 110 double paperWidth = paper.getWidth(); 111 double paperX = paper.getImageableX(); 112 double paperY = paper.getImageableY(); 113 paper.setImageableArea(paperX * marginValue, paperY * marginValue, 114 paperWidth - (paperX * 2 * marginValue), 115 paperHeight - (paperY * 2 * marginValue)); 116 pageFormat.setPaper(paper); 117 118 java.awt.print.PrinterJob.getPrinterJob().pageDialog(pageFormat); 119 } 120 } 121 122 class TestUI implements Runnable { 123 124 private static JFrame mainFrame; 125 private static JPanel mainControlPanel; 126 127 private static JTextArea instructionTextArea; 128 129 private static JPanel resultButtonPanel; 130 private static JButton passButton; 131 private static JButton failButton; 132 133 private static JPanel testPanel; 134 private static JButton testButton; 135 private static JLabel buttonPressCountLabel; 136 137 private static GridBagLayout layout; 138 private final CountDownLatch latch; 139 public boolean testResult = false; 140 141 @Override 142 public void run() { 143 try { 144 createUI(); 145 }catch(Exception e) { 146 147 } 148 } 149 150 public TestUI(CountDownLatch latch) throws Exception { 151 this.latch = latch; 152 } 153 154 public final void createUI() { 155 156 mainFrame = new JFrame("PDialogTest"); 157 158 layout = new GridBagLayout(); 159 mainControlPanel = new JPanel(layout); 160 resultButtonPanel = new JPanel(layout); 161 testPanel = new JPanel(layout); 162 163 GridBagConstraints gbc = new GridBagConstraints(); 164 165 // Create Test instructions 166 String instructions 167 = "This test displays the print setup dialog with margins \n" 168 + "displayed as inches(Use equivalent conversion if different)\n" 169 + "\n" 170 + "Step 1: Verify that margins displays as 1 for left, right,\n" 171 + " top and bottom margins, then click OK.\n" 172 + "Step 2: Verify that margins displays as 2 for left, right,\n" 173 + " top and bottom margins, then click OK (A new value \n" 174 + " 2 has been set by setting a new imagable area).\n" 175 + "Step 3: Verify that margins dipslays as 2 for left, right,\n" 176 + " top and bottom margins, then click OK (The newly set\n" 177 + " imageable area retains even after closing in the\n" 178 + " previous step).\n" 179 + "Step 4: Verify that margins displays as 3 for left, right,\n" 180 + " top and bottom margins, then click OK (A new value \n" 181 + " 3 has been set by setting a new imagable area).\n" 182 + "Step 5: Verify that margins dipslays as 3 for left, right,\n" 183 + " top and bottom margins, then click OK (The newly set\n" 184 + " imageable area retains even after closing in the\n" 185 + " previous step).\n" 186 + "Click on 'pass' if successful else click on 'fail'\n"; 187 188 instructionTextArea = new JTextArea(); 189 instructionTextArea.setText(instructions); 190 instructionTextArea.setEditable(false); 191 instructionTextArea.setBorder(BorderFactory. 192 createTitledBorder("Test Instructions")); 193 194 gbc.gridx = 0; 195 gbc.gridy = 0; 196 gbc.fill = GridBagConstraints.HORIZONTAL; 197 mainControlPanel.add(instructionTextArea, gbc); 198 199 gbc.gridx = 0; 200 gbc.gridy = 1; 201 testPanel.add(Box.createVerticalStrut(50)); 202 203 mainControlPanel.add(testPanel); 204 205 // Create resultButtonPanel with Pass, Fail buttons 206 passButton = new JButton("Pass"); 207 passButton.setActionCommand("Pass"); 208 passButton.addActionListener((ActionEvent e) -> { 209 System.out.println("Pass Button pressed!"); 210 testResult = true; 211 latch.countDown(); 212 disposeUI(); 213 }); 214 215 failButton = new JButton("Fail"); 216 failButton.setActionCommand("Fail"); 217 failButton.addActionListener((ActionEvent e) -> { 218 System.out.println("Fail Button pressed!"); 219 testResult = false; 220 latch.countDown(); 221 disposeUI(); 222 }); 223 224 gbc.gridx = 0; 225 gbc.gridy = 0; 226 resultButtonPanel.add(passButton, gbc); 227 228 gbc.gridx = 1; 229 gbc.gridy = 0; 230 resultButtonPanel.add(failButton, gbc); 231 232 gbc.gridx = 0; 233 gbc.gridy = 1; 234 mainControlPanel.add(resultButtonPanel, gbc); 235 236 mainFrame.add(mainControlPanel); 237 mainFrame.pack(); 238 mainFrame.setVisible(true); 239 } 240 241 public void disposeUI() { 242 mainFrame.dispose(); 243 } 244 }