下面列出了javax.swing.text.SimpleAttributeSet#addAttribute ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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());
}
public void test122663_AddBothMatch() throws BadLocationException {
doc.insertString(0, "01234567890123456789", null);
SimpleAttributeSet attribsA = new SimpleAttributeSet();
SimpleAttributeSet attribsB = new SimpleAttributeSet();
attribsA.addAttribute("set-name", "attribsA");
attribsB.addAttribute("set-name", "attribsB");
OffsetsBag bag = new OffsetsBag(doc);
bag.addHighlight(10, 15, attribsA);
bag.addHighlight(10, 15, attribsB);
assertMarks("Wrong highlights", createOffsetsBag(10, 15, attribsB), bag);
}
protected SimpleAttributeSet createAttributes()
{
// called from getAttributes()
SimpleAttributeSet res = super.createAttributes();
res.addAttribute(Constants.ATTRIBUTE_REPLACED_CONTENT, content);
return res;
}
private static AttributeSet getHyperlinkPressedAS () {
if (hyperlinkPressedAS == null) {
SimpleAttributeSet as = new SimpleAttributeSet ();
as.addAttribute (StyleConstants.Foreground, Color.red);
as.addAttribute (StyleConstants.Underline, Color.red);
hyperlinkPressedAS = as;
}
return hyperlinkPressedAS;
}
private AttributeSet getHighlightAS () {
if (highlightAS == null) {
SimpleAttributeSet as = new SimpleAttributeSet ();
as.addAttribute (StyleConstants.Background, color);
highlightAS = as;
}
return highlightAS;
}
/**
* 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();
}
}
/**
* 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();
}
}
private static SimpleAttributeSet createColoring(
String colorName,
String displayName,
String defaultColor,
String foreground,
String background,
String underline,
String waveunderline,
String strikethrough,
String fontName,
String fontType
) {
SimpleAttributeSet coloring = new SimpleAttributeSet();
coloring.addAttribute(StyleConstants.NameAttribute, colorName);
coloring.addAttribute(EditorStyleConstants.DisplayName, displayName);
if (defaultColor != null)
coloring.addAttribute(EditorStyleConstants.Default, defaultColor);
if (foreground != null)
coloring.addAttribute(StyleConstants.Foreground, readColor(foreground));
if (background != null)
coloring.addAttribute(StyleConstants.Background, readColor(background));
if (strikethrough != null)
coloring.addAttribute(StyleConstants.StrikeThrough, readColor(strikethrough));
if (underline != null)
coloring.addAttribute(StyleConstants.Underline, readColor(underline));
if (waveunderline != null)
coloring.addAttribute(EditorStyleConstants.WaveUnderlineColor, readColor(waveunderline));
if (fontName != null)
coloring.addAttribute(StyleConstants.FontFamily, fontName);
if (fontType != null) {
if (fontType.toLowerCase().indexOf("bold") >= 0)
coloring.addAttribute(StyleConstants.Bold, Boolean.TRUE);
if (fontType.toLowerCase().indexOf("italic") >= 0)
coloring.addAttribute(StyleConstants.Italic, Boolean.TRUE);
}
return coloring;
}
/**
* 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();
}
}
/**
* 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();
}
}
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"));
}
public void testRemoveCompleteOverlap() {
OffsetsBag hs = new OffsetsBag(doc);
SimpleAttributeSet attribsA = new SimpleAttributeSet();
attribsA.addAttribute("set-name", "attribsA");
hs.addHighlight(10, 20, attribsA);
hs.removeHighlights(5, 25, false);
OffsetGapList<OffsetsBag.Mark> marks = hs.getMarks();
assertEquals("Wrong number of highlights", 0, marks.size());
}
public void testRemoveSplit() {
OffsetsBag hs = new OffsetsBag(doc);
SimpleAttributeSet attribsA = new SimpleAttributeSet();
attribsA.addAttribute("set-name", "attribsA");
hs.addHighlight(10, 25, attribsA);
hs.removeHighlights(15, 20, false);
OffsetGapList<OffsetsBag.Mark> marks = hs.getMarks();
assertEquals("Wrong number of highlights", 0, marks.size());
}
private static AttributeSet getHyperlinkAS () {
if (hyperlinkAS == null) {
SimpleAttributeSet as = new SimpleAttributeSet ();
as.addAttribute (StyleConstants.Foreground, Color.blue);
as.addAttribute (StyleConstants.Underline, Color.blue);
hyperlinkAS = as;
}
return hyperlinkAS;
}
/**
* Handle Empty Tag.
*/
protected void handleEmptyTag(TagElement tag) throws ChangedCharSetException {
Element elem = tag.getElement();
if (elem == dtd.meta && !ignoreCharSet) {
SimpleAttributeSet atts = getAttributes();
if (atts != null) {
String content = (String)atts.getAttribute(HTML.Attribute.CONTENT);
if (content != null) {
if ("content-type".equalsIgnoreCase((String)atts.getAttribute(HTML.Attribute.HTTPEQUIV))) {
if (!content.equalsIgnoreCase("text/html") &&
!content.equalsIgnoreCase("text/plain")) {
throw new ChangedCharSetException(content, false);
}
} else if ("charset" .equalsIgnoreCase((String)atts.getAttribute(HTML.Attribute.HTTPEQUIV))) {
throw new ChangedCharSetException(content, true);
}
}
}
}
if (inbody != 0 || elem == dtd.meta || elem == dtd.base || elem == dtd.isindex || elem == dtd.style || elem == dtd.link) {
if (debugFlag) {
if (tag.fictional()) {
debug("Empty Tag: " + tag.getHTMLTag() + " pos: " + getCurrentPos());
} else {
debug("Empty Tag: " + tag.getHTMLTag() + " attributes: "
+ getAttributes() + " pos: " + getCurrentPos());
}
}
if (tag.fictional()) {
SimpleAttributeSet attrs = new SimpleAttributeSet();
attrs.addAttribute(HTMLEditorKit.ParserCallback.IMPLIED,
Boolean.TRUE);
callback.handleSimpleTag(tag.getHTMLTag(), attrs,
getBlockStartPosition());
} else {
callback.handleSimpleTag(tag.getHTMLTag(), getAttributes(),
getBlockStartPosition());
flushAttributes();
}
}
}
/**
* Handle Empty Tag.
*/
protected void handleEmptyTag(TagElement tag) throws ChangedCharSetException {
Element elem = tag.getElement();
if (elem == dtd.meta && !ignoreCharSet) {
SimpleAttributeSet atts = getAttributes();
if (atts != null) {
String content = (String)atts.getAttribute(HTML.Attribute.CONTENT);
if (content != null) {
if ("content-type".equalsIgnoreCase((String)atts.getAttribute(HTML.Attribute.HTTPEQUIV))) {
if (!content.equalsIgnoreCase("text/html") &&
!content.equalsIgnoreCase("text/plain")) {
throw new ChangedCharSetException(content, false);
}
} else if ("charset" .equalsIgnoreCase((String)atts.getAttribute(HTML.Attribute.HTTPEQUIV))) {
throw new ChangedCharSetException(content, true);
}
}
}
}
if (inbody != 0 || elem == dtd.meta || elem == dtd.base || elem == dtd.isindex || elem == dtd.style || elem == dtd.link) {
if (debugFlag) {
if (tag.fictional()) {
debug("Empty Tag: " + tag.getHTMLTag() + " pos: " + getCurrentPos());
} else {
debug("Empty Tag: " + tag.getHTMLTag() + " attributes: "
+ getAttributes() + " pos: " + getCurrentPos());
}
}
if (tag.fictional()) {
SimpleAttributeSet attrs = new SimpleAttributeSet();
attrs.addAttribute(HTMLEditorKit.ParserCallback.IMPLIED,
Boolean.TRUE);
callback.handleSimpleTag(tag.getHTMLTag(), attrs,
getBlockStartPosition());
} else {
callback.handleSimpleTag(tag.getHTMLTag(), getAttributes(),
getBlockStartPosition());
flushAttributes();
}
}
}
public void testSetColors() {
MimePath mimePath = MimePath.parse("text/x-type-A");
Lookup lookup = MimeLookup.getLookup(mimePath);
FontColorSettings fcs = lookup.lookup(FontColorSettings.class);
assertNotNull("Can't find FontColorSettings", fcs);
// Check preconditions
AttributeSet attribs = fcs.getTokenFontColors("test-set-all");
assertNotNull("Can't find test-set-all coloring", attribs);
assertEquals("Wrong background color",
new Color(0x0A0B0C), attribs.getAttribute(StyleConstants.Background));
assertEquals("Wrong foreground color",
new Color(0x0D0E0F), attribs.getAttribute(StyleConstants.Foreground));
assertEquals("Wrong underline color",
new Color(0x010203), attribs.getAttribute(StyleConstants.Underline));
assertEquals("Wrong strikeThrough color",
new Color(0x040506), attribs.getAttribute(StyleConstants.StrikeThrough));
assertEquals("Wrong waveUnderline color",
new Color(0x070809), attribs.getAttribute(EditorStyleConstants.WaveUnderlineColor));
// Prepare new coloring
SimpleAttributeSet newAttribs = new SimpleAttributeSet();
newAttribs.addAttribute(StyleConstants.NameAttribute, "test-set-all");
newAttribs.addAttribute(StyleConstants.Background, new Color(0xFFFFF0));
newAttribs.addAttribute(StyleConstants.Foreground, new Color(0xFFFFF1));
newAttribs.addAttribute(StyleConstants.Underline, new Color(0xFFFFF2));
newAttribs.addAttribute(StyleConstants.StrikeThrough, new Color(0xFFFFF3));
newAttribs.addAttribute(EditorStyleConstants.WaveUnderlineColor, new Color(0xFFFFF4));
// Change the coloring
setOneColoring("text/x-type-A", newAttribs);
// Check that the new attributes were set
fcs = lookup.lookup(FontColorSettings.class);
assertNotNull("Can't find FontColorSettings", fcs);
attribs = fcs.getTokenFontColors("test-set-all");
assertNotNull("Can't find test-set-all coloring", attribs);
assertEquals("Wrong background color",
new Color(0xFFFFF0), attribs.getAttribute(StyleConstants.Background));
assertEquals("Wrong foreground color",
new Color(0xFFFFF1), attribs.getAttribute(StyleConstants.Foreground));
assertEquals("Wrong underline color",
new Color(0xFFFFF2), attribs.getAttribute(StyleConstants.Underline));
assertEquals("Wrong strikeThrough color",
new Color(0xFFFFF3), attribs.getAttribute(StyleConstants.StrikeThrough));
assertEquals("Wrong waveUnderline color",
new Color(0xFFFFF4), attribs.getAttribute(EditorStyleConstants.WaveUnderlineColor));
}
/**
* Handle Empty Tag.
*/
protected void handleEmptyTag(TagElement tag) throws ChangedCharSetException {
Element elem = tag.getElement();
if (elem == dtd.meta && !ignoreCharSet) {
SimpleAttributeSet atts = getAttributes();
if (atts != null) {
String content = (String)atts.getAttribute(HTML.Attribute.CONTENT);
if (content != null) {
if ("content-type".equalsIgnoreCase((String)atts.getAttribute(HTML.Attribute.HTTPEQUIV))) {
if (!content.equalsIgnoreCase("text/html") &&
!content.equalsIgnoreCase("text/plain")) {
throw new ChangedCharSetException(content, false);
}
} else if ("charset" .equalsIgnoreCase((String)atts.getAttribute(HTML.Attribute.HTTPEQUIV))) {
throw new ChangedCharSetException(content, true);
}
}
}
}
if (inbody != 0 || elem == dtd.meta || elem == dtd.base || elem == dtd.isindex || elem == dtd.style || elem == dtd.link) {
if (debugFlag) {
if (tag.fictional()) {
debug("Empty Tag: " + tag.getHTMLTag() + " pos: " + getCurrentPos());
} else {
debug("Empty Tag: " + tag.getHTMLTag() + " attributes: "
+ getAttributes() + " pos: " + getCurrentPos());
}
}
if (tag.fictional()) {
SimpleAttributeSet attrs = new SimpleAttributeSet();
attrs.addAttribute(HTMLEditorKit.ParserCallback.IMPLIED,
Boolean.TRUE);
callback.handleSimpleTag(tag.getHTMLTag(), attrs,
getBlockStartPosition());
} else {
callback.handleSimpleTag(tag.getHTMLTag(), getAttributes(),
getBlockStartPosition());
flushAttributes();
}
}
}
/**
* Handle Empty Tag.
*/
protected void handleEmptyTag(TagElement tag) throws ChangedCharSetException {
Element elem = tag.getElement();
if (elem == dtd.meta && !ignoreCharSet) {
SimpleAttributeSet atts = getAttributes();
if (atts != null) {
String content = (String)atts.getAttribute(HTML.Attribute.CONTENT);
if (content != null) {
if ("content-type".equalsIgnoreCase((String)atts.getAttribute(HTML.Attribute.HTTPEQUIV))) {
if (!content.equalsIgnoreCase("text/html") &&
!content.equalsIgnoreCase("text/plain")) {
throw new ChangedCharSetException(content, false);
}
} else if ("charset" .equalsIgnoreCase((String)atts.getAttribute(HTML.Attribute.HTTPEQUIV))) {
throw new ChangedCharSetException(content, true);
}
}
}
}
if (inbody != 0 || elem == dtd.meta || elem == dtd.base || elem == dtd.isindex || elem == dtd.style || elem == dtd.link) {
if (debugFlag) {
if (tag.fictional()) {
debug("Empty Tag: " + tag.getHTMLTag() + " pos: " + getCurrentPos());
} else {
debug("Empty Tag: " + tag.getHTMLTag() + " attributes: "
+ getAttributes() + " pos: " + getCurrentPos());
}
}
if (tag.fictional()) {
SimpleAttributeSet attrs = new SimpleAttributeSet();
attrs.addAttribute(HTMLEditorKit.ParserCallback.IMPLIED,
Boolean.TRUE);
callback.handleSimpleTag(tag.getHTMLTag(), attrs,
getBlockStartPosition());
} else {
callback.handleSimpleTag(tag.getHTMLTag(), getAttributes(),
getBlockStartPosition());
flushAttributes();
}
}
}
private void addMiddle(int middleMarks) {
OffsetsBag hs = new OffsetsBag(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");
for (int i = 0; i < middleMarks + 1; i++) {
hs.addHighlight(10 * i + 10, 10 * i + 20, i % 2 == 0 ? attribsA : attribsB);
}
hs.addHighlight(15, middleMarks * 10 + 15, attribsC);
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"));
assertEquals("2. highlight - wrong start offset (middleMarks = " + middleMarks + ")",
15, marks.get(1).getOffset());
assertEquals("2. highlight - wrong end offset (middleMarks = " + middleMarks + ")",
middleMarks * 10 + 15, marks.get(2).getOffset());
assertEquals("2. highlight - wrong attribs (middleMarks = " + middleMarks + ")",
"attribsC", marks.get(1).getAttributes().getAttribute("set-name"));
assertEquals("3. highlight - wrong start offset (middleMarks = " + middleMarks + ")",
middleMarks * 10 + 15, marks.get(2).getOffset());
assertEquals("3. highlight - wrong end offset (middleMarks = " + middleMarks + ")",
(middleMarks + 2) * 10, marks.get(3).getOffset());
assertEquals("3. highlight - wrong attribs (middleMarks = " + middleMarks + ")",
middleMarks % 2 == 0 ? "attribsA" : "attribsB", marks.get(2).getAttributes().getAttribute("set-name"));
assertNull(" 3. highlight - wrong end (middleMarks = " + middleMarks + ")",
marks.get(3).getAttributes());
}