|
| 1 | +/* |
| 2 | + * Copyright (c) 2002, 2025, 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 4519537 4522866 |
| 26 | + * @summary Tests that text and components in paragraph views line up at their baselines. |
| 27 | + * @library /java/awt/regtesthelpers |
| 28 | + * @build PassFailJFrame |
| 29 | + * @run main/manual BaselineTest |
| 30 | + */ |
| 31 | + |
| 32 | +import java.awt.Color; |
| 33 | +import java.awt.Dimension; |
| 34 | +import java.awt.Font; |
| 35 | +import java.awt.Graphics; |
| 36 | +import java.awt.GridLayout; |
| 37 | + |
| 38 | +import javax.swing.JFrame; |
| 39 | +import javax.swing.JLabel; |
| 40 | +import javax.swing.JScrollPane; |
| 41 | +import javax.swing.JTextPane; |
| 42 | + |
| 43 | +import javax.swing.text.ComponentView; |
| 44 | +import javax.swing.text.BadLocationException; |
| 45 | +import javax.swing.text.Document; |
| 46 | +import javax.swing.text.Element; |
| 47 | +import javax.swing.text.SimpleAttributeSet; |
| 48 | +import javax.swing.text.StyleConstants; |
| 49 | +import javax.swing.text.StyledEditorKit; |
| 50 | +import javax.swing.text.View; |
| 51 | +import javax.swing.text.ViewFactory; |
| 52 | + |
| 53 | +public class BaselineTest { |
| 54 | + |
| 55 | + static final String INSTRUCTIONS = """ |
| 56 | + Test that components displayed in a JTextPane properly respect their vertical alignment. |
| 57 | + There are two text panes, stacked vertically with similar content except the bottom components are taller. |
| 58 | + The content consists of a leading and trailing text string, with pink coloured components between. |
| 59 | + The text string content means the strings 'Default Size Text' and 'Large Size Text'. |
| 60 | + Text content baseline is at the bottom of CAPITAL letters in the text. |
| 61 | + Each pink component has a string displaying its alignment setting in the range 0.0 to 1.0 |
| 62 | + NB: The position of the strings "align = 0.0" etc is not important, it is the component position that matters. |
| 63 | + 0.0 means it should be aligned with its top at the text content baseline, |
| 64 | + 1.0 means it should be aligned with its bottom at the text content baseline. |
| 65 | + A value in between will be a proportional alignment, eg 0.5 is centered on the text content baseline |
| 66 | + If the content displays as described, the test PASSES. |
| 67 | + """; |
| 68 | + |
| 69 | + public static void main(String[] args) throws Exception { |
| 70 | + PassFailJFrame.builder() |
| 71 | + .title("BaselineTest Test Instructions") |
| 72 | + .instructions(INSTRUCTIONS) |
| 73 | + .columns(60) |
| 74 | + .rows(12) |
| 75 | + .testUI(BaselineTest::createUI) |
| 76 | + .positionTestUIBottomRowCentered() |
| 77 | + .build() |
| 78 | + .awaitAndCheck(); |
| 79 | + } |
| 80 | + |
| 81 | + public static JFrame createUI() { |
| 82 | + JFrame frame = new JFrame("BaselineTest"); |
| 83 | + frame.setLayout(new GridLayout(2, 1)); |
| 84 | + |
| 85 | + JTextPane prefPane = new JTextPane(); |
| 86 | + initJTextPane(prefPane); |
| 87 | + frame.add(new JScrollPane(prefPane)); |
| 88 | + |
| 89 | + JTextPane variablePane = new JTextPane(); |
| 90 | + variablePane.setEditorKit(new CustomEditorKit()); |
| 91 | + initJTextPane(variablePane); |
| 92 | + frame.add(new JScrollPane(variablePane)); |
| 93 | + frame.setSize(800, 400); |
| 94 | + return frame; |
| 95 | + } |
| 96 | + |
| 97 | + static void initJTextPane(JTextPane tp) { |
| 98 | + |
| 99 | + try { |
| 100 | + Document doc = tp.getDocument(); |
| 101 | + |
| 102 | + doc.insertString(0, " Default Size Text ", null); |
| 103 | + tp.setCaretPosition(doc.getLength()); |
| 104 | + tp.insertComponent(new PaintLabel(0.0f)); |
| 105 | + tp.insertComponent(new PaintLabel(0.2f)); |
| 106 | + tp.insertComponent(new PaintLabel(0.5f)); |
| 107 | + tp.insertComponent(new PaintLabel(0.7f)); |
| 108 | + tp.insertComponent(new PaintLabel(1.0f)); |
| 109 | + SimpleAttributeSet set = new SimpleAttributeSet(); |
| 110 | + StyleConstants.setFontSize(set, 20); |
| 111 | + tp.setCaretPosition(doc.getLength()); |
| 112 | + doc.insertString(doc.getLength(), " Large Size Text ", set); |
| 113 | + } catch (BadLocationException ble) { |
| 114 | + throw new RuntimeException(ble); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + static class PaintLabel extends JLabel { |
| 119 | + |
| 120 | + private int pref = 40; |
| 121 | + private int min = pref - 30; |
| 122 | + private int max = pref + 30; |
| 123 | + |
| 124 | + public PaintLabel(float align) { |
| 125 | + |
| 126 | + setAlignmentY(align); |
| 127 | + String alignStr = String.valueOf(align); |
| 128 | + |
| 129 | + setText("align = " + alignStr); |
| 130 | + setOpaque(true); |
| 131 | + setBackground(Color.PINK); |
| 132 | + } |
| 133 | + |
| 134 | + public Dimension getMinimumSize() { |
| 135 | + return new Dimension(super.getMinimumSize().width, min); |
| 136 | + } |
| 137 | + |
| 138 | + public Dimension getPreferredSize() { |
| 139 | + return new Dimension(super.getPreferredSize().width, pref); |
| 140 | + } |
| 141 | + |
| 142 | + public Dimension getMaximumSize() { |
| 143 | + return new Dimension(super.getMaximumSize().width, max); |
| 144 | + } |
| 145 | + |
| 146 | + public void paintComponent(Graphics g) { |
| 147 | + g.setColor(Color.PINK); |
| 148 | + g.fillRect(0, 0, getWidth(), getHeight()); |
| 149 | + int y = (int)(getAlignmentY() * getHeight()); |
| 150 | + g.setColor(Color.BLACK); |
| 151 | + g.drawLine(0, y, getWidth(), y); |
| 152 | + g.drawString(getText(), 0, 10); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + static class CustomComponentView extends ComponentView { |
| 157 | + |
| 158 | + public CustomComponentView(Element elem) { |
| 159 | + super(elem); |
| 160 | + } |
| 161 | + |
| 162 | + public int getResizeWeight(int axis) { |
| 163 | + return 1; |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | + static class CustomEditorKit extends StyledEditorKit implements ViewFactory { |
| 168 | + |
| 169 | + public View create(Element elem) { |
| 170 | + if (StyleConstants.ComponentElementName.equals(elem.getName())) { |
| 171 | + return new CustomComponentView(elem); |
| 172 | + } else { |
| 173 | + return super.getViewFactory().create(elem); |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + public ViewFactory getViewFactory() { |
| 178 | + return this; |
| 179 | + } |
| 180 | +} |
| 181 | + |
| 182 | +} |
0 commit comments