Example usage for javax.swing.text SimpleAttributeSet addAttribute

List of usage examples for javax.swing.text SimpleAttributeSet addAttribute

Introduction

In this page you can find the example usage for javax.swing.text SimpleAttributeSet addAttribute.

Prototype

public void addAttribute(Object name, Object value) 

Source Link

Document

Adds an attribute to the list.

Usage

From source file:tufts.vue.RichTextBox.java

/***********
 *******NOTES FROM FONT EDITOR PANEL TO HELP WITH COPY/PASTE OF STYLES.
globalSizeListener = new ActionListener(){
        //from  w w w  .  j a  va2  s.c  o m
   public void actionPerformed(ActionEvent fe) 
   {
              
      if (suspendItemListeners)
         return;
              
      if (lwtext == null)
         return;
        
      lwtext.richLabelBox.selectAll();
      final String textSize = mSizeField.getSelectedItem().toString();
      SHTMLDocument doc = (SHTMLDocument)lwtext.richLabelBox.getDocument();
      SimpleAttributeSet set = new SimpleAttributeSet();
        Util.styleSheet().addCSSAttribute(set, CSS.Attribute.FONT_SIZE,
                 textSize);
        set.addAttribute(HTML.Attribute.SIZE, textSize);
        lwtext.richLabelBox.applyAttributes(set, false);
                
      lwtext.richLabelBox.select(0,0);
      //lwtext.setLabel0(lwtext.richLabelBox.getRichText(), false);
      lwtext.richLabelBox.setSize(lwtext.richLabelBox.getPreferredSize());                                        
               
          if (lwtext.getParent() !=null)
              lwtext.getParent().layout();                                                  
                 
       lwtext.notify(lwtext.richLabelBox, LWKey.Repaint);                     
               
               
   }
 };
                     
  globalFaceListener = new ActionListener(){ 
     public void actionPerformed(ActionEvent fe) 
     {                
        if (suspendItemListeners)
         return;
              
        if (lwtext == null)
         return;
        lwtext.richLabelBox.selectAll();
      SHTMLDocument doc = (SHTMLDocument)lwtext.richLabelBox.getDocument();
      SimpleAttributeSet set = new SimpleAttributeSet();
        Util.styleSheet().addCSSAttribute(set, CSS.Attribute.FONT_FAMILY,
                 mFontCombo.getSelectedItem().toString());
        set.addAttribute(HTML.Attribute.FACE, mFontCombo.getSelectedItem().toString());
        lwtext.richLabelBox.applyAttributes(set, false);
                
      lwtext.richLabelBox.select(0,0);
           
      lwtext.richLabelBox.setSize(lwtext.richLabelBox.getPreferredSize());                                        
               
          if (lwtext.getParent() !=null)
              lwtext.getParent().layout();
                 
                 
                
                 
       lwtext.notify(lwtext.richLabelBox, LWKey.Repaint); 
     }
   };
           
     private final PropertyChangeListener RichTextColorChangeListener =
new PropertyChangeListener() {
    public void propertyChange(PropertyChangeEvent e) {
        
        if (e instanceof LWPropertyChangeEvent == false)
            return;
                
        final RichTextBox activeRTB = (RichTextBox) VUE.getActive(RichTextBox.class);
                
                
        if (activeRTB == null && lwtext == null) {
            // no problem: just ignore if there's no active edit
            //tufts.Util.printStackTrace("FEP propertyChange: no active RichTextBox: " + e);
            return;
        }
                
        if (lwtext !=null)
           lwtext.richLabelBox.selectAll();
                
               
        final Color color = (Color) e.getNewValue();
        final SimpleAttributeSet set = new SimpleAttributeSet();
        final String colorString = "#" + Integer.toHexString(color.getRGB()).substring(2);
        toggleBulletsAction.setColor(colorString);
        toggleNumbersAction.setColor(colorString);
        Util.styleSheet().addCSSAttribute(set, CSS.Attribute.COLOR, colorString);
                
        set.addAttribute(HTML.Attribute.COLOR, colorString);
        if (activeRTB != null)
           activeRTB.applyAttributes(set, false);
        else
           lwtext.richLabelBox.applyAttributes(set, false);
                
        if (lwtext !=null)
        {
           lwtext.richLabelBox.select(0,0);
                    
           lwtext.richLabelBox.select(0,0);
          //lwtext.setLabel0(lwtext.richLabelBox.getRichText(), false);
          lwtext.richLabelBox.setSize(lwtext.richLabelBox.getPreferredSize());                                        
                   
              if (lwtext.getParent() !=null)
                  lwtext.getParent().layout();
                     
                     
                    
                     
           lwtext.notify(lwtext.richLabelBox, LWKey.Repaint); 
        }
    }
};
 ************/
