Package javax.swing.text

Examples of javax.swing.text.Document


      public void actionPerformed(ActionEvent evt)
      {
        try
        {
            Document doc = MessagePanel.this.getDocument();
            doc.remove(0, doc.getLength());
            _lastMessage = null;
        }
        catch (BadLocationException ex)
        {
          s_log.error("Error clearing document", ex);
View Full Code Here


                    getViewport().getViewPosition().y +
                            (int) textEditor.getVisibleRect().getHeight())
            );

            // translate offsets to lines
            Document doc = textEditor.getDocument();
            int startline = doc.getDefaultRootElement().getElementIndex(start) + 1;
            int endline = doc.getDefaultRootElement().getElementIndex(end) + 1;
            Font f = textEditor.getFont();
            int fontHeight = g.getFontMetrics(f).getHeight();
            int fontDesc = g.getFontMetrics(f).getDescent();
            int startingY = -1;
View Full Code Here

    JEditorPane editorPane = new JEditorPane("text/html", srcText);
    editorPane.setEditable(false);

    List elements = new ArrayList();

    Document document = editorPane.getDocument();

    Element root = document.getDefaultRootElement();
    if (root != null)
    {
      addElements(elements, root);
    }

    int startOffset = 0;
    int endOffset = 0;
    int crtOffset = 0;
    String chunk = null;
    JRPrintHyperlink hyperlink = null;
    Element element = null;
    Element parent = null;
    boolean bodyOccurred = false;
    int[] orderedListIndex = new int[elements.size()];
    String whitespace = "    ";
    String[] whitespaces = new String[elements.size()];
    for(int i = 0; i < elements.size(); i++)
    {
      whitespaces[i] = "";
    }
    JRStyledText styledText = new JRStyledText();
   
    for(int i = 0; i < elements.size(); i++)
    {
      if (bodyOccurred && chunk != null)
      {
        styledText.append(chunk);
        Map styleAttributes = getAttributes(element.getAttributes());
        if (hyperlink != null)
        {
          styleAttributes.put(JRTextAttribute.HYPERLINK, hyperlink);
          hyperlink = null;
        }
        if (!styleAttributes.isEmpty())
        {
          styledText.addRun(new JRStyledText.Run(styleAttributes,
              startOffset + crtOffset, endOffset + crtOffset));
        }
      }

      chunk = null;
      element = (Element)elements.get(i);
      parent = element.getParentElement();
      startOffset = element.getStartOffset();
      endOffset = element.getEndOffset();
      AttributeSet attrs = element.getAttributes();

      Object elementName = attrs.getAttribute(AbstractDocument.ElementNameAttribute);
      Object object = (elementName != null) ? null : attrs.getAttribute(StyleConstants.NameAttribute);
      if (object instanceof HTML.Tag)
      {
       
        HTML.Tag htmlTag = (HTML.Tag) object;
        if(htmlTag == Tag.BODY)
        {
          bodyOccurred = true;
          crtOffset = - startOffset;
        }
        else if(htmlTag == Tag.BR)
        {
          chunk = "\n";
        }
        else if(htmlTag == Tag.OL)
        {
          orderedListIndex[i] = 0;
          String parentName = parent.getName().toLowerCase();
          whitespaces[i] = whitespaces[elements.indexOf(parent)] + whitespace;
          if(parentName.equals("li"))
          {
            chunk = "";
          }
          else
          {
            chunk = "\n";
            ++crtOffset;
          }
        }
        else if(htmlTag == Tag.UL)
        {
          whitespaces[i] = whitespaces[elements.indexOf(parent)] + whitespace;

          String parentName = parent.getName().toLowerCase();
          if(parentName.equals("li"))
          {
            chunk = "";
          }
          else
          {
            chunk = "\n";
            ++crtOffset;
          }
         
        }
        else if(htmlTag == Tag.LI)
        {
         
          whitespaces[i] = whitespaces[elements.indexOf(parent)];
          if(element.getElement(0) != null &&
              (element.getElement(0).getName().toLowerCase().equals("ol") || element.getElement(0).getName().toLowerCase().equals("ul"))
              )
          {
            chunk = "";
          }
          else if(parent.getName().equals("ol"))
          {
            int index = elements.indexOf(parent);
            chunk = whitespaces[index] + String.valueOf(++orderedListIndex[index]) + ".  ";
          }
          else
          {
            chunk = whitespaces[elements.indexOf(parent)] + "\u2022  ";
          }
          crtOffset += chunk.length();
        }
        else if (element instanceof LeafElement)
        {
          if (element instanceof RunElement)
          {
            RunElement runElement = (RunElement)element;
            AttributeSet attrSet = (AttributeSet)runElement.getAttribute(Tag.A);
            if (attrSet != null)
            {
              hyperlink = new JRBasePrintHyperlink();
              hyperlink.setHyperlinkType(HyperlinkTypeEnum.REFERENCE);
              hyperlink.setHyperlinkReference((String)attrSet.getAttribute(HTML.Attribute.HREF));
              hyperlink.setLinkTarget((String)attrSet.getAttribute(HTML.Attribute.TARGET));
            }
          }
          try
          {
            chunk = document.getText(startOffset, endOffset - startOffset);
          }
          catch(BadLocationException e)
          {
            if (log.isDebugEnabled())
            {
View Full Code Here

      // Plain Text components don't fire this events.
    }
   
    private void displayEditInfo(DocumentEvent e) {
      String text = null;
      Document doc = (Document)e.getDocument();
      try {
        text = doc.getText(0, doc.getLength());
      }
      catch (BadLocationException es) {
        throw new Error("bad location in AddVMDialog");
      }
      if (which.equals("name"))
View Full Code Here

    JEditorPane editorPane = new JEditorPane("text/rtf", srcText);
    editorPane.setEditable(false);

    List elements = new ArrayList();

    Document document = editorPane.getDocument();

    Element root = document.getDefaultRootElement();
    if (root != null)
    {
      addElements(elements, root);
    }

    String chunk = null;
    Element element = null;
    int startOffset = 0;
    int endOffset = 0;
   
    JRStyledText styledText = new JRStyledText();
    styledText.setGlobalAttributes(new HashMap());
    for(int i = 0; i < elements.size(); i++)
    {
      if (chunk != null)
      {
        styledText.append(chunk);
        styledText.addRun(new JRStyledText.Run(getAttributes(element.getAttributes()), startOffset, endOffset));
      }

      chunk = null;
      element = (Element)elements.get(i);
      startOffset = element.getStartOffset();
      endOffset = element.getEndOffset();

      try
      {
        chunk = document.getText(startOffset, endOffset - startOffset);
      }
      catch(BadLocationException e)
      {
        if (log.isDebugEnabled())
        {
View Full Code Here

            try {
                final Caret caret = messageTextArea.getCaret();
                final int p0 = Math.min(caret.getDot(), caret.getMark());
                final int p1 = Math.max(caret.getDot(), caret.getMark());

                final Document document = messageTextArea.getDocument();
               
                // FIXME: maybe check for a blank before insert of smiley text???
                if (document instanceof PlainDocument) {
                    ((PlainDocument) document).replace(p0, p1 - p0, chosedSmileyText, null);
                } else {
                    if (p0 != p1) {
                        document.remove(p0, p1 - p0);
                    }
                    document.insertString(p0, chosedSmileyText, null);
                }
            } catch (final Throwable ble) {
                logger.log(Level.SEVERE, "Problem while pasting text.", ble);
            }
        }
View Full Code Here

                final Caret caret = sourceTextComponent.getCaret();
                final int p0 = Math.min(caret.getDot(), caret.getMark());
                final int p1 = Math.max(caret.getDot(), caret.getMark());

                final Document document = sourceTextComponent.getDocument();

                if (document instanceof PlainDocument) {
                    ((PlainDocument) document).replace(p0, p1 - p0, text, null);
                } else {
                    if (p0 != p1) {
                        document.remove(p0, p1 - p0);
                    }
                    document.insertString(p0, text, null);
                }
            } catch (final IOException ioe) {
                logger.log(Level.SEVERE, "Problem while pasting text.", ioe);
            } catch (final UnsupportedFlavorException ufe) {
                logger.log(Level.SEVERE, "Problem while pasting text.", ufe);
View Full Code Here

        log.warn(message, exception);

    }
   
    void appendString(String message, Color c) {
        Document doc = console.getDocument();
        if(doc.getLength() != 0) {
            message = "\n" + message;
        }
        SimpleAttributeSet attr = new SimpleAttributeSet();
        StyleConstants.setForeground(attr, c);
        try {
            doc.insertString(doc.getLength(), message, attr);
        }
        catch(Exception e) {           
        }
        console.setCaretPosition(doc.getLength());
        console.scrollRectToVisible(console.getVisibleRect());
    }
View Full Code Here

                // know??"
                text // regular "the tips text"
        };

        String[] initStyles = { "bold", "regular" };
        Document doc = new DefaultStyledDocument();
        taArea0.cut();
        // Insert text on the panel
        try {
            for (int i = 0; i < initString.length; i++) {
                doc.insertString(doc.getLength(), initString[i], taArea0
                        .getStyle(initStyles[i]));
            }
            taArea0.setDocument(doc);
        } catch (BadLocationException ble) {
            System.err.println("Couldn't insert initial text.");
View Full Code Here

     *
     * @param valid_chars
     *            String containing all the valid characters.
     */
    public void setValidCharacters(String valid_chars) {
        Document d = this.getDocument();
        if (d instanceof GPlainDocument) {
            ((GPlainDocument) d).setValidCharacters(valid_chars);
        }
    }
View Full Code Here

TOP

Related Classes of javax.swing.text.Document

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.