Java Code Examples for javax.swing.text.SimpleAttributeSet

The following examples show how to use javax.swing.text.SimpleAttributeSet. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: org.alloytools.alloy   Source File: OurConsole.java    License: Apache License 2.0 6 votes vote down vote up
static MutableAttributeSet style(String fontName, int fontSize, boolean boldness, boolean italic, boolean strike, Color color, int leftIndent) {


        fontName = AlloyGraphics.matchBestFontName(fontName);

        MutableAttributeSet s = new SimpleAttributeSet();
        StyleConstants.setFontFamily(s, fontName);
        StyleConstants.setFontSize(s, fontSize);
        StyleConstants.setLineSpacing(s, -0.2f);
        StyleConstants.setBold(s, boldness);
        StyleConstants.setItalic(s, italic);
        StyleConstants.setForeground(s, color);
        StyleConstants.setLeftIndent(s, leftIndent);
        StyleConstants.setStrikeThrough(s, strike);
        return s;
    }
 
Example 2
private SimpleAttributeSet buildText(TextBox box)
{
    VisualContext vc = box.getVisualContext();
    SimpleAttributeSet attr = new SimpleAttributeSet();

    attr.addAttribute(Constants.ATTRIBUTE_FONT_VARIANT, vc.getFontVariant());
    attr.addAttribute(Constants.ATTRIBUTE_TEXT_DECORATION, vc.getTextDecoration());
    attr.addAttribute(Constants.ATTRIBUTE_FONT, vc.getFont());
    attr.addAttribute(Constants.ATTRIBUTE_FOREGROUND, vc.getColor());

    attr.addAttribute(SwingBoxDocument.ElementNameAttribute, Constants.TEXT_BOX);
    attr.addAttribute(Constants.ATTRIBUTE_ANCHOR_REFERENCE, new Anchor());
    attr.addAttribute(Constants.ATTRIBUTE_BOX_REFERENCE, box);

    return attr;
}
 
Example 3
Source Project: netbeans-mmd-plugin   Source File: JHtmlLabel.java    License: Apache License 2.0 6 votes vote down vote up
private void cacheLinkElements() {
  this.linkCache = new ArrayList<>();
  final View view = (View) this.getClientProperty("html"); //NOI18N
  if (view != null) {
    final HTMLDocument doc = (HTMLDocument) view.getDocument();
    final HTMLDocument.Iterator it = doc.getIterator(HTML.Tag.A);
    while (it.isValid()) {
      final SimpleAttributeSet s = (SimpleAttributeSet) it.getAttributes();
      final String link = (String) s.getAttribute(HTML.Attribute.HREF);
      if (link != null) {
        this.linkCache.add(new HtmlLinkAddress(link, it.getStartOffset(), it.getEndOffset()));
      }
      it.next();
    }
  }
}
 
Example 4
/**
 * Creates a new {@link LogRecordEntry} which automatically formats the given {@link LogRecord}
 * with the RapidMiner Studio log styling and default logging format.
 *
 * @param logRecord
 */
public LogRecordEntry(LogRecord logRecord) {

	logLevel = logRecord.getLevel();
	initialString = logRecord.getMessage();

	simpleAttributeSet = new SimpleAttributeSet();
	if (logRecord.getLevel().intValue() >= Level.SEVERE.intValue()) {
		StyleConstants.setForeground(simpleAttributeSet, COLOR_ERROR);
		StyleConstants.setBold(simpleAttributeSet, true);
	} else if (logRecord.getLevel().intValue() >= Level.WARNING.intValue()) {
		StyleConstants.setForeground(simpleAttributeSet, COLOR_WARNING);
		StyleConstants.setBold(simpleAttributeSet, true);
	} else if (logRecord.getLevel().intValue() >= Level.INFO.intValue()) {
		StyleConstants.setForeground(simpleAttributeSet, COLOR_INFO);
		StyleConstants.setBold(simpleAttributeSet, false);
	} else {
		StyleConstants.setForeground(simpleAttributeSet, COLOR_DEFAULT);
		StyleConstants.setBold(simpleAttributeSet, false);
	}

	formattedString = formatter.format(logRecord);
}
 
