1 7 package javax.swing.plaf.basic; 8 9 import java.awt.*; 10 import java.awt.event.*; 11 import java.beans.*; 12 import java.net.URL ; 13 import java.net.MalformedURLException ; 14 import javax.swing.*; 15 import javax.swing.text.*; 16 import javax.swing.text.html.*; 17 import javax.swing.plaf.*; 18 import javax.swing.border.*; 19 20 21 36 public class BasicEditorPaneUI extends BasicTextUI { 37 38 44 public static ComponentUI createUI(JComponent c) { 45 return new BasicEditorPaneUI (); 46 } 47 48 51 public BasicEditorPaneUI() { 52 super(); 53 } 54 55 62 protected String getPropertyPrefix() { 63 return "EditorPane"; 64 } 65 66 71 public void installUI(JComponent c) { 72 super.installUI(c); 73 updateDisplayProperties(c.getFont(), 74 c.getForeground()); 75 } 76 77 82 public void uninstallUI(JComponent c) { 83 cleanDisplayProperties(); 84 super.uninstallUI(c); 85 } 86 87 94 public EditorKit getEditorKit(JTextComponent tc) { 95 JEditorPane pane = (JEditorPane) getComponent(); 96 return pane.getEditorKit(); 97 } 98 99 103 ActionMap getActionMap() { 104 ActionMap am = new ActionMapUIResource(); 105 am.put("requestFocus", new FocusAction()); 106 EditorKit editorKit = getEditorKit(getComponent()); 107 if (editorKit != null) { 108 Action[] actions = editorKit.getActions(); 109 if (actions != null) { 110 addActions(am, actions); 111 } 112 } 113 am.put(TransferHandler.getCutAction().getValue(Action.NAME), 114 TransferHandler.getCutAction()); 115 am.put(TransferHandler.getCopyAction().getValue(Action.NAME), 116 TransferHandler.getCopyAction()); 117 am.put(TransferHandler.getPasteAction().getValue(Action.NAME), 118 TransferHandler.getPasteAction()); 119 return am; 120 } 121 122 132 protected void propertyChange(PropertyChangeEvent evt) { 133 String name = evt.getPropertyName(); 134 if ("editorKit".equals(name)) { 135 ActionMap map = SwingUtilities.getUIActionMap(getComponent()); 136 if (map != null) { 137 Object oldValue = evt.getOldValue(); 138 if (oldValue instanceof EditorKit) { 139 Action[] actions = ((EditorKit)oldValue).getActions(); 140 if (actions != null) { 141 removeActions(map, actions); 142 } 143 } 144 Object newValue = evt.getNewValue(); 145 if (newValue instanceof EditorKit) { 146 Action[] actions = ((EditorKit)newValue).getActions(); 147 if (actions != null) { 148 addActions(map, actions); 149 } 150 } 151 } 152 updateFocusTraversalKeys(); 153 } else if ("editable".equals(name)) { 154 updateFocusTraversalKeys(); 155 } else if ("foreground".equals(name) 156 || "font".equals(name) 157 || "document".equals(name) 158 || JEditorPane.W3C_LENGTH_UNITS.equals(name) 159 || JEditorPane.HONOR_DISPLAY_PROPERTIES.equals(name) 160 ) { 161 JComponent c = getComponent(); 162 updateDisplayProperties(c.getFont(), c.getForeground()); 163 if ( JEditorPane.W3C_LENGTH_UNITS.equals(name) 164 || JEditorPane.HONOR_DISPLAY_PROPERTIES.equals(name) ) { 165 modelChanged(); 166 } 167 if ("foreground".equals(name)) { 168 Object honorDisplayPropertiesObject = c. 169 getClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES); 170 boolean honorDisplayProperties = false; 171 if (honorDisplayPropertiesObject instanceof Boolean ) { 172 honorDisplayProperties = 173 ((Boolean )honorDisplayPropertiesObject).booleanValue(); 174 } 175 if (honorDisplayProperties) { 176 modelChanged(); 177 } 178 } 179 180 181 } 182 } 183 184 void removeActions(ActionMap map, Action[] actions) { 185 int n = actions.length; 186 for (int i = 0; i < n; i++) { 187 Action a = actions[i]; 188 map.remove(a.getValue(Action.NAME)); 189 } 190 } 191 192 void addActions(ActionMap map, Action[] actions) { 193 int n = actions.length; 194 for (int i = 0; i < n; i++) { 195 Action a = actions[i]; 196 map.put(a.getValue(Action.NAME), a); 197 } 198 } 199 200 void updateDisplayProperties(Font font, Color fg) { 201 JComponent c = getComponent(); 202 Object honorDisplayPropertiesObject = c. 203 getClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES); 204 boolean honorDisplayProperties = false; 205 Object w3cLengthUnitsObject = c.getClientProperty(JEditorPane. 206 W3C_LENGTH_UNITS); 207 boolean w3cLengthUnits = false; 208 if (honorDisplayPropertiesObject instanceof Boolean ) { 209 honorDisplayProperties = 210 ((Boolean )honorDisplayPropertiesObject).booleanValue(); 211 } 212 if (w3cLengthUnitsObject instanceof Boolean ) { 213 w3cLengthUnits = ((Boolean )w3cLengthUnitsObject).booleanValue(); 214 } 215 if (this instanceof BasicTextPaneUI 216 || honorDisplayProperties) { 217 Document doc = getComponent().getDocument(); 219 if (doc instanceof StyledDocument) { 220 if (doc instanceof HTMLDocument 221 && honorDisplayProperties) { 222 updateCSS(font, fg); 223 } else { 224 updateStyle(font, fg); 225 } 226 } 227 } else { 228 cleanDisplayProperties(); 229 } 230 if ( w3cLengthUnits ) { 231 Document doc = getComponent().getDocument(); 232 if (doc instanceof HTMLDocument) { 233 StyleSheet documentStyleSheet = 234 ((HTMLDocument)doc).getStyleSheet(); 235 documentStyleSheet.addRule("W3C_LENGTH_UNITS_ENABLE"); 236 } 237 } else { 238 Document doc = getComponent().getDocument(); 239 if (doc instanceof HTMLDocument) { 240 StyleSheet documentStyleSheet = 241 ((HTMLDocument)doc).getStyleSheet(); 242 documentStyleSheet.addRule("W3C_LENGTH_UNITS_DISABLE"); 243 } 244 245 } 246 } 247 248 253 private static final String FONT_ATTRIBUTE_KEY = "FONT_ATTRIBUTE_KEY"; 254 255 void cleanDisplayProperties() { 256 Document document = getComponent().getDocument(); 257 if (document instanceof HTMLDocument) { 258 StyleSheet documentStyleSheet = 259 ((HTMLDocument)document).getStyleSheet(); 260 StyleSheet[] styleSheets = documentStyleSheet.getStyleSheets(); 261 if (styleSheets != null) { 262 for (StyleSheet s : styleSheets) { 263 if (s instanceof StyleSheetUIResource) { 264 documentStyleSheet.removeStyleSheet(s); 265 documentStyleSheet.addRule("BASE_SIZE_DISABLE"); 266 break; 267 } 268 } 269 } 270 Style style = ((StyledDocument) document).getStyle(StyleContext.DEFAULT_STYLE); 271 style.removeAttribute(FONT_ATTRIBUTE_KEY); 272 } 273 } 274 275 static class StyleSheetUIResource extends StyleSheet implements UIResource { 276 } 277 278 private void updateCSS(Font font, Color fg) { 279 JTextComponent component = getComponent(); 280 Document document = component.getDocument(); 281 if (document instanceof HTMLDocument) { 282 StyleSheet styleSheet = new StyleSheetUIResource(); 283 StyleSheet documentStyleSheet = 284 ((HTMLDocument)document).getStyleSheet(); 285 StyleSheet[] styleSheets = documentStyleSheet.getStyleSheets(); 286 if (styleSheets != null) { 287 for (StyleSheet s : styleSheets) { 288 if (s instanceof StyleSheetUIResource) { 289 documentStyleSheet.removeStyleSheet(s); 290 } 291 } 292 } 293 String cssRule = com.sun.java.swing. 294 SwingUtilities2.displayPropertiesToCSS(font, 295 fg); 296 styleSheet.addRule(cssRule); 297 documentStyleSheet.addStyleSheet(styleSheet); 298 documentStyleSheet.addRule("BASE_SIZE " + 299 component.getFont().getSize()); 300 Style style = ((StyledDocument) document).getStyle(StyleContext.DEFAULT_STYLE); 301 style.addAttribute(FONT_ATTRIBUTE_KEY, font); 302 } 303 } 304 305 private void updateStyle(Font font, Color fg) { 306 updateFont(font); 307 updateForeground(fg); 308 } 309 310 316 private void updateForeground(Color color) { 317 StyledDocument doc = (StyledDocument)getComponent().getDocument(); 318 Style style = doc.getStyle(StyleContext.DEFAULT_STYLE); 319 320 if (style == null) { 321 return; 322 } 323 324 if (color == null) { 325 style.removeAttribute(StyleConstants.Foreground); 326 } else { 327 StyleConstants.setForeground(style, color); 328 } 329 } 330 331 337 private void updateFont(Font font) { 338 StyledDocument doc = (StyledDocument)getComponent().getDocument(); 339 Style style = doc.getStyle(StyleContext.DEFAULT_STYLE); 340 341 if (style == null) { 342 return; 343 } 344 345 if (font == null) { 346 style.removeAttribute(StyleConstants.FontFamily); 347 style.removeAttribute(StyleConstants.FontSize); 348 style.removeAttribute(StyleConstants.Bold); 349 style.removeAttribute(StyleConstants.Italic); 350 style.removeAttribute(FONT_ATTRIBUTE_KEY); 351 } else { 352 StyleConstants.setFontFamily(style, font.getName()); 353 StyleConstants.setFontSize(style, font.getSize()); 354 StyleConstants.setBold(style, font.isBold()); 355 StyleConstants.setItalic(style, font.isItalic()); 356 style.addAttribute(FONT_ATTRIBUTE_KEY, font); 357 } 358 } 359 } 360 361 362 | Popular Tags |