Package javax.swing.text

Examples of javax.swing.text.Document


     *
     * @param pattern
     *            The regular expression with which the matching is done.
     */
    public void setPattern(String pattern) {
        Document d = this.getDocument();
        if (d instanceof GPlainDocument) {
            ((GPlainDocument) d).setPattern(pattern);
        }
    }
View Full Code Here


   * @param e
   *            The Event on which this class is working.
   */
  private void process(DocumentEvent e) {
    this.removeAllHighlights();
    Document document = e.getDocument();
    try {
      String text = document.getText(0, document.getLength());
      for (int i = 0; i < highlights.length; i++) {
        String current = highlights[i];
        int index = text.indexOf(current);
        while (index >= 0) {
          this.addHighlight(index, index + current.length(), painter);
View Full Code Here

     * @return the location of the end of the range
     *
     * @exception BadLocationException if the range is invalid
     */
    static int drawUnselectedText( View view, XMLScanner scanner, XMLContext context, Graphics g, int x, int y, int start, int end) throws BadLocationException {
        Document doc = view.getDocument();
        Style lastStyle = null;
        String lastToken = null;
        int mark = start;

        Color forceColor = null;
        if (view instanceof XMLView) {
            Color bgPaintColor = ((XMLView)view).getBgPaintColor();
            if (bgPaintColor != null) {
                if ( bgPaintColor.equals(XMLView.ERROR_COLOR) || bgPaintColor.equals(XMLView.DEBUG_STOP_COLOR) ) {
                    forceColor = Color.white;
                }
            }
        }

        while ( start < end) {
            updateScanner( scanner, doc, start);

            int p = Math.min( scanner.getEndOffset(), end);
            p = (p <= start) ? end : p;

            Style style = context.getStyle( scanner.token);

            // If the style changes, do paint...
            if ( style != lastStyle && lastStyle != null) {
                // color change, flush what we have
                g.setColor( forceColor != null ? forceColor : context.getForeground( lastStyle));
                g.setFont( g.getFont().deriveFont( context.getFontStyle( lastStyle)));

                Segment text = getLineBuffer();
                doc.getText( mark, start - mark, text);

                x = Utilities.drawTabbedText( text, x, y, g, (TabExpander)view, mark);
                mark = start;
            }

            lastToken = scanner.token;
            lastStyle = style;
            start = p;
        }

        // flush remaining
        g.setColor( forceColor != null ? forceColor : context.getForeground( lastStyle));
        g.setFont( g.getFont().deriveFont( context.getFontStyle( lastStyle)));
        Segment text = getLineBuffer();
        doc.getText( mark, end - mark, text);

        x = Utilities.drawTabbedText( text, x, y, g, (TabExpander)view, mark);

        return x;
    }
View Full Code Here

     * @return the location of the end of the range
     *
     * @exception BadLocationException if the range is invalid
     */
    static int drawSelectedText( View view, XMLScanner scanner, XMLContext context, Graphics g, int x, int y, int start, int end) throws BadLocationException {
        Document doc = view.getDocument();
        Style lastStyle = null;
        String lastToken = null;
        int mark = start;

        g.setColor(Color.white);

        while ( start < end) {
            updateScanner( scanner, doc, start);

            int p = Math.min( scanner.getEndOffset(), end);
            p = (p <= start) ? end : p;

            Style style = context.getStyle( scanner.token);

            // If the style changes, do paint...
            if ( style != lastStyle && lastStyle != null) {
                // color change, flush what we have
                g.setFont( g.getFont().deriveFont( context.getFontStyle( lastStyle)));

                Segment text = getLineBuffer();
                doc.getText( mark, start - mark, text);

                x = Utilities.drawTabbedText( text, x, y, g, (TabExpander)view, mark);
                mark = start;
            }

            lastToken = scanner.token;
            lastStyle = style;
            start = p;
        }

        // flush remaining
        g.setFont( g.getFont().deriveFont( context.getFontStyle( lastStyle)));
        Segment text = getLineBuffer();
        doc.getText( mark, end - mark, text);

        x = Utilities.drawTabbedText( text, x, y, g, (TabExpander)view, mark);

        return x;
    }
View Full Code Here

     */
    public WrappedXMLView( XMLContext context, Element elem, boolean wrapStyleWord) throws IOException {
        super( elem, wrapStyleWord);

        this.context = context;
        Document doc = getDocument();

        scanner = new XMLScanner( doc);
    }
View Full Code Here

    /**
     * Performs auto completion.
     */
    public void autoComplete() {
        try {
            Document document = this.xmlPane.getDocument();
            int offset = this.xmlPane.getCaretPosition();
            String text = document.getText(0, offset);

            int openindex = text.lastIndexOf('<');
            int closeindex = text.lastIndexOf('>');

            if (openindex > closeindex) {                   // inside tag definition
View Full Code Here

            }
        }
    }

    private void completeAttribute(String name) throws BadLocationException {
        Document document = xmlPane.getDocument();
        int pos = xmlPane.getCaretPosition();
        String cursorChar = document.getText(pos, 1);
        boolean toAppendSpace = !">".equals(cursorChar) && !"".equals(cursorChar.trim()) && !"/".equals(cursorChar);
        String template = (name + "=\"\"" + (toAppendSpace ? " " : "")).substring(this.prefixLength);

        document.insertString(pos, template, null);
        xmlPane.setCaretPosition( xmlPane.getCaretPosition() - 1 );
    }
View Full Code Here

        document.insertString(pos, template, null);
        xmlPane.setCaretPosition( xmlPane.getCaretPosition() - 1 );
    }

    private void completeAttributeValue(String value) throws BadLocationException {
        Document document = xmlPane.getDocument();
        int pos = xmlPane.getCaretPosition();
        String text = document.getText(0, pos);
        int startTagIndex = text.lastIndexOf("<");
        if (startTagIndex >= 0) {
            int quoteIndex = Math.max( text.lastIndexOf("\""), text.lastIndexOf("\'") );
            if (quoteIndex > 0 && quoteIndex > startTagIndex) {
                document.remove(quoteIndex + 1, pos - quoteIndex - 1);
                document.insertString(quoteIndex + 1, value, null);
            }
        }
    }
View Full Code Here

            }
        }
    }

    private void completeTag(String name) throws BadLocationException {
        Document document = xmlPane.getDocument();
        int pos = xmlPane.getCaretPosition();

        if ( CDATA_NAME.equals(name) ) {
            document.insertString(pos, "<![CDATA[  ]]>".substring(this.prefixLength), null);
            xmlPane.setCaretPosition( xmlPane.getCaretPosition() - 4 );
        } else if ( XML_COMMENT_NAME.equals(name) ) {
            document.insertString(pos, "<!--  -->".substring(this.prefixLength), null);
            xmlPane.setCaretPosition( xmlPane.getCaretPosition() - 4 );
        } else {
            ElementInfo info = DefinitionResolver.getElementInfo(name);
            if (info != null) {
                String template = info.getTemplate(true).substring(this.prefixLength);
                document.insertString(pos, template, null);
                int closingIndex = template.lastIndexOf("</");
                if (closingIndex >= 0) {
                    xmlPane.setCaretPosition( xmlPane.getCaretPosition() - template.length() + closingIndex );
                }
            }
View Full Code Here

    private class CompleterKeyListener extends KeyAdapter {
        public void keyPressed(KeyEvent e) {
            char ch = e.getKeyChar();
            int code = e.getKeyCode();
            if ( (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || ch == '-' || code == KeyEvent.VK_BACK_SPACE ) {
                Document document = xmlPane.getDocument();
                int pos = xmlPane.getCaretPosition();
                try {
                    // deleting or inserting new character
                    if (code == MenuKeyEvent.VK_BACK_SPACE) {
                        if ( pos > 0 && document.getLength() > 0 ) {
                            document.remove(pos - 1, 1);
                        }
                    } else {
                        document.insertString(pos, String.valueOf(ch), null);
                    }
                } catch (BadLocationException e1) {
                    e1.printStackTrace();
                }
                popupMenu.setVisible(false);
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.