Example 5
Source Project: netbeans   Source File: GuardedBlockSuppressLayer.java    License: Apache License 2.0 6 votes vote down vote up
private AttributeSet getAttribs(String coloringName, boolean extendsEol, boolean extendsEmptyLine) {
    FontColorSettings fcs = MimeLookup.getLookup(mimeType).lookup(FontColorSettings.class);
    AttributeSet attribs = fcs.getFontColors(coloringName);
    
    if (attribs == null) {
        attribs = SimpleAttributeSet.EMPTY;
    } else if (extendsEol || extendsEmptyLine) {
        attribs = AttributesUtilities.createImmutable(
            attribs, 
            AttributesUtilities.createImmutable(
                ATTR_EXTENDS_EOL, Boolean.valueOf(extendsEol),
                ATTR_EXTENDS_EMPTY_LINE, Boolean.valueOf(extendsEmptyLine))
        );
    }
    
    return attribs;
}
 
Example 6
Source Project: netbeans   Source File: PositionsBagTest.java    License: Apache License 2.0 6 votes vote down vote up
public void testRemoveAligned2Clip() {
    PositionsBag hs = new PositionsBag(doc);
    SimpleAttributeSet attribsA = new SimpleAttributeSet();
    
    attribsA.addAttribute("set-name", "attribsA");
    
    hs.addHighlight(pos(10), pos(40), attribsA);
    hs.removeHighlights(pos(10), pos(20), true);
    GapList<Position> marks = hs.getMarks();
    GapList<AttributeSet> atttributes = hs.getAttributes();
    
    assertEquals("Wrong number of highlights", 2, marks.size());
    assertEquals("1. highlight - wrong start offset", 20, marks.get(0).getOffset());
    assertEquals("1. highlight - wrong end offset", 40, marks.get(1).getOffset());
    assertEquals("1. highlight - wrong attribs", "attribsA", atttributes.get(0).getAttribute("set-name"));
    assertNull("  1. highlight - wrong end", atttributes.get(1));
    
    hs.removeHighlights(pos(30), pos(40), true);
    
    assertEquals("Wrong number of highlights", 2, marks.size());
    assertEquals("1. highlight - wrong start offset", 20, marks.get(0).getOffset());
    assertEquals("1. highlight - wrong end offset", 30, marks.get(1).getOffset());
    assertEquals("1. highlight - wrong attribs", "attribsA", atttributes.get(0).getAttribute("set-name"));
    assertNull("  1. highlight - wrong end", atttributes.get(1));
}
 
Example 7
Source Project: netbeans   Source File: AnnotationColorsPanel.java    License: Apache License 2.0 6 votes vote down vote up
public VersioningSystemColors(OptionsPanelColorProvider provider) {
    this.colors = provider.getColors();
    if (colors == null) {
        throw new NullPointerException("Null colors for " + provider); // NOI18N
    }
    this.provider = provider;
    // initialize saved colors list
    savedColorAttributes = new ArrayList<AttributeSet>(colors.size());
    for (Map.Entry<String, Color[]> e : colors.entrySet()) {
        SimpleAttributeSet sas = new SimpleAttributeSet();
        StyleConstants.setBackground(sas, e.getValue()[0]);
        sas.addAttribute(StyleConstants.NameAttribute, e.getKey());
        sas.addAttribute(EditorStyleConstants.DisplayName, e.getKey());
        savedColorAttributes.add(sas);
    }
}
 
