📄 stylecontext.java
字号:
StringBuffer sb = new StringBuffer(); sb.append("[StyleContext.SmallattributeSet:"); for (int i = 0; i < attrs.length; ++i) { sb.append(" ("); sb.append(attrs[i].toString()); sb.append("="); sb.append(attrs[i+1].toString()); sb.append(")"); } sb.append("]"); return sb.toString(); } } // FIXME: official javadocs suggest that these might be more usefully // implemented using a WeakHashMap, but not sure if that works most // places or whether it really matters anyways. // // FIXME: also not sure if these tables ought to be static (singletons), // shared across all StyleContexts. I think so, but it's not clear in // docs. revert to non-shared if you think it matters. /** * The name of the default style. */ public static final String DEFAULT_STYLE = "default"; /** * The default style for this style context. */ NamedStyle defaultStyle = new NamedStyle(DEFAULT_STYLE, null); static Hashtable sharedAttributeSets = new Hashtable(); static Hashtable sharedFonts = new Hashtable(); static StyleContext defaultStyleContext = new StyleContext(); static final int compressionThreshold = 9; EventListenerList listenerList; Hashtable styleTable; /** * Creates a new instance of the style context. Add the default style * to the style table. */ public StyleContext() { listenerList = new EventListenerList(); styleTable = new Hashtable(); styleTable.put(DEFAULT_STYLE, defaultStyle); } protected SmallAttributeSet createSmallAttributeSet(AttributeSet a) { return new SmallAttributeSet(a); } protected MutableAttributeSet createLargeAttributeSet(AttributeSet a) { return new SimpleAttributeSet(a); } public void addChangeListener(ChangeListener listener) { listenerList.add(ChangeListener.class, listener); } public void removeChangeListener(ChangeListener listener) { listenerList.remove(ChangeListener.class, listener); } public ChangeListener[] getChangeListeners() { return (ChangeListener[]) listenerList.getListeners(ChangeListener.class); } public Style addStyle(String name, Style parent) { Style newStyle = new NamedStyle(name, parent); if (name != null) styleTable.put(name, newStyle); return newStyle; } public void removeStyle(String name) { styleTable.remove(name); } /** * Get the style from the style table. If the passed name * matches {@link #DEFAULT_STYLE}, returns the default style. * Otherwise returns the previously defined style of * <code>null</code> if the style with the given name is not defined. * * @param name the name of the style. * * @return the style with the given name or null if no such defined. */ public Style getStyle(String name) { return (Style) styleTable.get(name); } /** * Get the names of the style. The returned enumeration always * contains at least one member, the default style. */ public Enumeration getStyleNames() { return styleTable.keys(); } // // StyleContexts only understand the "simple" model of fonts present in // pre-java2d systems: fonts are a family name, a size (integral number // of points), and a mask of style parameters (plain, bold, italic, or // bold|italic). We have an inner class here called SimpleFontSpec which // holds such triples. // // A SimpleFontSpec can be built for *any* AttributeSet because the size, // family, and style keys in an AttributeSet have default values (defined // over in StyleConstants). // // We keep a static cache mapping SimpleFontSpecs to java.awt.Fonts, so // that we reuse Fonts between styles and style contexts. // private static class SimpleFontSpec { String family; int style; int size; public SimpleFontSpec(String family, int style, int size) { this.family = family; this.style = style; this.size = size; } public boolean equals(Object obj) { return (obj != null) && (obj instanceof SimpleFontSpec) && (((SimpleFontSpec)obj).family.equals(this.family)) && (((SimpleFontSpec)obj).style == this.style) && (((SimpleFontSpec)obj).size == this.size); } public int hashCode() { return family.hashCode() + style + size; } } public Font getFont(AttributeSet attr) { String family = StyleConstants.getFontFamily(attr); int style = Font.PLAIN; if (StyleConstants.isBold(attr)) style += Font.BOLD; if (StyleConstants.isItalic(attr)) style += Font.ITALIC; int size = StyleConstants.getFontSize(attr); return getFont(family, style, size); } public Font getFont(String family, int style, int size) { SimpleFontSpec spec = new SimpleFontSpec(family, style, size); if (sharedFonts.containsKey(spec)) return (Font) sharedFonts.get(spec); else { Font tmp = new Font(family, style, size); sharedFonts.put(spec, tmp); return tmp; } } public FontMetrics getFontMetrics(Font f) { return Toolkit.getDefaultToolkit().getFontMetrics(f); } public Color getForeground(AttributeSet a) { return StyleConstants.getForeground(a); } public Color getBackground(AttributeSet a) { return StyleConstants.getBackground(a); } protected int getCompressionThreshold() { return compressionThreshold; } public static StyleContext getDefaultStyleContext() { return defaultStyleContext; } public AttributeSet addAttribute(AttributeSet old, Object name, Object value) { if (old instanceof MutableAttributeSet) { ((MutableAttributeSet)old).addAttribute(name, value); return old; } else { MutableAttributeSet mutable = createLargeAttributeSet(old); mutable.addAttribute(name, value); if (mutable.getAttributeCount() >= getCompressionThreshold()) return mutable; else { SmallAttributeSet small = createSmallAttributeSet(mutable); if (sharedAttributeSets.containsKey(small)) small = (SmallAttributeSet) sharedAttributeSets.get(small); else sharedAttributeSets.put(small,small); return small; } } } public AttributeSet addAttributes(AttributeSet old, AttributeSet attributes) { if (old instanceof MutableAttributeSet) { ((MutableAttributeSet)old).addAttributes(attributes); return old; } else { MutableAttributeSet mutable = createLargeAttributeSet(old); mutable.addAttributes(attributes); if (mutable.getAttributeCount() >= getCompressionThreshold()) return mutable; else { SmallAttributeSet small = createSmallAttributeSet(mutable); if (sharedAttributeSets.containsKey(small)) small = (SmallAttributeSet) sharedAttributeSets.get(small); else sharedAttributeSets.put(small,small); return small; } } } public AttributeSet getEmptySet() { AttributeSet e = createSmallAttributeSet(null); if (sharedAttributeSets.containsKey(e)) e = (AttributeSet) sharedAttributeSets.get(e); else sharedAttributeSets.put(e, e); return e; } public void reclaim(AttributeSet attributes) { if (sharedAttributeSets.containsKey(attributes)) sharedAttributeSets.remove(attributes); } public AttributeSet removeAttribute(AttributeSet old, Object name) { if (old instanceof MutableAttributeSet) { ((MutableAttributeSet)old).removeAttribute(name); if (old.getAttributeCount() < getCompressionThreshold()) { SmallAttributeSet small = createSmallAttributeSet(old); if (!sharedAttributeSets.containsKey(small)) sharedAttributeSets.put(small,small); old = (AttributeSet) sharedAttributeSets.get(small); } return old; } else { MutableAttributeSet mutable = createLargeAttributeSet(old); mutable.removeAttribute(name); SmallAttributeSet small = createSmallAttributeSet(mutable); if (sharedAttributeSets.containsKey(small)) small = (SmallAttributeSet) sharedAttributeSets.get(small); else sharedAttributeSets.put(small,small); return small; } } public AttributeSet removeAttributes(AttributeSet old, AttributeSet attributes) { return removeAttributes(old, attributes.getAttributeNames()); } public AttributeSet removeAttributes(AttributeSet old, Enumeration names) { if (old instanceof MutableAttributeSet) { ((MutableAttributeSet)old).removeAttributes(names); if (old.getAttributeCount() < getCompressionThreshold()) { SmallAttributeSet small = createSmallAttributeSet(old); if (!sharedAttributeSets.containsKey(small)) sharedAttributeSets.put(small,small); old = (AttributeSet) sharedAttributeSets.get(small); } return old; } else { MutableAttributeSet mutable = createLargeAttributeSet(old); mutable.removeAttributes(names); SmallAttributeSet small = createSmallAttributeSet(mutable); if (sharedAttributeSets.containsKey(small)) small = (SmallAttributeSet) sharedAttributeSets.get(small); else sharedAttributeSets.put(small,small); return small; } } // FIXME: there's some sort of quasi-serialization stuff in here which I // have left incomplete; I'm not sure I understand the intent properly. public static Object getStaticAttribute(Object key) { throw new InternalError("not implemented"); } public static Object getStaticAttributeKey(Object key) { throw new InternalError("not implemented"); } public static void readAttributeSet(ObjectInputStream in, MutableAttributeSet a) throws ClassNotFoundException, IOException { throw new InternalError("not implemented"); } public static void writeAttributeSet(ObjectOutputStream out, AttributeSet a) throws IOException { throw new InternalError("not implemented"); } public void readAttributes(ObjectInputStream in, MutableAttributeSet a) throws ClassNotFoundException, IOException { throw new InternalError("not implemented"); } public void writeAttributes(ObjectOutputStream out, AttributeSet a) throws IOException { throw new InternalError("not implemented"); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -