1 7 8 package javax.swing.plaf.synth; 9 10 import javax.swing.*; 11 import javax.swing.text.*; 12 import javax.swing.event.*; 13 import javax.swing.plaf.*; 14 import javax.swing.plaf.basic.BasicTextFieldUI ; 15 import java.awt.*; 16 import java.beans.PropertyChangeEvent ; 17 18 import sun.swing.plaf.synth.SynthUI; 19 20 36 class SynthTextFieldUI extends BasicTextFieldUI implements SynthUI { 37 private SynthStyle style; 38 39 45 public static ComponentUI createUI(JComponent c) { 46 return new SynthTextFieldUI (); 47 } 48 49 public SynthTextFieldUI() { 50 super(); 51 } 52 53 private void updateStyle(JTextComponent comp) { 54 SynthContext context = getContext(comp, ENABLED); 55 SynthStyle oldStyle = style; 56 57 style = SynthLookAndFeel.updateStyle(context, this); 58 59 if (style != oldStyle) { 60 SynthTextFieldUI.updateStyle(comp, context, getPropertyPrefix()); 61 62 if (oldStyle != null) { 63 uninstallKeyboardActions(); 64 installKeyboardActions(); 65 } 66 } 67 context.dispose(); 68 } 69 70 static void updateStyle(JTextComponent comp, SynthContext context, 71 String prefix) { 72 SynthStyle style = context.getStyle(); 73 74 Color color = comp.getCaretColor(); 75 if (color == null || color instanceof UIResource) { 76 comp.setCaretColor( 77 (Color)style.get(context, prefix + ".caretForeground")); 78 } 79 80 Color fg = comp.getForeground(); 81 if (fg == null || fg instanceof UIResource) { 82 fg = style.getColorForState(context, ColorType.TEXT_FOREGROUND); 83 if (fg != null) { 84 comp.setForeground(fg); 85 } 86 } 87 88 Object ar = style.get(context, prefix + ".caretAspectRatio"); 89 if (ar instanceof Number ) { 90 comp.putClientProperty("caretAspectRatio", ar); 91 } 92 93 context.setComponentState(SELECTED | FOCUSED); 94 95 Color s = comp.getSelectionColor(); 96 if (s == null || s instanceof UIResource) { 97 comp.setSelectionColor( 98 style.getColor(context, ColorType.TEXT_BACKGROUND)); 99 } 100 101 Color sfg = comp.getSelectedTextColor(); 102 if (sfg == null || sfg instanceof UIResource) { 103 comp.setSelectedTextColor( 104 style.getColor(context, ColorType.TEXT_FOREGROUND)); 105 } 106 107 context.setComponentState(DISABLED); 108 109 Color dfg = comp.getDisabledTextColor(); 110 if (dfg == null || dfg instanceof UIResource) { 111 comp.setDisabledTextColor( 112 style.getColor(context, ColorType.TEXT_FOREGROUND)); 113 } 114 115 Insets margin = comp.getMargin(); 116 if (margin == null || margin instanceof UIResource) { 117 margin = (Insets)style.get(context, prefix + ".margin"); 118 119 if (margin == null) { 120 margin = SynthLookAndFeel.EMPTY_UIRESOURCE_INSETS; 122 } 123 comp.setMargin(margin); 124 } 125 126 Caret caret = comp.getCaret(); 127 if (caret instanceof UIResource) { 128 Object o = style.get(context, prefix + ".caretBlinkRate"); 129 if (o != null && o instanceof Integer ) { 130 Integer rate = (Integer )o; 131 caret.setBlinkRate(rate.intValue()); 132 } 133 } 134 } 135 136 public SynthContext getContext(JComponent c) { 137 return getContext(c, getComponentState(c)); 138 } 139 140 private SynthContext getContext(JComponent c, int state) { 141 return SynthContext.getContext(SynthContext .class, c, 142 SynthLookAndFeel.getRegion(c), style, state); 143 } 144 145 private int getComponentState(JComponent c) { 146 return SynthLookAndFeel.getComponentState(c); 147 } 148 149 public void update(Graphics g, JComponent c) { 150 SynthContext context = getContext(c); 151 152 SynthLookAndFeel.update(context, g); 153 paintBackground(context, g, c); 154 paint(context, g); 155 context.dispose(); 156 } 157 158 166 protected void paint(SynthContext context, Graphics g) { 167 super.paint(g, getComponent()); 168 } 169 170 void paintBackground(SynthContext context, Graphics g, JComponent c) { 171 context.getPainter().paintTextFieldBackground(context, g, 0, 0, 172 c.getWidth(), c.getHeight()); 173 } 174 175 public void paintBorder(SynthContext context, Graphics g, int x, 176 int y, int w, int h) { 177 context.getPainter().paintTextFieldBorder(context, g, x, y, w, h); 178 } 179 180 protected void paintBackground(Graphics g) { 181 } 183 184 195 protected void propertyChange(PropertyChangeEvent evt) { 196 if (SynthLookAndFeel.shouldUpdateStyle(evt)) { 197 updateStyle((JTextComponent)evt.getSource()); 198 } 199 super.propertyChange(evt); 200 } 201 202 protected void installDefaults() { 203 updateStyle((JTextComponent)getComponent()); 204 } 205 206 protected void uninstallDefaults() { 207 SynthContext context = getContext(getComponent(), ENABLED); 208 209 getComponent().putClientProperty("caretAspectRatio", null); 210 211 style.uninstallDefaults(context); 212 context.dispose(); 213 style = null; 214 super.uninstallDefaults(); 215 } 216 217 public void installUI(JComponent c) { 218 super.installUI(c); 219 } 220 } 221 | Popular Tags |