Example 8
Source Project: zxpoly   Source File: JHtmlLabel.java    License: GNU General Public License v3.0 6 votes vote down vote up
private void cacheLinkElements() {
  this.linkCache = new ArrayList<>();
  final View view = (View) this.getClientProperty("html"); //NOI18N
  if (view != null) {
    final HTMLDocument doc = (HTMLDocument) view.getDocument();
    final HTMLDocument.Iterator it = doc.getIterator(HTML.Tag.A);
    while (it.isValid()) {
      final SimpleAttributeSet s = (SimpleAttributeSet) it.getAttributes();
      final String link = (String) s.getAttribute(HTML.Attribute.HREF);
      if (link != null) {
        this.linkCache.add(new HtmlLinkAddress(link, it.getStartOffset(), it.getEndOffset()));
      }
      it.next();
    }
  }
}
 
Example 9
Source Project: netbeans   Source File: DiffColorsPanel.java    License: Apache License 2.0 6 votes vote down vote up
private void updateData () {
    int index = lCategories.getSelectedIndex();
    if (index < 0) return;
    
    List<AttributeSet> categories = getCategories();
    AttributeSet category = categories.get(lCategories.getSelectedIndex());
    SimpleAttributeSet c = new SimpleAttributeSet(category);
    
    Color color = cbBackground.getSelectedColor();
    if (color != null) {
        c.addAttribute(StyleConstants.Background, color);
    } else {
        c.removeAttribute(StyleConstants.Background);
    }
    
    categories.set(index, c);
}
 
Example 10
Source Project: netbeans   Source File: MergePane.java    License: Apache License 2.0 6 votes vote down vote up
void addHighlight (StyledDocument doc, int line1, int line2, final java.awt.Color color) {
    if (line1 > 0) {
        --line1;
    }
    SimpleAttributeSet attrs = new SimpleAttributeSet();
    StyleConstants.setBackground(attrs, color);
    attrs.addAttribute(HighlightsContainer.ATTR_EXTENDS_EOL, Boolean.TRUE);
    int startOffset = getRowStartFromLineOffset(doc, line1);
    int endOffset = getRowStartFromLineOffset(doc, line2);
    synchronized (highlights) {
        ListIterator<HighLight> it = highlights.listIterator();
        HighLight toAdd = new HighLight(startOffset, endOffset, attrs);
        while (it.hasNext()) {
            HighLight highlight = it.next();
            if (highlight.contains(startOffset)) {
                it.remove();
                break;
            } else if (highlight.precedes(startOffset)) {
                it.previous();
                break;
            }
        }
        it.add(toAdd);
    }
    fireHilitingChanged();
}
 
Example 11
Source Project: netbeans   Source File: BlockHighlighting.java    License: Apache License 2.0 6 votes vote down vote up
private AttributeSet getAttribs(String coloringName, boolean extendsEol, boolean extendsEmptyLine) {
    FontColorSettings fcs = MimeLookup.getLookup(getMimeType(component)).lookup(FontColorSettings.class);
    AttributeSet attribs = fcs.getFontColors(coloringName);
    
    if (attribs == null) {
        attribs = SimpleAttributeSet.EMPTY;
    } else if (extendsEol || extendsEmptyLine) {
        attribs = AttributesUtilities.createImmutable(
            attribs, 
            AttributesUtilities.createImmutable(
                ATTR_EXTENDS_EOL, Boolean.valueOf(extendsEol),
                ATTR_EXTENDS_EMPTY_LINE, Boolean.valueOf(extendsEmptyLine))
        );
    }
    
    return attribs;
}
 
Example 12
Source Project: netbeans   Source File: CompoundHighlightsContainerTest.java    License: Apache License 2.0 6 votes vote down vote up
public void testEvents() {
    PlainDocument doc = new PlainDocument();
    PositionsBag hsA = new PositionsBag(doc);
    PositionsBag hsB = new PositionsBag(doc);
    
    CompoundHighlightsContainer chc = new CompoundHighlightsContainer(doc, new HighlightsContainer [] { hsA, hsB });
    Listener listener = new Listener();
    chc.addHighlightsChangeListener(listener);
    
    hsA.addHighlight(new SimplePosition(10), new SimplePosition(20), new SimpleAttributeSet());
    assertEquals("Wrong number of events", 1, listener.eventsCnt);
    assertEquals("Wrong change start offset", 10, listener.lastEventStartOffset);
    assertEquals("Wrong change end offset", 20, listener.lastEventEndOffset);

    listener.reset();
    hsB.addHighlight(new SimplePosition(11), new SimplePosition(12), new SimpleAttributeSet());
    assertEquals("Wrong number of events", 1, listener.eventsCnt);
    assertEquals("Wrong change start offset", 11, listener.lastEventStartOffset);
    assertEquals("Wrong change end offset", 12, listener.lastEventEndOffset);
}
 
