KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > editor > CollapsedView


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.editor;
21
22 import java.awt.Color JavaDoc;
23 import java.awt.Container JavaDoc;
24 import java.awt.Font JavaDoc;
25 import java.awt.FontMetrics JavaDoc;
26 import java.awt.Graphics JavaDoc;
27 import java.awt.Point JavaDoc;
28 import java.awt.Rectangle JavaDoc;
29 import java.awt.Shape JavaDoc;
30 import javax.swing.JComponent JavaDoc;
31 import javax.swing.event.DocumentEvent JavaDoc;
32 import javax.swing.text.AttributeSet JavaDoc;
33 import javax.swing.text.BadLocationException JavaDoc;
34 import javax.swing.text.Caret JavaDoc;
35 import javax.swing.text.Document JavaDoc;
36 import javax.swing.text.Element JavaDoc;
37 import javax.swing.text.JTextComponent JavaDoc;
38 import javax.swing.text.Position JavaDoc;
39 import javax.swing.text.View JavaDoc;
40 import javax.swing.text.ViewFactory JavaDoc;
41 import org.netbeans.editor.ext.ExtEditorUI;
42 import org.netbeans.editor.ext.ToolTipSupport;
43 import org.netbeans.editor.view.spi.LockView;
44
45 /**
46  * View over collapsed area of the fold.
47  * <br>
48  * The collapsed area spans one or more lines and it is presented as three dots.
49  *
50  * @author Martin Roskanin
51  */

