jdk/test/javax/swing/text/TableView/I18nLayoutTest.java
changeset 46338 e84b501fa52e
parent 46337 307e52ec20cd
parent 44236 d1cab6c7e608
child 46339 b80e814907b1
equal deleted inserted replaced
46337:307e52ec20cd 46338:e84b501fa52e
     1 /*
       
     2  * Copyright (c) 2007, 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 /*
       
    25  * @test
       
    26  * @key headful
       
    27  * @bug 8133864
       
    28  * @summary  Wrong display, when the document I18n properties is true.
       
    29  * @author Semyon Sadetsky
       
    30  * @run main I18nLayoutTest
       
    31  */
       
    32 
       
    33 import javax.swing.*;
       
    34 import javax.swing.text.*;
       
    35 import java.awt.*;
       
    36 import java.util.ArrayList;
       
    37 
       
    38 public class I18nLayoutTest extends JFrame {
       
    39 
       
    40     private static int height;
       
    41     JEditorPane edit = new JEditorPane();
       
    42     private static I18nLayoutTest frame;
       
    43 
       
    44     public I18nLayoutTest() {
       
    45         super("Code example for a TableView bug");
       
    46         setUndecorated(true);
       
    47         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       
    48         edit.setEditorKit(new CodeBugEditorKit());
       
    49         initCodeBug();
       
    50         this.getContentPane().add(new JScrollPane(edit));
       
    51         this.pack();
       
    52         this.setLocationRelativeTo(null);
       
    53 
       
    54     }
       
    55 
       
    56     private void initCodeBug() {
       
    57         CodeBugDocument doc = (CodeBugDocument) edit.getDocument();
       
    58         try {
       
    59             doc.insertString(0, "TextB TextE", null);
       
    60         } catch (BadLocationException ex) {
       
    61         }
       
    62         doc.insertTable(6, 4, 3);
       
    63         try {
       
    64             doc.insertString(7, "Cell11", null);
       
    65             doc.insertString(14, "Cell12", null);
       
    66             doc.insertString(21, "Cell13", null);
       
    67             doc.insertString(28, "Cell21", null);
       
    68             doc.insertString(35, "Cell22", null);
       
    69             doc.insertString(42, "Cell23", null);
       
    70             doc.insertString(49, "Cell31", null);
       
    71             doc.insertString(56, "Cell32", null);
       
    72             doc.insertString(63, "Cell33", null);
       
    73             doc.insertString(70, "Cell41", null);
       
    74             doc.insertString(77, "Cell42", null);
       
    75             doc.insertString(84, "Cell43", null);
       
    76         } catch (BadLocationException ex) {
       
    77         }
       
    78     }
       
    79 
       
    80     public static void main(String[] args) throws Exception {
       
    81         SwingUtilities.invokeAndWait(new Runnable() {
       
    82             @Override
       
    83             public void run() {
       
    84                 frame = new I18nLayoutTest();
       
    85                 frame.setVisible(true);
       
    86             }
       
    87         });
       
    88         Robot robot = new Robot();
       
    89         robot.delay(200);
       
    90         robot.waitForIdle();
       
    91         SwingUtilities.invokeAndWait(new Runnable() {
       
    92             @Override
       
    93             public void run() {
       
    94                 height = frame.getHeight();
       
    95             }
       
    96         });
       
    97         SwingUtilities.invokeAndWait(new Runnable() {
       
    98             @Override
       
    99             public void run() {
       
   100                 frame.dispose();
       
   101             }
       
   102         });
       
   103         if (height < 32) {
       
   104             throw new RuntimeException(
       
   105                     "TableView layout height is wrong " + height);
       
   106         }
       
   107         System.out.println("ok");
       
   108     }
       
   109 }
       
   110 
       
   111 //------------------------------------------------------------------------------
       
   112 class CodeBugEditorKit extends StyledEditorKit {
       
   113 
       
   114     ViewFactory defaultFactory = new TableFactory();
       
   115 
       
   116     @Override
       
   117     public ViewFactory getViewFactory() {
       
   118         return defaultFactory;
       
   119     }
       
   120 
       
   121     @Override
       
   122     public Document createDefaultDocument() {
       
   123         return new CodeBugDocument();
       
   124     }
       
   125 }
       
   126 //------------------------------------------------------------------------------
       
   127 
       
   128 class TableFactory implements ViewFactory {
       
   129 
       
   130     @Override
       
   131     public View create(Element elem) {
       
   132         String kind = elem.getName();
       
   133         if (kind != null) {
       
   134             if (kind.equals(AbstractDocument.ContentElementName)) {
       
   135                 return new LabelView(elem);
       
   136             } else if (kind.equals(AbstractDocument.ParagraphElementName)) {
       
   137                 return new ParagraphView(elem);
       
   138             } else if (kind.equals(AbstractDocument.SectionElementName)) {
       
   139                 return new BoxView(elem, View.Y_AXIS);
       
   140             } else if (kind.equals(StyleConstants.ComponentElementName)) {
       
   141                 return new ComponentView(elem);
       
   142             } else if (kind.equals(CodeBugDocument.ELEMENT_TABLE)) {
       
   143                 return new tableView(elem);
       
   144             } else if (kind.equals(StyleConstants.IconElementName)) {
       
   145                 return new IconView(elem);
       
   146             }
       
   147         }
       
   148         // default to text display
       
   149         return new LabelView(elem);
       
   150 
       
   151     }
       
   152 }
       
   153 //------------------------------------------------------------------------------
       
   154 
       
   155 //------------------------------------------------------------------------------
       
   156 class tableView extends TableView implements ViewFactory {
       
   157 
       
   158     public tableView(Element elem) {
       
   159         super(elem);
       
   160     }
       
   161 
       
   162     @Override
       
   163     public void setParent(View parent) {
       
   164         super.setParent(parent);
       
   165     }
       
   166 
       
   167     @Override
       
   168     public void setSize(float width, float height) {
       
   169         super.setSize(width, height);
       
   170     }
       
   171 
       
   172     @Override
       
   173     public ViewFactory getViewFactory() {
       
   174         return this;
       
   175     }
       
   176 
       
   177     @Override
       
   178     public float getMinimumSpan(int axis) {
       
   179         return getPreferredSpan(axis);
       
   180     }
       
   181 
       
   182     @Override
       
   183     public float getMaximumSpan(int axis) {
       
   184         return getPreferredSpan(axis);
       
   185     }
       
   186 
       
   187     @Override
       
   188     public float getAlignment(int axis) {
       
   189         return 0.5f;
       
   190     }
       
   191 
       
   192     @Override
       
   193     public float getPreferredSpan(int axis) {
       
   194         if (axis == 0) return super.getPreferredSpan(0);
       
   195         float preferredSpan = super.getPreferredSpan(axis);
       
   196         return preferredSpan;
       
   197     }
       
   198 
       
   199     @Override
       
   200     public void paint(Graphics g, Shape allocation) {
       
   201         super.paint(g, allocation);
       
   202         Rectangle alloc = allocation.getBounds();
       
   203         int lastY = alloc.y + alloc.height - 1;
       
   204         g.drawLine(alloc.x, lastY, alloc.x + alloc.width, lastY);
       
   205     }
       
   206 
       
   207     @Override
       
   208     protected void paintChild(Graphics g, Rectangle alloc, int index) {
       
   209         super.paintChild(g, alloc, index);
       
   210         int lastX = alloc.x + alloc.width;
       
   211         g.drawLine(alloc.x, alloc.y, lastX, alloc.y);
       
   212     }
       
   213 
       
   214     @Override
       
   215     public View create(Element elem) {
       
   216         String kind = elem.getName();
       
   217         if (kind != null) {
       
   218             if (kind.equals(CodeBugDocument.ELEMENT_TR)) {
       
   219                 return new trView(elem);
       
   220             } else if (kind.equals(CodeBugDocument.ELEMENT_TD)) {
       
   221                 return new BoxView(elem, View.Y_AXIS);
       
   222 
       
   223             }
       
   224         }
       
   225 
       
   226         // default is to delegate to the normal factory
       
   227         View p = getParent();
       
   228         if (p != null) {
       
   229             ViewFactory f = p.getViewFactory();
       
   230             if (f != null) {
       
   231                 return f.create(elem);
       
   232             }
       
   233         }
       
   234 
       
   235         return null;
       
   236     }
       
   237 
       
   238     public class trView extends TableRow {
       
   239         @Override
       
   240         public void setParent(View parent) {
       
   241             super.setParent(parent);
       
   242         }
       
   243 
       
   244         public trView(Element elem) {
       
   245             super(elem);
       
   246         }
       
   247 
       
   248         public float getMinimumSpan(int axis) {
       
   249             return getPreferredSpan(axis);
       
   250         }
       
   251 
       
   252         public float getMaximumSpan(int axis) {
       
   253             return getPreferredSpan(axis);
       
   254         }
       
   255 
       
   256         public float getAlignment(int axis) {
       
   257             return 0f;
       
   258         }
       
   259 
       
   260         @Override
       
   261         protected void paintChild(Graphics g, Rectangle alloc, int index) {
       
   262             super.paintChild(g, alloc, index);
       
   263             int lastY = alloc.y + alloc.height - 1;
       
   264             g.drawLine(alloc.x, alloc.y, alloc.x, lastY);
       
   265             int lastX = alloc.x + alloc.width;
       
   266             g.drawLine(lastX, alloc.y, lastX, lastY);
       
   267         }
       
   268     }
       
   269 
       
   270     ;
       
   271 }
       
   272 
       
   273 //------------------------------------------------------------------------------
       
   274 class CodeBugDocument extends DefaultStyledDocument {
       
   275 
       
   276     public static final String ELEMENT_TABLE = "table";
       
   277     public static final String ELEMENT_TR = "table cells row";
       
   278     public static final String ELEMENT_TD = "table data cell";
       
   279 
       
   280     public CodeBugDocument() {
       
   281         putProperty("i18n", Boolean.TRUE);
       
   282     }
       
   283 
       
   284 
       
   285     protected void insertTable(int offset, int rowCount, int colCount) {
       
   286         try {
       
   287             ArrayList Specs = new ArrayList();
       
   288             ElementSpec gapTag = new ElementSpec(new SimpleAttributeSet(),
       
   289                     ElementSpec.ContentType, "\n".toCharArray(), 0, 1);
       
   290             Specs.add(gapTag);
       
   291 
       
   292             SimpleAttributeSet tableAttrs = new SimpleAttributeSet();
       
   293             tableAttrs.addAttribute(ElementNameAttribute, ELEMENT_TABLE);
       
   294             ElementSpec tableStart =
       
   295                     new ElementSpec(tableAttrs, ElementSpec.StartTagType);
       
   296             Specs.add(tableStart); //start table tag
       
   297 
       
   298 
       
   299             fillRowSpecs(Specs, rowCount, colCount);
       
   300 
       
   301             ElementSpec[] spec = new ElementSpec[Specs.size()];
       
   302             Specs.toArray(spec);
       
   303 
       
   304             this.insert(offset, spec);
       
   305         } catch (BadLocationException ex) {
       
   306         }
       
   307     }
       
   308 
       
   309     protected void fillRowSpecs(ArrayList Specs, int rowCount, int colCount) {
       
   310         SimpleAttributeSet rowAttrs = new SimpleAttributeSet();
       
   311         rowAttrs.addAttribute(ElementNameAttribute, ELEMENT_TR);
       
   312         for (int i = 0; i < rowCount; i++) {
       
   313             ElementSpec rowStart =
       
   314                     new ElementSpec(rowAttrs, ElementSpec.StartTagType);
       
   315             Specs.add(rowStart);
       
   316 
       
   317             fillCellSpecs(Specs, colCount);
       
   318 
       
   319             ElementSpec rowEnd =
       
   320                     new ElementSpec(rowAttrs, ElementSpec.EndTagType);
       
   321             Specs.add(rowEnd);
       
   322         }
       
   323 
       
   324     }
       
   325 
       
   326     protected void fillCellSpecs(ArrayList Specs, int colCount) {
       
   327         for (int i = 0; i < colCount; i++) {
       
   328             SimpleAttributeSet cellAttrs = new SimpleAttributeSet();
       
   329             cellAttrs.addAttribute(ElementNameAttribute, ELEMENT_TD);
       
   330 
       
   331             ElementSpec cellStart =
       
   332                     new ElementSpec(cellAttrs, ElementSpec.StartTagType);
       
   333             Specs.add(cellStart);
       
   334 
       
   335             ElementSpec parStart = new ElementSpec(new SimpleAttributeSet(),
       
   336                     ElementSpec.StartTagType);
       
   337             Specs.add(parStart);
       
   338             ElementSpec parContent = new ElementSpec(new SimpleAttributeSet(),
       
   339                     ElementSpec.ContentType, "\n".toCharArray(), 0, 1);
       
   340             Specs.add(parContent);
       
   341             ElementSpec parEnd = new ElementSpec(new SimpleAttributeSet(),
       
   342                     ElementSpec.EndTagType);
       
   343             Specs.add(parEnd);
       
   344             ElementSpec cellEnd =
       
   345                     new ElementSpec(cellAttrs, ElementSpec.EndTagType);
       
   346             Specs.add(cellEnd);
       
   347         }
       
   348     }
       
   349 }