Example 13
Source Project: netbeans   Source File: PositionsBagMemoryTest.java    License: Apache License 2.0 6 votes vote down vote up
private void checkMemoryConsumption(boolean merging, boolean bestCase) {
    PositionsBag bag = new PositionsBag(new PlainDocument(), merging);

    for(int i = 0; i < CNT; i++) {
        if (bestCase) {
            bag.addHighlight(new SimplePosition(i * 10), new SimplePosition((i + 1) * 10), SimpleAttributeSet.EMPTY);
        } else {
            bag.addHighlight(new SimplePosition(i * 10), new SimplePosition(i* 10 + 5), SimpleAttributeSet.EMPTY);
        }
    }

    compact(bag);
    
    assertSize("PositionsBag of " + CNT + " highlights " + (bestCase ? "(best case)" : "(worst case)"),
        Collections.singleton(bag), bestCase ? 8500 : 16500, new MF());
}
 
Example 14
Source Project: netbeans   Source File: MergingOffsetsBagTest.java    License: Apache License 2.0 6 votes vote down vote up
public void testAddCompleteMatchOverlap() {
    OffsetsBag hs = new OffsetsBag(doc, true);
    SimpleAttributeSet attribsA = new SimpleAttributeSet();
    SimpleAttributeSet attribsB = new SimpleAttributeSet();
    
    attribsA.addAttribute("set-A", "attribsA");
    attribsB.addAttribute("set-B", "attribsB");
    
    hs.addHighlight(10, 20, attribsA);
    hs.addHighlight(10, 20, attribsB);
    OffsetGapList<OffsetsBag.Mark> marks = hs.getMarks();
    
    assertEquals("Wrong number of highlights", 2, marks.size());
    assertEquals("1. highlight - wrong start offset", 10, marks.get(0).getOffset());
    assertEquals("1. highlight - wrong end offset", 20, marks.get(1).getOffset());
    assertAttribs("1. highlight - wrong attribs", marks.get(0).getAttributes(), "set-A", "set-B");
    assertNull("1. highlight - wrong end", marks.get(1).getAttributes());
}
 
Example 15
Source Project: netbeans   Source File: OffsetsBagTest.java    License: Apache License 2.0 6 votes vote down vote up
public void testAddLeftOverlap() {
    OffsetsBag hs = new OffsetsBag(doc);
    SimpleAttributeSet attribsA = new SimpleAttributeSet();
    SimpleAttributeSet attribsB = new SimpleAttributeSet();
    
    attribsA.addAttribute("set-name", "attribsA");
    attribsB.addAttribute("set-name", "attribsB");
    
    hs.addHighlight(10, 20, attribsA);
    hs.addHighlight(5, 15, attribsB);
    OffsetGapList<OffsetsBag.Mark> marks = hs.getMarks();
    
    assertEquals("Wrong number of highlights", 3, marks.size());
    assertEquals("1. highlight - wrong start offset", 5, marks.get(0).getOffset());
    assertEquals("1. highlight - wrong end offset", 15, marks.get(1).getOffset());
    assertEquals("1. highlight - wrong attribs", "attribsB", marks.get(0).getAttributes().getAttribute("set-name"));
    
    assertEquals("2. highlight - wrong start offset", 15, marks.get(1).getOffset());
    assertEquals("2. highlight - wrong end offset", 20, marks.get(2).getOffset());
    assertEquals("2. highlight - wrong attribs", "attribsA", marks.get(1).getAttributes().getAttribute("set-name"));
    assertNull("  2. highlight - wrong end", marks.get(2).getAttributes());
}
 
