Package javax.swing.text

Examples of javax.swing.text.StyledDocument


    stats = new JTextPane();
    stats.setEditable(false);
    stats.setBackground(getBackground());

    // Add styles to use for different types of status messages
    StyledDocument doc = (StyledDocument) stats.getDocument();

    Style style = doc.addStyle(STYLE_REDIRECT, null);
    StyleConstants.setForeground(style, REDIRECT_COLOR);

    style = doc.addStyle(STYLE_CLIENT_ERROR, null);
    StyleConstants.setForeground(style, CLIENT_ERROR_COLOR);

    style = doc.addStyle(STYLE_SERVER_ERROR, null);
    StyleConstants.setForeground(style, SERVER_ERROR_COLOR);

    JScrollPane pane = makeScrollPane(stats);
    pane.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
    return pane;
View Full Code Here


     * Renders the content.
     */
    private void init() {
        String content = getContent();
        setContentType("text/html");
        StyledDocument doc = getStyledDocument();
        SimpleAttributeSet s = new SimpleAttributeSet();
        StyleConstants.setAlignment(s, StyleConstants.ALIGN_CENTER);
        StyleConstants.setBold(s, true);
        try {
            doc.setParagraphAttributes(0,content.length(), s, false);
            doc.insertString(0, content, s);
            if (_showWebsite) {
              String webContent = Version.getWebSite();
              SimpleAttributeSet w = new SimpleAttributeSet();
                StyleConstants.setAlignment(w, StyleConstants.ALIGN_CENTER);
                StyleConstants.setUnderline(w, true);
                SimpleAttributeSet hrefAttr = new SimpleAttributeSet();
                hrefAttr.addAttribute(HTML.Attribute.HREF, Version.getWebSite());
                w.addAttribute(HTML.Tag.A, hrefAttr);
                doc.setParagraphAttributes(content.length(),webContent.length(), w, false);
                doc.insertString(content.length(), webContent, w);
                if (Desktop.isDesktopSupported()){
                  addMouseListener(this);
                  addMouseMotionListener(this);
                }
            }
View Full Code Here

  /**
   * Creates the annotation display.
   */
  private void displayAnnotations() {
    // for speed, detach document from text pane before updating
    StyledDocument doc = (StyledDocument) textPane.getDocument();
    Document blank = new DefaultStyledDocument();
    textPane.setDocument(blank);

    // make sure annotationCheckboxPanel is showing
    if (legendScrollPane.getViewport().getView() != annotationCheckboxPanel) {
      legendScrollPane.setViewportView(annotationCheckboxPanel);
    }

    // add text from CAS
    try {
      doc.remove(0, doc.getLength());
      doc.insertString(0, mCAS.getDocumentText(), new SimpleAttributeSet());
    } catch (BadLocationException e) {
      throw new RuntimeException(e);
    }

    // Iterate over annotations
    FSIterator iter = mCAS.getAnnotationIndex().iterator();
    Hashtable checkBoxes = new Hashtable();
    HashSet checkBoxesDone = new HashSet();
    while (iter.isValid()) {
      AnnotationFS fs = (AnnotationFS) iter.get();
      iter.moveToNext();

      Type type = fs.getType();

      // have we seen this type before?
      JCheckBox checkbox = (JCheckBox) mTypeToCheckboxMap.get(type);
      if (checkbox == null) {
        // check that type should be displayed
        if ((mDisplayedTypeNames == null || typeNamesContains(mDisplayedTypeNames, type.getName()))
                && !typeNamesContains(mHiddenTypeNames, type.getName())) {
          // if mTypeNameToColorMap exists, get color from there
          Color c = (Color) mTypeNameToColorMap.get(type.getName());
          if (c == null) // assign next available color
          {
            c = COLORS[mTypeNameToColorMap.size() % COLORS.length];
            mTypeNameToColorMap.put(type.getName(), c);
          }
          // This next section required until priorities work properly
          // HashSet noCheckSet = new HashSet();
          String noCheckArray[] = {
          // "org.apache.jresporator.PROPER",
          // "DOCSTRUCT_ANNOT_TYPE",
          // "VOCAB_ANNOT_TYPE"
          };
          for (int i = 0; i < noCheckArray.length; i++) {
            noCheckSet.add(noCheckArray[i]);
          }
          // end of section

          // should type be initially selected?
          boolean selected = ((mInitiallySelectedTypeNames == null &&
          // document annotation is not initially selected in default case
                  !CAS.TYPE_NAME_DOCUMENT_ANNOTATION.equals(type.getName()) && !noCheckSet
                  .contains(type.getName()) // priorities JMP
          ) || (mInitiallySelectedTypeNames != null && typeNamesContains(
                  mInitiallySelectedTypeNames, type.getName())));

          // add checkbox
          checkbox = new JCheckBox(type.getShortName(), selected);
          checkbox.setToolTipText(type.getName());
          checkbox.addActionListener(this);
          checkbox.setBackground(c);
          // annotationCheckboxPanel.add(checkbox); do it later JMP
          checkBoxes.put(type.getName(), checkbox);
          checkBoxesDone.add(checkbox);
          // add to (Type, Checkbox) map
          mTypeToCheckboxMap.put(type, checkbox);
        } else {
          // this type is not hidden, skip it
          continue;
        }
      }
      // if checkbox is checked, assign color to text
      if (checkbox.isSelected()) {
        int begin = fs.getBegin();
        int end = fs.getEnd();

        // Be careful of 0-length annotations and annotations that span the
        // entire document. In either of these cases, if we try to set
        // background color, it will set the input text style, which is not
        // what we want.
        if (begin == 0 && end == mCAS.getDocumentText().length()) {
          end--;
        }

        if (begin < end) {
          MutableAttributeSet attrs = new SimpleAttributeSet();
          StyleConstants.setBackground(attrs, checkbox.getBackground());
          doc.setCharacterAttributes(begin, end - begin, attrs, false);
        }
      }
    }

    // now populate panel with checkboxes in order specified in user file. JMP
View Full Code Here

  /**
   * Creates the entity display.
   */
  private void displayEntities() {
    // for speed, detach document from text pane before updating
    StyledDocument doc = (StyledDocument) textPane.getDocument();
    Document blank = new DefaultStyledDocument();
    textPane.setDocument(blank);

    // make sure entityCheckboxPanel is showing
    if (legendScrollPane.getViewport().getView() != entityCheckboxPanel) {
      legendScrollPane.setViewportView(entityCheckboxPanel);
    }

    // add text from CAS
    try {
      doc.remove(0, doc.getLength());
      doc.insertString(0, mCAS.getDocumentText(), new SimpleAttributeSet());
    } catch (BadLocationException e) {
      throw new RuntimeException(e);
    }

    // Iterate over EntityAnnotations using JCAS, because the EntityResolver interface
    // uses JCAS as a convenience to the user.
    JCas jcas;
    try {
      // NOTE: for a large type system, this can take a few seconds, which results in a
      // noticeable delay when the user first switches to Entity mode.
      jcas = mCAS.getJCas();
    } catch (CASException e) {
      throw new RuntimeException(e);
    }
    FSIterator iter = jcas.getAnnotationIndex().iterator();
    while (iter.isValid()) {
      Annotation annot = (Annotation) iter.get();
      iter.moveToNext();

      // find out what entity this annotation represents
      EntityResolver.Entity entity = mEntityResolver.getEntity(annot);

      //if not an entity, skip it
      if (entity == null)
        continue;
     
      // have we seen this entity before?
      JCheckBox checkbox = (JCheckBox) mEntityToCheckboxMap.get(entity);
      if (checkbox == null) {
        // assign next available color
        Color c = COLORS[mEntityToCheckboxMap.size() % COLORS.length];
        // add checkbox
        checkbox = new JCheckBox(entity.getCanonicalForm(), true);
        checkbox.setToolTipText(entity.getCanonicalForm());
        checkbox.addActionListener(this);
        checkbox.setBackground(c);
        entityCheckboxPanel.add(checkbox);
        // add to (Entity, Checkbox) map
        mEntityToCheckboxMap.put(entity, checkbox);
      }

      // if checkbox is checked, assign color to text
      if (checkbox.isSelected()) {
        int begin = annot.getBegin();
        int end = annot.getEnd();
        // be careful of 0-length annotation. If we try to set background color when there
        // is no selection, it will set the input text style, which is not what we want.
        if (begin != end) {
          MutableAttributeSet attrs = new SimpleAttributeSet();
          StyleConstants.setBackground(attrs, checkbox.getBackground());
          doc.setCharacterAttributes(begin, end - begin, attrs, false);
        }
      }
    }

    // add/remove checkboxes from display as determined by the
View Full Code Here

      // match
      int pos = 0;
      while (matcher.find(pos)) {
        MutableAttributeSet attrs = new SimpleAttributeSet();
        StyleConstants.setBold(attrs, true);
        StyledDocument doc = (StyledDocument) textPane.getDocument();
        doc.setCharacterAttributes(matcher.start(), matcher.end() - matcher.start(), attrs, false);

        if (pos == matcher.end()) // infinite loop check
          break;
        else
          pos = matcher.end();
      }
    }
    // Spans
    int docLength = mCAS.getDocumentText().length();
    int len = mBoldfaceSpans.length;
    len -= len % 2; // to avoid ArrayIndexOutOfBoundsException if some numbskull passes in an
    // odd-length array
    int i = 0;
    while (i < len) {
      int begin = mBoldfaceSpans[i];
      int end = mBoldfaceSpans[i + 1];
      if (begin >= 0 && begin <= docLength && end >= 0 && end <= docLength) {
        MutableAttributeSet attrs = new SimpleAttributeSet();
        StyleConstants.setBold(attrs, true);
        StyledDocument doc = (StyledDocument) textPane.getDocument();
        doc.setCharacterAttributes(begin, end - begin, attrs, false);
      }
      i += 2;
    }
  }
View Full Code Here

  }

  private String convert(InputStream rtfDocumentInputStream) throws IOException {
    RTFEditorKit aRtfEditorkit = new RTFEditorKit();

    StyledDocument styledDoc = new DefaultStyledDocument();

    String textDocument;

    try {
      aRtfEditorkit.read(rtfDocumentInputStream, styledDoc, 0);

      textDocument = styledDoc.getText(0, styledDoc.getLength());
    } catch (BadLocationException e) {
      throw new IOException("Error during parsing");
    }

    return textDocument;
View Full Code Here

        assertFalse(Color.BLUE.equals(style.getAttribute(StyleConstants.Foreground)));
        textPane.setForeground(Color.BLUE);
        assertEquals(Color.BLUE, style.getAttribute(StyleConstants.Foreground));
        // Document
        style.addAttribute(StyleConstants.Subscript, Boolean.TRUE);
        StyledDocument newDoc = new DefaultStyledDocument();
        Style newStyle = newDoc.getStyle(StyleContext.DEFAULT_STYLE);
        assertNull(newStyle.getAttribute(StyleConstants.FontSize));
        assertNull(newStyle.getAttribute(StyleConstants.FontFamily));
        newStyle.addAttribute(StyleConstants.FontFamily, "family2");
        newStyle.addAttribute(StyleConstants.FontSize, new Integer(10));
        newStyle.addAttribute(StyleConstants.Italic, Boolean.FALSE);
View Full Code Here

    public void testProPertyChange_FontFamilyName() {
        Style style = textPane.getStyle(StyleContext.DEFAULT_STYLE);
        textPane.setFont(font);
        assertFalse(font.getFamily().equals(style.getAttribute(StyleConstants.FontFamily)));
        assertEquals(font.getName(), style.getAttribute(StyleConstants.FontFamily));
        StyledDocument newDoc = new DefaultStyledDocument();
        Style newStyle = newDoc.getStyle(StyleContext.DEFAULT_STYLE);
        textPane.setDocument(newDoc);
        assertFalse(font.getFamily().equals(newStyle.getAttribute(StyleConstants.FontFamily)));
        assertEquals(font.getName(), newStyle.getAttribute(StyleConstants.FontFamily));
    }
View Full Code Here

            protected EditorKit createDefaultEditorKit() {
                return new StyledEditorKit();
            }
        };
        pane.setEditorKit(new StyledEditorKit());
        StyledDocument doc = (StyledDocument) pane.getDocument();
        StyledEditorKit kit = (StyledEditorKit) pane.getEditorKit();
        try {
            MutableAttributeSet attrs = new SimpleAttributeSet();
            StyleConstants.setUnderline(attrs, true);
            doc.insertString(0, "Hello word!", attrs);
            pane.setCaretPosition(4);
            attrs = new SimpleAttributeSet();
            StyleConstants.setIcon(attrs, MetalIconFactory.getTreeFolderIcon());
            doc.insertString(4, " ", attrs);
            pane.setCaretPosition(4);
            doc.insertString(4, "\n", kit.getInputAttributes());
            assertFalse(StyleConstants.isUnderline(kit.getInputAttributes()));
            pane.setCaretPosition(5);
            assertFalse(StyleConstants.isUnderline(kit.getInputAttributes()));
        } catch (BadLocationException e) {
        }
View Full Code Here

        StyleConstants.setUnderline(attrs, false);
        textPane.getDocument().insertString(6, "world", attrs);
    }

    public void testReplaceSelection_AtTheBeginningOfParagraph() {
        StyledDocument doc = textPane.getStyledDocument();
        try {
            attrs = new SimpleAttributeSet();
            StyleConstants.setUnderline(attrs, true);
            doc.insertString(0, "Hello word!", attrs);
            textPane.setCaretPosition(4);
            textPane.insertIcon(MetalIconFactory.getTreeFolderIcon());
            textPane.setCaretPosition(4);
            textPane.replaceSelection("\n");
            textPane.setCaretPosition(5);
            textPane.replaceSelection("a");
            textPane.setCaretPosition(6);
            textPane.replaceSelection("b");
            assertNull(StyleConstants.getIcon(doc.getCharacterElement(5).getAttributes()));
            assertNull(StyleConstants.getIcon(doc.getCharacterElement(6).getAttributes()));
        } catch (BadLocationException e) {
        }
    }
View Full Code Here

TOP

Related Classes of javax.swing.text.StyledDocument

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.