Java Code Examples for javax.swing.text.SimpleAttributeSet#EMPTY

The following examples show how to use javax.swing.text.SimpleAttributeSet#EMPTY . 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 File: AttributesUtilities.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Creates an immutable <code>AttributeSet</code> as a copy of <code>AttributeSet</code>s
 * passed into this method. If the <code>AttributeSet</code>s
 * contain attributes with the same name the resulting <code>AttributeSet</code>
 * will return value of the first attribute it will find going through
 * the sets in the order as they were passed in.
 *
 * @param sets    The <code>AttributeSet</code>s which attributes will become
 *                a contents of the newly created <code>AttributeSet</code>.
 *
 * @return The new immutable <code>AttributeSet</code>.
 */
public static AttributeSet createImmutable(AttributeSet... sets) {
    if (true) {
        return org.netbeans.modules.editor.settings.AttrSet.merge(sets);
    }
    HashMap<Object, Object> map = new HashMap<Object, Object>();
    
    for(int i = sets.length - 1; i >= 0; i--) {
        AttributeSet set = sets[i];
        for(Enumeration<?> keys = set.getAttributeNames(); keys.hasMoreElements(); ) {
            Object attrKey = keys.nextElement();
            Object attrValue = set.getAttribute(attrKey);

            map.put(attrKey, attrValue);
        }
    }

    return map.size() > 0 ? new Immutable(map) : SimpleAttributeSet.EMPTY;
}
 
Example 2
Source File: GuardedBlockSuppressLayer.java    From netbeans with 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 3
Source File: NonLexerSyntaxHighlighting.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private AttributeSet findAttribs(TokenItem tokenItem) {
    synchronized (this) {
        AttributeSet attribs = attribsCache.get(tokenItem.getTokenID());

        if (attribs == null) {
            FontColorSettings fcs = MimeLookup.getLookup(mimePath).lookup(FontColorSettings.class);
            if (fcs != null) {
                attribs = findFontAndColors(fcs, tokenItem);
                if (attribs == null) {
                    attribs = SimpleAttributeSet.EMPTY;
                }
                
                attribsCache.put(tokenItem.getTokenID(), attribs);
            } else {
                LOG.warning("Can't find FCS for mime path: '" + mimePath.getPath() + "'"); //NOI18N
            }
        }

        return attribs == null ? SimpleAttributeSet.EMPTY : attribs;
    }
}
 
Example 4
Source File: CaretBasedBlockHighlighting.java    From netbeans with Apache License 2.0 6 votes vote down vote up
void setAttrs(Lookup.Result<FontColorSettings> result) {
    if (Boolean.TRUE.equals(component.getClientProperty("AsTextField"))) {
        if (UIManager.get("TextField.selectionBackground") != null) {
            attribs = AttributesUtilities.createImmutable(
                    StyleConstants.Background, (Color) UIManager.get("TextField.selectionBackground"),
                    StyleConstants.Foreground, (Color) UIManager.get("TextField.selectionForeground"));
        } else {
            final JTextField referenceTextField = (JTextField) new JComboBox<String>().getEditor().getEditorComponent();
            attribs = AttributesUtilities.createImmutable(
                    StyleConstants.Background, referenceTextField.getSelectionColor(),
                    StyleConstants.Foreground, referenceTextField.getSelectedTextColor());
        }
        return;
    }
    FontColorSettings fcs = result.allInstances().iterator().next();
    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)));
    }
}
 
Example 5
Source File: BlockHighlighting.java    From netbeans with 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 6
Source File: ComposedTextHighlighting.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private AttributeSet translateAttributes(Map<AttributedCharacterIterator.Attribute, ?> source) {
    for(AttributedCharacterIterator.Attribute sourceKey : source.keySet()) {
        Object sourceValue = source.get(sourceKey);
        
        // Ignore any non-input method related highlights
        if (!(sourceValue instanceof InputMethodHighlight)) {
            continue;
        }
        
        InputMethodHighlight imh = (InputMethodHighlight) sourceValue;
        
        if (imh.isSelected()) {
            return highlightInverse;
        } else {
            return highlightUnderlined;
        }
    }
    
    LOG.fine("No translation for " + source);
    return SimpleAttributeSet.EMPTY;
}
 
