1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
/* * @(#)BasicEditorPaneUI.java 1.39 06/04/20 * * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package javax.swing.plaf.basic; import java.awt.*; import java.awt.event.*; import java.beans.*; import java.net.URL; import java.net.MalformedURLException; import javax.swing.*; import javax.swing.text.*; import javax.swing.text.html.*; import javax.swing.plaf.*; import javax.swing.border.*; /** * Provides the look and feel for a JEditorPane. * <p> * <strong>Warning:</strong> * Serialized objects of this class will not be compatible with * future Swing releases. The current serialization support is * appropriate for short term storage or RMI between applications running * the same version of Swing. As of 1.4, support for long term storage * of all JavaBeans<sup><font size="-2">TM</font></sup> * has been added to the <code>java.beans</code> package. * Please see {@link java.beans.XMLEncoder}. * * @author Timothy Prinzing * @version 1.39 04/20/06 */ public class BasicEditorPaneUI extends BasicTextUI { /** * Creates a UI for the JTextPane. * * @param c the JTextPane component * @return the UI */ public static ComponentUI createUI(JComponent c) { return new BasicEditorPaneUI(); } /** * Creates a new BasicEditorPaneUI. */ public BasicEditorPaneUI() { super(); } /** * Fetches the name used as a key to lookup properties through the * UIManager. This is used as a prefix to all the standard * text properties. * * @return the name ("EditorPane") */ protected String getPropertyPrefix() { return "EditorPane"; } /** *{@inheritDoc} * * @since 1.5 */ public void installUI(JComponent c) { super.installUI(c); updateDisplayProperties(c.getFont(), c.getForeground()); } /** *{@inheritDoc} * * @since 1.5 */ public void uninstallUI(JComponent c) { cleanDisplayProperties(); super.uninstallUI(c); } /** * Fetches the EditorKit for the UI. This is whatever is * currently set in the associated JEditorPane. * * @return the editor capabilities * @see TextUI#getEditorKit */ public EditorKit getEditorKit(JTextComponent tc) { JEditorPane pane = (JEditorPane) getComponent(); return pane.getEditorKit(); } /** * Fetch an action map to use. The map for a JEditorPane * is not shared because it changes with the EditorKit. */ ActionMap getActionMap() { ActionMap am = new ActionMapUIResource(); am.put("requestFocus", new FocusAction()); EditorKit editorKit = getEditorKit(getComponent()); if (editorKit != null) { Action[] actions = editorKit.getActions(); if (actions != null) { addActions(am, actions); } } am.put(TransferHandler.getCutAction().getValue(Action.NAME), TransferHandler.getCutAction()); am.put(TransferHandler.getCopyAction().getValue(Action.NAME), TransferHandler.getCopyAction()); am.put(TransferHandler.getPasteAction().getValue(Action.NAME), TransferHandler.getPasteAction()); return am; } /** * This method gets called when a bound property is changed * on the associated JTextComponent. This is a hook * which UI implementations may change to reflect how the * UI displays bound properties of JTextComponent subclasses. * This is implemented to rebuild the ActionMap based upon an * EditorKit change. * * @param evt the property change event */ protected void propertyChange(PropertyChangeEvent evt) { super.propertyChange(evt); String name = evt.getPropertyName(); if ("editorKit".equals(name)) { ActionMap map = SwingUtilities.getUIActionMap(getComponent()); if (map != null) { Object oldValue = evt.getOldValue(); if (oldValue instanceof EditorKit) { Action[] actions = ((EditorKit)oldValue).getActions(); if (actions != null) { removeActions(map, actions); } } Object newValue = evt.getNewValue(); if (newValue instanceof EditorKit) { Action[] actions = ((EditorKit)newValue).getActions(); if (actions != null) { addActions(map, actions); } } } updateFocusTraversalKeys(); } else if ("editable".equals(name)) { updateFocusTraversalKeys(); } else if ("foreground".equals(name) || "font".equals(name) || "document".equals(name) || JEditorPane.W3C_LENGTH_UNITS.equals(name) || JEditorPane.HONOR_DISPLAY_PROPERTIES.equals(name) ) { JComponent c = getComponent(); updateDisplayProperties(c.getFont(), c.getForeground()); if ( JEditorPane.W3C_LENGTH_UNITS.equals(name) || JEditorPane.HONOR_DISPLAY_PROPERTIES.equals(name) ) { modelChanged(); } if ("foreground".equals(name)) { Object honorDisplayPropertiesObject = c. getClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES); boolean honorDisplayProperties = false; if (honorDisplayPropertiesObject instanceof Boolean) { honorDisplayProperties = ((Boolean)honorDisplayPropertiesObject).booleanValue(); } if (honorDisplayProperties) { modelChanged(); } } } } void removeActions(ActionMap map, Action[] actions) { int n = actions.length; for (int i = 0; i < n; i++) { Action a = actions[i]; map.remove(a.getValue(Action.NAME)); } } void addActions(ActionMap map, Action[] actions) { int n = actions.length; for (int i = 0; i < n; i++) { Action a = actions[i]; map.put(a.getValue(Action.NAME), a); } } void updateDisplayProperties(Font font, Color fg) { JComponent c = getComponent(); Object honorDisplayPropertiesObject = c. getClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES); boolean honorDisplayProperties = false; Object w3cLengthUnitsObject = c.getClientProperty(JEditorPane. W3C_LENGTH_UNITS); boolean w3cLengthUnits = false; if (honorDisplayPropertiesObject instanceof Boolean) { honorDisplayProperties = ((Boolean)honorDisplayPropertiesObject).booleanValue(); } if (w3cLengthUnitsObject instanceof Boolean) { w3cLengthUnits = ((Boolean)w3cLengthUnitsObject).booleanValue(); } if (this instanceof BasicTextPaneUI || honorDisplayProperties) { //using equals because can not use UIResource for Boolean Document doc = getComponent().getDocument(); if (doc instanceof StyledDocument) { if (doc instanceof HTMLDocument && honorDisplayProperties) { updateCSS(font, fg); } else { updateStyle(font, fg); } } } else { cleanDisplayProperties(); } if ( w3cLengthUnits ) { Document doc = getComponent().getDocument(); if (doc instanceof HTMLDocument) { StyleSheet documentStyleSheet = ((HTMLDocument)doc).getStyleSheet(); documentStyleSheet.addRule("W3C_LENGTH_UNITS_ENABLE"); } } else { Document doc = getComponent().getDocument(); if (doc instanceof HTMLDocument) { StyleSheet documentStyleSheet = ((HTMLDocument)doc).getStyleSheet(); documentStyleSheet.addRule("W3C_LENGTH_UNITS_DISABLE"); } } } /** * Attribute key to reference the default font. * used in javax.swing.text.StyleContext.getFont * to resolve the default font. */ private static final String FONT_ATTRIBUTE_KEY = "FONT_ATTRIBUTE_KEY"; void cleanDisplayProperties() { Document document = getComponent().getDocument(); if (document instanceof HTMLDocument) { StyleSheet documentStyleSheet = ((HTMLDocument)document).getStyleSheet(); StyleSheet[] styleSheets = documentStyleSheet.getStyleSheets(); if (styleSheets != null) { for (StyleSheet s : styleSheets) { if (s instanceof StyleSheetUIResource) { documentStyleSheet.removeStyleSheet(s); documentStyleSheet.addRule("BASE_SIZE_DISABLE"); break; } } } Style style = ((StyledDocument) document).getStyle(StyleContext.DEFAULT_STYLE); if (style.getAttribute(FONT_ATTRIBUTE_KEY) != null) { style.removeAttribute(FONT_ATTRIBUTE_KEY); } } } static class StyleSheetUIResource extends StyleSheet implements UIResource { } private void updateCSS(Font font, Color fg) { JTextComponent component = getComponent(); Document document = component.getDocument(); if (document instanceof HTMLDocument) { StyleSheet styleSheet = new StyleSheetUIResource(); StyleSheet documentStyleSheet = ((HTMLDocument)document).getStyleSheet(); StyleSheet[] styleSheets = documentStyleSheet.getStyleSheets(); if (styleSheets != null) { for (StyleSheet s : styleSheets) { if (s instanceof StyleSheetUIResource) { documentStyleSheet.removeStyleSheet(s); } } } String cssRule = sun.swing. SwingUtilities2.displayPropertiesToCSS(font, fg); styleSheet.addRule(cssRule); documentStyleSheet.addStyleSheet(styleSheet); documentStyleSheet.addRule("BASE_SIZE " + component.getFont().getSize()); Style style = ((StyledDocument) document).getStyle(StyleContext.DEFAULT_STYLE); if (! font.equals(style.getAttribute(FONT_ATTRIBUTE_KEY))) { style.addAttribute(FONT_ATTRIBUTE_KEY, font); } } } private void updateStyle(Font font, Color fg) { updateFont(font); updateForeground(fg); } /** * Update the color in the default style of the document. * * @param color the new color to use or null to remove the color attribute * from the document's style */ private void updateForeground(Color color) { StyledDocument doc = (StyledDocument)getComponent().getDocument(); Style style = doc.getStyle(StyleContext.DEFAULT_STYLE); if (style == null) { return; } if (color == null) { if (style.getAttribute(StyleConstants.Foreground) != null) { style.removeAttribute(StyleConstants.Foreground); } } else { if (! color.equals(StyleConstants.getForeground(style))) { StyleConstants.setForeground(style, color); } } } /** * Update the font in the default style of the document. * * @param font the new font to use or null to remove the font attribute * from the document's style */ private void updateFont(Font font) { StyledDocument doc = (StyledDocument)getComponent().getDocument(); Style style = doc.getStyle(StyleContext.DEFAULT_STYLE); if (style == null) { return; } String fontFamily = (String) style.getAttribute(StyleConstants.FontFamily); Integer fontSize = (Integer) style.getAttribute(StyleConstants.FontSize); Boolean isBold = (Boolean) style.getAttribute(StyleConstants.Bold); Boolean isItalic = (Boolean) style.getAttribute(StyleConstants.Italic); Font fontAttribute = (Font) style.getAttribute(FONT_ATTRIBUTE_KEY); if (font == null) { if (fontFamily != null) { style.removeAttribute(StyleConstants.FontFamily); } if (fontSize != null) { style.removeAttribute(StyleConstants.FontSize); } if (isBold != null) { style.removeAttribute(StyleConstants.Bold); } if (isItalic != null) { style.removeAttribute(StyleConstants.Italic); } if (fontAttribute != null) { style.removeAttribute(FONT_ATTRIBUTE_KEY); } } else { if (! font.getName().equals(fontFamily)) { StyleConstants.setFontFamily(style, font.getName()); } if (fontSize == null || fontSize.intValue() != font.getSize()) { StyleConstants.setFontSize(style, font.getSize()); } if (isBold == null || isBold.booleanValue() != font.isBold()) { StyleConstants.setBold(style, font.isBold()); } if (isItalic == null || isItalic.booleanValue() != font.isItalic()) { StyleConstants.setItalic(style, font.isItalic()); } if (! font.equals(fontAttribute)) { style.addAttribute(FONT_ATTRIBUTE_KEY, font); } } } }