home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 2000 March / pcp161a.iso / handson / files / copyjava.exe / com / sun / java / swing / text / DefaultTextUI.class (.txt) < prev    next >
Encoding:
Java Class File  |  1998-02-26  |  9.6 KB  |  312 lines

  1. package com.sun.java.swing.text;
  2.  
  3. import com.sun.java.swing.JComponent;
  4. import com.sun.java.swing.UIManager;
  5. import com.sun.java.swing.border.Border;
  6. import com.sun.java.swing.plaf.TextUI;
  7. import com.sun.java.swing.plaf.UIResource;
  8. import java.awt.Color;
  9. import java.awt.Container;
  10. import java.awt.Dimension;
  11. import java.awt.Font;
  12. import java.awt.Graphics;
  13. import java.awt.Insets;
  14. import java.awt.Point;
  15. import java.awt.Rectangle;
  16. import java.awt.Shape;
  17. import java.beans.PropertyChangeEvent;
  18. import java.io.IOException;
  19. import java.io.ObjectInputStream;
  20. import java.io.Serializable;
  21.  
  22. public abstract class DefaultTextUI extends TextUI implements ViewFactory, Serializable {
  23.    private static final EditorKit defaultKit = new DefaultEditorKit();
  24.    transient JTextComponent editor;
  25.    transient boolean painted = false;
  26.    transient RootView rootView = new RootView(this);
  27.    transient UpdateHandler updateHandler = new UpdateHandler(this);
  28.  
  29.    protected Caret createCaret() {
  30.       return new DefaultCaret();
  31.    }
  32.  
  33.    protected Highlighter createHighlighter() {
  34.       return new DefaultHighlighter();
  35.    }
  36.  
  37.    protected abstract Keymap createKeymap();
  38.  
  39.    protected void propertyChange(PropertyChangeEvent var1) {
  40.    }
  41.  
  42.    protected abstract String getPropertyPrefix();
  43.  
  44.    protected void installDefaults(JComponent var1) {
  45.       String var2 = this.getPropertyPrefix();
  46.       Font var3 = this.editor.getFont();
  47.       if (var3 == null || var3 instanceof UIResource) {
  48.          this.editor.setFont(UIManager.getFont(var2 + ".font"));
  49.       }
  50.  
  51.       Color var4 = this.editor.getBackground();
  52.       if (var4 == null || var4 instanceof UIResource) {
  53.          this.editor.setBackground(UIManager.getColor(var2 + ".background"));
  54.       }
  55.  
  56.       Color var5 = this.editor.getForeground();
  57.       if (var5 == null || var5 instanceof UIResource) {
  58.          this.editor.setForeground(UIManager.getColor(var2 + ".foreground"));
  59.       }
  60.  
  61.       Color var6 = this.editor.getCaretColor();
  62.       if (var6 == null || var6 instanceof UIResource) {
  63.          this.editor.setCaretColor(UIManager.getColor(var2 + ".caretForeground"));
  64.       }
  65.  
  66.       Color var7 = this.editor.getSelectionColor();
  67.       if (var7 == null || var7 instanceof UIResource) {
  68.          this.editor.setSelectionColor(UIManager.getColor(var2 + ".selectionBackground"));
  69.       }
  70.  
  71.       Color var8 = this.editor.getSelectedTextColor();
  72.       if (var8 == null || var8 instanceof UIResource) {
  73.          this.editor.setSelectedTextColor(UIManager.getColor(var2 + ".selectionForeground"));
  74.       }
  75.  
  76.       Color var9 = this.editor.getDisabledTextColor();
  77.       if (var9 == null || var9 instanceof UIResource) {
  78.          this.editor.setDisabledTextColor(UIManager.getColor(var2 + ".inactiveForeground"));
  79.       }
  80.  
  81.       Border var10 = this.editor.getBorder();
  82.       if (var10 == null || var10 instanceof UIResource) {
  83.          this.editor.setBorder(UIManager.getBorder(var2 + ".border"));
  84.       }
  85.  
  86.       Caret var11 = this.createCaret();
  87.       this.editor.setCaret(var11);
  88.       Object var12 = UIManager.get(var2 + ".caretBlinkRate");
  89.       if (var12 != null && var12 instanceof Integer) {
  90.          Integer var13 = (Integer)var12;
  91.          var11.setBlinkRate(var13);
  92.       }
  93.  
  94.    }
  95.  
  96.    protected void uninstallDefaults(JComponent var1) {
  97.       if (this.editor.getCaretColor() instanceof UIResource) {
  98.          this.editor.setCaretColor((Color)null);
  99.       }
  100.  
  101.       if (this.editor.getSelectionColor() instanceof UIResource) {
  102.          this.editor.setSelectionColor((Color)null);
  103.       }
  104.  
  105.       if (this.editor.getDisabledTextColor() instanceof UIResource) {
  106.          this.editor.setDisabledTextColor((Color)null);
  107.       }
  108.  
  109.       if (this.editor.getSelectedTextColor() instanceof UIResource) {
  110.          this.editor.setSelectedTextColor((Color)null);
  111.       }
  112.  
  113.       if (this.editor.getBorder() instanceof UIResource) {
  114.          this.editor.setBorder((Border)null);
  115.       }
  116.  
  117.       this.editor.setCaret((Caret)null);
  118.    }
  119.  
  120.    protected void installListeners(JComponent var1) {
  121.    }
  122.  
  123.    protected void uninstallListeners(JComponent var1) {
  124.    }
  125.  
  126.    protected void paintBackground(Graphics var1) {
  127.       var1.setColor(this.editor.getBackground());
  128.       Dimension var2 = this.editor.getSize();
  129.       var1.fillRect(0, 0, var2.width, var2.height);
  130.    }
  131.  
  132.    protected final JTextComponent getComponent() {
  133.       return this.editor;
  134.    }
  135.  
  136.    protected void modelChanged() {
  137.       ViewFactory var1 = this.rootView.getViewFactory();
  138.       Document var2 = this.editor.getDocument();
  139.       Element var3 = var2.getDefaultRootElement();
  140.       this.setView(var1.create(var3));
  141.    }
  142.  
  143.    protected final void setView(View var1) {
  144.       this.rootView.setView(var1);
  145.       this.painted = false;
  146.       this.editor.invalidate();
  147.    }
  148.  
  149.    protected void paintSafely(Graphics var1) {
  150.       this.painted = true;
  151.       Highlighter var2 = this.editor.getHighlighter();
  152.       Caret var3 = this.editor.getCaret();
  153.       if (this.editor.isOpaque()) {
  154.          this.paintBackground(var1);
  155.       }
  156.  
  157.       if (var2 != null) {
  158.          var2.paint(var1);
  159.       }
  160.  
  161.       Rectangle var4 = this.getVisibleEditorRect();
  162.       this.rootView.paint(var1, var4);
  163.       if (var3 != null) {
  164.          var3.paint(var1);
  165.       }
  166.  
  167.    }
  168.  
  169.    public void installUI(JComponent var1) {
  170.       if (var1 instanceof JTextComponent) {
  171.          this.editor = (JTextComponent)var1;
  172.          this.installDefaults(var1);
  173.          this.editor.setOpaque(true);
  174.          this.editor.setAutoscrolls(true);
  175.          this.editor.setHighlighter(this.createHighlighter());
  176.          this.editor.addPropertyChangeListener(this.updateHandler);
  177.          Document var2 = this.editor.getDocument();
  178.          if (var2 == null) {
  179.             this.editor.setDocument(this.getEditorKit().createDefaultDocument());
  180.          } else {
  181.             var2.addDocumentListener(this.updateHandler);
  182.             this.modelChanged();
  183.          }
  184.  
  185.          this.installListeners(var1);
  186.          this.editor.setKeymap(this.createKeymap());
  187.       } else {
  188.          throw new Error("TextUI needs JTextComponent");
  189.       }
  190.    }
  191.  
  192.    public void uninstallUI(JComponent var1) {
  193.       this.editor.removePropertyChangeListener(this.updateHandler);
  194.       this.editor.getDocument().removeDocumentListener(this.updateHandler);
  195.       this.painted = false;
  196.       this.uninstallDefaults(var1);
  197.       this.editor.setHighlighter((Highlighter)null);
  198.       this.rootView.setView((View)null);
  199.       ((Container)var1).removeAll();
  200.       this.editor.setKeymap((Keymap)null);
  201.       this.uninstallListeners(var1);
  202.    }
  203.  
  204.    public final void paint(Graphics var1, JComponent var2) {
  205.       if (this.rootView.getViewCount() > 0 && this.rootView.getView(0) != null) {
  206.          SafePainter var3 = new SafePainter(this, var1);
  207.          Document var4 = this.editor.getDocument();
  208.          var4.render(var3);
  209.       }
  210.  
  211.    }
  212.  
  213.    public Dimension getPreferredSize(JComponent var1) {
  214.       Insets var2 = var1.getInsets();
  215.       return new Dimension((int)Math.min((long)this.rootView.getPreferredSpan(0) + (long)var2.left + (long)var2.right, 2147483647L), (int)Math.min((long)this.rootView.getPreferredSpan(1) + (long)var2.top + (long)var2.bottom, 2147483647L));
  216.    }
  217.  
  218.    public Dimension getMinimumSize(JComponent var1) {
  219.       Insets var2 = var1.getInsets();
  220.       long var3 = this.rootView.getResizeWeight(0) > 0 ? 1L : Math.min((long)this.rootView.getPreferredSpan(0) + (long)var2.left + (long)var2.right, 2147483647L);
  221.       long var5 = this.rootView.getResizeWeight(1) > 0 ? 1L : Math.min((long)this.rootView.getPreferredSpan(1) + (long)var2.top + (long)var2.bottom, 2147483647L);
  222.       return new Dimension((int)var3, (int)var5);
  223.    }
  224.  
  225.    public Dimension getMaximumSize(JComponent var1) {
  226.       Insets var2 = var1.getInsets();
  227.       long var3 = this.rootView.getResizeWeight(0) > 0 ? 2147483647L : Math.min((long)this.rootView.getPreferredSpan(0) + (long)var2.left + (long)var2.right, 2147483647L);
  228.       long var5 = this.rootView.getResizeWeight(1) > 0 ? 2147483647L : Math.min((long)this.rootView.getPreferredSpan(1) + (long)var2.top + (long)var2.bottom, 2147483647L);
  229.       return new Dimension((int)var3, (int)var5);
  230.    }
  231.  
  232.    protected Rectangle getVisibleEditorRect() {
  233.       Rectangle var1 = new Rectangle(this.editor.getSize());
  234.       Insets var2 = this.editor.getInsets();
  235.       var1.x += var2.left;
  236.       var1.y += var2.top;
  237.       var1.width -= var2.left + var2.right;
  238.       var1.height -= var2.top + var2.bottom;
  239.       return var1;
  240.    }
  241.  
  242.    public Rectangle modelToView(int var1) throws BadLocationException {
  243.       if (this.painted) {
  244.          Rectangle var2 = this.getVisibleEditorRect();
  245.          Shape var3 = this.rootView.modelToView(var1, var2);
  246.          return var3.getBounds();
  247.       } else {
  248.          return null;
  249.       }
  250.    }
  251.  
  252.    public int viewToModel(Point var1) {
  253.       if (this.painted) {
  254.          Rectangle var2 = this.getVisibleEditorRect();
  255.          return this.rootView.viewToModel((float)var1.x, (float)var1.y, var2);
  256.       } else {
  257.          return -1;
  258.       }
  259.    }
  260.  
  261.    public void damageRange(int var1, int var2) {
  262.       if (this.painted) {
  263.          Rectangle var3 = this.getVisibleEditorRect();
  264.  
  265.          try {
  266.             Shape var4 = this.rootView.modelToView(var1, var3);
  267.             Shape var5 = this.rootView.modelToView(var2, var3);
  268.             if (var4 != null && var5 != null) {
  269.                Rectangle var6 = var4.getBounds();
  270.                Rectangle var7 = var5.getBounds();
  271.                if (var6.y == var7.y) {
  272.                   this.editor.repaint(var6.x, var6.y, var7.x - var6.x + 1, var6.height);
  273.                   return;
  274.                }
  275.  
  276.                this.editor.repaint(var3.x, var6.y, var3.width, var7.y - var6.y + var7.height);
  277.                return;
  278.             }
  279.          } catch (BadLocationException var8) {
  280.             return;
  281.          }
  282.       }
  283.  
  284.    }
  285.  
  286.    public EditorKit getEditorKit() {
  287.       return defaultKit;
  288.    }
  289.  
  290.    public View getRootView() {
  291.       return this.rootView;
  292.    }
  293.  
  294.    public Insets getDefaultMargin() {
  295.       return new Insets(0, 0, 0, 0);
  296.    }
  297.  
  298.    public View create(Element var1) {
  299.       return null;
  300.    }
  301.  
  302.    public View create(Element var1, int var2, int var3) {
  303.       return null;
  304.    }
  305.  
  306.    private void readObject(ObjectInputStream var1) throws ClassNotFoundException, IOException {
  307.       var1.defaultReadObject();
  308.       this.rootView = new RootView(this);
  309.       this.updateHandler = new UpdateHandler(this);
  310.    }
  311. }
  312.