Source Code Cross Referenced for FieldView.java in  » 6.0-JDK-Core » swing » javax » swing » text » 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 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001        /*
002         * Copyright 1997-2005 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;
026
027        import java.awt.*;
028        import javax.swing.*;
029        import javax.swing.event.*;
030
031        /**
032         * Extends the multi-line plain text view to be suitable
033         * for a single-line editor view.  If the view is 
034         * allocated extra space, the field must adjust for it.
035         * If the hosting component is a JTextField, this view
036         * will manage the ranges of the associated BoundedRangeModel
037         * and will adjust the horizontal allocation to match the
038         * current visibility settings of the JTextField.
039         *
040         * @author  Timothy Prinzing
041         * @version 1.33 05/05/07
042         * @see     View
043         */
044        public class FieldView extends PlainView {
045
046            /**
047             * Constructs a new FieldView wrapped on an element.
048             *
049             * @param elem the element
050             */
051            public FieldView(Element elem) {
052                super (elem);
053            }
054
055            /**
056             * Fetches the font metrics associated with the component hosting
057             * this view.
058             *
059             * @return the metrics
060             */
061            protected FontMetrics getFontMetrics() {
062                Component c = getContainer();
063                return c.getFontMetrics(c.getFont());
064            }
065
066            /**
067             * Adjusts the allocation given to the view
068             * to be a suitable allocation for a text field.
069             * If the view has been allocated more than the 
070             * preferred span vertically, the allocation is
071             * changed to be centered vertically.  Horizontally
072             * the view is adjusted according to the horizontal
073             * alignment property set on the associated JTextField
074             * (if that is the type of the hosting component).
075             *
076             * @param a the allocation given to the view, which may need
077             *  to be adjusted.
078             * @return the allocation that the superclass should use.
079             */
080            protected Shape adjustAllocation(Shape a) {
081                if (a != null) {
082                    Rectangle bounds = a.getBounds();
083                    int vspan = (int) getPreferredSpan(Y_AXIS);
084                    int hspan = (int) getPreferredSpan(X_AXIS);
085                    if (bounds.height != vspan) {
086                        int slop = bounds.height - vspan;
087                        bounds.y += slop / 2;
088                        bounds.height -= slop;
089                    }
090
091                    // horizontal adjustments
092                    Component c = getContainer();
093                    if (c instanceof  JTextField) {
094                        JTextField field = (JTextField) c;
095                        BoundedRangeModel vis = field.getHorizontalVisibility();
096                        int max = Math.max(hspan, bounds.width);
097                        int value = vis.getValue();
098                        int extent = Math.min(max, bounds.width - 1);
099                        if ((value + extent) > max) {
100                            value = max - extent;
101                        }
102                        vis.setRangeProperties(value, extent, vis.getMinimum(),
103                                max, false);
104                        if (hspan < bounds.width) {
105                            // horizontally align the interior
106                            int slop = bounds.width - 1 - hspan;
107
108                            int align = ((JTextField) c)
109                                    .getHorizontalAlignment();
110                            if (Utilities.isLeftToRight(c)) {
111                                if (align == LEADING) {
112                                    align = LEFT;
113                                } else if (align == TRAILING) {
114                                    align = RIGHT;
115                                }
116                            } else {
117                                if (align == LEADING) {
118                                    align = RIGHT;
119                                } else if (align == TRAILING) {
120                                    align = LEFT;
121                                }
122                            }
123
124                            switch (align) {
125                            case SwingConstants.CENTER:
126                                bounds.x += slop / 2;
127                                bounds.width -= slop;
128                                break;
129                            case SwingConstants.RIGHT:
130                                bounds.x += slop;
131                                bounds.width -= slop;
132                                break;
133                            }
134                        } else {
135                            // adjust the allocation to match the bounded range.
136                            bounds.width = hspan;
137                            bounds.x -= vis.getValue();
138                        }
139                    }
140                    return bounds;
141                }
142                return null;
143            }
144
145            /**
146             * Update the visibility model with the associated JTextField
147             * (if there is one) to reflect the current visibility as a
148             * result of changes to the document model.  The bounded
149             * range properties are updated.  If the view hasn't yet been
150             * shown the extent will be zero and we just set it to be full
151             * until determined otherwise.
152             */
153            void updateVisibilityModel() {
154                Component c = getContainer();
155                if (c instanceof  JTextField) {
156                    JTextField field = (JTextField) c;
157                    BoundedRangeModel vis = field.getHorizontalVisibility();
158                    int hspan = (int) getPreferredSpan(X_AXIS);
159                    int extent = vis.getExtent();
160                    int maximum = Math.max(hspan, extent);
161                    extent = (extent == 0) ? maximum : extent;
162                    int value = maximum - extent;
163                    int oldValue = vis.getValue();
164                    if ((oldValue + extent) > maximum) {
165                        oldValue = maximum - extent;
166                    }
167                    value = Math.max(0, Math.min(value, oldValue));
168                    vis.setRangeProperties(value, extent, 0, maximum, false);
169                }
170            }
171
172            // --- View methods -------------------------------------------
173
174            /**
175             * Renders using the given rendering surface and area on that surface.
176             * The view may need to do layout and create child views to enable
177             * itself to render into the given allocation.
178             *
179             * @param g the rendering surface to use
180             * @param a the allocated region to render into
181             *
182             * @see View#paint
183             */
184            public void paint(Graphics g, Shape a) {
185                Rectangle r = (Rectangle) a;
186                g.clipRect(r.x, r.y, r.width, r.height);
187                super .paint(g, a);
188            }
189
190            /**
191             * Adjusts <code>a</code> based on the visible region and returns it.
192             */
193            Shape adjustPaintRegion(Shape a) {
194                return adjustAllocation(a);
195            }
196
197            /**
198             * Determines the preferred span for this view along an
199             * axis.
200             *
201             * @param axis may be either View.X_AXIS or View.Y_AXIS
202             * @return   the span the view would like to be rendered into >= 0.
203             *           Typically the view is told to render into the span
204             *           that is returned, although there is no guarantee.  
205             *           The parent may choose to resize or break the view.
206             */
207            public float getPreferredSpan(int axis) {
208                switch (axis) {
209                case View.X_AXIS:
210                    Segment buff = SegmentCache.getSharedSegment();
211                    Document doc = getDocument();
212                    int width;
213                    try {
214                        FontMetrics fm = getFontMetrics();
215                        doc.getText(0, doc.getLength(), buff);
216                        width = Utilities.getTabbedTextWidth(buff, fm, 0, this ,
217                                0);
218                        if (buff.count > 0) {
219                            Component c = getContainer();
220                            firstLineOffset = sun.swing.SwingUtilities2
221                                    .getLeftSideBearing(
222                                            (c instanceof  JComponent) ? (JComponent) c
223                                                    : null, fm,
224                                            buff.array[buff.offset]);
225                            firstLineOffset = Math.max(0, -firstLineOffset);
226                        } else {
227                            firstLineOffset = 0;
228                        }
229                    } catch (BadLocationException bl) {
230                        width = 0;
231                    }
232                    SegmentCache.releaseSharedSegment(buff);
233                    return width + firstLineOffset;
234                default:
235                    return super .getPreferredSpan(axis);
236                }
237            }
238
239            /**
240             * Determines the resizability of the view along the
241             * given axis.  A value of 0 or less is not resizable.
242             *
243             * @param axis View.X_AXIS or View.Y_AXIS
244             * @return the weight -> 1 for View.X_AXIS, else 0
245             */
246            public int getResizeWeight(int axis) {
247                if (axis == View.X_AXIS) {
248                    return 1;
249                }
250                return 0;
251            }
252
253            /**
254             * Provides a mapping from the document model coordinate space
255             * to the coordinate space of the view mapped to it.
256             *
257             * @param pos the position to convert >= 0
258             * @param a the allocated region to render into
259             * @return the bounding box of the given position
260             * @exception BadLocationException  if the given position does not
261             *   represent a valid location in the associated document
262             * @see View#modelToView
263             */
264            public Shape modelToView(int pos, Shape a, Position.Bias b)
265                    throws BadLocationException {
266                return super .modelToView(pos, adjustAllocation(a), b);
267            }
268
269            /**
270             * Provides a mapping from the view coordinate space to the logical
271             * coordinate space of the model.
272             *
273             * @param fx the X coordinate >= 0.0f
274             * @param fy the Y coordinate >= 0.0f
275             * @param a the allocated region to render into
276             * @return the location within the model that best represents the
277             *  given point in the view
278             * @see View#viewToModel
279             */
280            public int viewToModel(float fx, float fy, Shape a,
281                    Position.Bias[] bias) {
282                return super .viewToModel(fx, fy, adjustAllocation(a), bias);
283            }
284
285            /**
286             * Gives notification that something was inserted into the document
287             * in a location that this view is responsible for.
288             *
289             * @param changes the change information from the associated document
290             * @param a the current allocation of the view
291             * @param f the factory to use to rebuild if the view has children
292             * @see View#insertUpdate
293             */
294            public void insertUpdate(DocumentEvent changes, Shape a,
295                    ViewFactory f) {
296                super .insertUpdate(changes, adjustAllocation(a), f);
297                updateVisibilityModel();
298            }
299
300            /**
301             * Gives notification that something was removed from the document
302             * in a location that this view is responsible for.
303             *
304             * @param changes the change information from the associated document
305             * @param a the current allocation of the view
306             * @param f the factory to use to rebuild if the view has children
307             * @see View#removeUpdate
308             */
309            public void removeUpdate(DocumentEvent changes, Shape a,
310                    ViewFactory f) {
311                super.removeUpdate(changes, adjustAllocation(a), f);
312                updateVisibilityModel();
313            }
314
315        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.