Menu

Concatenate TextPrint

Help
2008-03-18
2013-04-10
  • Nobody/Anonymous

    Hi,

    I would like to Concatenate more TextPrint Object in order to print Text with different Colors , for example :

    <blue>This is blue</blue><green>This is green</green>

    The aim of this is to print the syntax Highlighting of my TextEditor.
    I tried this with GribPrint but it is not the right solution because of the weight of the columns.

    Do you have any Idea ?

    Thanks
    Woolite

     
    • Matthew Hall

      Matthew Hall - 2008-03-18

      Have you looked at StyledTextPrint?

      StyledTextPrint text = new StyledTextPrint();
      TextStyle style = new TextStyle();
      text.append("This is blue", style.foreground(0x0000FF));
      text.append("This is green", style.foreground(0x00FF00));

       
    • Nobody/Anonymous

      Thanks Matthew,

      It was exactly that I was looking for.

      For your Information, I had to extends the TextStyle class, to be able to have text with different foregrounds and FontData.

      I first tried :

      <code>
      StyledTextPrint text = new StyledTextPrint();
      TextStyle style = new TextStyle();
      style.foreground(new RGB(0,0,255));
      style.font(new FontData("Courier",6,SWT.BOLD));
      text.append("This is blue", style);
      </code>

      But this didn't work.
      So I created a TextStyle2 class (See at the end of the message).
      And wrote :

      <code>
      StyledTextPrint text = new StyledTextPrint();
      TextStyle2 style = new TextStyle2();
      style.setForeground(new RGB(0,0,255));
      style.setFontData(new FontData("Courier",6,SWT.BOLD));
      text.append("This is blue", style);
      </code>

      This works fine :-)
      Was it volunteer that we can't set the properties on a TextStyle class ?

      Best Regards,
      Woolite

      <code>
      //-------------------------------------
      // The TextStyle2 class 

          protected class TextStyle2 extends TextStyle{
              RGB foreground;
              FontData font;
             
              public RGB setForeground(RGB color){
                  this.foreground = color;
                  return color;
              }
             
              public RGB getForeground(){
                  return this.foreground;
              }
             
              public FontData setFontData(FontData font){
                  this.font = font;
                  return font;
              }
             
              public FontData getFontData(){
                  return this.font;
              }
             
          }
      </code>

       
      • Matthew Hall

        Matthew Hall - 2008-03-19

        TextStyle is immutable.  Each method call creates a new TextStyle, which you should assign back to the local variable.

        StyledTextPrint text = new StyledTextPrint(); 
        TextStyle style = new TextStyle(); 
        style = style.foreground(new RGB(0,0,255));
        style = style.font(new FontData("Courier",6,SWT.BOLD));
        text.append("This is blue", style);

        This pattern is intended to support styling text according to the difference from the main text style, e.g.:

        style = new TextStyle().foreground(0x000000).fontHeight(12);
        text.append("This word is ",       style)
            .append("green",               style.foreground(0x00FF00))
            .append(", this word is ",     style)
            .append("large",               style.fontHeight(24))
            .append(", and this word is ", style)
            .append("both",                style.foreground(0x00FF00).fontHeight(24));   

        Regards,

        Matthew

         
    • Nobody/Anonymous

      Thanks for the useful Help

      Best Regards,
      Woolite

       

Log in to post a comment.

MongoDB Logo MongoDB