52 class CollapsedView extends View JavaDoc implements SettingsChangeListener {
53
54     private Position JavaDoc startPos;
55     
56     private Position JavaDoc endPos;
57     
58     private String JavaDoc foldDescription;
59
60     private Font JavaDoc font;
61     
62     private Color JavaDoc foreColor;
63     
64     private Color JavaDoc backColor;
65     
66     
67     /** Creates a new instance of CollapsedView */
68     public CollapsedView(Element JavaDoc elem, Position JavaDoc startPos, Position JavaDoc endPos, String JavaDoc foldDescription) {
69         super(elem);
70         
71         this.startPos = startPos;
72         this.endPos = endPos;
73         this.foldDescription = foldDescription;
74         Settings.addSettingsChangeListener(this);
75     }
76     
77     private JTextComponent JavaDoc getComponent() {
78         return (JTextComponent JavaDoc)getContainer();
79     }
80     
81     private BaseTextUI getBaseTextUI(){
82         JTextComponent JavaDoc comp = getComponent();
83         return (comp!=null)?(BaseTextUI)comp.getUI():null;
84     }
85     
86     private EditorUI getEditorUI(){
87         BaseTextUI btui = getBaseTextUI();
88         return (btui!=null) ? btui.getEditorUI() : null;
89     }
90     
91     public void insertUpdate(DocumentEvent JavaDoc e, Shape JavaDoc a, ViewFactory JavaDoc f) {
92     }
93     
94     public void removeUpdate(DocumentEvent JavaDoc e, Shape JavaDoc a, ViewFactory JavaDoc f) {
95     }
96     
97     public void changedUpdate(DocumentEvent JavaDoc e, Shape JavaDoc a, ViewFactory JavaDoc f) {
98     }
99     
100     public Document JavaDoc getDocument() {
101         View JavaDoc parent = getParent();
102         return (parent == null) ? null : parent.getDocument();
103     }
104     
105     public int getStartOffset() {
106         return startPos.getOffset();
107     }
108     
109     public int getEndOffset() {
110         return endPos.getOffset();
111     }
112     
113     protected void forwardUpdate(DocumentEvent.ElementChange JavaDoc ec,
114                       DocumentEvent JavaDoc e, Shape JavaDoc a, ViewFactory JavaDoc f) {
115     }
116     
117     protected void forwardUpdateToView(View JavaDoc v, DocumentEvent JavaDoc e,
118                        Shape JavaDoc a, ViewFactory JavaDoc f) {
119     }
120     
121     public float getAlignment(int axis) {
122     return 0f;
123     }
124     
125     public float getPreferredSpan(int axis){
126         switch (axis) {
127             case Y_AXIS:
128                 return getEditorUI().getLineHeight();
129             case X_AXIS:
130                 return getCollapsedFoldStringWidth();
131         }
132         return 1f;
133     }
134
135     
136     private int getCollapsedFoldStringWidth() {
137         JTextComponent JavaDoc comp = getComponent();
138         if (comp==null) return 0;
139         FontMetrics JavaDoc fm = FontMetricsCache.getFontMetrics(getColoringFont(), comp);
140         if (fm==null) return 0;
141         return fm.stringWidth(foldDescription);
142     }
143     
144     public Shape JavaDoc modelToView(int pos, Shape JavaDoc a, Position.Bias JavaDoc b) throws BadLocationException JavaDoc {
145         return new Rectangle JavaDoc(a.getBounds().x, a.getBounds().y, getCollapsedFoldStringWidth(), getEditorUI().getLineHeight());
146     }
147     
148     public int viewToModel(float x, float y, Shape JavaDoc a, Position.Bias JavaDoc[] biasReturn) {
149         return getStartOffset();
150     }
151     
152     public void paint(Graphics JavaDoc g, Shape JavaDoc allocation){
153         Rectangle JavaDoc allocRect = allocation.getBounds();
154         g.setColor(getBackColor());
155         int x = allocRect.x+2;
156         int y = allocRect.y;
157         int width = allocRect.width-1;
158         int height = allocRect.height-1;
159         g.fillRect(x, y, width, height);
160         g.setColor(getForeColor());
161         g.setFont(getColoringFont());
162         g.drawRect(x, y, width, height);
163         g.drawString(foldDescription, x, y + getEditorUI().getLineAscent()-1);
164     }
165     
166     public int getNextVisualPositionFrom(int pos, Position.Bias JavaDoc b, Shape JavaDoc a,
167                      int direction, Position.Bias JavaDoc[] biasRet)
168       throws BadLocationException JavaDoc {
169     biasRet[0] = Position.Bias.Forward;
170     switch (direction) {
171     case NORTH:
172     case SOUTH:
173     {
174         JTextComponent JavaDoc target = (JTextComponent JavaDoc) getContainer();
175         Caret JavaDoc c = (target != null) ? target.getCaret() : null;
176         // YECK! Ideally, the x location from the magic caret position
177
// would be passed in.
178
Point JavaDoc mcp;
179         if (c != null) {
180         mcp = c.getMagicCaretPosition();
181         }
182         else {
183         mcp = null;
184         }
185         int x;
186         if (mcp == null) {
187         Rectangle JavaDoc loc = target.modelToView(pos);
188         x = (loc == null) ? 0 : loc.x;
189         }
190         else {
191         x = mcp.x;
192         }
193         if (direction == NORTH) {
194         pos = Utilities.getPositionAbove(target, pos, x);
195         }
196         else {
197         pos = Utilities.getPositionBelow(target, pos, x);
198         }
199     }
200         break;
201     case WEST:
202         if(pos == -1) {
203         pos = Math.max(0, getStartOffset());
204         }
205         else {
206                 if (b == Position.Bias.Backward){
207                     pos = Math.max(0, getStartOffset());
208                 }else{
209                     pos = Math.max(0, getStartOffset() - 1);
210                 }
211         }
212         break;
213     case EAST:
214         if(pos == -1) {
215         pos = getStartOffset();
216         }
217         else {
218         pos = Math.min(getEndOffset(), getDocument().getLength());
219                 //JTextComponent target = (JTextComponent) getContainer();
220
//if (target!=null && Utilities.getRowEnd(target, pos) == pos) pos = Math.min(pos+1, getDocument().getLength());
221
}
222         break;
223     default:
224         throw new IllegalArgumentException JavaDoc("Bad direction: " + direction); // NOI18N
225
}
226     return pos;
227     }
228
229     private View JavaDoc getExpandedView(){
230         Element JavaDoc parentElem = getElement().getParentElement();
231         int sei = parentElem.getElementIndex(getStartOffset());
232         int so = parentElem.getElement(sei).getStartOffset();
233         
234         int eei = parentElem.getElementIndex(getEndOffset());
235         int eo = parentElem.getElement(eei).getEndOffset();
236         
237         LockView fakeView = new LockView(
238         new DrawEngineFakeDocView(parentElem, so, eo, false)
239         );
240         RootView rootView = new RootView();
241         rootView.setView(fakeView);
242         return fakeView;
243     }
244     
245     public String JavaDoc getToolTipText(float x, float y, Shape JavaDoc allocation){
246         ToolTipSupport tts = ((ExtEditorUI)getEditorUI()).getToolTipSupport();
247         JComponent JavaDoc toolTip = new FoldingToolTip(getExpandedView(), getEditorUI());
248         tts.setToolTip(toolTip, PopupManager.ScrollBarBounds, PopupManager.Largest, -FoldingToolTip.BORDER_WIDTH, 0);
249         return "";
250     }
251     
252     public void settingsChange(SettingsChangeEvent evt) {
253         if (evt == null || org.netbeans.editor.Utilities.getKitClass(getComponent()) != evt.getKitClass()) return;
254         
255         String JavaDoc defaultColoringName = SettingsNames.DEFAULT_COLORING+SettingsNames.COLORING_NAME_SUFFIX;
256         String JavaDoc foldingColoringName = SettingsNames.CODE_FOLDING_COLORING+SettingsNames.COLORING_NAME_SUFFIX;
257         EditorUI editorUI = getEditorUI();
258         if (editorUI==null) return;
259         Coloring foldingColoring = editorUI.getColoring(SettingsNames.CODE_FOLDING_COLORING);
260         Coloring defaultColoring = editorUI.getDefaultColoring();
261         
262         Font JavaDoc foldingFont = null;
263         Color JavaDoc foldingForeColor = null;
264         Color JavaDoc foldingBackColor = null;
265         if (foldingColoring!=null){
266             foldingFont = foldingColoring.getFont();
267             foldingForeColor = foldingColoring.getForeColor();
268             foldingBackColor = foldingColoring.getBackColor();
269         }
270         
271         if (defaultColoringName.equals(evt.getSettingName())){
272             
273             if (foldingForeColor == null){
274                 // inherited fore color
275
Color JavaDoc tempColor = getDefaultForeColor();
276                 if (!tempColor.equals(foreColor)){
277                     foreColor = tempColor;
278                 }
279             }
280
281             if (foldingBackColor == null){
282                 // inherited back color
283
Color JavaDoc tempColor = getDefaultBackColor();
284                 if (!tempColor.equals(backColor)){
285                     backColor = tempColor;
286                 }
287             }
288             
289             // Font size change
290
Font JavaDoc tempFont = getDefaultColoringFont();
291             if (!tempFont.equals(font) && foldingFont==null){
292                 // if different font and foldingFont is inherited
293
font = tempFont;
294                 //setPreferredSize(new Dimension(font.getSize(), component.getHeight()));
295
}
296             //repaint();
297

298         }else if (foldingColoringName.equals(evt.getSettingName())){
299             // Code folding coloring change
300
if (foldingColoring == null) return;
301
302             Color JavaDoc tempColor = foldingColoring.getForeColor();
303             foreColor = (tempColor!=null) ? tempColor : getDefaultForeColor();
304             
305             tempColor = foldingColoring.getBackColor();
306             backColor = (tempColor!=null) ? tempColor : getDefaultBackColor();
307             
308             if (foldingFont == null){ //inherit
309
Font JavaDoc tempFont = getDefaultColoringFont();
310                 if (!tempFont.equals(font)){
311                     font = tempFont;
312                     //setPreferredSize(new Dimension(font.getSize(), component.getHeight()));
313
}
314             }else{
315                 if (!foldingFont.equals(font)){
316                     font = foldingFont;
317                     //setPreferredSize(new Dimension(font.getSize(), component.getHeight()));
318
}
319             }
320             
321             //repaint();
322
}
323     }
324     
325     private Font JavaDoc getDefaultColoringFont(){
326         // font in folding coloring not available, get default (or inherited)
327
EditorUI editorUI = getEditorUI();
328         if (editorUI!=null){
329             Coloring defaultColoring = editorUI.getDefaultColoring();
330             if (defaultColoring!=null){
331                 if (defaultColoring.getFont() != null){
332                     return defaultColoring.getFont();
333                 }
334             }
335         }
336         return SettingsDefaults.defaultFont;
337     }
338     
339     protected Font JavaDoc getColoringFont(){
340         if (font != null) return font;
341         EditorUI editorUI = getEditorUI();
342         if (editorUI!=null){
343             Coloring foldColoring = editorUI.getColoring(SettingsNames.CODE_FOLDING_COLORING);
344             if (foldColoring != null){
345                 if (foldColoring.getFont()!=null){
346                     font = foldColoring.getFont();
347                     return font;
348                 }
349             }
350         }
351         font = getDefaultColoringFont();
352         return font;
353     }
354     
355     protected Color JavaDoc getForeColor(){
356         if (foreColor != null) return foreColor;
357         EditorUI editorUI = getEditorUI();
358         if (editorUI!=null){
359             Coloring foldColoring = editorUI.getColoring(SettingsNames.CODE_FOLDING_COLORING);
360             if (foldColoring != null && foldColoring.getForeColor()!=null){
361                 foreColor = foldColoring.getForeColor();
362                 return foreColor;
363             }
364         }
365         foreColor = getDefaultForeColor();
366         return foreColor;
367     }
368     
369     private Color JavaDoc getDefaultForeColor(){
370         EditorUI editorUI = getEditorUI();
371         if (editorUI!=null){
372             // font in folding coloring not available, get default (or inherited)
373
Coloring defaultColoring = editorUI.getDefaultColoring();
374             if (defaultColoring!=null && defaultColoring.getForeColor()!=null){
375                 return defaultColoring.getForeColor();
376             }
377         }
378         return SettingsDefaults.defaultForeColor;
379     }
380     
381     private Color JavaDoc getDefaultBackColor(){
382         EditorUI editorUI = getEditorUI();
383         if (editorUI!=null){
384             // font in folding coloring not available, get default (or inherited)
385
Coloring defaultColoring = editorUI.getDefaultColoring();
386             if (defaultColoring!=null){
387                 return defaultColoring.getBackColor();
388             }
389         }
390         return SettingsDefaults.defaultBackColor;
391     }
392     
393     protected Color JavaDoc getBackColor(){
394         if (backColor != null) return backColor;
395         EditorUI editorUI = getEditorUI();
396         if (editorUI!=null){
397             Coloring foldColoring = editorUI.getColoring(SettingsNames.CODE_FOLDING_COLORING);
398             if (foldColoring != null && foldColoring.getBackColor()!=null){
399                 backColor = foldColoring.getBackColor();
400                 return backColor;
401             }
402         }
403         backColor = getDefaultBackColor();
404         return backColor;
405     }
406
407     
408     class RootView extends View JavaDoc {
409
410         RootView() {
411             super(null);
412         }
413
414         void setView(View JavaDoc v) {
415             if (view != null) {
416                 // get rid of back reference so that the old
417
// hierarchy can be garbage collected.
418
view.setParent(null);
419             }
420             view = v;
421             if (view != null) {
422                 view.setParent(this);
423             }
424         }
425
426     /**
427      * Fetches the attributes to use when rendering. At the root
428      * level there are no attributes. If an attribute is resolved
429      * up the view hierarchy this is the end of the line.
430      */

431         public AttributeSet JavaDoc getAttributes() {
432         return null;
433     }
434
435         /**
436          * Determines the preferred span for this view along an axis.
437          *
438          * @param axis may be either X_AXIS or Y_AXIS
439          * @return the span the view would like to be rendered into.
440          * Typically the view is told to render into the span
441          * that is returned, although there is no guarantee.
442          * The parent may choose to resize or break the view.
443          */

444         public float getPreferredSpan(int axis) {
445             if (view != null) {
446                 return view.getPreferredSpan(axis);
447             }
448             return 10;
449         }
450
451         /**
452          * Determines the minimum span for this view along an axis.
453          *
454          * @param axis may be either X_AXIS or Y_AXIS
455          * @return the span the view would like to be rendered into.
456          * Typically the view is told to render into the span
457          * that is returned, although there is no guarantee.
458          * The parent may choose to resize or break the view.
459          */

460         public float getMinimumSpan(int axis) {
461             if (view != null) {
462                 return view.getMinimumSpan(axis);
463             }
464             return 10;
465         }
466
467         /**
468          * Determines the maximum span for this view along an axis.
469          *
470          * @param axis may be either X_AXIS or Y_AXIS
471          * @return the span the view would like to be rendered into.
472          * Typically the view is told to render into the span
473          * that is returned, although there is no guarantee.
474          * The parent may choose to resize or break the view.
475          */

476         public float getMaximumSpan(int axis) {
477         return Integer.MAX_VALUE;
478         }
479
480         /**
481          * Specifies that a preference has changed.
482          * Child views can call this on the parent to indicate that
483          * the preference has changed. The root view routes this to
484          * invalidate on the hosting component.
485          * <p>
486          * This can be called on a different thread from the
487          * event dispatching thread and is basically unsafe to
488          * propagate into the component. To make this safe,
489          * the operation is transferred over to the event dispatching
490          * thread for completion. It is a design goal that all view
491          * methods be safe to call without concern for concurrency,
492          * and this behavior helps make that true.
493          *
494          * @param child the child view
495          * @param width true if the width preference has changed
496          * @param height true if the height preference has changed
497          */

498         public void preferenceChanged(View JavaDoc child, boolean width, boolean height) {
499             
500         }
501
502         /**
503          * Determines the desired alignment for this view along an axis.
504          *
505          * @param axis may be either X_AXIS or Y_AXIS
506          * @return the desired alignment, where 0.0 indicates the origin
507          * and 1.0 the full span away from the origin
508          */

509         public float getAlignment(int axis) {
510             if (view != null) {
511                 return view.getAlignment(axis);
512             }
513             return 0;
514         }
515
516         /**
517          * Renders the view.
518          *
519          * @param g the graphics context
520          * @param allocation the region to render into
521          */

522         public void paint(Graphics JavaDoc g, Shape JavaDoc allocation) {
523             if (view != null) {
524                 Rectangle JavaDoc alloc = (allocation instanceof Rectangle JavaDoc) ?
525                   (Rectangle JavaDoc)allocation : allocation.getBounds();
526         setSize(alloc.width, alloc.height);
527                 view.paint(g, allocation);
528             }
529         }
530         
531         /**
532          * Sets the view parent.
533          *
534          * @param parent the parent view
535          */

536         public void setParent(View JavaDoc parent) {
537             throw new Error JavaDoc("Can't set parent on root view"); // NOI18N
538
}
539
540         /**
541          * Returns the number of views in this view. Since
542          * this view simply wraps the root of the view hierarchy
543          * it has exactly one child.
544          *
545          * @return the number of views
546          * @see #getView
547          */

548         public int getViewCount() {
549             return 1;
550         }
551
552         /**
553          * Gets the n-th view in this container.
554          *
555          * @param n the number of the view to get
556          * @return the view
557          */

558         public View JavaDoc getView(int n) {
559             return view;
560         }
561
562     /**
563      * Returns the child view index representing the given position in
564      * the model. This is implemented to return the index of the only
565      * child.
566      *
567      * @param pos the position >= 0
568      * @return index of the view representing the given position, or
569      * -1 if no view represents that position
570      * @since 1.3
571      */

572         public int getViewIndex(int pos, Position.Bias JavaDoc b) {
573         return 0;
574     }
575     
576         /**
577          * Fetches the allocation for the given child view.
578          * This enables finding out where various views
579          * are located, without assuming the views store
580          * their location. This returns the given allocation
581          * since this view simply acts as a gateway between
582          * the view hierarchy and the associated component.
583          *
584          * @param index the index of the child
585          * @param a the allocation to this view.
586          * @return the allocation to the child
587          */

588         public Shape JavaDoc getChildAllocation(int index, Shape JavaDoc a) {
589             return a;
590         }
591
592         /**
593          * Provides a mapping from the document model coordinate space
594          * to the coordinate space of the view mapped to it.
595          *
596          * @param pos the position to convert
597          * @param a the allocated region to render into
598          * @return the bounding box of the given position
599          */

600         public Shape JavaDoc modelToView(int pos, Shape JavaDoc a, Position.Bias JavaDoc b) throws BadLocationException JavaDoc {
601             if (view != null) {
602                 return view.modelToView(pos, a, b);
603             }
604             return null;
605         }
606
607     /**
608      * Provides a mapping from the document model coordinate space
609      * to the coordinate space of the view mapped to it.
610      *
611      * @param p0 the position to convert >= 0
612      * @param b0 the bias toward the previous character or the
613      * next character represented by p0, in case the
614      * position is a boundary of two views.
615      * @param p1 the position to convert >= 0
616      * @param b1 the bias toward the previous character or the
617      * next character represented by p1, in case the
618      * position is a boundary of two views.
619      * @param a the allocated region to render into
620      * @return the bounding box of the given position is returned
621      * @exception BadLocationException if the given position does
622      * not represent a valid location in the associated document
623      * @exception IllegalArgumentException for an invalid bias argument
624      * @see View#viewToModel
625      */

626     public Shape JavaDoc modelToView(int p0, Position.Bias JavaDoc b0, int p1, Position.Bias JavaDoc b1, Shape JavaDoc a) throws BadLocationException JavaDoc {
627         if (view != null) {
628         return view.modelToView(p0, b0, p1, b1, a);
629         }
630         return null;
631     }
632
633         /**
634          * Provides a mapping from the view coordinate space to the logical
635          * coordinate space of the model.
636          *
637          * @param x x coordinate of the view location to convert
638          * @param y y coordinate of the view location to convert
639          * @param a the allocated region to render into
640          * @return the location within the model that best represents the
641          * given point in the view
642          */

643         public int viewToModel(float x, float y, Shape JavaDoc a, Position.Bias JavaDoc[] bias) {
644             if (view != null) {
645                 int retValue = view.viewToModel(x, y, a, bias);
646         return retValue;
647             }
648             return -1;
649         }
650
651         /**
652          * Provides a way to determine the next visually represented model
653          * location that one might place a caret. Some views may not be visible,
654          * they might not be in the same order found in the model, or they just
655          * might not allow access to some of the locations in the model.
656          *
657          * @param pos the position to convert >= 0
658          * @param a the allocated region to render into
659          * @param direction the direction from the current position that can
660          * be thought of as the arrow keys typically found on a keyboard.
661          * This may be SwingConstants.WEST, SwingConstants.EAST,
662          * SwingConstants.NORTH, or SwingConstants.SOUTH.
663          * @return the location within the model that best represents the next
664          * location visual position.
665          * @exception BadLocationException
666          * @exception IllegalArgumentException for an invalid direction
667          */

668         public int getNextVisualPositionFrom(int pos, Position.Bias JavaDoc b, Shape JavaDoc a,
669                                              int direction,
670                                              Position.Bias JavaDoc[] biasRet)
671             throws BadLocationException JavaDoc {
672             if( view != null ) {
673                 int nextPos = view.getNextVisualPositionFrom(pos, b, a,
674                              direction, biasRet);
675         if(nextPos != -1) {
676             pos = nextPos;
677         }
678         else {
679             biasRet[0] = b;
680         }
681             }
682             return pos;
683         }
684
685         /**
686          * Gives notification that something was inserted into the document
687          * in a location that this view is responsible for.
688          *
689          * @param e the change information from the associated document
690          * @param a the current allocation of the view
691          * @param f the factory to use to rebuild if the view has children
692          */

693         public void insertUpdate(DocumentEvent JavaDoc e, Shape JavaDoc a, ViewFactory JavaDoc f) {
694             if (view != null) {
695                 view.insertUpdate(e, a, f);
696             }
697         }
698         
699         /**
700          * Gives notification that something was removed from the document
701          * in a location that this view is responsible for.
702          *
703          * @param e the change information from the associated document
704          * @param a the current allocation of the view
705          * @param f the factory to use to rebuild if the view has children
706          */

707         public void removeUpdate(DocumentEvent JavaDoc e, Shape JavaDoc a, ViewFactory JavaDoc f) {
708             if (view != null) {
709                 view.removeUpdate(e, a, f);
710             }
711         }
712
713         /**
714          * Gives notification from the document that attributes were changed
715          * in a location that this view is responsible for.
716          *
717          * @param e the change information from the associated document
718          * @param a the current allocation of the view
719          * @param f the factory to use to rebuild if the view has children
720          */

721         public void changedUpdate(DocumentEvent JavaDoc e, Shape JavaDoc a, ViewFactory JavaDoc f) {
722             if (view != null) {
723                 view.changedUpdate(e, a, f);
724             }
725         }
726
727         /**
728          * Returns the document model underlying the view.
729          *
730          * @return the model
731          */

732         public Document JavaDoc getDocument() {
733             EditorUI editorUI = getEditorUI();
734             return (editorUI==null) ? null : editorUI.getDocument();
735         }
736         
737         /**
738          * Returns the starting offset into the model for this view.
739          *
740          * @return the starting offset
741          */

742         public int getStartOffset() {
743             if (view != null) {
744                 return view.getStartOffset();
745             }
746             return getElement().getStartOffset();
747         }
748
749         /**
750          * Returns the ending offset into the model for this view.
751          *
752          * @return the ending offset
753          */

754         public int getEndOffset() {
755             if (view != null) {
756                 return view.getEndOffset();
757             }
758             return getElement().getEndOffset();
759         }
760
761         /**
762          * Gets the element that this view is mapped to.
763          *
764          * @return the view
765          */

766         public Element JavaDoc getElement() {
767             if (view != null) {
768                 return view.getElement();
769             }
770             return view.getDocument().getDefaultRootElement();
771         }
772
773         /**
774          * Breaks this view on the given axis at the given length.
775          *
776          * @param axis may be either X_AXIS or Y_AXIS
777          * @param len specifies where a break is desired in the span
778          * @param the current allocation of the view
779          * @return the fragment of the view that represents the given span
780          * if the view can be broken, otherwise null
781          */

782         public View JavaDoc breakView(int axis, float len, Shape JavaDoc a) {
783             throw new Error JavaDoc("Can't break root view"); // NOI18N
784
}
785
786         /**
787          * Determines the resizability of the view along the
788          * given axis. A value of 0 or less is not resizable.
789          *
790          * @param axis may be either X_AXIS or Y_AXIS
791          * @return the weight
792          */

793         public int getResizeWeight(int axis) {
794             if (view != null) {
795                 return view.getResizeWeight(axis);
796             }
797             return 0;
798         }
799
800         /**
801          * Sets the view size.
802          *
803          * @param width the width
804          * @param height the height
805          */

806         public void setSize(float width, float height) {
807             if (view != null) {
808                 view.setSize(width, height);
809             }
810         }
811
812         /**
813          * Fetches the container hosting the view. This is useful for
814          * things like scheduling a repaint, finding out the host
815          * components font, etc. The default implementation
816          * of this is to forward the query to the parent view.
817          *
818          * @return the container
819          */

820         public Container JavaDoc getContainer() {
821             EditorUI editorUI = getEditorUI();
822             return (editorUI==null) ? null : editorUI.getComponent();
823         }
824         
825         /**
826          * Fetches the factory to be used for building the
827          * various view fragments that make up the view that
828          * represents the model. This is what determines
829          * how the model will be represented. This is implemented
830          * to fetch the factory provided by the associated
831          * EditorKit unless that is null, in which case this
832          * simply returns the BasicTextUI itself which allows
833          * subclasses to implement a simple factory directly without
834          * creating extra objects.
835          *
836          * @return the factory
837          */

838         public ViewFactory JavaDoc getViewFactory() {
839             EditorUI editorUI = getEditorUI();
840             if (editorUI!=null){
841                 BaseKit kit = Utilities.getKit(editorUI.getComponent());
842
843                 ViewFactory JavaDoc f = kit.getViewFactory();
844                 if (f != null) {
845                     return f;
846                 }
847             }
848             return getBaseTextUI();
849         }
850
851         private View JavaDoc view;
852
853     }
854     
855     
856 }
857
Popular Tags