Source Code Cross Referenced for InlineView.java in  » 6.0-JDK-Core » swing » javax » swing » text » html » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Home
Java Source Code / Java Documentation
1.6.0 JDK Core
2.6.0 JDK Modules
3.6.0 JDK Modules com.sun
4.6.0 JDK Modules com.sun.java
5.6.0 JDK Modules sun
6.6.0 JDK Platform
7.Ajax
8.Apache Harmony Java SE
9.Aspect oriented
10.Authentication Authorization
11.Blogger System
12.Build
13.Byte Code
14.Cache
15.Chart
16.Chat
17.Code Analyzer
18.Collaboration
19.Content Management System
20.Database Client
21.Database DBMS
22.Database JDBC Connection Pool
23.Database ORM
24.Development
25.EJB Server
26.ERP CRM Financial
27.ESB
28.Forum
29.Game
30.GIS
31.Graphic 3D
32.Graphic Library
33.Groupware
34.HTML Parser
35.IDE
36.IDE Eclipse
37.IDE Netbeans
38.Installer
39.Internationalization Localization
40.Inversion of Control
41.Issue Tracking
42.J2EE
43.J2ME
44.JBoss
45.JMS
46.JMX
47.Library
48.Mail Clients
49.Music
50.Net
51.Parser
52.PDF
53.Portal
54.Profiler
55.Project Management
56.Report
57.RSS RDF
58.Rule Engine
59.Science
60.Scripting
61.Search Engine
62.Security
63.Sevlet Container
64.Source Control
65.Swing Library
66.Template Engine
67.Test Coverage
68.Testing
69.UML
70.Web Crawler
71.Web Framework
72.Web Mail
73.Web Server
74.Web Services
75.Web Services apache cxf 2.2.6
76.Web Services AXIS2
77.Wiki Engine
78.Workflow Engines
79.XML
80.XML UI
Java Source Code / Java Documentation » 6.0 JDK Core » swing » javax.swing.text.html 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001        /*
002         * Copyright 1998-2006 Sun Microsystems, Inc.  All Rights Reserved.
003         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004         *
005         * This code is free software; you can redistribute it and/or modify it
006         * under the terms of the GNU General Public License version 2 only, as
007         * published by the Free Software Foundation.  Sun designates this
008         * particular file as subject to the "Classpath" exception as provided
009         * by Sun in the LICENSE file that accompanied this code.
010         *
011         * This code is distributed in the hope that it will be useful, but WITHOUT
012         * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013         * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014         * version 2 for more details (a copy is included in the LICENSE file that
015         * accompanied this code).
016         *
017         * You should have received a copy of the GNU General Public License version
018         * 2 along with this work; if not, write to the Free Software Foundation,
019         * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020         *
021         * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022         * CA 95054 USA or visit www.sun.com if you need additional information or
023         * have any questions.
024         */
025        package javax.swing.text.html;
026
027        import java.awt.*;
028        import java.text.BreakIterator;
029        import javax.swing.event.DocumentEvent;
030        import javax.swing.text.*;
031
032        /**
033         * Displays the <dfn>inline element</dfn> styles
034         * based upon css attributes.
035         *
036         * @author  Timothy Prinzing
037         * @version 1.33 05/05/07
038         */
039        public class InlineView extends LabelView {
040
041            /**
042             * Constructs a new view wrapped on an element.
043             *
044             * @param elem the element
045             */
046            public InlineView(Element elem) {
047                super (elem);
048                StyleSheet sheet = getStyleSheet();
049                attr = sheet.getViewAttributes(this );
050            }
051
052            /**
053             * Gives notification that something was inserted into 
054             * the document in a location that this view is responsible for.
055             * If either parameter is <code>null</code>, behavior of this method is
056             * implementation dependent.
057             *  
058             * @param e the change information from the associated document
059             * @param a the current allocation of the view
060             * @param f the factory to use to rebuild if the view has children
061             * @since 1.5
062             * @see View#insertUpdate
063             */
064            public void insertUpdate(DocumentEvent e, Shape a, ViewFactory f) {
065                super .insertUpdate(e, a, f);
066                longestWordSpan = -1.0f;
067            }
068
069            /**
070             * Gives notification that something was removed from the document
071             * in a location that this view is responsible for.
072             * If either parameter is <code>null</code>, behavior of this method is
073             * implementation dependent. 
074             *
075             * @param e the change information from the associated document
076             * @param a the current allocation of the view
077             * @param f the factory to use to rebuild if the view has children
078             * @since 1.5
079             * @see View#removeUpdate
080             */
081            public void removeUpdate(DocumentEvent e, Shape a, ViewFactory f) {
082                super .removeUpdate(e, a, f);
083                longestWordSpan = -1.0f;
084            }
085
086            /**
087             * Gives notification from the document that attributes were changed
088             * in a location that this view is responsible for.
089             *
090             * @param e the change information from the associated document
091             * @param a the current allocation of the view
092             * @param f the factory to use to rebuild if the view has children
093             * @see View#changedUpdate
094             */
095            public void changedUpdate(DocumentEvent e, Shape a, ViewFactory f) {
096                super .changedUpdate(e, a, f);
097                StyleSheet sheet = getStyleSheet();
098                attr = sheet.getViewAttributes(this );
099                longestWordSpan = -1.0f;
100                preferenceChanged(null, true, true);
101            }
102
103            /**
104             * Fetches the attributes to use when rendering.  This is
105             * implemented to multiplex the attributes specified in the
106             * model with a StyleSheet.
107             */
108            public AttributeSet getAttributes() {
109                return attr;
110            }
111
112            /**
113             * Determines how attractive a break opportunity in 
114             * this view is.  This can be used for determining which
115             * view is the most attractive to call <code>breakView</code>
116             * on in the process of formatting.  A view that represents
117             * text that has whitespace in it might be more attractive
118             * than a view that has no whitespace, for example.  The
119             * higher the weight, the more attractive the break.  A
120             * value equal to or lower than <code>BadBreakWeight</code>
121             * should not be considered for a break.  A value greater
122             * than or equal to <code>ForcedBreakWeight</code> should
123             * be broken.
124             * <p>
125             * This is implemented to provide the default behavior
126             * of returning <code>BadBreakWeight</code> unless the length
127             * is greater than the length of the view in which case the 
128             * entire view represents the fragment.  Unless a view has
129             * been written to support breaking behavior, it is not
130             * attractive to try and break the view.  An example of
131             * a view that does support breaking is <code>LabelView</code>.
132             * An example of a view that uses break weight is 
133             * <code>ParagraphView</code>.
134             *
135             * @param axis may be either View.X_AXIS or View.Y_AXIS
136             * @param pos the potential location of the start of the 
137             *   broken view >= 0.  This may be useful for calculating tab
138             *   positions.
139             * @param len specifies the relative length from <em>pos</em>
140             *   where a potential break is desired >= 0.
141             * @return the weight, which should be a value between
142             *   ForcedBreakWeight and BadBreakWeight.
143             * @see LabelView
144             * @see ParagraphView
145             * @see javax.swing.text.View#BadBreakWeight
146             * @see javax.swing.text.View#GoodBreakWeight
147             * @see javax.swing.text.View#ExcellentBreakWeight
148             * @see javax.swing.text.View#ForcedBreakWeight
149             */
150            public int getBreakWeight(int axis, float pos, float len) {
151                if (nowrap) {
152                    return BadBreakWeight;
153                }
154                return super .getBreakWeight(axis, pos, len);
155            }
156
157            /**
158             * Tries to break this view on the given axis. Refer to
159             * {@link javax.swing.text.View#breakView} for a complete
160             * description of this method.
161             * <p>Behavior of this method is unspecified in case <code>axis</code>
162             * is neither <code>View.X_AXIS</code> nor <code>View.Y_AXIS</code>, and
163             * in case <code>offset</code>, <code>pos</code>, or <code>len</code>
164             * is null.
165             *   
166             * @param axis may be either <code>View.X_AXIS</code> or
167             *		<code>View.Y_AXIS</code>
168             * @param offset the location in the document model
169             *   that a broken fragment would occupy >= 0.  This
170             *   would be the starting offset of the fragment
171             *   returned
172             * @param pos the position along the axis that the
173             *  broken view would occupy >= 0.  This may be useful for
174             *  things like tab calculations
175             * @param len specifies the distance along the axis
176             *  where a potential break is desired >= 0
177             * @return the fragment of the view that represents the
178             *  given span.
179             * @since 1.5 
180             * @see javax.swing.text.View#breakView
181             */
182            public View breakView(int axis, int offset, float pos, float len) {
183                InlineView view = (InlineView) super .breakView(axis, offset,
184                        pos, len);
185                if (view != this ) {
186                    view.longestWordSpan = -1;
187                }
188                return view;
189            }
190
191            /**
192             * Fetch the span of the longest word in the view.
193             */
194            float getLongestWordSpan() {
195                if (longestWordSpan < 0.0f) {
196                    longestWordSpan = calculateLongestWordSpan();
197                }
198                return longestWordSpan;
199            }
200
201            float calculateLongestWordSpan() {
202                float rv = 0f;
203                Document doc = getDocument();
204                //AbstractDocument.MultiByteProperty
205                final Object MultiByteProperty = "multiByte";
206                if (doc != null
207                        && Boolean.TRUE.equals(doc
208                                .getProperty(MultiByteProperty))) {
209                    rv = calculateLongestWordSpanUseBreakIterator();
210                } else {
211                    rv = calculateLongestWordSpanUseWhitespace();
212                }
213                return rv;
214            }
215
216            float calculateLongestWordSpanUseBreakIterator() {
217                float span = 0;
218                Document doc = getDocument();
219                int p0 = getStartOffset();
220                int p1 = getEndOffset();
221                if (p1 > p0) {
222                    try {
223                        FontMetrics metrics = getFontMetrics();
224                        Segment segment = new Segment();
225                        doc.getText(p0, p1 - p0, segment);
226                        Container c = getContainer();
227                        BreakIterator line;
228                        if (c != null) {
229                            line = BreakIterator.getLineInstance(c.getLocale());
230                        } else {
231                            line = BreakIterator.getLineInstance();
232                        }
233                        line.setText(segment);
234                        int start = line.first();
235                        for (int end = line.next(); end != BreakIterator.DONE; start = end, end = line
236                                .next()) {
237                            if (end > start) {
238                                span = Math.max(span, metrics.charsWidth(
239                                        segment.array, start, end - start));
240                            }
241                        }
242                    } catch (BadLocationException ble) {
243                        // If the text can't be retrieved, it can't influence the size.
244                    }
245                }
246                return span;
247            }
248
249            float calculateLongestWordSpanUseWhitespace() {
250                float span = 0;
251                Document doc = getDocument();
252                int p0 = getStartOffset();
253                int p1 = getEndOffset();
254                if (p1 > p0) {
255                    try {
256                        Segment segment = new Segment();
257                        doc.getText(p0, p1 - p0, segment);
258                        final int CONTENT = 0;
259                        final int SPACES = 1;
260                        int state = CONTENT;
261                        int start = segment.offset;
262                        int end = start;
263                        FontMetrics metrics = getFontMetrics();
264                        final int lastIndex = segment.offset + segment.count
265                                - 1;
266                        for (int i = segment.offset; i <= lastIndex; i++) {
267                            boolean updateSpan = false;
268                            if (Character.isWhitespace(segment.array[i])) {
269                                if (state == CONTENT) {
270                                    //we got a word
271                                    updateSpan = true;
272                                    state = SPACES;
273                                }
274                            } else {
275                                if (state == SPACES) {
276                                    //first non space
277                                    start = i;
278                                    end = start;
279                                    state = CONTENT;
280                                } else {
281                                    end = i;
282                                }
283                                //handle last word
284                                if (i == lastIndex) {
285                                    updateSpan = true;
286                                }
287                            }
288                            if (updateSpan) {
289                                if (end > start) {
290                                    span = Math.max(span, metrics.charsWidth(
291                                            segment.array, start, end - start
292                                                    + 1));
293                                }
294                            }
295
296                        }
297                    } catch (BadLocationException ble) {
298                        // If the text can't be retrieved, it can't influence the size.
299                    }
300                }
301                return span;
302            }
303
304            /**
305             * Set the cached properties from the attributes.
306             */
307            protected void setPropertiesFromAttributes() {
308                super .setPropertiesFromAttributes();
309                AttributeSet a = getAttributes();
310                Object decor = a.getAttribute(CSS.Attribute.TEXT_DECORATION);
311                boolean u = (decor != null) ? (decor.toString().indexOf(
312                        "underline") >= 0) : false;
313                setUnderline(u);
314                boolean s = (decor != null) ? (decor.toString().indexOf(
315                        "line-through") >= 0) : false;
316                setStrikeThrough(s);
317                Object vAlign = a.getAttribute(CSS.Attribute.VERTICAL_ALIGN);
318                s = (vAlign != null) ? (vAlign.toString().indexOf("sup") >= 0)
319                        : false;
320                setSuperscript(s);
321                s = (vAlign != null) ? (vAlign.toString().indexOf("sub") >= 0)
322                        : false;
323                setSubscript(s);
324
325                Object whitespace = a.getAttribute(CSS.Attribute.WHITE_SPACE);
326                if ((whitespace != null) && whitespace.equals("nowrap")) {
327                    nowrap = true;
328                } else {
329                    nowrap = false;
330                }
331
332                HTMLDocument doc = (HTMLDocument) getDocument();
333                // fetches background color from stylesheet if specified
334                Color bg = doc.getBackground(a);
335                if (bg != null) {
336                    setBackground(bg);
337                }
338            }
339
340            protected StyleSheet getStyleSheet() {
341                HTMLDocument doc = (HTMLDocument) getDocument();
342                return doc.getStyleSheet();
343            }
344
345            private boolean nowrap;
346            private AttributeSet attr;
347            private float longestWordSpan = -1.0f;
348        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.