home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 144 / DPCS0200.iso / Internet / Supanet / system / swing.jar / javax / swing / plaf / basic / BasicTextUI.class (.txt) < prev    next >
Encoding:
Java Class File  |  1998-11-05  |  12.4 KB  |  479 lines

  1. package javax.swing.plaf.basic;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Component;
  5. import java.awt.Container;
  6. import java.awt.Dimension;
  7. import java.awt.Font;
  8. import java.awt.Graphics;
  9. import java.awt.Insets;
  10. import java.awt.Point;
  11. import java.awt.Rectangle;
  12. import java.awt.Shape;
  13. import java.beans.PropertyChangeEvent;
  14. import javax.swing.JComponent;
  15. import javax.swing.UIManager;
  16. import javax.swing.border.Border;
  17. import javax.swing.plaf.TextUI;
  18. import javax.swing.plaf.UIResource;
  19. import javax.swing.text.AbstractDocument;
  20. import javax.swing.text.BadLocationException;
  21. import javax.swing.text.Caret;
  22. import javax.swing.text.DefaultEditorKit;
  23. import javax.swing.text.Document;
  24. import javax.swing.text.EditorKit;
  25. import javax.swing.text.Element;
  26. import javax.swing.text.Highlighter;
  27. import javax.swing.text.JTextComponent;
  28. import javax.swing.text.Keymap;
  29. import javax.swing.text.Position;
  30. import javax.swing.text.View;
  31. import javax.swing.text.ViewFactory;
  32. import javax.swing.text.Position.Bias;
  33.  
  34. public abstract class BasicTextUI extends TextUI implements ViewFactory {
  35.    private static final EditorKit defaultKit = new DefaultEditorKit();
  36.    transient JTextComponent editor;
  37.    transient boolean painted = false;
  38.    transient RootView rootView = new RootView(this);
  39.    transient UpdateHandler updateHandler = new UpdateHandler(this);
  40.  
  41.    public View create(Element var1) {
  42.       return null;
  43.    }
  44.  
  45.    public View create(Element var1, int var2, int var3) {
  46.       return null;
  47.    }
  48.  
  49.    protected Caret createCaret() {
  50.       return new BasicCaret();
  51.    }
  52.  
  53.    protected Highlighter createHighlighter() {
  54.       return new BasicHighlighter();
  55.    }
  56.  
  57.    protected Keymap createKeymap() {
  58.       String var1 = this.getKeymapName();
  59.       Keymap var2 = JTextComponent.getKeymap(var1);
  60.       if (var2 == null) {
  61.          Keymap var3 = JTextComponent.getKeymap("default");
  62.          var2 = JTextComponent.addKeymap(var1, var3);
  63.          String var4 = this.getPropertyPrefix();
  64.          Object var5 = UIManager.get(var4 + ".keyBindings");
  65.          if (var5 != null && var5 instanceof JTextComponent.KeyBinding[]) {
  66.             JTextComponent.KeyBinding[] var6 = (JTextComponent.KeyBinding[])var5;
  67.             JTextComponent.loadKeymap(var2, var6, this.getComponent().getActions());
  68.          }
  69.       }
  70.  
  71.       return var2;
  72.    }
  73.  
  74.    public void damageRange(JTextComponent var1, int var2, int var3) {
  75.       this.damageRange(var1, var2, var3, Bias.Forward, Bias.Backward);
  76.    }
  77.  
  78.    public void damageRange(JTextComponent var1, int var2, int var3, Position.Bias var4, Position.Bias var5) {
  79.       if (this.painted) {
  80.          Rectangle var6 = this.getVisibleEditorRect();
  81.          Document var7 = var1.getDocument();
  82.  
  83.          try {
  84.             if (var7 instanceof AbstractDocument) {
  85.                ((AbstractDocument)var7).readLock();
  86.             }
  87.  
  88.             Shape var10 = this.rootView.modelToView(var2, var4, var3, var5, var6);
  89.             Rectangle var11 = var10 instanceof Rectangle ? (Rectangle)var10 : var10.getBounds();
  90.             this.editor.repaint(var11.x, var11.y, var11.width, var11.height);
  91.          } catch (BadLocationException var14) {
  92.          } finally {
  93.             if (var7 instanceof AbstractDocument) {
  94.                ((AbstractDocument)var7).readUnlock();
  95.             }
  96.  
  97.          }
  98.       }
  99.  
  100.    }
  101.  
  102.    protected final JTextComponent getComponent() {
  103.       return this.editor;
  104.    }
  105.  
  106.    public EditorKit getEditorKit(JTextComponent var1) {
  107.       return defaultKit;
  108.    }
  109.  
  110.    protected String getKeymapName() {
  111.       String var1 = this.getClass().getName();
  112.       int var2 = var1.lastIndexOf(46);
  113.       if (var2 >= 0) {
  114.          var1 = var1.substring(var2 + 1, var1.length());
  115.       }
  116.  
  117.       return var1;
  118.    }
  119.  
  120.    public Dimension getMaximumSize(JComponent var1) {
  121.       Document var2 = this.editor.getDocument();
  122.       Insets var3 = var1.getInsets();
  123.       Dimension var4 = new Dimension();
  124.  
  125.       try {
  126.          if (var2 instanceof AbstractDocument) {
  127.             ((AbstractDocument)var2).readLock();
  128.          }
  129.  
  130.          var4.width = (int)Math.min((long)this.rootView.getMaximumSpan(0) + (long)var3.left + (long)var3.right, 2147483647L);
  131.          var4.height = (int)Math.min((long)this.rootView.getMaximumSpan(1) + (long)var3.top + (long)var3.bottom, 2147483647L);
  132.       } finally {
  133.          if (var2 instanceof AbstractDocument) {
  134.             ((AbstractDocument)var2).readUnlock();
  135.          }
  136.  
  137.       }
  138.  
  139.       return var4;
  140.    }
  141.  
  142.    public Dimension getMinimumSize(JComponent var1) {
  143.       Document var2 = this.editor.getDocument();
  144.       Insets var3 = var1.getInsets();
  145.       Dimension var4 = new Dimension();
  146.  
  147.       try {
  148.          if (var2 instanceof AbstractDocument) {
  149.             ((AbstractDocument)var2).readLock();
  150.          }
  151.  
  152.          var4.width = (int)this.rootView.getMinimumSpan(0) + var3.left + var3.right;
  153.          var4.height = (int)this.rootView.getMinimumSpan(1) + var3.top + var3.bottom;
  154.       } finally {
  155.          if (var2 instanceof AbstractDocument) {
  156.             ((AbstractDocument)var2).readUnlock();
  157.          }
  158.  
  159.       }
  160.  
  161.       return var4;
  162.    }
  163.  
  164.    public int getNextVisualPositionFrom(JTextComponent var1, int var2, Position.Bias var3, int var4, Position.Bias[] var5) throws BadLocationException {
  165.       if (this.painted) {
  166.          Rectangle var6 = this.getVisibleEditorRect();
  167.          return this.rootView.getNextVisualPositionFrom(var2, var3, var6, var4, var5);
  168.       } else {
  169.          return -1;
  170.       }
  171.    }
  172.  
  173.    public Dimension getPreferredSize(JComponent var1) {
  174.       Document var2 = this.editor.getDocument();
  175.       Insets var3 = var1.getInsets();
  176.       Dimension var4 = ((Component)var1).getSize();
  177.  
  178.       try {
  179.          if (var2 instanceof AbstractDocument) {
  180.             ((AbstractDocument)var2).readLock();
  181.          }
  182.  
  183.          if (var4.width > var3.left + var3.right && var4.height > var3.top + var3.bottom) {
  184.             this.rootView.setSize((float)(var4.width - var3.left - var3.right), (float)(var4.height - var3.top - var3.bottom));
  185.          }
  186.  
  187.          var4.width = (int)Math.min((long)this.rootView.getPreferredSpan(0) + (long)var3.left + (long)var3.right, 2147483647L);
  188.          var4.height = (int)Math.min((long)this.rootView.getPreferredSpan(1) + (long)var3.top + (long)var3.bottom, 2147483647L);
  189.       } finally {
  190.          if (var2 instanceof AbstractDocument) {
  191.             ((AbstractDocument)var2).readUnlock();
  192.          }
  193.  
  194.       }
  195.  
  196.       return var4;
  197.    }
  198.  
  199.    protected abstract String getPropertyPrefix();
  200.  
  201.    public View getRootView(JTextComponent var1) {
  202.       return this.rootView;
  203.    }
  204.  
  205.    protected Rectangle getVisibleEditorRect() {
  206.       Rectangle var1 = new Rectangle(this.editor.getSize());
  207.       Insets var2 = this.editor.getInsets();
  208.       var1.x += var2.left;
  209.       var1.y += var2.top;
  210.       var1.width -= var2.left + var2.right;
  211.       var1.height -= var2.top + var2.bottom;
  212.       return var1;
  213.    }
  214.  
  215.    protected void installDefaults() {
  216.       String var1 = this.getPropertyPrefix();
  217.       Font var2 = this.editor.getFont();
  218.       if (var2 == null || var2 instanceof UIResource) {
  219.          this.editor.setFont(UIManager.getFont(var1 + ".font"));
  220.       }
  221.  
  222.       Color var3 = this.editor.getBackground();
  223.       if (var3 == null || var3 instanceof UIResource) {
  224.          this.editor.setBackground(UIManager.getColor(var1 + ".background"));
  225.       }
  226.  
  227.       Color var4 = this.editor.getForeground();
  228.       if (var4 == null || var4 instanceof UIResource) {
  229.          this.editor.setForeground(UIManager.getColor(var1 + ".foreground"));
  230.       }
  231.  
  232.       Color var5 = this.editor.getCaretColor();
  233.       if (var5 == null || var5 instanceof UIResource) {
  234.          this.editor.setCaretColor(UIManager.getColor(var1 + ".caretForeground"));
  235.       }
  236.  
  237.       Color var6 = this.editor.getSelectionColor();
  238.       if (var6 == null || var6 instanceof UIResource) {
  239.          this.editor.setSelectionColor(UIManager.getColor(var1 + ".selectionBackground"));
  240.       }
  241.  
  242.       Color var7 = this.editor.getSelectedTextColor();
  243.       if (var7 == null || var7 instanceof UIResource) {
  244.          this.editor.setSelectedTextColor(UIManager.getColor(var1 + ".selectionForeground"));
  245.       }
  246.  
  247.       Color var8 = this.editor.getDisabledTextColor();
  248.       if (var8 == null || var8 instanceof UIResource) {
  249.          this.editor.setDisabledTextColor(UIManager.getColor(var1 + ".inactiveForeground"));
  250.       }
  251.  
  252.       Border var9 = this.editor.getBorder();
  253.       if (var9 == null || var9 instanceof UIResource) {
  254.          this.editor.setBorder(UIManager.getBorder(var1 + ".border"));
  255.       }
  256.  
  257.       Insets var10 = this.editor.getMargin();
  258.       if (var10 == null || var10 instanceof UIResource) {
  259.          this.editor.setMargin(UIManager.getInsets(var1 + ".margin"));
  260.       }
  261.  
  262.       Caret var11 = this.editor.getCaret();
  263.       if (var11 == null || var11 instanceof UIResource) {
  264.          var11 = this.createCaret();
  265.          this.editor.setCaret(var11);
  266.          Object var12 = UIManager.get(var1 + ".caretBlinkRate");
  267.          if (var12 != null && var12 instanceof Integer) {
  268.             Integer var13 = (Integer)var12;
  269.             var11.setBlinkRate(var13);
  270.          }
  271.       }
  272.  
  273.       Highlighter var15 = this.editor.getHighlighter();
  274.       if (var15 == null || var15 instanceof UIResource) {
  275.          this.editor.setHighlighter(this.createHighlighter());
  276.       }
  277.  
  278.    }
  279.  
  280.    protected void installKeyboardActions() {
  281.       this.editor.setKeymap(this.createKeymap());
  282.    }
  283.  
  284.    protected void installListeners() {
  285.    }
  286.  
  287.    public void installUI(JComponent var1) {
  288.       if (var1 instanceof JTextComponent) {
  289.          this.editor = (JTextComponent)var1;
  290.          this.installDefaults();
  291.          this.editor.setOpaque(true);
  292.          this.editor.setAutoscrolls(true);
  293.          this.editor.addPropertyChangeListener(this.updateHandler);
  294.          Document var2 = this.editor.getDocument();
  295.          if (var2 == null) {
  296.             this.editor.setDocument(this.getEditorKit(this.editor).createDefaultDocument());
  297.          } else {
  298.             var2.addDocumentListener(this.updateHandler);
  299.             this.modelChanged();
  300.          }
  301.  
  302.          this.installListeners();
  303.          this.installKeyboardActions();
  304.       } else {
  305.          throw new Error("TextUI needs JTextComponent");
  306.       }
  307.    }
  308.  
  309.    protected void modelChanged() {
  310.       ViewFactory var1 = this.rootView.getViewFactory();
  311.       Document var2 = this.editor.getDocument();
  312.       Element var3 = var2.getDefaultRootElement();
  313.       this.setView(var1.create(var3));
  314.    }
  315.  
  316.    public Rectangle modelToView(JTextComponent var1, int var2) throws BadLocationException {
  317.       Document var3 = this.editor.getDocument();
  318.       Shape var4 = null;
  319.  
  320.       try {
  321.          if (var3 instanceof AbstractDocument) {
  322.             ((AbstractDocument)var3).readLock();
  323.          }
  324.  
  325.          if (this.painted) {
  326.             Rectangle var7 = this.getVisibleEditorRect();
  327.             var4 = this.rootView.modelToView(var2, var7);
  328.          }
  329.       } finally {
  330.          if (var3 instanceof AbstractDocument) {
  331.             ((AbstractDocument)var3).readUnlock();
  332.          }
  333.  
  334.       }
  335.  
  336.       return (Rectangle)var4;
  337.    }
  338.  
  339.    public Rectangle modelToView(JTextComponent var1, int var2, Position.Bias var3) throws BadLocationException {
  340.       if (this.painted) {
  341.          Rectangle var4 = this.getVisibleEditorRect();
  342.          Shape var5 = this.rootView.modelToView(var2, var4, var3);
  343.          return var5.getBounds();
  344.       } else {
  345.          return null;
  346.       }
  347.    }
  348.  
  349.    public final void paint(Graphics var1, JComponent var2) {
  350.       if (this.rootView.getViewCount() > 0 && this.rootView.getView(0) != null) {
  351.          SafePainter var3 = new SafePainter(this, var1);
  352.          Document var4 = this.editor.getDocument();
  353.          var4.render(var3);
  354.       }
  355.  
  356.    }
  357.  
  358.    protected void paintBackground(Graphics var1) {
  359.       var1.setColor(this.editor.getBackground());
  360.       Dimension var2 = this.editor.getSize();
  361.       var1.fillRect(0, 0, var2.width, var2.height);
  362.    }
  363.  
  364.    protected void paintSafely(Graphics var1) {
  365.       this.painted = true;
  366.       Highlighter var2 = this.editor.getHighlighter();
  367.       Caret var3 = this.editor.getCaret();
  368.       if (this.editor.isOpaque()) {
  369.          this.paintBackground(var1);
  370.       }
  371.  
  372.       if (var2 != null) {
  373.          var2.paint(var1);
  374.       }
  375.  
  376.       Rectangle var4 = this.getVisibleEditorRect();
  377.       this.rootView.paint(var1, var4);
  378.       if (var3 != null) {
  379.          var3.paint(var1);
  380.       }
  381.  
  382.    }
  383.  
  384.    protected void propertyChange(PropertyChangeEvent var1) {
  385.    }
  386.  
  387.    protected final void setView(View var1) {
  388.       this.editor.removeAll();
  389.       this.rootView.setView(var1);
  390.       this.painted = false;
  391.       this.editor.revalidate();
  392.    }
  393.  
  394.    protected void uninstallDefaults() {
  395.       if (this.editor.getCaretColor() instanceof UIResource) {
  396.          this.editor.setCaretColor((Color)null);
  397.       }
  398.  
  399.       if (this.editor.getSelectionColor() instanceof UIResource) {
  400.          this.editor.setSelectionColor((Color)null);
  401.       }
  402.  
  403.       if (this.editor.getDisabledTextColor() instanceof UIResource) {
  404.          this.editor.setDisabledTextColor((Color)null);
  405.       }
  406.  
  407.       if (this.editor.getSelectedTextColor() instanceof UIResource) {
  408.          this.editor.setSelectedTextColor((Color)null);
  409.       }
  410.  
  411.       if (this.editor.getBorder() instanceof UIResource) {
  412.          this.editor.setBorder((Border)null);
  413.       }
  414.  
  415.       if (this.editor.getMargin() instanceof UIResource) {
  416.          this.editor.setMargin((Insets)null);
  417.       }
  418.  
  419.       if (this.editor.getCaret() instanceof UIResource) {
  420.          this.editor.setCaret((Caret)null);
  421.       }
  422.  
  423.       if (this.editor.getHighlighter() instanceof UIResource) {
  424.          this.editor.setHighlighter((Highlighter)null);
  425.       }
  426.  
  427.    }
  428.  
  429.    protected void uninstallKeyboardActions() {
  430.       this.editor.setKeymap((Keymap)null);
  431.    }
  432.  
  433.    protected void uninstallListeners() {
  434.    }
  435.  
  436.    public void uninstallUI(JComponent var1) {
  437.       this.editor.removePropertyChangeListener(this.updateHandler);
  438.       this.editor.getDocument().removeDocumentListener(this.updateHandler);
  439.       this.painted = false;
  440.       this.uninstallDefaults();
  441.       this.rootView.setView((View)null);
  442.       ((Container)var1).removeAll();
  443.       this.uninstallKeyboardActions();
  444.       this.uninstallListeners();
  445.    }
  446.  
  447.    public int viewToModel(JTextComponent var1, Point var2) {
  448.       int var3 = -1;
  449.       Document var4 = this.editor.getDocument();
  450.  
  451.       try {
  452.          if (var4 instanceof AbstractDocument) {
  453.             ((AbstractDocument)var4).readLock();
  454.          }
  455.  
  456.          if (this.painted) {
  457.             Rectangle var7 = this.getVisibleEditorRect();
  458.             var3 = this.rootView.viewToModel((float)var2.x, (float)var2.y, var7);
  459.          }
  460.       } finally {
  461.          if (var4 instanceof AbstractDocument) {
  462.             ((AbstractDocument)var4).readUnlock();
  463.          }
  464.  
  465.       }
  466.  
  467.       return var3;
  468.    }
  469.  
  470.    public int viewToModel(JTextComponent var1, Point var2, Position.Bias[] var3) {
  471.       if (this.painted) {
  472.          Rectangle var4 = this.getVisibleEditorRect();
  473.          return this.rootView.viewToModel((float)var2.x, (float)var2.y, var4, var3);
  474.       } else {
  475.          return -1;
  476.       }
  477.    }
  478. }
  479.