Example 16
Source Project: netbeans   Source File: ColorsManager.java    License: Apache License 2.0 5 votes vote down vote up
private static AttributeSet getUnusedFieldAttributes () {
    if (unusedFieldAttributeSet == null) {
        SimpleAttributeSet sas = new SimpleAttributeSet ();
        StyleConstants.setForeground (sas, new Color (115, 115, 115));
        StyleConstants.setBold (sas, true);
        unusedFieldAttributeSet = sas;
    }
    return unusedFieldAttributeSet;
}
 
Example 17
Source Project: jdk1.8-source-analysis   Source File: DocumentParser.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Handle Start Tag.
 */
protected void handleStartTag(TagElement tag) {

    Element elem = tag.getElement();
    if (elem == dtd.body) {
        inbody++;
    } else if (elem == dtd.html) {
    } else if (elem == dtd.head) {
        inhead++;
    } else if (elem == dtd.title) {
        intitle++;
    } else if (elem == dtd.style) {
        instyle++;
    } else if (elem == dtd.script) {
        inscript++;
    }
    if (debugFlag) {
        if (tag.fictional()) {
            debug("Start Tag: " + tag.getHTMLTag() + " pos: " + getCurrentPos());
        } else {
            debug("Start Tag: " + tag.getHTMLTag() + " attributes: " +
                  getAttributes() + " pos: " + getCurrentPos());
        }
    }
    if (tag.fictional()) {
        SimpleAttributeSet attrs = new SimpleAttributeSet();
        attrs.addAttribute(HTMLEditorKit.ParserCallback.IMPLIED,
                           Boolean.TRUE);
        callback.handleStartTag(tag.getHTMLTag(), attrs,
                                getBlockStartPosition());
    } else {
        callback.handleStartTag(tag.getHTMLTag(), getAttributes(),
                                getBlockStartPosition());
        flushAttributes();
    }
}
 
Example 18
Source Project: netbeans   Source File: PositionsBagTest.java    License: Apache License 2.0 5 votes vote down vote up
public void testRemoveCompleteOverlap() {
    PositionsBag hs = new PositionsBag(doc);
    SimpleAttributeSet attribsA = new SimpleAttributeSet();
    
    attribsA.addAttribute("set-name", "attribsA");
    
    hs.addHighlight(pos(10), pos(20), attribsA);
    hs.removeHighlights(5, 25);
    GapList<Position> marks = hs.getMarks();
    
    assertEquals("Wrong number of highlights", 0, marks.size());
}
 
Example 19
@Override
public void renderElementBackground(ElementBox elem)
{
    SimpleAttributeSet attr = buildElementBackground(elem);
    attr.addAttribute(Constants.ATTRIBUTE_DRAWING_ORDER, order++);
    elements.add(new ElementSpec(attr, ElementSpec.ContentType, "*".toCharArray(), 0, 1));
}
 
Example 20
Source Project: netbeans   Source File: ColorsManager.java    License: Apache License 2.0 5 votes vote down vote up
private static AttributeSet getParameterAttributes () {
    if (parameterAttributeSet == null) {
        SimpleAttributeSet sas = new SimpleAttributeSet ();
        StyleConstants.setForeground (sas, new Color (160, 96, 1));
        parameterAttributeSet = sas;
    }
    return parameterAttributeSet;
}
 
Example 21
protected SimpleAttributeSet createAttributes()
{
    SimpleAttributeSet res = new SimpleAttributeSet();
    res.addAttribute(Constants.ATTRIBUTE_ANCHOR_REFERENCE, anchor);
    res.addAttribute(Constants.ATTRIBUTE_BOX_REFERENCE, box);
    return res;
}
 
