File Source: Line.java

         /* 
    P/P   *  Method: com.dmdirc.addons.ui_swing.textpane.Line__static_init
          */
     1  /*
     2   * Copyright (c) 2006-2008 Chris Smith, Shane Mc Cormack, Gregory Holmes
     3   *
     4   * Permission is hereby granted, free of charge, to any person obtaining a copy
     5   * of this software and associated documentation files (the "Software"), to deal
     6   * in the Software without restriction, including without limitation the rights
     7   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     8   * copies of the Software, and to permit persons to whom the Software is
     9   * furnished to do so, subject to the following conditions:
    10   *
    11   * The above copyright notice and this permission notice shall be included in
    12   * all copies or substantial portions of the Software.
    13   *
    14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    20   * SOFTWARE.
    21   */
    22  package com.dmdirc.addons.ui_swing.textpane;
    23  
    24  import com.dmdirc.config.ConfigManager;
    25  import com.dmdirc.logger.ErrorLevel;
    26  import com.dmdirc.logger.Logger;
    27  import com.dmdirc.ui.messages.IRCTextAttribute;
    28  import com.dmdirc.ui.messages.Styliser;
    29  
    30  import java.awt.Font;
    31  import java.awt.font.TextAttribute;
    32  import java.text.AttributedString;
    33  import java.util.Arrays;
    34  import java.util.Enumeration;
    35  
    36  import javax.swing.UIManager;
    37  import javax.swing.text.AttributeSet;
    38  import javax.swing.text.BadLocationException;
    39  import javax.swing.text.Element;
    40  import javax.swing.text.StyleConstants.CharacterConstants;
    41  import javax.swing.text.StyleConstants.ColorConstants;
    42  import javax.swing.text.StyleConstants.FontConstants;
    43  import javax.swing.text.StyledDocument;
    44  
    45  /**
    46   * Represents a line of text in IRC.
    47   */
    48  public class Line {
    49  
    50      private final String[] lineParts;
    51      private final ConfigManager config;
    52      private int lineHeight;
    53  
    54      /**
    55       * Creates a new line.
    56       * 
    57       * @param lineParts Parts of the line
    58       * @param config Configuration manager for this line
    59       */
             /* 
    P/P       *  Method: void com.dmdirc.addons.ui_swing.textpane.Line(String[], ConfigManager)
              * 
              *  Presumptions:
              *    javax.swing.UIManager:getFont(...)@63 != null
              * 
              *  Postconditions:
              *    this.config == config
              *    init'ed(this.config)
              *    init'ed(this.lineHeight)
              *    this.lineParts == lineParts
              *    init'ed(this.lineParts)
              */
    60      public Line(final String[] lineParts, final ConfigManager config) {
    61          this.lineParts = lineParts;
    62          this.config = config;
    63          this.lineHeight = UIManager.getFont("TextPane.font").getSize();
    64      }
    65  
    66      /**
    67       * Returns the line parts of this line.
    68       *
    69       * @return Lines parts
    70       */
    71      public String[] getLineParts() {
                 /* 
    P/P           *  Method: String[] getLineParts()
                  * 
                  *  Postconditions:
                  *    return_value == this.lineParts
                  *    init'ed(return_value)
                  */
    72          return lineParts;
    73      }
    74  
    75      /**
    76       * Returns the length of the specified line
    77       * 
    78       * @return Length of the line
    79       */
    80      public int getLength() {
                 /* 
    P/P           *  Method: int getLength()
                  * 
                  *  Preconditions:
                  *    this.lineParts != null
                  *    this.lineParts.length <= 232-1
                  *    (soft) this.lineParts[...] != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
    81          int length = 0;
    82          for (String linePart : lineParts) {
    83              length += linePart.length();
    84          }
    85          return length;
    86      }
    87  
    88      /**
    89       * Returns the height of the specified line.
    90       * 
    91       * @return Line height
    92       */
    93      public int getHeight() {
                 /* 
    P/P           *  Method: int getHeight()
                  * 
                  *  Preconditions:
                  *    init'ed(this.lineHeight)
                  * 
                  *  Postconditions:
                  *    return_value == this.lineHeight
                  *    init'ed(return_value)
                  */
    94          return lineHeight;
    95      }
    96  
    97      /**
    98       * Returns the Line text at the specified number.
    99       *
   100       * @return Line at the specified number or null
   101       */
   102      public String getText() {
                 /* 
    P/P           *  Method: String getText()
                  * 
                  *  Preconditions:
                  *    this.lineParts != null
                  *    this.lineParts.length <= 232-1
                  *    (soft) init'ed(this.lineParts[...])
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   103          StringBuilder lineText = new StringBuilder();
   104          for (String linePart : lineParts) {
   105              lineText.append(linePart);
   106          }
   107          return Styliser.stipControlCodes(lineText.toString());
   108      }
   109  
   110      /**
   111       * Returns the Line text at the specified number.
   112       *
   113       * @return Line at the specified number or null
   114       *
   115       * @since 0.6.3m1
   116       */
   117      public String getStyledText() {
                 /* 
    P/P           *  Method: String getStyledText()
                  * 
                  *  Preconditions:
                  *    this.lineParts != null
                  *    this.lineParts.length <= 232-1
                  *    (soft) init'ed(this.lineParts[...])
                  * 
                  *  Postconditions:
                  *    init'ed(java.lang.StringBuilder:toString(...)._tainted)
                  *    return_value == &amp;java.lang.StringBuilder:toString(...)
                  */
   118          StringBuilder lineText = new StringBuilder();
   119          for (String linePart : lineParts) {
   120              lineText.append(linePart);
   121          }
   122          return lineText.toString();
   123      }
   124  
   125      /**
   126       * Converts a StyledDocument into an AttributedString.
   127       *
   128       * @return AttributedString representing the specified StyledDocument
   129       */
   130      public AttributedString getStyled() {
                 /* 
    P/P           *  Method: AttributedString getStyled()
                  * 
                  *  Preconditions:
                  *    this.config != null
                  *    (soft) this.config.sources != null
                  * 
                  *  Presumptions:
                  *    init'ed(com.dmdirc.logger.ErrorLevel.MEDIUM)
                  *    init'ed(com.dmdirc.ui.messages.IRCTextAttribute.CHANNEL)
                  *    init'ed(com.dmdirc.ui.messages.IRCTextAttribute.HYPERLINK)
                  *    init'ed(com.dmdirc.ui.messages.IRCTextAttribute.NICKNAME)
                  *    com.dmdirc.ui.messages.Styliser:getStyledString(...)@131 != null
                  *    ...
                  * 
                  *  Postconditions:
                  *    return_value in Addr_Set{&amp;new AttributedString(getStyled#1),&amp;new AttributedString(getStyled#4)}
                  *    init'ed(this.lineHeight)
                  *    new AttributedString(getStyled#1) num objects == 1
                  *    new AttributedString(getStyled#4) num objects <= 1
                  * 
                  *  Test Vectors:
                  *    java.text.AttributedCharacterIterator:getEndIndex(...)@159: {0}, {-231..-1, 1..232-1}
                  *    java.text.AttributedCharacterIterator:getEndIndex(...)@233: {-231..-1, 1..232-1}, {0}
                  *    java.util.Enumeration:hasMoreElements(...)@171: {0}, {1}
                  */
   131          final StyledDocument doc = Styliser.getStyledString(lineParts);
   132  
   133          AttributedString attString = null;
   134          final Element line = doc.getParagraphElement(0);
   135          try {
   136              attString = new AttributedString(line.getDocument().getText(0,
   137                      line.getDocument().getLength()));
   138          } catch (BadLocationException ex) {
   139              Logger.userError(ErrorLevel.MEDIUM,
   140                      "Unable to insert styled string: " +
   141                      ex.getMessage());
   142          }
   143  
   144          final Font defaultFont = UIManager.getFont("TextPane.font");
   145          String fontName = null;
   146          int fontSize = -1;
   147          if (config.hasOptionString("ui", "textPaneFontName")) {
   148              fontName = config.getOption("ui", "textPaneFontName");
   149          } else {
   150              fontName = defaultFont.getName();
   151          }
   152          //TODO issue 2251
   153          //if (config.hasOptionString("ui", "textPaneFontSize")) {
   154          //    fontSize = config.getOptionInt("ui", "textPaneFontSize");
   155          //} else {
   156              fontSize = defaultFont.getSize();
   157          //}
   158          lineHeight = fontSize;
   159          if (attString.getIterator().getEndIndex() != 0) {
   160              final Font font = new Font(fontName, Font.PLAIN, fontSize);
   161              attString.addAttribute(TextAttribute.SIZE, font.getSize());
   162              attString.addAttribute(TextAttribute.FAMILY, font.getFamily());
   163          }
   164  
   165          for (int i = 0; i < line.getElementCount(); i++) {
   166              final Element element = line.getElement(i);
   167  
   168              final AttributeSet as = element.getAttributes();
   169              final Enumeration<?> ae = as.getAttributeNames();
   170  
   171              while (ae.hasMoreElements()) {
   172                  final Object attrib = ae.nextElement();
   173  
   174                  if (attrib == IRCTextAttribute.HYPERLINK) {
   175                      //Hyperlink
   176                      attString.addAttribute(IRCTextAttribute.HYPERLINK,
   177                              as.getAttribute(attrib), element.getStartOffset(),
   178                              element.getEndOffset());
   179                  } else if (attrib == IRCTextAttribute.NICKNAME) {
   180                      //Nicknames
   181                      attString.addAttribute(IRCTextAttribute.NICKNAME,
   182                              as.getAttribute(attrib), element.getStartOffset(),
   183                              element.getEndOffset());
   184                  } else if (attrib == IRCTextAttribute.CHANNEL) {
   185                      //Channels
   186                      attString.addAttribute(IRCTextAttribute.CHANNEL,
   187                              as.getAttribute(attrib), element.getStartOffset(),
   188                              element.getEndOffset());
   189                  } else if (attrib == ColorConstants.Foreground) {
   190                      //Foreground
   191                      attString.addAttribute(TextAttribute.FOREGROUND,
   192                              as.getAttribute(attrib), element.getStartOffset(),
   193                              element.getEndOffset());
   194                  } else if (attrib == ColorConstants.Background) {
   195                      //Background
   196                      attString.addAttribute(TextAttribute.BACKGROUND,
   197                              as.getAttribute(attrib), element.getStartOffset(),
   198                              element.getEndOffset());
   199                  } else if (attrib == FontConstants.Bold) {
   200                      //Bold
   201                      attString.addAttribute(TextAttribute.WEIGHT,
   202                              TextAttribute.WEIGHT_BOLD, element.getStartOffset(),
   203                              element.getEndOffset());
   204                  } else if (attrib == FontConstants.Family) {
   205                      //Family
   206                      attString.addAttribute(TextAttribute.FAMILY,
   207                              as.getAttribute(attrib), element.getStartOffset(),
   208                              element.getEndOffset());
   209                  } else if (attrib == FontConstants.Italic) {
   210                      //italics
   211                      attString.addAttribute(TextAttribute.POSTURE,
   212                              TextAttribute.POSTURE_OBLIQUE,
   213                              element.getStartOffset(),
   214                              element.getEndOffset());
   215                  } else if (attrib == CharacterConstants.Underline) {
   216                      //Underline
   217                      attString.addAttribute(TextAttribute.UNDERLINE,
   218                              TextAttribute.UNDERLINE_ON, element.getStartOffset(),
   219                              element.getEndOffset());
   220                  } else if (attrib == IRCTextAttribute.SMILEY) {
   221                      /* Lets avoid showing broken smileys shall we!
   222                      final Image image = IconManager.getIconManager().getImage((String) as.getAttribute(attrib)).
   223                      getScaledInstance(14, 14, Image.SCALE_DEFAULT);
   224                      ImageGraphicAttribute iga = new ImageGraphicAttribute(image, 
   225                      (int) BOTTOM_ALIGNMENT, 5, 5);
   226                      attString.addAttribute(TextAttribute.CHAR_REPLACEMENT, iga,
   227                      element.getStartOffset(), element.getEndOffset());
   228                       */
   229                  }
   230              }
   231          }
   232  
   233          if (attString.getIterator().getEndIndex() == 0) {
   234              return new AttributedString("\n");
   235          }
   236  
   237          return attString;
   238      }
   239  
   240      /** {@inheritDoc} */
   241      @Override
   242      public boolean equals(Object obj) {
                 /* 
    P/P           *  Method: bool equals(Object)
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   243          if (obj instanceof Line) {
   244              return Arrays.equals(((Line) obj).getLineParts(), getLineParts());
   245          }
   246          return false;
   247      }
   248  
   249      /** {@inheritDoc} */
   250      @Override
   251      public int hashCode() {
                 /* 
    P/P           *  Method: int hashCode()
                  * 
                  *  Preconditions:
                  *    this.lineParts != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   252          return getLineParts().hashCode();
   253      }
   254  }








SofCheck Inspector Build Version : 2.17854
Line.java 2009-Jun-25 01:54:24
Line.class 2009-Sep-02 17:04:14