Example 7
Source File: EmbeddingHighlightsContainer.java    From netbeans with Apache License 2.0 5 votes vote down vote up
EmbeddingHighlightsContainer(Document document) {
    this.document = document;

    //try load the background from tpl settings
    FontColorSettings fcs = MimeLookup.getLookup(TplDataLoader.MIME_TYPE).lookup(FontColorSettings.class);
    Color tplBG = null;
    if (fcs != null) {
        tplBG = getColoring(fcs, TPL_BACKGROUND_TOKEN_NAME);
    }

    tplBackground = tplBG == null ? SimpleAttributeSet.EMPTY : AttributesUtilities.createImmutable(
        StyleConstants.Background, tplBG, ATTR_EXTENDS_EOL, Boolean.TRUE);
}
 
Example 8
Source File: AttributesUtilities.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a proxy <code>AttributeSet</code> that will delegate to the
 * <code>AttributeSet</code>s passed in as a parameter. If the <code>AttributeSet</code>s
 * contain attributes with the same name the composite <code>AttributeSet</code>
 * will return value of the first attribute it will find going through
 * the sets in the order as they were passed in.
 *
 * @param sets    The <code>AttributeSet</code>s to delegate to.
 *
 * @return The new composite <code>AttributeSet</code> that will delegate
 *         to the <code>sets</code> passed in.
 */
public static AttributeSet createComposite(AttributeSet... sets) {
    if (true) {
        return org.netbeans.modules.editor.settings.AttrSet.merge(sets);
    }
    if (sets.length == 0) {
        return SimpleAttributeSet.EMPTY;
    } else if (sets.length == 1) {
        return sets[0];
    } else {
        LinkedList<AttributeSet> all = new LinkedList<AttributeSet>();

        for(AttributeSet s : sets) {
            if (s instanceof AttributesUtilities.CompositeAttributeSet) {
                all.addAll(((AttributesUtilities.CompositeAttributeSet) s).getDelegates());
            } else if (s != null && s != SimpleAttributeSet.EMPTY) {
                all.add(s);
            }
        }

        switch (all.size()) {
            case 0: return SimpleAttributeSet.EMPTY;
            case 1: return all.get(0);
            case 2: return new Composite2(all.get(0), all.get(1));
            case 3: return new Composite4(all.get(0), all.get(1), all.get(2), null);
            case 4: return new Composite4(all.get(0), all.get(1), all.get(2), all.get(3));
            default: return new BigComposite(all);
        }
    }
}
 
Example 9
Source File: CompoundAttributes.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private AttributeSet firstItemAttrs() {
    AttributeSet attrs = highlightItems[0].getAttributes();
    if (attrs == null) {
        attrs = SimpleAttributeSet.EMPTY;
    }
    return attrs;
}
 
Example 10
Source File: BracesMatchHighlighting.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public BracesMatchHighlighting(JTextComponent component, Document document) {
    this.document = document;

    String mimeType = getMimeType(component);
    MimePath mimePath = mimeType == null ? MimePath.EMPTY : MimePath.parse(mimeType);

    // Load the colorings
    FontColorSettings fcs = MimeLookup.getLookup(mimePath).lookup(FontColorSettings.class);
    AttributeSet match = fcs.getFontColors(BRACES_MATCH_COLORING);
    AttributeSet mismatch = fcs.getFontColors(BRACES_MISMATCH_COLORING);
    AttributeSet matchMultichar = fcs.getFontColors(BRACES_MATCH_MULTICHAR_COLORING);
    AttributeSet mismatchMultichar = fcs.getFontColors(BRACES_MISMATCH_MULTICHAR_COLORING);
    this.bracesMatchColoring = match != null ? match : SimpleAttributeSet.EMPTY;
    this.bracesMismatchColoring = mismatch != null ? mismatch : SimpleAttributeSet.EMPTY;
    this.bracesMatchMulticharColoring = matchMultichar != null ? matchMultichar : SimpleAttributeSet.EMPTY;
    this.bracesMismatchMulticharColoring = mismatchMultichar != null ? mismatchMultichar : SimpleAttributeSet.EMPTY;
    
    // Create and hook up the highlights bag
    this.bag = new OffsetsBag(document, true);
    this.bag.addHighlightsChangeListener(this);
    
    // Hook up the component
    this.component = component;
    this.component.addPropertyChangeListener(WeakListeners.propertyChange(this, this.component));

    // Hook up the caret
    this.caret = component.getCaret();
    if (this.caret != null) {
        this.caretListener = WeakListeners.change(this, this.caret);
        this.caret.addChangeListener(caretListener);
    }

    // Refresh the layer
    refresh();
}
 