Example 22
Source Project: netbeans   Source File: OffsetsBagTest.java    License: Apache License 2.0 5 votes vote down vote up
private void removeMiddleClip(int middleMarks) {
    OffsetsBag hs = new OffsetsBag(doc);
    SimpleAttributeSet attribsA = new SimpleAttributeSet();
    SimpleAttributeSet attribsB = new SimpleAttributeSet();
    
    attribsA.addAttribute("set-name", "attribsA");
    attribsB.addAttribute("set-name", "attribsB");
    
    for (int i = 0; i < middleMarks + 1; i++) {
        hs.addHighlight(10 * i + 10, 10 * i + 20, i % 2 == 0 ? attribsA : attribsB);
    }
    
    hs.removeHighlights(15, middleMarks * 10 + 15, true);
    OffsetGapList<OffsetsBag.Mark> marks = hs.getMarks();
    
    assertEquals("Wrong number of highlights (middleMarks = " + middleMarks + ")", 
        4, marks.size());
    assertEquals("1. highlight - wrong start offset (middleMarks = " + middleMarks + ")", 
        10, marks.get(0).getOffset());
    assertEquals("1. highlight - wrong end offset (middleMarks = " + middleMarks + ")", 
        15, marks.get(1).getOffset());
    assertEquals("1. highlight - wrong attribs (middleMarks = " + middleMarks + ")", 
        "attribsA", marks.get(0).getAttributes().getAttribute("set-name"));
    assertNull("  1. highlight - wrong end (middleMarks = " + middleMarks + ")", 
        marks.get(1).getAttributes());

    assertEquals("2. highlight - wrong start offset (middleMarks = " + middleMarks + ")", 
        middleMarks * 10 + 15, marks.get(2).getOffset());
    assertEquals("2. highlight - wrong end offset (middleMarks = " + middleMarks + ")", 
        (middleMarks + 2) * 10, marks.get(3).getOffset());
    assertEquals("2. highlight - wrong attribs (middleMarks = " + middleMarks + ")", 
        middleMarks % 2 == 0 ? "attribsA" : "attribsB", marks.get(2).getAttributes().getAttribute("set-name"));
    assertNull("  2. highlight - wrong end (middleMarks = " + middleMarks + ")", 
        marks.get(3).getAttributes());
}
 
Example 23
Source Project: netbeans   Source File: PositionsBagTest.java    License: Apache License 2.0 5 votes vote down vote up
private void removeMiddleClip(int middleMarks) {
    PositionsBag hs = new PositionsBag(doc);
    SimpleAttributeSet attribsA = new SimpleAttributeSet();
    SimpleAttributeSet attribsB = new SimpleAttributeSet();
    
    attribsA.addAttribute("set-name", "attribsA");
    attribsB.addAttribute("set-name", "attribsB");
    
    for (int i = 0; i < middleMarks + 1; i++) {
        hs.addHighlight(pos(10 * i + 10), pos(10 * i + 20), i % 2 == 0 ? attribsA : attribsB);
    }
    
    hs.removeHighlights(pos(15), pos(middleMarks * 10 + 15), true);
    GapList<Position> marks = hs.getMarks();
    GapList<AttributeSet> atttributes = hs.getAttributes();
    
    assertEquals("Wrong number of highlights (middleMarks = " + middleMarks + ")", 
        4, marks.size());
    assertEquals("1. highlight - wrong start offset (middleMarks = " + middleMarks + ")", 
        10, marks.get(0).getOffset());
    assertEquals("1. highlight - wrong end offset (middleMarks = " + middleMarks + ")", 
        15, marks.get(1).getOffset());
    assertEquals("1. highlight - wrong attribs (middleMarks = " + middleMarks + ")", 
        "attribsA", atttributes.get(0).getAttribute("set-name"));
    assertNull("  1. highlight - wrong end (middleMarks = " + middleMarks + ")", 
        atttributes.get(1));

    assertEquals("2. highlight - wrong start offset (middleMarks = " + middleMarks + ")", 
        middleMarks * 10 + 15, marks.get(2).getOffset());
    assertEquals("2. highlight - wrong end offset (middleMarks = " + middleMarks + ")", 
        (middleMarks + 2) * 10, marks.get(3).getOffset());
    assertEquals("2. highlight - wrong attribs (middleMarks = " + middleMarks + ")", 
        middleMarks % 2 == 0 ? "attribsA" : "attribsB", atttributes.get(2).getAttribute("set-name"));
    assertNull("  2. highlight - wrong end (middleMarks = " + middleMarks + ")", 
        atttributes.get(3));
}
 
