下面列出了javax.swing.text.AttributeSet#getAttributeNames ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/** Creates a new instance of Highlighting */
public Highlighting(Document doc) {
AttributeSet firstLineFontColor = MimeLookup.getLookup(MimePath.get("text/x-java")).lookup(FontColorSettings.class).getTokenFontColors("javadoc-first-sentence"); //NOI18N
AttributeSet commentFontColor = MimeLookup.getLookup(MimePath.get("text/x-java")).lookup(FontColorSettings.class).getTokenFontColors("comment"); //NOI18N
if(firstLineFontColor != null && commentFontColor != null) {
Collection<Object> attrs = new LinkedList<>();
for (Enumeration<?> e = firstLineFontColor.getAttributeNames(); e.hasMoreElements(); ) {
Object key = e.nextElement();
Object value = firstLineFontColor.getAttribute(key);
if (!commentFontColor.containsAttribute(key, value)) {
attrs.add(key);
attrs.add(value);
}
}
fontColor = AttributesUtilities.createImmutable(attrs.toArray());
} else {
fontColor = AttributesUtilities.createImmutable();
LOG.warning("FontColorSettings for javadoc-first-sentence or comment are not available."); //NOI18N
}
this.document = doc;
hierarchy = TokenHierarchy.get(document);
if (hierarchy != null) {
hierarchy.addTokenHierarchyListener(WeakListeners.create(TokenHierarchyListener.class, this, hierarchy));
}
}
private void translateEmbeddedAttributes(AttributeSet htmlAttrSet,
MutableAttributeSet cssAttrSet) {
Enumeration<?> keys = htmlAttrSet.getAttributeNames();
if (htmlAttrSet.getAttribute(StyleConstants.NameAttribute) ==
HTML.Tag.HR) {
// HR needs special handling due to us treating it as a leaf.
translateAttributes(HTML.Tag.HR, htmlAttrSet, cssAttrSet);
}
while (keys.hasMoreElements()) {
Object key = keys.nextElement();
if (key instanceof HTML.Tag) {
HTML.Tag tag = (HTML.Tag)key;
Object o = htmlAttrSet.getAttribute(tag);
if (o != null && o instanceof AttributeSet) {
translateAttributes(tag, (AttributeSet)o, cssAttrSet);
}
} else if (key instanceof CSS.Attribute) {
cssAttrSet.addAttribute(key, htmlAttrSet.getAttribute(key));
}
}
}
public boolean containsAttributes(AttributeSet attributes) {
for(Enumeration<?> keys = attributes.getAttributeNames(); keys.hasMoreElements(); ) {
Object key = keys.nextElement();
Object value = attributes.getAttribute(key);
if (!containsAttribute(key, value)) {
return false;
}
}
return true;
}
private static Object getAttribute(AttributeSet attributes, String name) {
for (Enumeration<?> enumeration = attributes.getAttributeNames(); enumeration.hasMoreElements();) {
Object nameKey = enumeration.nextElement();
if (name.equals(nameKey.toString())) {
return attributes.getAttribute(nameKey);
}
}
return null;
}
@Override
public boolean containsAttributes(AttributeSet attributes) {
for(Enumeration<?> keys = attributes.getAttributeNames(); keys.hasMoreElements(); ) {
Object key = keys.nextElement();
Object value = attributes.getAttribute(key);
if (!containsAttribute(key, value)) {
return false;
}
}
return true;
}
private void dumpAttribs(AttributeSet attribs, String name, boolean tokenColoring) {
// if (!allFcsi[0].getMimePath().getPath().equals("text/x-java")) { //NOI18N
// return;
// }
StringBuilder sb = new StringBuilder();
sb.append("Attribs for base mime path '"); //NOI18N
sb.append(allFcsi[0].getMimePath().getPath());
sb.append("' and "); //NOI18N
if (tokenColoring) {
sb.append("token '"); //NOI18N
} else {
sb.append("highlight '"); //NOI18N
}
sb.append(name);
sb.append("' = {"); //NOI18N
Enumeration<?> keys = attribs.getAttributeNames();
while (keys.hasMoreElements()) {
Object key = keys.nextElement();
Object value = attribs.getAttribute(key);
sb.append("'").append(key).append("' = '").append(value).append("'"); //NOI18N
if (keys.hasMoreElements()) {
sb.append(", "); //NOI18N
}
}
sb.append("} CompoundFCS.this = "); //NOI18N
sb.append(this.toString());
System.out.println(sb.toString());
}
private static Map<String, Map<String, String>> normalize(Collection<AttributeSet> colorings) {
Map<String, Map<String, String>> norm = new TreeMap<String, Map<String, String>>();
for(AttributeSet as : colorings) {
String name = (String) as.getAttribute(StyleConstants.NameAttribute);
assertNotNull("NameAttribute should not be null", name);
name = "'" + name + "'";
assertFalse("Duplicate AttributeSet with name " + name, norm.containsKey(name));
Map<String, String> attribs = new TreeMap<String, String>();
norm.put(name, attribs);
Enumeration<? extends Object> names = as.getAttributeNames();
while(names.hasMoreElements()) {
Object attrName = names.nextElement();
Object attrValue = as.getAttribute(attrName);
String normalizedName = attrName == null ? "'null'" : "'" + attrName.toString() + "'";
String normalizedValue = attrValue == null ? "'null'" : "'" + attrValue.toString() + "'";
assertFalse("Duplicate attribute '" + normalizedName + "'", attribs.containsKey(normalizedName));
attribs.put(normalizedName, normalizedValue);
}
}
return norm;
}
public void addAttributes(AttributeSet attr)
{
Enumeration as = attr.getAttributeNames();
while(as.hasMoreElements()) {
Object el = as.nextElement();
backing.put(el, attr.getAttribute(el));
}
}
public void addAttributes(AttributeSet attr)
{
Enumeration as = attr.getAttributeNames();
while(as.hasMoreElements()) {
Object el = as.nextElement();
backing.put(el, attr.getAttribute(el));
}
}
public static String getAttribute(Element e, Attribute ab){
AttributeSet mAb=e.getAttributes();
for (Enumeration it = mAb.getAttributeNames() ; it.hasMoreElements() ;) {
Object s= it.nextElement();
if (s.equals(ab))
return (String) mAb.getAttribute(ab);
}
return null;
}
private void dumpAndSelect(int row, int col, int select) {
oper.setCaretPosition(row, col);
oper.pressKey(KeyEvent.VK_BACK_SPACE, KeyEvent.ALT_DOWN_MASK);
JDialogOperator jdo = new JDialogOperator(MainWindowOperator.getDefault());
JListOperator jlo = new JListOperator(jdo);
ListModel model = jlo.getModel();
int i;
for (i = 0; i < model.getSize(); i++) {
Object item = model.getElementAt(i);
if(item instanceof CodeDeleter) {
CodeDeleter codeDeleter = (CodeDeleter) item;
ref(codeDeleter.getDisplayName());
HighlightsSequence highlights = codeDeleter.getHighlight().getHighlights(0, oper.getText().length());
while(highlights.moveNext()) {
ref(highlights.getStartOffset()+" "+highlights.getEndOffset());
AttributeSet attributes = highlights.getAttributes();
Enumeration<?> attributeNames = attributes.getAttributeNames();
while(attributeNames.hasMoreElements()) {
Object nextElement = attributeNames.nextElement();
ref(nextElement+" "+attributes.getAttribute(nextElement));
}
}
}
}
if(select>-1) {
jlo.selectItem(select);
ref(oper.getText());
}
}
public void addAttributes(AttributeSet attr)
{
Enumeration as = attr.getAttributeNames();
while(as.hasMoreElements()) {
Object el = as.nextElement();
backing.put(el, attr.getAttribute(el));
}
}
public void addAttributes(AttributeSet attr)
{
Enumeration<?> as = attr.getAttributeNames();
while(as.hasMoreElements()) {
Object el = as.nextElement();
backing.put(el, attr.getAttribute(el));
}
}
public synchronized boolean containsAttributes(AttributeSet attributes) {
for(Enumeration<?> names = attributes.getAttributeNames(); names.hasMoreElements(); ) {
Object name = names.nextElement();
Object value = attributes.getAttribute(name);
if (!containsAttribute(name, value)) {
return false;
}
}
return true;
}
public boolean containsAttributes(AttributeSet attributes) {
for(Enumeration<?> keys = attributes.getAttributeNames(); keys.hasMoreElements(); ) {
Object key = keys.nextElement();
Object value = attributes.getAttribute(key);
if (!containsAttribute(key, value)) {
return false;
}
}
return true;
}
@SuppressWarnings("LeakingThisInConstructor")
public ElementTreePanel(JTextComponent editor) {
this.editor = editor;
Document document = editor.getDocument();
// Create the tree.
treeModel = new ElementTreeModel(document);
tree = new JTree(treeModel) {
@Override
public String convertValueToText(Object value, boolean selected,
boolean expanded, boolean leaf,
int row, boolean hasFocus) {
// Should only happen for the root
if (!(value instanceof Element)) {
return value.toString();
}
Element e = (Element) value;
AttributeSet as = e.getAttributes().copyAttributes();
String asString;
if (as != null) {
StringBuilder retBuffer = new StringBuilder("[");
Enumeration names = as.getAttributeNames();
while (names.hasMoreElements()) {
Object nextName = names.nextElement();
if (nextName != StyleConstants.ResolveAttribute) {
retBuffer.append(" ");
retBuffer.append(nextName);
retBuffer.append("=");
retBuffer.append(as.getAttribute(nextName));
}
}
retBuffer.append(" ]");
asString = retBuffer.toString();
} else {
asString = "[ ]";
}
if (e.isLeaf()) {
return e.getName() + " [" + e.getStartOffset() + ", " + e.
getEndOffset() + "] Attributes: " + asString;
}
return e.getName() + " [" + e.getStartOffset() + ", " + e.
getEndOffset() + "] Attributes: " + asString;
}
};
tree.addTreeSelectionListener(this);
tree.setDragEnabled(true);
// Don't show the root, it is fake.
tree.setRootVisible(false);
// Since the display value of every node after the insertion point
// changes every time the text changes and we don't generate a change
// event for all those nodes the display value can become off.
// This can be seen as '...' instead of the complete string value.
// This is a temporary workaround, increase the needed size by 15,
// hoping that will be enough.
tree.setCellRenderer(new DefaultTreeCellRenderer() {
@Override
public Dimension getPreferredSize() {
Dimension retValue = super.getPreferredSize();
if (retValue != null) {
retValue.width += 15;
}
return retValue;
}
});
// become a listener on the document to update the tree.
document.addDocumentListener(this);
// become a PropertyChangeListener to know when the Document has
// changed.
editor.addPropertyChangeListener(this);
// Become a CaretListener
editor.addCaretListener(this);
// configure the panel and frame containing it.
setLayout(new BorderLayout());
add(new JScrollPane(tree), BorderLayout.CENTER);
// Add a label above tree to describe what is being shown
JLabel label = new JLabel("Elements that make up the current document",
SwingConstants.CENTER);
label.setFont(new Font("Dialog", Font.BOLD, 14));
add(label, BorderLayout.NORTH);
setPreferredSize(new Dimension(400, 400));
}
@SuppressWarnings("LeakingThisInConstructor")
public ElementTreePanel(JTextComponent editor) {
this.editor = editor;
Document document = editor.getDocument();
// Create the tree.
treeModel = new ElementTreeModel(document);
tree = new JTree(treeModel) {
@Override
public String convertValueToText(Object value, boolean selected,
boolean expanded, boolean leaf,
int row, boolean hasFocus) {
// Should only happen for the root
if (!(value instanceof Element)) {
return value.toString();
}
Element e = (Element) value;
AttributeSet as = e.getAttributes().copyAttributes();
String asString;
if (as != null) {
StringBuilder retBuffer = new StringBuilder("[");
Enumeration names = as.getAttributeNames();
while (names.hasMoreElements()) {
Object nextName = names.nextElement();
if (nextName != StyleConstants.ResolveAttribute) {
retBuffer.append(" ");
retBuffer.append(nextName);
retBuffer.append("=");
retBuffer.append(as.getAttribute(nextName));
}
}
retBuffer.append(" ]");
asString = retBuffer.toString();
} else {
asString = "[ ]";
}
if (e.isLeaf()) {
return e.getName() + " [" + e.getStartOffset() + ", " + e.
getEndOffset() + "] Attributes: " + asString;
}
return e.getName() + " [" + e.getStartOffset() + ", " + e.
getEndOffset() + "] Attributes: " + asString;
}
};
tree.addTreeSelectionListener(this);
tree.setDragEnabled(true);
// Don't show the root, it is fake.
tree.setRootVisible(false);
// Since the display value of every node after the insertion point
// changes every time the text changes and we don't generate a change
// event for all those nodes the display value can become off.
// This can be seen as '...' instead of the complete string value.
// This is a temporary workaround, increase the needed size by 15,
// hoping that will be enough.
tree.setCellRenderer(new DefaultTreeCellRenderer() {
@Override
public Dimension getPreferredSize() {
Dimension retValue = super.getPreferredSize();
if (retValue != null) {
retValue.width += 15;
}
return retValue;
}
});
// become a listener on the document to update the tree.
document.addDocumentListener(this);
// become a PropertyChangeListener to know when the Document has
// changed.
editor.addPropertyChangeListener(this);
// Become a CaretListener
editor.addCaretListener(this);
// configure the panel and frame containing it.
setLayout(new BorderLayout());
add(new JScrollPane(tree), BorderLayout.CENTER);
// Add a label above tree to describe what is being shown
JLabel label = new JLabel("Elements that make up the current document",
SwingConstants.CENTER);
label.setFont(new Font("Dialog", Font.BOLD, 14));
add(label, BorderLayout.NORTH);
setPreferredSize(new Dimension(400, 400));
}
@SuppressWarnings("LeakingThisInConstructor")
public ElementTreePanel(JTextComponent editor) {
this.editor = editor;
Document document = editor.getDocument();
// Create the tree.
treeModel = new ElementTreeModel(document);
tree = new JTree(treeModel) {
@Override
public String convertValueToText(Object value, boolean selected,
boolean expanded, boolean leaf,
int row, boolean hasFocus) {
// Should only happen for the root
if (!(value instanceof Element)) {
return value.toString();
}
Element e = (Element) value;
AttributeSet as = e.getAttributes().copyAttributes();
String asString;
if (as != null) {
StringBuilder retBuffer = new StringBuilder("[");
Enumeration names = as.getAttributeNames();
while (names.hasMoreElements()) {
Object nextName = names.nextElement();
if (nextName != StyleConstants.ResolveAttribute) {
retBuffer.append(" ");
retBuffer.append(nextName);
retBuffer.append("=");
retBuffer.append(as.getAttribute(nextName));
}
}
retBuffer.append(" ]");
asString = retBuffer.toString();
} else {
asString = "[ ]";
}
if (e.isLeaf()) {
return e.getName() + " [" + e.getStartOffset() + ", " + e.
getEndOffset() + "] Attributes: " + asString;
}
return e.getName() + " [" + e.getStartOffset() + ", " + e.
getEndOffset() + "] Attributes: " + asString;
}
};
tree.addTreeSelectionListener(this);
tree.setDragEnabled(true);
// Don't show the root, it is fake.
tree.setRootVisible(false);
// Since the display value of every node after the insertion point
// changes every time the text changes and we don't generate a change
// event for all those nodes the display value can become off.
// This can be seen as '...' instead of the complete string value.
// This is a temporary workaround, increase the needed size by 15,
// hoping that will be enough.
tree.setCellRenderer(new DefaultTreeCellRenderer() {
@Override
public Dimension getPreferredSize() {
Dimension retValue = super.getPreferredSize();
if (retValue != null) {
retValue.width += 15;
}
return retValue;
}
});
// become a listener on the document to update the tree.
document.addDocumentListener(this);
// become a PropertyChangeListener to know when the Document has
// changed.
editor.addPropertyChangeListener(this);
// Become a CaretListener
editor.addCaretListener(this);
// configure the panel and frame containing it.
setLayout(new BorderLayout());
add(new JScrollPane(tree), BorderLayout.CENTER);
// Add a label above tree to describe what is being shown
JLabel label = new JLabel("Elements that make up the current document",
SwingConstants.CENTER);
label.setFont(new Font("Dialog", Font.BOLD, 14));
add(label, BorderLayout.NORTH);
setPreferredSize(new Dimension(400, 400));
}
@SuppressWarnings("LeakingThisInConstructor")
public ElementTreePanel(JTextComponent editor) {
this.editor = editor;
Document document = editor.getDocument();
// Create the tree.
treeModel = new ElementTreeModel(document);
tree = new JTree(treeModel) {
@Override
public String convertValueToText(Object value, boolean selected,
boolean expanded, boolean leaf,
int row, boolean hasFocus) {
// Should only happen for the root
if (!(value instanceof Element)) {
return value.toString();
}
Element e = (Element) value;
AttributeSet as = e.getAttributes().copyAttributes();
String asString;
if (as != null) {
StringBuilder retBuffer = new StringBuilder("[");
Enumeration names = as.getAttributeNames();
while (names.hasMoreElements()) {
Object nextName = names.nextElement();
if (nextName != StyleConstants.ResolveAttribute) {
retBuffer.append(" ");
retBuffer.append(nextName);
retBuffer.append("=");
retBuffer.append(as.getAttribute(nextName));
}
}
retBuffer.append(" ]");
asString = retBuffer.toString();
} else {
asString = "[ ]";
}
if (e.isLeaf()) {
return e.getName() + " [" + e.getStartOffset() + ", " + e.
getEndOffset() + "] Attributes: " + asString;
}
return e.getName() + " [" + e.getStartOffset() + ", " + e.
getEndOffset() + "] Attributes: " + asString;
}
};
tree.addTreeSelectionListener(this);
tree.setDragEnabled(true);
// Don't show the root, it is fake.
tree.setRootVisible(false);
// Since the display value of every node after the insertion point
// changes every time the text changes and we don't generate a change
// event for all those nodes the display value can become off.
// This can be seen as '...' instead of the complete string value.
// This is a temporary workaround, increase the needed size by 15,
// hoping that will be enough.
tree.setCellRenderer(new DefaultTreeCellRenderer() {
@Override
public Dimension getPreferredSize() {
Dimension retValue = super.getPreferredSize();
if (retValue != null) {
retValue.width += 15;
}
return retValue;
}
});
// become a listener on the document to update the tree.
document.addDocumentListener(this);
// become a PropertyChangeListener to know when the Document has
// changed.
editor.addPropertyChangeListener(this);
// Become a CaretListener
editor.addCaretListener(this);
// configure the panel and frame containing it.
setLayout(new BorderLayout());
add(new JScrollPane(tree), BorderLayout.CENTER);
// Add a label above tree to describe what is being shown
JLabel label = new JLabel("Elements that make up the current document",
SwingConstants.CENTER);
label.setFont(new Font("Dialog", Font.BOLD, 14));
add(label, BorderLayout.NORTH);
setPreferredSize(new Dimension(400, 400));
}
private void translateAttributes(HTML.Tag tag,
AttributeSet htmlAttrSet,
MutableAttributeSet cssAttrSet) {
Enumeration<?> names = htmlAttrSet.getAttributeNames();
while (names.hasMoreElements()) {
Object name = names.nextElement();
if (name instanceof HTML.Attribute) {
HTML.Attribute key = (HTML.Attribute)name;
/*
* HTML.Attribute.ALIGN needs special processing.
* It can map to 1 of many(3) possible CSS attributes
* depending on the nature of the tag the attribute is
* part off and depending on the value of the attribute.
*/
if (key == HTML.Attribute.ALIGN) {
String htmlAttrValue = (String)htmlAttrSet.getAttribute(HTML.Attribute.ALIGN);
if (htmlAttrValue != null) {
CSS.Attribute cssAttr = getCssAlignAttribute(tag, htmlAttrSet);
if (cssAttr != null) {
Object o = getCssValue(cssAttr, htmlAttrValue);
if (o != null) {
cssAttrSet.addAttribute(cssAttr, o);
}
}
}
} else {
if (key == HTML.Attribute.SIZE && !isHTMLFontTag(tag)) {
/*
* The html size attribute has a mapping in the CSS world only
* if it is par of a font or base font tag.
*/
} else if (tag == HTML.Tag.TABLE && key == HTML.Attribute.BORDER) {
int borderWidth = getTableBorder(htmlAttrSet);
if (borderWidth > 0) {
translateAttribute(HTML.Attribute.BORDER, Integer.toString(borderWidth), cssAttrSet);
}
} else {
translateAttribute(key, (String) htmlAttrSet.getAttribute(key), cssAttrSet);
}
}
} else if (name instanceof CSS.Attribute) {
cssAttrSet.addAttribute(name, htmlAttrSet.getAttribute(name));
}
}
}