Package java.text

Examples of java.text.AttributedString$AttributeRange


         * buffer.
         */
        public AttributedString toAttributedString() {
            switch (count) {
            case 0:
                return new AttributedString(" ");
            case 1:
                return new AttributedString((String)strings.get(0),
                                            (Map)attributes.get(0));
            }

            StringBuffer sb = new StringBuffer();
            Iterator it = strings.iterator();
            while (it.hasNext()) {
                sb.append((String)it.next());
            }

            AttributedString result = new AttributedString(sb.toString());

            // Set the attributes

            Iterator sit = strings.iterator();
            Iterator ait = attributes.iterator();
            int idx = 0;
            while (sit.hasNext()) {
                String s = (String)sit.next();
                int nidx = idx + s.length();
                Map m = (Map)ait.next();
                Iterator kit = m.keySet().iterator();
                Iterator vit = m.values().iterator();
                while (kit.hasNext()) {
                    Attribute attr = (Attribute)kit.next();
                    Object val = vit.next();
                    result.addAttribute(attr, val, idx, nidx);
                }
                idx = nidx;
            }

            return result;
View Full Code Here


     *  
     * @param column the column index
     * @param useMergedCells whether to use the contents of merged cells when calculating the width of the column
     */
    public void autoSizeColumn(int column, boolean useMergedCells) {
        AttributedString str;
        TextLayout layout;
        /**
         * Excel measures columns in units of 1/256th of a character width
         * but the docs say nothing about what particular character is used.
         * '0' looks to be a good choice.
         */
        char defaultChar = '0';
      
        /**
         * This is the multiple that the font height is scaled by when determining the
         * boundary of rotated text.
         */
        double fontHeightMultiple = 2.0;
      
        FontRenderContext frc = new FontRenderContext(null, true, true);

        HSSFWorkbook wb = new HSSFWorkbook(_book);
        HSSFFont defaultFont = wb.getFontAt((short) 0);

        str = new AttributedString("" + defaultChar);
        copyAttributes(defaultFont, str, 0, 1);
        layout = new TextLayout(str.getIterator(), frc);
        int defaultCharWidth = (int)layout.getAdvance();

        double width = -1;
        rows:
        for (Iterator<Row> it = rowIterator(); it.hasNext();) {
            HSSFRow row = (HSSFRow) it.next();
            HSSFCell cell = row.getCell(column);

            if (cell == null) {
                continue;
            }

            int colspan = 1;
            for (int i = 0 ; i < getNumMergedRegions(); i++) {
                CellRangeAddress region = getMergedRegion(i);
                if (containsCell(region, row.getRowNum(), column)) {
                    if (!useMergedCells) {
                        // If we're not using merged cells, skip this one and move on to the next.
                        continue rows;
                    }
                    cell = row.getCell(region.getFirstColumn());
                    colspan = 1 + region.getLastColumn() - region.getFirstColumn();
                }
            }

            HSSFCellStyle style = cell.getCellStyle();
            int cellType = cell.getCellType();
            if(cellType == HSSFCell.CELL_TYPE_FORMULA) cellType = cell.getCachedFormulaResultType();
           
            HSSFFont font = wb.getFontAt(style.getFontIndex());

            if (cellType == HSSFCell.CELL_TYPE_STRING) {
                HSSFRichTextString rt = cell.getRichStringCellValue();
                String[] lines = rt.getString().split("\\n");
                for (int i = 0; i < lines.length; i++) {
                    String txt = lines[i] + defaultChar;
                    str = new AttributedString(txt);
                    copyAttributes(font, str, 0, txt.length());

                    if (rt.numFormattingRuns() > 0) {
                        for (int j = 0; j < lines[i].length(); j++) {
                            int idx = rt.getFontAtIndex(j);
                            if (idx != 0) {
                                HSSFFont fnt = wb.getFontAt((short) idx);
                                copyAttributes(fnt, str, j, j + 1);
                            }
                        }
                    }

                    layout = new TextLayout(str.getIterator(), frc);
                    if(style.getRotation() != 0){
                        /*
                         * Transform the text using a scale so that it's height is increased by a multiple of the leading,
                         * and then rotate the text before computing the bounds. The scale results in some whitespace around
                         * the unrotated top and bottom of the text that normally wouldn't be present if unscaled, but
                         * is added by the standard Excel autosize.
                         */
                        AffineTransform trans = new AffineTransform();
                        trans.concatenate(AffineTransform.getRotateInstance(style.getRotation()*2.0*Math.PI/360.0));
                        trans.concatenate(
                        AffineTransform.getScaleInstance(1, fontHeightMultiple)
                        );
                        width = Math.max(width, ((layout.getOutline(trans).getBounds().getWidth() / colspan) / defaultCharWidth) + cell.getCellStyle().getIndention());
                    } else {
                        width = Math.max(width, ((layout.getBounds().getWidth() / colspan) / defaultCharWidth) + cell.getCellStyle().getIndention());
                    }
                }
            } else {
                String sval = null;
                if (cellType == HSSFCell.CELL_TYPE_NUMERIC) {
                    String dfmt = style.getDataFormatString();
                    String format = dfmt == null ? null : dfmt.replaceAll("\"", "");
                    double value = cell.getNumericCellValue();
                    try {
                        NumberFormat fmt;
                        if ("General".equals(format))
                            sval = "" + value;
                        else
                        {
                            fmt = new DecimalFormat(format);
                            sval = fmt.format(value);
                        }
                    } catch (Exception e) {
                        sval = "" + value;
                    }
                } else if (cellType == HSSFCell.CELL_TYPE_BOOLEAN) {
                    sval = String.valueOf(cell.getBooleanCellValue());
                }
                if(sval != null) {
                    String txt = sval + defaultChar;
                    str = new AttributedString(txt);
                    copyAttributes(font, str, 0, txt.length());

                    layout = new TextLayout(str.getIterator(), frc);
                    if(style.getRotation() != 0){
                        /*
                         * Transform the text using a scale so that it's height is increased by a multiple of the leading,
                         * and then rotate the text before computing the bounds. The scale results in some whitespace around
                         * the unrotated top and bottom of the text that normally wouldn't be present if unscaled, but
View Full Code Here

  }

  private void test2(TestHarness harness)
  {
    harness.checkPoint("getRunStart(AttributedCharacterIterator.Attribute);");
    AttributedString as = new AttributedString("ABCDEFG");
    as.addAttribute(TextAttribute.LANGUAGE, "English");
    as.addAttribute(TextAttribute.FOREGROUND, Color.red, 2, 4);
    AttributedCharacterIterator aci = as.getIterator();
    harness.check(aci.getRunStart(TextAttribute.LANGUAGE), 0);
    aci.setIndex(3);
    harness.check(aci.getRunStart(TextAttribute.FOREGROUND), 2);
  }
View Full Code Here

  }

  private void test3(TestHarness harness)
  {
    harness.checkPoint("getRunStart(Set);");
    AttributedString as = new AttributedString("ABCDEFG");
    as.addAttribute(TextAttribute.LANGUAGE, "English");
    AttributedCharacterIterator aci = as.getIterator();
   
    // try null set
    harness.check(aci.getRunStart((Set) null), 0);
   
    AttributedCharacterIterator aci2 = as.getIterator(null, 4, 7);
    harness.check(aci2.getRunStart((Set) null), 4);
  }
View Full Code Here

  }
 
  public void test1(TestHarness harness)
  {
    harness.checkPoint("()");
    AttributedString as = new AttributedString("ABCDEFGHIJ");
    as.addAttribute(TextAttribute.LANGUAGE, "English");
    as.addAttribute(TextAttribute.FOREGROUND, Color.red, 2, 4);
    as.addAttribute(TextAttribute.BACKGROUND, Color.blue, 7, 8);
    AttributedCharacterIterator aci = as.getIterator();
    harness.check(aci.getRunLimit(), 2);
    aci.setIndex(2);
    harness.check(aci.getRunLimit(), 4);
    aci.setIndex(5);
    harness.check(aci.getRunLimit(), 7);
    aci.setIndex(7);
    harness.check(aci.getRunLimit(), 8);
    aci.setIndex(8);
    harness.check(aci.getRunLimit(), 10);
   
    // try an empty string
    as = new AttributedString("");
    aci = as.getIterator();
    harness.check(aci.getRunLimit(), 0);
   
    // try a string with no attributes
    as = new AttributedString("ABC");
    aci = as.getIterator();
    harness.check(aci.getRunLimit(), 3);
  }
View Full Code Here

  }
 
  public void test2(TestHarness harness)
  {
    harness.checkPoint("(AttributedCharacterIterator.Attribute)");
    AttributedString as = new AttributedString("ABCDEFGHIJ");
    as.addAttribute(TextAttribute.LANGUAGE, "English");
    as.addAttribute(TextAttribute.FOREGROUND, Color.red, 2, 4);
    AttributedCharacterIterator aci = as.getIterator();
    harness.check(aci.getRunLimit(TextAttribute.LANGUAGE), 10);
    harness.check(aci.getRunLimit(TextAttribute.FOREGROUND), 2);
    harness.check(aci.getRunLimit(TextAttribute.BACKGROUND), 10);
    aci.setIndex(2);
    harness.check(aci.getRunLimit(TextAttribute.LANGUAGE), 10);
    harness.check(aci.getRunLimit(TextAttribute.FOREGROUND), 4);
    harness.check(aci.getRunLimit(TextAttribute.BACKGROUND), 10);
    aci.setIndex(4);
    harness.check(aci.getRunLimit(TextAttribute.LANGUAGE), 10);
    harness.check(aci.getRunLimit(TextAttribute.FOREGROUND), 10);
    harness.check(aci.getRunLimit(TextAttribute.BACKGROUND), 10);
   
    // try an empty string
    as = new AttributedString("");
    aci = as.getIterator();
    harness.check(aci.getRunLimit(TextAttribute.FOREGROUND), 0);
   
    // try a string with no attributes
    as = new AttributedString("ABC");
    aci = as.getIterator();
    harness.check(aci.getRunLimit(TextAttribute.FOREGROUND), 3);
  }
View Full Code Here

  }
 
  public void test3(TestHarness harness)
  {
    harness.checkPoint("(Set)");   
    AttributedString as = new AttributedString("ABCDEFGHIJ");
    as.addAttribute(TextAttribute.LANGUAGE, "English");
    as.addAttribute(TextAttribute.FOREGROUND, Color.red, 2, 4);
    as.addAttribute(TextAttribute.BACKGROUND, Color.yellow, 3, 5);
    AttributedCharacterIterator aci = as.getIterator();
    Set set0 = new HashSet();
    Set set1 = new HashSet();
    set1.add(TextAttribute.LANGUAGE);
    Set set2 = new HashSet();
    set2.add(TextAttribute.FOREGROUND);
    set2.add(TextAttribute.BACKGROUND);
    Set set3 = new HashSet();
    set3.add(TextAttribute.LANGUAGE);
    set3.add(TextAttribute.FOREGROUND);
    set3.add(TextAttribute.BACKGROUND);
    harness.check(aci.getRunLimit(set0), 10);
    harness.check(aci.getRunLimit(set1), 10);
    harness.check(aci.getRunLimit(set2), 2);
    harness.check(aci.getRunLimit(set3), 2);
    aci.setIndex(2);
    harness.check(aci.getRunLimit(set0), 10);
    harness.check(aci.getRunLimit(set1), 10);
    harness.check(aci.getRunLimit(set2), 3);
    harness.check(aci.getRunLimit(set3), 3);
    aci.setIndex(3);
    harness.check(aci.getRunLimit(set0), 10);
    harness.check(aci.getRunLimit(set1), 10);
    harness.check(aci.getRunLimit(set2), 4);
    harness.check(aci.getRunLimit(set3), 4);
    aci.setIndex(4);
    harness.check(aci.getRunLimit(set0), 10);
    harness.check(aci.getRunLimit(set1), 10);
    harness.check(aci.getRunLimit(set2), 5);
    harness.check(aci.getRunLimit(set3), 5);
    aci.setIndex(5);
    harness.check(aci.getRunLimit(set0), 10);
    harness.check(aci.getRunLimit(set1), 10);
    harness.check(aci.getRunLimit(set2), 10);
    harness.check(aci.getRunLimit(set3), 10);
    // try an empty string
    as = new AttributedString("");
    aci = as.getIterator();
    harness.check(aci.getRunLimit(set0), 0);
    harness.check(aci.getRunLimit(set1), 0);
    harness.check(aci.getRunLimit(set2), 0);
    harness.check(aci.getRunLimit(set3), 0);
   
    // try a string with no attributes
    as = new AttributedString("ABC");
    aci = as.getIterator();
    harness.check(aci.getRunLimit(set0), 3);
    harness.check(aci.getRunLimit(set1), 3);
    harness.check(aci.getRunLimit(set2), 3);
    harness.check(aci.getRunLimit(set3), 3);
  }
View Full Code Here

  }

  private void test1(TestHarness harness)
  {
    harness.checkPoint("addAttribute(AttributedCharacterIterator.Attribute, Object);");
    AttributedString as = new AttributedString("ABCDEFG");
    as.addAttribute(AttributedCharacterIterator.Attribute.LANGUAGE, Locale.ENGLISH);
    AttributedCharacterIterator aci = as.getIterator();
    harness.check(aci.getRunStart(AttributedCharacterIterator.Attribute.LANGUAGE) == 0);
    harness.check(aci.getRunLimit(AttributedCharacterIterator.Attribute.LANGUAGE) == 7);
   
    // test adding an attribute to a zero length string
    boolean pass = false;
    as = new AttributedString("");
    try
    {
      as.addAttribute(AttributedCharacterIterator.Attribute.LANGUAGE, "Unknown");
    }
    catch (IllegalArgumentException e)
    {
      pass = true;  
    }
    harness.check(pass);
   
    // test adding an attribute with a null value (permitted)
    pass = true;
    as = new AttributedString("123");
    try
    {
      as.addAttribute(AttributedCharacterIterator.Attribute.LANGUAGE, null);
    }
    catch (Exception e)
    {
      pass = false;  
    }
    harness.check(pass);
    aci = as.getIterator();
    Map attributes = aci.getAttributes();
    harness.check(attributes.get(AttributedCharacterIterator.Attribute.LANGUAGE), null);
  }
View Full Code Here

  }

  private void test2(TestHarness harness)
  {
     harness.checkPoint("addAttribute(AttributedCharacterIterator.Attribute, Object, int, int);");
     AttributedString as = new AttributedString("ABCDEFG");
     as.addAttribute(AttributedCharacterIterator.Attribute.LANGUAGE, "Unknown", 2, 4);
     AttributedCharacterIterator aci = as.getIterator();
     harness.check(aci.getRunStart(AttributedCharacterIterator.Attribute.LANGUAGE), 0);
     harness.check(aci.getRunLimit(AttributedCharacterIterator.Attribute.LANGUAGE), 2);
     aci.next();
     aci.next();
     aci.next();
     harness.check(aci.getRunStart(AttributedCharacterIterator.Attribute.LANGUAGE), 2);
     harness.check(aci.getRunLimit(AttributedCharacterIterator.Attribute.LANGUAGE), 4);    
     // if beginIndex < 0, there should be an IllegalArgumentException
     boolean pass = false;
     try
     {
       as = new AttributedString("ABC");
       as.addAttribute(AttributedCharacterIterator.Attribute.LANGUAGE, Locale.FRANCE, -1, 1);
     }
     catch (IllegalArgumentException e)
     {
       pass = true
     }
     harness.check(pass);
    
     // if end index > length of string, there should be an
     // IllegalArgumentException
     pass = false;
     try
     {
       as = new AttributedString("XYZ");
       as.addAttribute(AttributedCharacterIterator.Attribute.LANGUAGE,
            Locale.FRANCE, 1, 3);     
     }
     catch (IllegalArgumentException e)
     {
       pass = true;
     }
     harness.check(true);
    
     // if start index == end index, there should be an IllegalArgumentException
     pass = false;
     try
     {
       as = new AttributedString("123");
       as.addAttribute(AttributedCharacterIterator.Attribute.LANGUAGE,
            Locale.FRANCE, 1, 1);     
     }
     catch (IllegalArgumentException e)
     {
       pass = true
View Full Code Here

    // is null
    boolean pass = false;
    try
    {
      /* AttributedString as = */
          new AttributedString((AttributedCharacterIterator) null);
    }
    catch (NullPointerException e)
    {
      pass = true;
    }
View Full Code Here

TOP

Related Classes of java.text.AttributedString$AttributeRange

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.