Example 24
Source Project: ghidra   Source File: AbstractDetailsPanel.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Adds text to a string buffer as an html-formatted string, adding formatting information
 * as specified.
 * @param buffer the string buffer to add to
 * @param string the string to add
 * @param attributes the formatting instructions
 */
protected void insertHTMLString(StringBuilder buffer, String string,
		SimpleAttributeSet attributes) {

	if (string == null) {
		return;
	}

	buffer.append("<FONT COLOR=\"");

	Color foregroundColor = (Color) attributes.getAttribute(StyleConstants.Foreground);
	buffer.append(HTMLUtilities.toHexString(foregroundColor));

	buffer.append("\" FACE=\"");
	buffer.append(attributes.getAttribute(StyleConstants.FontFamily).toString());

	buffer.append("\">");

	Boolean isBold = (Boolean) attributes.getAttribute(StyleConstants.Bold);
	isBold = (isBold == null) ? Boolean.FALSE : isBold;
	String text = HTMLUtilities.escapeHTML(string);
	if (isBold) {
		text = HTMLUtilities.bold(text);
	}

	buffer.append(text);

	buffer.append("</FONT>");
}
 
Example 25
Source Project: netbeans   Source File: SyntaxHighlighting.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * @param token non-null token.
 * @return attributes for tokenId or null if none found.
 */
synchronized AttributeSet findAttrs(Token token) {
    if (token.id() != TextmateTokenId.TEXTMATE) {
        return null;
    }
    
    List<AttributeSet> attrs = new ArrayList<>();
    List<String> categories = (List<String>) token.getProperty("categories");
    
    for (String category : categories) {
        if (category.startsWith("meta.embedded")) {
            attrs.clear();
        } else {
            attrs.add(scopeName2Coloring.computeIfAbsent(category, c -> {
                String cat = category;

                while (true) {
                    AttributeSet currentAttrs = fcs.getTokenFontColors(cat);

                    if (currentAttrs != null) {
                        return currentAttrs;
                    }

                    int dot = cat.lastIndexOf('.');

                    if (dot == (-1))
                        break;

                    cat = cat.substring(0, dot);
                }

                return SimpleAttributeSet.EMPTY;
            }));
        }
    }

    return AttributesUtilities.createComposite(attrs.toArray(new AttributeSet[0]));
}
 