// this called every time setText is called to ensure we get
// the font style encoded in our owning LWComponent
void copyStyle(LWComponent c) {
    if (DEBUG.TEXT)
        out("copyStyle " + c);

    //Basic Setup
    LWText srcText = (LWText) c;
    RichTextBox srcBox = srcText.getRichLabelBox();
    selectAll();
    SHTMLDocument srcDoc = (SHTMLDocument) srcBox.getDocument();
    SHTMLDocument doc = (SHTMLDocument) getDocument();
    SimpleAttributeSet set = new SimpleAttributeSet();
    SimpleAttributeSet alignSet = new SimpleAttributeSet();
    //Gather source information

    Element paragraphElement = srcDoc.getParagraphElement(1);

    if (paragraphElement.getName().equals("p-implied")) //we're in a list item
        paragraphElement = paragraphElement.getParentElement();

    AttributeSet paragraphAttributeSet = paragraphElement.getAttributes();

    Element charElem = srcDoc.getCharacterElement(1);
    AttributeSet charSet = charElem.getAttributes();
    Enumeration characterAttributeEnum = charSet.getAttributeNames();
    Enumeration elementEnum = paragraphAttributeSet.getAttributeNames();

    //Apply some attributes
    // System.out.println(paragraphElement.toString());
    while (elementEnum.hasMoreElements()) {
        Object o = elementEnum.nextElement();

        boolean isAlignSet = false;
        //System.out.println("P :: " +o.toString());
        if (o.toString().equals("text-align") && paragraphAttributeSet.getAttribute(o).toString().equals("left")
                && !isAlignSet) {
            //Left Align
            Util.styleSheet().addCSSAttribute(alignSet, CSS.Attribute.TEXT_ALIGN,
                    paragraphAttributeSet.getAttribute(o).toString());
            alignSet.addAttribute(HTML.Attribute.ALIGN, paragraphAttributeSet.getAttribute(o).toString());
        } else if (o.toString().equals("text-align")
                && paragraphAttributeSet.getAttribute(o).toString().equals("center") && !isAlignSet) {
            //Center Align
            Util.styleSheet().addCSSAttribute(alignSet, CSS.Attribute.TEXT_ALIGN,
                    paragraphAttributeSet.getAttribute(o).toString());
            alignSet.addAttribute(HTML.Attribute.ALIGN, paragraphAttributeSet.getAttribute(o).toString());
        } else if (o.toString().equals("text-align")
                && paragraphAttributeSet.getAttribute(o).toString().equals("right") && !isAlignSet) {
            //Right Align
            Util.styleSheet().addCSSAttribute(alignSet, CSS.Attribute.TEXT_ALIGN,
                    paragraphAttributeSet.getAttribute(o).toString());
            alignSet.addAttribute(HTML.Attribute.ALIGN, paragraphAttributeSet.getAttribute(o).toString());
        }

        if ((o.toString().equals("font-size")) || (o.toString().equals("size"))) {
            //Font Size
            Util.styleSheet().addCSSAttribute(set, CSS.Attribute.FONT_SIZE,
                    paragraphAttributeSet.getAttribute(o).toString());
            set.addAttribute(HTML.Attribute.SIZE, paragraphAttributeSet.getAttribute(o).toString());
        }

        else if ((o.toString().equals("font-family")) || (o.toString().equals("font-face"))
                || (o.toString().equals("face"))) {
            //Font Face
            Util.styleSheet().addCSSAttribute(set, CSS.Attribute.FONT_FAMILY,
                    paragraphAttributeSet.getAttribute(o).toString());
            set.addAttribute(HTML.Attribute.FACE, paragraphAttributeSet.getAttribute(o).toString());

        }
    }

    boolean isBold = false;
    boolean isItalic = false;
    boolean isUnderline = false;

    while (characterAttributeEnum.hasMoreElements()) {

        Object o = characterAttributeEnum.nextElement();
        //System.out.println("C :: " +o.toString() + "XXX " + charSet.getAttribute(o).toString());
        //System.out.println("Character element : " + o.toString() + " , " + charSet.getAttribute(o));
        if ((o.toString().equals("color"))) {
            //Color
            Util.styleSheet().addCSSAttribute(set, CSS.Attribute.COLOR, charSet.getAttribute(o).toString());
            set.addAttribute(HTML.Attribute.COLOR, charSet.getAttribute(o).toString());
        }

        if ((o.toString().equals("font-size")) || (o.toString().equals("size"))) {
            //Font Size
            Util.styleSheet().addCSSAttribute(set, CSS.Attribute.FONT_SIZE, charSet.getAttribute(o).toString());
            set.addAttribute(HTML.Attribute.SIZE, charSet.getAttribute(o).toString());
        }

        if ((o.toString().equals("font-family")) || (o.toString().equals("font-face"))
                || (o.toString().equals("face"))) {
            //Font Face
            Util.styleSheet().addCSSAttribute(set, CSS.Attribute.FONT_FAMILY,
                    charSet.getAttribute(o).toString());
            set.addAttribute(HTML.Attribute.FACE, charSet.getAttribute(o).toString());
        }

        if ((o.toString().equals("font-weight") && charSet.getAttribute(o).toString().equals("bold"))
                || o.toString().equals("b")) {
            Util.styleSheet().addCSSAttribute(set, CSS.Attribute.FONT_WEIGHT,
                    charSet.getAttribute(o).toString());
        }
        if ((o.toString().equals("font-style") && charSet.getAttribute(o).toString().equals("italic"))
                || o.toString().equals("i")) {
            Util.styleSheet().addCSSAttribute(set, CSS.Attribute.FONT_STYLE,
                    charSet.getAttribute(o).toString());
        }
        if ((o.toString().equals("text-decoration") && charSet.getAttribute(o).toString().equals("underline"))
                || o.toString().equals("u")) {
            Util.styleSheet().addCSSAttribute(set, CSS.Attribute.TEXT_DECORATION,
                    charSet.getAttribute(o).toString());
        }
    } //done looking at character attributes                                                

    applyAttributes(set, false);
    lwc.notify(this, LWKey.Repaint);
    applyAttributes(alignSet, true);
    lwc.notify(this, LWKey.Repaint);
    clearSelection();
    try {
        setSize(getPreferredSize());
    } catch (NullPointerException npe) {
    }
    /*      set = new SimpleAttributeSet();
          final String colorString = "#" + Integer.toHexString(color.getRGB()).substring(2);
          toggleBulletsAction.setColor(colorString);
          toggleNumbersAction.setColor(colorString);
          Util.styleSheet().addCSSAttribute(set, CSS.Attribute.COLOR, colorString);
                  
          set.addAttribute(HTML.Attribute.COLOR, colorString);
          if (activeRTB != null)
             activeRTB.applyAttributes(set, false);
          else
             lwtext.richLabelBox.applyAttributes(set, false);
                  
          if (lwtext !=null)
          {
             lwtext.richLabelBox.select(0,0);
            
             lwtext.richLabelBox.select(0,0);
            //lwtext.setLabel0(lwtext.richLabelBox.getRichText(), false);
            lwtext.richLabelBox.setSize(lwtext.richLabelBox.getPreferredSize());                                        
                     
      if (lwtext.getParent() !=null)
          lwtext.getParent().layout();
             
             
            
             
             lwtext.notify(lwtext.richLabelBox, LWKey.Repaint); 
          }*/
}

From source file:tufts.vue.RichTextBox.java

public void overrideTextColor(Color c) {
    //System.out.println("OVERRIDE TEXT COLOR : " + c.toString());
    SimpleAttributeSet set = new SimpleAttributeSet();
    String colorString = "#" + Integer.toHexString(c.getRGB()).substring(2);
    com.lightdev.app.shtm.Util.styleSheet().addCSSAttribute(set, CSS.Attribute.COLOR, colorString);

    set.addAttribute(HTML.Attribute.COLOR, colorString);
    this.applyAttributesGlobally(set, true, false);
}