1 7 8 package javax.swing.plaf.synth; 9 10 import javax.swing.*; 11 import javax.swing.text.*; 12 import javax.swing.plaf.*; 13 import java.beans.PropertyChangeEvent ; 14 import java.awt.*; 15 16 32 class SynthTextPaneUI extends SynthEditorPaneUI { 33 34 40 public static ComponentUI createUI(JComponent c) { 41 return new SynthTextPaneUI (); 42 } 43 44 51 protected String getPropertyPrefix() { 52 return "TextPane"; 53 } 54 55 public void installUI(JComponent c) { 56 super.installUI(c); 57 updateForeground(c.getForeground()); 58 updateFont(c.getFont()); 59 } 60 61 72 protected void propertyChange(PropertyChangeEvent evt) { 73 super.propertyChange(evt); 74 75 String name = evt.getPropertyName(); 76 77 if (name.equals("foreground")) { 78 updateForeground((Color)evt.getNewValue()); 79 } else if (name.equals("font")) { 80 updateFont((Font)evt.getNewValue()); 81 } else if (name.equals("document")) { 82 JComponent comp = getComponent(); 83 updateForeground(comp.getForeground()); 84 updateFont(comp.getFont()); 85 } 86 } 87 88 94 private void updateForeground(Color color) { 95 StyledDocument doc = (StyledDocument)getComponent().getDocument(); 96 Style style = doc.getStyle(StyleContext.DEFAULT_STYLE); 97 98 if (style == null) { 99 return; 100 } 101 102 if (color == null) { 103 style.removeAttribute(StyleConstants.Foreground); 104 } else { 105 StyleConstants.setForeground(style, color); 106 } 107 } 108 109 115 private void updateFont(Font font) { 116 StyledDocument doc = (StyledDocument)getComponent().getDocument(); 117 Style style = doc.getStyle(StyleContext.DEFAULT_STYLE); 118 119 if (style == null) { 120 return; 121 } 122 123 if (font == null) { 124 style.removeAttribute(StyleConstants.FontFamily); 125 style.removeAttribute(StyleConstants.FontSize); 126 style.removeAttribute(StyleConstants.Bold); 127 style.removeAttribute(StyleConstants.Italic); 128 } else { 129 StyleConstants.setFontFamily(style, font.getName()); 130 StyleConstants.setFontSize(style, font.getSize()); 131 StyleConstants.setBold(style, font.isBold()); 132 StyleConstants.setItalic(style, font.isItalic()); 133 } 134 } 135 136 void paintBackground(SynthContext context, Graphics g, JComponent c) { 137 context.getPainter().paintTextPaneBackground(context, g, 0, 0, 138 c.getWidth(), c.getHeight()); 139 } 140 141 public void paintBorder(SynthContext context, Graphics g, int x, 142 int y, int w, int h) { 143 context.getPainter().paintTextPaneBorder(context, g, x, y, w, h); 144 } 145 } 146 | Popular Tags |