/[classpath]/classpath/javax/swing/plaf/basic/BasicTextUI.java
ViewVC logotype

Diff of /classpath/javax/swing/plaf/basic/BasicTextUI.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.28 by rabbit78, Fri Jul 29 11:06:27 2005 UTC revision 1.29 by rabbit78, Fri Jul 29 14:13:09 2005 UTC
# Line 78  import javax.swing.text.Position; Line 78  import javax.swing.text.Position;
78  import javax.swing.text.View;  import javax.swing.text.View;
79  import javax.swing.text.ViewFactory;  import javax.swing.text.ViewFactory;
80    
81    /**
82     * The abstract base class from which the UI classes for Swings text
83     * components are derived. This provides most of the functionality for
84     * the UI classes.
85     *
86     * @author original author unknown
87     * @author Roman Kennke (roman@kennke.org)
88     */
89  public abstract class BasicTextUI extends TextUI  public abstract class BasicTextUI extends TextUI
90    implements ViewFactory    implements ViewFactory
91  {  {
92      /**
93       * A {@link DefaultCaret} that implements {@link UIResource}.
94       */
95    public static class BasicCaret extends DefaultCaret    public static class BasicCaret extends DefaultCaret
96      implements UIResource      implements UIResource
97    {    {
# Line 90  public abstract class BasicTextUI extend Line 100  public abstract class BasicTextUI extend
100      }      }
101    }    }
102    
103      /**
104       * A {@link DefaultHighlighter} that implements {@link UIResource}.
105       */
106    public static class BasicHighlighter extends DefaultHighlighter    public static class BasicHighlighter extends DefaultHighlighter
107      implements UIResource      implements UIResource
108    {    {
# Line 97  public abstract class BasicTextUI extend Line 110  public abstract class BasicTextUI extend
110      {      {
111      }      }
112    }    }
113      
114      /**
115       * This view forms the root of the View hierarchy. However, it delegates
116       * most calls to another View which is the real root of the hierarchy.
117       * The purpose is to make sure that all Views in the hierarchy, including
118       * the (real) root have a well-defined parent to which they can delegate
119       * calls like {@link #preferenceChanged}, {@link #getViewFactory} and
120       * {@link #getContainer}.
121       */
122    private class RootView extends View    private class RootView extends View
123    {    {
124        /** The real root view. */
125      private View view;      private View view;
126        
127        /**
128         * Creates a new RootView.
129         */
130      public RootView()      public RootView()
131      {      {
132        super(null);        super(null);
133      }      }
134    
135      // View methods.      /**
136         * Returns the ViewFactory for this RootView. If the current EditorKit
137         * provides a ViewFactory, this is used. Otherwise the TextUI itself
138         * is returned as a ViewFactory.
139         *
140         * @return the ViewFactory for this RootView
141         */
142      public ViewFactory getViewFactory()      public ViewFactory getViewFactory()
143      {      {
144        ViewFactory factory = null;        ViewFactory factory = null;
# Line 119  public abstract class BasicTextUI extend Line 149  public abstract class BasicTextUI extend
149        return factory;        return factory;
150      }      }
151    
152        /**
153         * Sets the real root view.
154         *
155         * @param v the root view to set
156         */
157      public void setView(View v)      public void setView(View v)
158      {      {
159        if (view != null)        if (view != null)
# Line 130  public abstract class BasicTextUI extend Line 165  public abstract class BasicTextUI extend
165        view = v;        view = v;
166      }      }
167    
168        /**
169         * Returns the <code>Container</code> that contains this view. This
170         * normally will be the text component that is managed by this TextUI.
171         *
172         * @return the <code>Container</code> that contains this view
173         */
174      public Container getContainer()      public Container getContainer()
175      {      {
176        return textComponent;        return textComponent;
177      }      }
178        
179        /**
180         * Returns the preferred span along the specified <code>axis</code>.
181         * This is delegated to the real root view.
182         *
183         * @param axis the axis for which the preferred span is queried
184         *
185         * @return the preferred span along the axis
186         */
187      public float getPreferredSpan(int axis)      public float getPreferredSpan(int axis)
188      {      {
189        if (view != null)        if (view != null)
# Line 143  public abstract class BasicTextUI extend Line 192  public abstract class BasicTextUI extend
192        return Integer.MAX_VALUE;        return Integer.MAX_VALUE;
193      }      }
194    
195        /**
196         * Paints the view. This is delegated to the real root view.
197         *
198         * @param g the <code>Graphics</code> context to paint to
199         * @param s the allocation for the View
200         */
201      public void paint(Graphics g, Shape s)      public void paint(Graphics g, Shape s)
202      {      {
203        if (view != null)        if (view != null)
204          view.paint(g, s);          view.paint(g, s);
205      }      }
206    
207    
208        /**
209         * Maps a position in the document into the coordinate space of the View.
210         * The output rectangle usually reflects the font height but has a width
211         * of zero.
212         *
213         * This is delegated to the real root view.
214         *
215         * @param pos the position of the character in the model
216         * @param a the area that is occupied by the view
217         * @param bias either {@link Position.Bias.Forward} or
218         *        {@link Position.Bias.Backward} depending on the preferred
219         *        direction bias. If <code>null</code> this defaults to
220         *        <code>Position.Bias.Forward</code>
221         *
222         * @return a rectangle that gives the location of the document position
223         *         inside the view coordinate space
224         *
225         * @throws BadLocationException if <code>pos</code> is invalid
226         * @throws IllegalArgumentException if b is not one of the above listed
227         *         valid values
228         */
229      public Shape modelToView(int position, Shape a, Position.Bias bias)      public Shape modelToView(int position, Shape a, Position.Bias bias)
230        throws BadLocationException        throws BadLocationException
231      {      {
# Line 198  public abstract class BasicTextUI extend Line 275  public abstract class BasicTextUI extend
275      }      }
276    }    }
277    
278      /**
279       * Receives notifications when properties of the text component change.
280       */
281    class UpdateHandler implements PropertyChangeListener    class UpdateHandler implements PropertyChangeListener
282    {    {
283        /**
284         * Notifies when a property of the text component changes.
285         *
286         * @param event the PropertyChangeEvent describing the change
287         */
288      public void propertyChange(PropertyChangeEvent event)      public void propertyChange(PropertyChangeEvent event)
289      {      {
290        if (event.getPropertyName().equals("document"))        if (event.getPropertyName().equals("document"))
# Line 261  public abstract class BasicTextUI extend Line 346  public abstract class BasicTextUI extend
346      }      }
347    }    }
348    
349      /**
350       * The EditorKit used by this TextUI.
351       */
352      // FIXME: should probably be non-static.
353    static EditorKit kit = new DefaultEditorKit();    static EditorKit kit = new DefaultEditorKit();
354    
355      /**
356       * The root view.
357       */
358    RootView rootView = new RootView();    RootView rootView = new RootView();
359    
360      /**
361       * The text component that we handle.
362       */
363    JTextComponent textComponent;    JTextComponent textComponent;
364    
365      /**
366       * Receives notification when the model changes.
367       */
368    UpdateHandler updateHandler = new UpdateHandler();    UpdateHandler updateHandler = new UpdateHandler();
369    
370    /** The DocumentEvent handler. */    /** The DocumentEvent handler. */
371    DocumentHandler documentHandler = new DocumentHandler();    DocumentHandler documentHandler = new DocumentHandler();
372    
373      /**
374       * Creates a new <code>BasicTextUI</code> instance.
375       */
376    public BasicTextUI()    public BasicTextUI()
377    {    {
378    }    }
379    
380      /**
381       * Creates a {@link Caret} that should be installed into the text component.
382       *
383       * @return a caret that should be installed into the text component
384       */
385    protected Caret createCaret()    protected Caret createCaret()
386    {    {
387      return new BasicCaret();      return new BasicCaret();
388    }    }
389    
390      /**
391       * Creates a {@link Highlighter} that should be installed into the text
392       * component.
393       *
394       * @return a <code>Highlighter</code> for the text component
395       */
396    protected Highlighter createHighlighter()    protected Highlighter createHighlighter()
397    {    {
398      return new BasicHighlighter();      return new BasicHighlighter();
399    }    }
400      
401      /**
402       * The text component that is managed by this UI.
403       *
404       * @return the text component that is managed by this UI
405       */
406    protected final JTextComponent getComponent()    protected final JTextComponent getComponent()
407    {    {
408      return textComponent;      return textComponent;
409    }    }
410      
411      /**
412       * Installs this UI on the text component.
413       *
414       * @param c the text component on which to install the UI
415       */
416    public void installUI(final JComponent c)    public void installUI(final JComponent c)
417    {    {
418      super.installUI(c);      super.installUI(c);
# Line 311  public abstract class BasicTextUI extend Line 435  public abstract class BasicTextUI extend
435      installKeyboardActions();      installKeyboardActions();
436    }    }
437    
438      /**
439       * Installs UI defaults on the text components.
440       */
441    protected void installDefaults()    protected void installDefaults()
442    {    {
443      Caret caret = textComponent.getCaret();      Caret caret = textComponent.getCaret();
# Line 335  public abstract class BasicTextUI extend Line 462  public abstract class BasicTextUI extend
462      caret.setBlinkRate(defaults.getInt(prefix + ".caretBlinkRate"));      caret.setBlinkRate(defaults.getInt(prefix + ".caretBlinkRate"));
463    }    }
464    
465      /**
466       * This FocusListener triggers repaints on focus shift.
467       */
468    private FocusListener focuslistener = new FocusListener() {    private FocusListener focuslistener = new FocusListener() {
469        public void focusGained(FocusEvent e)        public void focusGained(FocusEvent e)
470        {        {
# Line 346  public abstract class BasicTextUI extend Line 476  public abstract class BasicTextUI extend
476        }        }
477      };      };
478    
479      /**
480       * Install all listeners on the text component.
481       */
482    protected void installListeners()    protected void installListeners()
483    {    {
484      textComponent.addFocusListener(focuslistener);      textComponent.addFocusListener(focuslistener);
# Line 379  public abstract class BasicTextUI extend Line 512  public abstract class BasicTextUI extend
512      return className;      return className;
513    }    }
514    
515      /**
516       * Creates the {@link Keymap} that is installed on the text component.
517       *
518       * @return the {@link Keymap} that is installed on the text component
519       */
520    protected Keymap createKeymap()    protected Keymap createKeymap()
521    {    {
522      String prefix = getPropertyPrefix();      String prefix = getPropertyPrefix();
# Line 397  public abstract class BasicTextUI extend Line 535  public abstract class BasicTextUI extend
535      return km;          return km;    
536    }    }
537    
538      /**
539       * Installs the keyboard actions on the text components.
540       */
541    protected void installKeyboardActions()    protected void installKeyboardActions()
542    {        {    
543      // load any bindings for the older Keymap interface      // load any bindings for the older Keymap interface
# Line 412  public abstract class BasicTextUI extend Line 553  public abstract class BasicTextUI extend
553      SwingUtilities.replaceUIActionMap(textComponent, getActionMap());      SwingUtilities.replaceUIActionMap(textComponent, getActionMap());
554    }    }
555    
556      /**
557       * Gets the input map for the specified <code>condition</code>.
558       *
559       * @param condition the condition for the InputMap
560       *
561       * @return the InputMap for the specified condition
562       */
563    InputMap getInputMap(int condition)    InputMap getInputMap(int condition)
564    {    {
565      String prefix = getPropertyPrefix();      String prefix = getPropertyPrefix();
# Line 429  public abstract class BasicTextUI extend Line 577  public abstract class BasicTextUI extend
577        }        }
578    }    }
579    
580      /**
581       * Returns the ActionMap to be installed on the text component.
582       *
583       * @return the ActionMap to be installed on the text component
584       */
585      // FIXME: The UIDefaults have no entries for .actionMap, so this should
586      // be handled somehow different.
587    ActionMap getActionMap()    ActionMap getActionMap()
588    {    {
589      String prefix = getPropertyPrefix();      String prefix = getPropertyPrefix();
# Line 442  public abstract class BasicTextUI extend Line 597  public abstract class BasicTextUI extend
597      return am;      return am;
598    }    }
599    
600      /**
601       * Creates an ActionMap to be installed on the text component.
602       *
603       * @return an ActionMap to be installed on the text component
604       */
605    ActionMap createActionMap()    ActionMap createActionMap()
606    {    {
607      Action[] actions = textComponent.getActions();      Action[] actions = textComponent.getActions();
# Line 455  public abstract class BasicTextUI extend Line 615  public abstract class BasicTextUI extend
615      return am;      return am;
616    }    }
617    
618      /**
619       * Uninstalls this TextUI from the text component.
620       *
621       * @param component the text component to uninstall the UI from
622       */
623    public void uninstallUI(final JComponent component)    public void uninstallUI(final JComponent component)
624    {    {
625      super.uninstallUI(component);      super.uninstallUI(component);
# Line 469  public abstract class BasicTextUI extend Line 634  public abstract class BasicTextUI extend
634      textComponent = null;      textComponent = null;
635    }    }
636    
637      /**
638       * Uninstalls all default properties that have previously been installed by
639       * this UI.
640       */
641    protected void uninstallDefaults()    protected void uninstallDefaults()
642    {    {
643      // Do nothing here.      // Do nothing here.
644    }    }
645    
646      /**
647       * Uninstalls all listeners that have previously been installed by
648       * this UI.
649       */
650    protected void uninstallListeners()    protected void uninstallListeners()
651    {    {
652      textComponent.removeFocusListener(focuslistener);      textComponent.removeFocusListener(focuslistener);
653    }    }
654    
655      /**
656       * Uninstalls all keyboard actions that have previously been installed by
657       * this UI.
658       */
659    protected void uninstallKeyboardActions()    protected void uninstallKeyboardActions()
660    {    {
661      // Do nothing here.      // FIXME: Uninstall keyboard actions here.
662    }    }
663      
664      /**
665       * Returns the property prefix by which the text component's UIDefaults
666       * are looked up.
667       *
668       * @return the property prefix by which the text component's UIDefaults
669       *     are looked up
670       */
671    protected abstract String getPropertyPrefix();    protected abstract String getPropertyPrefix();
672    
673      /**
674       * Returns the preferred size of the text component.
675       *
676       * @param c not used here
677       *
678       * @return the preferred size of the text component
679       */
680    public Dimension getPreferredSize(JComponent c)    public Dimension getPreferredSize(JComponent c)
681    {    {
682      View v = getRootView(textComponent);      View v = getRootView(textComponent);
# Line 501  public abstract class BasicTextUI extend Line 692  public abstract class BasicTextUI extend
692     *     *
693     * This returns (Integer.MAX_VALUE, Integer.MAX_VALUE).     * This returns (Integer.MAX_VALUE, Integer.MAX_VALUE).
694     *     *
695       * @param c not used here
696       *
697     * @return the maximum size for text components that use this UI     * @return the maximum size for text components that use this UI
698     */     */
699    public Dimension getMaximumSize(JComponent c)    public Dimension getMaximumSize(JComponent c)
# Line 509  public abstract class BasicTextUI extend Line 702  public abstract class BasicTextUI extend
702      return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);      return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
703    }    }
704    
705      /**
706       * Paints the text component.
707       *
708       * @param g the <code>Graphics</code> context to paint to
709       * @param c not used here
710       */
711    public final void paint(Graphics g, JComponent c)    public final void paint(Graphics g, JComponent c)
712    {    {
713      paintSafely(g);      paintSafely(g);
714    }    }
715    
716      /**
717       * Actually performs the painting.
718       *
719       * @param g the <code>Graphics</code> context to paint to
720       */
721    protected void paintSafely(Graphics g)    protected void paintSafely(Graphics g)
722    {    {
723      Caret caret = textComponent.getCaret();      Caret caret = textComponent.getCaret();
# Line 532  public abstract class BasicTextUI extend Line 736  public abstract class BasicTextUI extend
736        caret.paint(g);        caret.paint(g);
737    }    }
738    
739      /**
740       * Paints the background of the text component.
741       *
742       * @param g the <code>Graphics</code> context to paint to
743       */
744    protected void paintBackground(Graphics g)    protected void paintBackground(Graphics g)
745    {    {
746      g.setColor(textComponent.getBackground());      g.setColor(textComponent.getBackground());
747      g.fillRect(0, 0, textComponent.getWidth(), textComponent.getHeight());      g.fillRect(0, 0, textComponent.getWidth(), textComponent.getHeight());
748    }    }
749    
750      /**
751       * Marks the specified range inside the text component's model as
752       * damaged and queues a repaint request.
753       *
754       * @param t the text component
755       * @param p0 the start location inside the document model of the range that
756       *        is damaged
757       * @param p1 the end location inside the document model of the range that
758       *        is damaged
759       */
760    public void damageRange(JTextComponent t, int p0, int p1)    public void damageRange(JTextComponent t, int p0, int p1)
761    {    {
762      damageRange(t, p0, p1, null, null);      damageRange(t, p0, p1, null, null);
763    }    }
764    
765      /**
766       * Marks the specified range inside the text component's model as
767       * damaged and queues a repaint request. This variant of this method
768       * allows a {@link Position.Bias} object to be specified for the start
769       * and end location of the range.
770       *
771       * @param t the text component
772       * @param p0 the start location inside the document model of the range that
773       *        is damaged
774       * @param p1 the end location inside the document model of the range that
775       *        is damaged
776       * @param firstBias the bias for the start location
777       * @param secondBias the bias for the end location
778       */
779    public void damageRange(JTextComponent t, int p0, int p1,    public void damageRange(JTextComponent t, int p0, int p1,
780                            Position.Bias firstBias, Position.Bias secondBias)                            Position.Bias firstBias, Position.Bias secondBias)
781    {    {
782        // TODO: Implement me.
783    }    }
784    
785      /**
786       * Returns the {@link EditorKit} used for the text component that is managed
787       * by this UI.
788       *
789       * @param t the text component
790       *
791       * @return the {@link EditorKit} used for the text component that is managed
792       *         by this UI
793       */
794    public EditorKit getEditorKit(JTextComponent t)    public EditorKit getEditorKit(JTextComponent t)
795    {    {
796      return kit;      return kit;
797    }    }
798    
799      /**
800       * Gets the next position inside the document model that is visible on
801       * screen, starting from <code>pos</code>.
802       *
803       * @param t the text component
804       * @param pos the start positionn
805       * @param b the bias for pos
806       * @param direction the search direction
807       * @param biasRet filled by the method to indicate the bias of the return
808       *        value
809       *
810       * @return the next position inside the document model that is visible on
811       *         screen
812       */
813    public int getNextVisualPositionFrom(JTextComponent t, int pos,    public int getNextVisualPositionFrom(JTextComponent t, int pos,
814                                         Position.Bias b, int direction,                                         Position.Bias b, int direction,
815                                         Position.Bias[] biasRet)                                         Position.Bias[] biasRet)
816      throws BadLocationException      throws BadLocationException
817    {    {
818      return 0;      return 0; // TODO: Implement me.
819    }    }
820    
821      /**
822       * Returns the root {@link View} of a text component.
823       *
824       * @return the root {@link View} of a text component
825       */
826    public View getRootView(JTextComponent t)    public View getRootView(JTextComponent t)
827    {    {
828      return rootView;      return rootView;
829    }    }
830    
831      /**
832       * Maps a position in the document into the coordinate space of the View.
833       * The output rectangle usually reflects the font height but has a width
834       * of zero. A bias of {@link Position.Bias.Forward} is used in this method.
835       *
836       * @param pos the position of the character in the model
837       * @param a the area that is occupied by the view
838       *
839       * @return a rectangle that gives the location of the document position
840       *         inside the view coordinate space
841       *
842       * @throws BadLocationException if <code>pos</code> is invalid
843       * @throws IllegalArgumentException if b is not one of the above listed
844       *         valid values
845       */
846    public Rectangle modelToView(JTextComponent t, int pos)    public Rectangle modelToView(JTextComponent t, int pos)
847      throws BadLocationException      throws BadLocationException
848    {    {
849      return modelToView(t, pos, Position.Bias.Forward);      return modelToView(t, pos, Position.Bias.Forward);
850    }    }
851    
852      /**
853       * Maps a position in the document into the coordinate space of the View.
854       * The output rectangle usually reflects the font height but has a width
855       * of zero.
856       *
857       * @param pos the position of the character in the model
858       * @param a the area that is occupied by the view
859       * @param bias either {@link Position.Bias.Forward} or
860       *        {@link Position.Bias.Backward} depending on the preferred
861       *        direction bias. If <code>null</code> this defaults to
862       *        <code>Position.Bias.Forward</code>
863       *
864       * @return a rectangle that gives the location of the document position
865       *         inside the view coordinate space
866       *
867       * @throws BadLocationException if <code>pos</code> is invalid
868       * @throws IllegalArgumentException if b is not one of the above listed
869       *         valid values
870       */
871    public Rectangle modelToView(JTextComponent t, int pos, Position.Bias bias)    public Rectangle modelToView(JTextComponent t, int pos, Position.Bias bias)
872      throws BadLocationException      throws BadLocationException
873    {    {
874      return rootView.modelToView(pos, getVisibleEditorRect(), bias).getBounds();      return rootView.modelToView(pos, getVisibleEditorRect(), bias).getBounds();
875    }    }
876    
877      /**
878       * Maps a point in the <code>View</code> coordinate space to a position
879       * inside a document model.
880       *
881       * @param t the text component
882       * @param pt the point to be mapped
883       *
884       * @return the position inside the document model that corresponds to
885       *     <code>pt</code>
886       */
887    public int viewToModel(JTextComponent t, Point pt)    public int viewToModel(JTextComponent t, Point pt)
888    {    {
889      return viewToModel(t, pt, null);      return viewToModel(t, pt, null);
890    }    }
891    
892      /**
893       * Maps a point in the <code>View</code> coordinate space to a position
894       * inside a document model.
895       *
896       * @param t the text component
897       * @param pt the point to be mapped
898       * @param biasReturn filled in by the method to indicate the bias of the
899       *        return value
900       *
901       * @return the position inside the document model that corresponds to
902       *     <code>pt</code>
903       */
904    public int viewToModel(JTextComponent t, Point pt, Position.Bias[] biasReturn)    public int viewToModel(JTextComponent t, Point pt, Position.Bias[] biasReturn)
905    {    {
906      return 0;      return 0; // FIXME: Implement me.
907    }    }
908    
909      /**
910       * Creates a {@link View} for the specified {@link Element}.
911       *
912       * @param elem the <code>Element</code> to create a <code>View</code> for
913       *
914       * @see ViewFactory
915       */
916    public View create(Element elem)    public View create(Element elem)
917    {    {
918      // Subclasses have to implement this to get this functionality.      // Subclasses have to implement this to get this functionality.
919      return null;      return null;
920    }    }
921    
922      /**
923       * Creates a {@link View} for the specified {@link Element}.
924       *
925       * @param elem the <code>Element</code> to create a <code>View</code> for
926       * @param p0 the start offset
927       * @param p1 the end offset
928       *
929       * @see ViewFactory
930       */
931    public View create(Element elem, int p0, int p1)    public View create(Element elem, int p0, int p1)
932    {    {
933      // Subclasses have to implement this to get this functionality.      // Subclasses have to implement this to get this functionality.
934      return null;      return null;
935    }    }
936      
937      /**
938       * Returns the allocation to give the root view.
939       *
940       * @return the allocation to give the root view
941       *
942       * @specnote The allocation has nothing to do with visibility. According
943       *           to the specs the naming of this method is unfortunate and
944       *           has historical reasons
945       */
946    protected Rectangle getVisibleEditorRect()    protected Rectangle getVisibleEditorRect()
947    {    {
948      int width = textComponent.getWidth();      int width = textComponent.getWidth();
# Line 614  public abstract class BasicTextUI extend Line 957  public abstract class BasicTextUI extend
957                           height - insets.top + insets.bottom);                           height - insets.top + insets.bottom);
958    }    }
959    
960      /**
961       * Sets the root view for the text component.
962       *
963       * @param view the <code>View</code> to be set as root view
964       */
965    protected final void setView(View view)    protected final void setView(View view)
966    {    {
967      rootView.setView(view);      rootView.setView(view);
968    }    }
969    
970      /**
971       * Indicates that the model of a text component has changed. This
972       * triggers a rebuild of the view hierarchy.
973       */
974    protected void modelChanged()    protected void modelChanged()
975    {    {
976      if (textComponent == null || rootView == null)      if (textComponent == null || rootView == null)

Legend:
Removed from v.1.28  
changed lines
  Added in v.1.29

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26