Example usage for javax.swing.text Element getParentElement

List of usage examples for javax.swing.text Element getParentElement

Introduction

In this page you can find the example usage for javax.swing.text Element getParentElement.

Prototype

public Element getParentElement();

Source Link

Document

Fetches the parent element.

Usage

From source file:plugin.notes.gui.NotesView.java

private void handleBackspace() {
    // TODO: This sucks, clean it up
    Element elem;
    int pos = editor.getCaretPosition();
    StyledDocument htmlDoc = editor.getStyledDocument();

    try {/*from   w  w  w.ja v a 2  s. c  o  m*/
        if (pos > 0) {
            if ((editor.getSelectedText()) != null) {
                ExtendedHTMLEditorKit.delete(editor);
                return;
            }

            int sOffset = htmlDoc.getParagraphElement(pos).getStartOffset();

            if (sOffset == editor.getSelectionStart()) {

                if (ExtendedHTMLEditorKit
                        .checkParentsTag(htmlDoc.getParagraphElement(editor.getCaretPosition()), HTML.Tag.LI)) {
                    elem = ExtendedHTMLEditorKit
                            .getListItemParent(htmlDoc.getCharacterElement(editor.getCaretPosition()));
                    boolean content = false;
                    int so = elem.getStartOffset();
                    int eo = elem.getEndOffset();

                    if ((so + 1) < eo) {
                        char[] temp = editor.getText(so, eo - so).toCharArray();
                        for (char aTemp : temp) {
                            if (!Character.isWhitespace(aTemp)) {
                                content = true;
                            }
                        }
                    }

                    if (!content) {
                        elem.getParentElement();
                        ExtendedHTMLEditorKit.removeTag(editor, elem);
                        editor.setCaretPosition(sOffset - 1);
                        return;
                    }
                    editor.setCaretPosition(editor.getCaretPosition() - 1);
                    editor.moveCaretPosition(editor.getCaretPosition() - 2);
                    editor.replaceSelection("");
                    return;
                }
            }

            editor.replaceSelection("");
        }
    } catch (BadLocationException ble) {
        Logging.errorPrint(ble.getMessage(), ble);
    }
}

From source file:plugin.notes.gui.NotesView.java

private void manageListElement(ExtendedHTMLDocument htmlDoc) {
    Element h = ExtendedHTMLEditorKit.getListItemParent(htmlDoc.getCharacterElement(editor.getCaretPosition()));
    h.getParentElement();
    ExtendedHTMLEditorKit.removeTag(editor, h);
}

From source file:ru.gelin.fictionbook.reader.models.FBSimpleElementTest.java

@Test
public void testGetParentElementNull() {
    Node node = fb.getDocument().getRootElement();
    Element root = document.getElement(node);
    assertNull(root.getParentElement());
}

From source file:tufts.vue.RichTextBox.java

/***********
 *******NOTES FROM FONT EDITOR PANEL TO HELP WITH COPY/PASTE OF STYLES.
globalSizeListener = new ActionListener(){
        /*w  w w  .  j a v a  2 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); 
          }*/
}