Example 11
Source File: MasterMatcherTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testAreas() throws Exception {
    MockServices.setServices(MockMimeLookup.class);
    MockMimeLookup.setInstances(MimePath.EMPTY, new TestMatcher());
    
    AttributeSet EAS = SimpleAttributeSet.EMPTY;
    JEditorPane c = new JEditorPane();
    Document d = c.getDocument();
    OffsetsBag bag = new OffsetsBag(d);
    d.insertString(0, "text text { text } text", null);

    c.putClientProperty(MasterMatcher.PROP_MAX_BACKWARD_LOOKAHEAD, 256);
    c.putClientProperty(MasterMatcher.PROP_MAX_FORWARD_LOOKAHEAD, 256);
    
    TestMatcher.origin = new int [] { 2, 3 };
    TestMatcher.matches = new int [] { 10, 11 };
    
    MasterMatcher.get(c).highlight(d, 7, bag, EAS, EAS, EAS, EAS);
    TestMatcher.waitUntilCreated(1000);
    {
    TestMatcher tm = TestMatcher.lastMatcher;
    assertNotNull("No matcher created", tm);
    
    HighlightsSequence hs = bag.getHighlights(0, Integer.MAX_VALUE);
    assertTrue("Wrong number of highlighted areas", hs.moveNext());
    assertEquals("Wrong origin startOfset", 2, hs.getStartOffset());
    assertEquals("Wrong origin endOfset", 3, hs.getEndOffset());
    
    assertTrue("Wrong number of highlighted areas", hs.moveNext());
    assertEquals("Wrong match startOfset", 10, hs.getStartOffset());
    assertEquals("Wrong match endOfset", 11, hs.getEndOffset());
    }        
}
 
Example 12
Source File: TextRegionManager.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private static AttributeSet getSyncedTextBlocksHighlight() {
    FontColorSettings fcs = MimeLookup.getLookup(MimePath.EMPTY).lookup(FontColorSettings.class);
    AttributeSet as = fcs.getFontColors("synchronized-text-blocks-ext"); //NOI18N
    return as == null ? SimpleAttributeSet.EMPTY : as;
}
 
Example 13
Source File: AbstractPositionElement.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public AttributeSet getAttributes() {
    // Do not return null since Swing's view factories assume that this is non-null.
    return SimpleAttributeSet.EMPTY;
}
 
Example 14
Source File: LineRootElement.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public AttributeSet getAttributes() {
    // Do not return null since Swing's view factories assume that this is non-null.
    return SimpleAttributeSet.EMPTY;
}
 
Example 15
Source File: LineElement.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public AttributeSet getAttributes() {
    // Do not return null since Swing's view factories assume that this is non-null.
    return (attributes instanceof AttributeSet) ? (AttributeSet) attributes : SimpleAttributeSet.EMPTY;
}
 
Example 16
Source File: AbstractRootElement.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public AttributeSet getAttributes() {
    return SimpleAttributeSet.EMPTY;
}
 
Example 17
Source File: GsfSemanticLayer.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public AttributeSet getAttributes() {
    return (element != null)
            ? layer.getColoring(element.coloring, element.language)
            : SimpleAttributeSet.EMPTY;
}
 
Example 18
Source File: TextSearchHighlighting.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private AttributeSet getAttribs() {
    FontColorSettings fcs = MimeLookup.getLookup(mimePath).lookup(FontColorSettings.class);
    AttributeSet attribs = fcs.getFontColors(FontColorNames.HIGHLIGHT_SEARCH_COLORING);
    return attribs == null ? SimpleAttributeSet.EMPTY : attribs;
}
 
Example 19
Source File: BaseElement.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/** Get attributes of this element */
public AttributeSet getAttributes() {
    AttributeSet as = attrs;
    return as == null ? SimpleAttributeSet.EMPTY : as;
}
 
Example 20
Source File: LineElement.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public AttributeSet getAttributes() {
    AttributeSet as = attributes;
    return as == null ? SimpleAttributeSet.EMPTY : as;
}