KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > plaf > basic > BasicEditorPaneUI


1 /*
2  * @(#)BasicEditorPaneUI.java 1.33 05/11/10
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

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 JavaDoc;
13 import java.net.MalformedURLException JavaDoc;
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 /**
22  * Provides the look and feel for a JEditorPane.
23  * <p>
24  * <strong>Warning:</strong>
25  * Serialized objects of this class will not be compatible with
26  * future Swing releases. The current serialization support is
27  * appropriate for short term storage or RMI between applications running
28  * the same version of Swing. As of 1.4, support for long term storage
29  * of all JavaBeans<sup><font size="-2">TM</font></sup>
30  * has been added to the <code>java.beans</code> package.
31  * Please see {@link java.beans.XMLEncoder}.
32  *
33  * @author Timothy Prinzing
34  * @version 1.33 11/10/05
35  */

36 public class BasicEditorPaneUI extends BasicTextUI JavaDoc {
37
38     /**
39      * Creates a UI for the JTextPane.
40      *
41      * @param c the JTextPane component
42      * @return the UI
43      */

44     public static ComponentUI createUI(JComponent c) {
45         return new BasicEditorPaneUI JavaDoc();
46     }
47
48     /**
49      * Creates a new BasicEditorPaneUI.
50      */

51     public BasicEditorPaneUI() {
52     super();
53     }
54
55     /**
56      * Fetches the name used as a key to lookup properties through the
57      * UIManager. This is used as a prefix to all the standard
58      * text properties.
59      *
60      * @return the name ("EditorPane")
61      */

62     protected String JavaDoc getPropertyPrefix() {
63     return "EditorPane";
64     }
65
66     /**
67      *{@inheritDoc}
68      *
69      * @since 1.5
70      */

71     public void installUI(JComponent c) {
72         super.installUI(c);
73         updateDisplayProperties(c.getFont(),
74                                 c.getForeground());
75     }
76
77     /**
78      *{@inheritDoc}
79      *
80      * @since 1.5
81      */

82     public void uninstallUI(JComponent c) {
83         cleanDisplayProperties();
84         super.uninstallUI(c);
85     }
86     
87     /**
88      * Fetches the EditorKit for the UI. This is whatever is
89      * currently set in the associated JEditorPane.
90      *
91      * @return the editor capabilities
92      * @see TextUI#getEditorKit
93      */

94     public EditorKit getEditorKit(JTextComponent tc) {
95     JEditorPane pane = (JEditorPane) getComponent();
96     return pane.getEditorKit();
97     }
98
99     /**
100      * Fetch an action map to use. The map for a JEditorPane
101      * is not shared because it changes with the EditorKit.
102      */

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     /**
123      * This method gets called when a bound property is changed
124      * on the associated JTextComponent. This is a hook
125      * which UI implementations may change to reflect how the
126      * UI displays bound properties of JTextComponent subclasses.
127      * This is implemented to rebuild the ActionMap based upon an
128      * EditorKit change.
129      *
130      * @param evt the property change event
131      */

132     protected void propertyChange(PropertyChangeEvent evt) {
133         String JavaDoc name = evt.getPropertyName();
134     if ("editorKit".equals(name)) {
135         ActionMap map = SwingUtilities.getUIActionMap(getComponent());
136         if (map != null) {
137         Object JavaDoc 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 JavaDoc 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 JavaDoc honorDisplayPropertiesObject = c.
169                     getClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES);
170                 boolean honorDisplayProperties = false;
171                 if (honorDisplayPropertiesObject instanceof Boolean JavaDoc) {
172                     honorDisplayProperties =
173                         ((Boolean JavaDoc)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 JavaDoc honorDisplayPropertiesObject = c.
203             getClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES);
204         boolean honorDisplayProperties = false;
205         Object JavaDoc w3cLengthUnitsObject = c.getClientProperty(JEditorPane.
206                                                           W3C_LENGTH_UNITS);
207         boolean w3cLengthUnits = false;
208         if (honorDisplayPropertiesObject instanceof Boolean JavaDoc) {
209             honorDisplayProperties =
210                 ((Boolean JavaDoc)honorDisplayPropertiesObject).booleanValue();
211         }
212         if (w3cLengthUnitsObject instanceof Boolean JavaDoc) {
213             w3cLengthUnits = ((Boolean JavaDoc)w3cLengthUnitsObject).booleanValue();
214         }
215         if (this instanceof BasicTextPaneUI JavaDoc
216             || honorDisplayProperties) {
217              //using equals because can not use UIResource for Boolean
218
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     /**
249      * Attribute key to reference the default font.
250      * used in javax.swing.text.StyleContext.getFont
251      * to resolve the default font.
252      */

253     private static final String JavaDoc 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 JavaDoc 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     /**
311      * Update the color in the default style of the document.
312      *
313      * @param color the new color to use or null to remove the color attribute
314      * from the document's style
315      */

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     /**
332      * Update the font in the default style of the document.
333      *
334      * @param font the new font to use or null to remove the font attribute
335      * from the document's style
336      */

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