Example 26
Source Project: netbeans   Source File: PositionsBagTest.java    License: Apache License 2.0 5 votes vote down vote up
public void testAddAll() {
    PositionsBag hsA = new PositionsBag(doc);
    PositionsBag hsB = new PositionsBag(doc);
    
    SimpleAttributeSet attribsA = new SimpleAttributeSet();
    SimpleAttributeSet attribsB = new SimpleAttributeSet();
    SimpleAttributeSet attribsC = new SimpleAttributeSet();

    attribsA.addAttribute("set-name", "attribsA");
    attribsB.addAttribute("set-name", "attribsB");
    attribsC.addAttribute("set-name", "attribsC");
    
    hsA.addHighlight(pos(0), pos(30), attribsA);
    hsA.addHighlight(pos(10), pos(20), attribsB);
    GapList<Position> marksA = hsA.getMarks();
    GapList<AttributeSet> atttributesA = hsA.getAttributes();
    
    hsB.addHighlight(pos(0), pos(40), attribsC);
    hsB.addAllHighlights(hsA);
    GapList<Position> marksB = hsB.getMarks();
    GapList<AttributeSet> atttributesB = hsB.getAttributes();
    
    assertEquals("Wrong number of highlights", marksA.size() + 1, marksB.size());
    for (int i = 0; i < marksA.size() - 1; i++) {
        assertEquals(i + ". highlight - wrong start offset", 
            marksA.get(i).getOffset(), marksB.get(i).getOffset());
        assertEquals(i + ". highlight - wrong end offset", 
            marksA.get(i + 1).getOffset(), marksB.get(i + 1).getOffset());
        assertEquals(i + ". highlight - wrong attribs",
            atttributesA.get(i).getAttribute("set-name"),
            atttributesB.get(i).getAttribute("set-name"));
    }

    assertEquals("4. highlight - wrong start offset", 30, marksB.get(3).getOffset());
    assertEquals("4. highlight - wrong end offset", 40, marksB.get(4).getOffset());
    assertEquals("4. highlight - wrong attribs", "attribsC", atttributesB.get(3).getAttribute("set-name"));
}
 
Example 27
Source Project: bigtable-sql   Source File: MessagePanel.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Add the passed line to the end of the messages display. Position
 * display so the the newly added line will be displayed.
 *
 * @param line		The line to be added.
 * @param saSet		The SimpleAttributeSet to be used for for the string.
 */
private void addLine(String line, SimpleAttributeSet saSet)
{
	if (getDocument().getLength() > 0)
	{
		append("\n", saSet);
	}
	append(line, saSet);
	final int len = getDocument().getLength();
	select(len, len);
}
 
Example 28
Source Project: JDKSourceCode1.8   Source File: DocumentParser.java    License: MIT License 5 votes vote down vote up
/**
 * Handle Start Tag.
 */
protected void handleStartTag(TagElement tag) {

    Element elem = tag.getElement();
    if (elem == dtd.body) {
        inbody++;
    } else if (elem == dtd.html) {
    } else if (elem == dtd.head) {
        inhead++;
    } else if (elem == dtd.title) {
        intitle++;
    } else if (elem == dtd.style) {
        instyle++;
    } else if (elem == dtd.script) {
        inscript++;
    }
    if (debugFlag) {
        if (tag.fictional()) {
            debug("Start Tag: " + tag.getHTMLTag() + " pos: " + getCurrentPos());
        } else {
            debug("Start Tag: " + tag.getHTMLTag() + " attributes: " +
                  getAttributes() + " pos: " + getCurrentPos());
        }
    }
    if (tag.fictional()) {
        SimpleAttributeSet attrs = new SimpleAttributeSet();
        attrs.addAttribute(HTMLEditorKit.ParserCallback.IMPLIED,
                           Boolean.TRUE);
        callback.handleStartTag(tag.getHTMLTag(), attrs,
                                getBlockStartPosition());
    } else {
        callback.handleStartTag(tag.getHTMLTag(), getAttributes(),
                                getBlockStartPosition());
        flushAttributes();
    }
}
 
Example 29
Source Project: wpcleaner   Source File: HTMLPane.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Clear text.
 */
public void clearText() {
  Document doc = getDocument();
  if (doc != null) {
    if (doc instanceof StyledDocument) {
      StyledDocument styledDoc = (StyledDocument) doc;
      styledDoc.setCharacterAttributes(0, doc.getLength(), new SimpleAttributeSet(), true);
      styledDoc.setParagraphAttributes(0, doc.getLength(), new SimpleAttributeSet(), true);
    }
  }
  setText("");
}
 
Example 30
private SimpleAttributeSet createLinkStyle() {
    SimpleAttributeSet linkStyle = new SimpleAttributeSet();

    StyleConstants.setUnderline(linkStyle, true);
    StyleConstants.setBold(linkStyle, true);

    return linkStyle;
}