home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / demos / VisualAge for Java 2.0 Entry / setup / data1.cab / ide-e / IDE / cache / 1UFSLCP (.txt) < prev    next >
Encoding:
Java Class File  |  1998-09-16  |  11.2 KB  |  426 lines

  1. package com.sun.java.swing.plaf.metal;
  2.  
  3. import com.sun.java.swing.Icon;
  4. import com.sun.java.swing.JComponent;
  5. import com.sun.java.swing.JTabbedPane;
  6. import com.sun.java.swing.UIManager;
  7. import com.sun.java.swing.plaf.ComponentUI;
  8. import com.sun.java.swing.plaf.basic.BasicTabbedPaneUI;
  9. import java.awt.Color;
  10. import java.awt.Component;
  11. import java.awt.Dimension;
  12. import java.awt.FontMetrics;
  13. import java.awt.Graphics;
  14. import java.awt.Insets;
  15. import java.awt.Rectangle;
  16.  
  17. public class MetalTabbedPaneUI extends BasicTabbedPaneUI {
  18.    protected static Insets tabsOnTopTabAreaInsets = new Insets(4, 2, 0, 6);
  19.    protected static Insets tabsOnLeftTabAreaInsets = new Insets(6, 4, 0, 0);
  20.    protected static Insets tabsOnBottomTabAreaInsets = new Insets(0, 2, 4, 6);
  21.    protected static Insets tabsOnRightTabAreaInsets = new Insets(0, 0, 6, 4);
  22.    protected int minTabWidth = 40;
  23.    protected Color selectColor;
  24.    protected Color selectHighlight;
  25.  
  26.    protected int calculateXNudge(JTabbedPane pane, int tabPlacement, int tabIndex, boolean isSelected) {
  27.       return 0;
  28.    }
  29.  
  30.    protected int calculateYNudge(JTabbedPane pane, int tabPlacement, int tabIndex, boolean isSelected) {
  31.       return 0;
  32.    }
  33.  
  34.    public static ComponentUI createUI(JComponent x) {
  35.       return new MetalTabbedPaneUI();
  36.    }
  37.  
  38.    protected Color getColorForGap(JTabbedPane pane, int currentRun, int x, int y) {
  39.       int selectedIndex = pane.getSelectedIndex();
  40.       int startIndex = super.tabRuns[currentRun + 1];
  41.       int endIndex = ((BasicTabbedPaneUI)this).lastIndexInRun(currentRun + 1, pane.getTabCount());
  42.  
  43.       for(int i = startIndex; i <= endIndex; ++i) {
  44.          Rectangle tabBounds = ((BasicTabbedPaneUI)this).getTabBounds(pane, i);
  45.          int tabLeft = tabBounds.x;
  46.          int tabRight = tabBounds.x + tabBounds.width - 1;
  47.          if (tabLeft + 1 < x && tabRight - 4 > x) {
  48.             return selectedIndex == i ? this.selectColor : pane.getBackgroundAt(i);
  49.          }
  50.       }
  51.  
  52.       return ((Component)pane).getBackground();
  53.    }
  54.  
  55.    protected Insets getTabAreaInsets(JTabbedPane pane, int tabPlacement) {
  56.       Insets insets;
  57.       switch (tabPlacement) {
  58.          case 1:
  59.          default:
  60.             insets = tabsOnTopTabAreaInsets;
  61.             break;
  62.          case 2:
  63.             insets = tabsOnLeftTabAreaInsets;
  64.             break;
  65.          case 3:
  66.             insets = tabsOnBottomTabAreaInsets;
  67.             break;
  68.          case 4:
  69.             insets = tabsOnRightTabAreaInsets;
  70.       }
  71.  
  72.       return insets;
  73.    }
  74.  
  75.    protected int getTabOverlay(JTabbedPane pane, int tabPlacement) {
  76.       if (tabPlacement != 2 && tabPlacement != 4) {
  77.          return 0;
  78.       } else {
  79.          int maxTabHeight = ((BasicTabbedPaneUI)this).maxTabHeight(pane);
  80.          return maxTabHeight / 2;
  81.       }
  82.    }
  83.  
  84.    protected void installDefaults(JComponent c) {
  85.       JTabbedPane var10000 = (JTabbedPane)c;
  86.       super.installDefaults(c);
  87.       this.selectColor = UIManager.getColor("TabbedPane.selected");
  88.       this.selectHighlight = UIManager.getColor("TabbedPane.selectHighlight");
  89.    }
  90.  
  91.    protected int maxTabHeight(JTabbedPane pane, FontMetrics metrics) {
  92.       int height = metrics.getHeight();
  93.       boolean tallerIcons = false;
  94.  
  95.       for(int i = 0; i < pane.getTabCount(); ++i) {
  96.          Icon icon = pane.getIconAt(i);
  97.          if (icon != null && icon.getIconHeight() > height) {
  98.             tallerIcons = true;
  99.             break;
  100.          }
  101.       }
  102.  
  103.       return super.maxTabHeight(pane, metrics) - (tallerIcons ? BasicTabbedPaneUI.spacingHeight * 2 : 0);
  104.    }
  105.  
  106.    protected void normalizeTabRuns(JTabbedPane pane, int tabPlacement, int tabCount, int start, int max) {
  107.       if (tabPlacement == 1 || tabPlacement == 3) {
  108.          super.normalizeTabRuns(pane, tabPlacement, tabCount, start, max);
  109.       }
  110.  
  111.    }
  112.  
  113.    protected void padSelectedTab(JTabbedPane pane, int tabPlacement, int selectedIndex) {
  114.    }
  115.  
  116.    public void paint(Graphics g, JComponent c) {
  117.       JTabbedPane pane = (JTabbedPane)c;
  118.       int tabPlacement = pane.getTabPlacement();
  119.       Insets insets = c.getInsets();
  120.       Dimension size = ((Component)c).getSize();
  121.       if (((JComponent)pane).isOpaque()) {
  122.          g.setColor(((Component)c).getBackground());
  123.          switch (tabPlacement) {
  124.             case 1:
  125.             default:
  126.                g.fillRect(insets.left, insets.top, size.width - insets.right - insets.left, ((BasicTabbedPaneUI)this).totalTabHeight(pane, tabPlacement, super.runCount));
  127.                this.paintHighlightBelowTab(pane);
  128.                break;
  129.             case 2:
  130.                g.fillRect(insets.left, insets.top, ((BasicTabbedPaneUI)this).totalTabWidth(pane, tabPlacement, super.runCount), size.height - insets.bottom - insets.top);
  131.                break;
  132.             case 3:
  133.                int totalTabHeight = ((BasicTabbedPaneUI)this).totalTabHeight(pane, tabPlacement, super.runCount);
  134.                g.fillRect(insets.left, size.height - insets.bottom - totalTabHeight, size.width - insets.left - insets.right, totalTabHeight);
  135.                break;
  136.             case 4:
  137.                int totalTabWidth = ((BasicTabbedPaneUI)this).totalTabWidth(pane, tabPlacement, super.runCount);
  138.                g.fillRect(size.width - insets.right - totalTabWidth, insets.top, totalTabWidth, size.height - insets.top - insets.bottom);
  139.          }
  140.       }
  141.  
  142.       super.paint(g, c);
  143.    }
  144.  
  145.    protected void paintBottomTabBorder(JTabbedPane pane, int tabIndex, Graphics g, int x, int y, int w, int h, int btm, int rght, boolean isSelected) {
  146.       int currentRun = ((BasicTabbedPaneUI)this).getRunForTab(pane, pane.getTabCount(), tabIndex);
  147.       int lastIndex = ((BasicTabbedPaneUI)this).lastIndexInRun(currentRun, pane.getTabCount());
  148.       int firstIndex = super.tabRuns[currentRun];
  149.       int bottom = h - 1;
  150.       int right = w - 1;
  151.       if (this.shouldFillGap(pane, currentRun, tabIndex, x, y)) {
  152.          g.translate(x, y);
  153.          g.setColor(this.getColorForGap(pane, currentRun, x, y));
  154.          g.fillRect(1, bottom - 4, 3, 5);
  155.          g.fillRect(4, bottom - 1, 2, 2);
  156.          g.translate(-x, -y);
  157.       }
  158.  
  159.       g.translate(x, y);
  160.       g.setColor(super.tabDarkShadow);
  161.       g.drawLine(1, bottom - 5, 6, bottom);
  162.       g.drawLine(6, bottom, right, bottom);
  163.       if (tabIndex == lastIndex) {
  164.          g.drawLine(right, 0, right, bottom);
  165.       }
  166.  
  167.       if (tabIndex != super.tabRuns[super.runCount - 1]) {
  168.          g.drawLine(0, 0, 0, bottom);
  169.       } else {
  170.          g.drawLine(0, 0, 0, bottom - 6);
  171.       }
  172.  
  173.       g.setColor(isSelected ? this.selectHighlight : super.tabHighlight);
  174.       g.drawLine(1, bottom - 6, 6, bottom - 1);
  175.       g.drawLine(1, 0, 1, bottom - 6);
  176.       if (tabIndex == firstIndex && tabIndex != super.tabRuns[super.runCount - 1]) {
  177.          g.setColor(pane.getSelectedIndex() == super.tabRuns[currentRun + 1] ? this.selectHighlight : super.tabHighlight);
  178.          g.drawLine(1, bottom - 4, 1, bottom);
  179.       }
  180.  
  181.       g.translate(-x, -y);
  182.    }
  183.  
  184.    protected void paintContentBorderBottomEdge(Graphics g, int tabPlacement, int selectedIndex, int x, int y, int w, int h) {
  185.       g.setColor(super.tabDarkShadow);
  186.       g.drawLine(x, y + (h - 1), x + (w - 1), y + (h - 1));
  187.    }
  188.  
  189.    protected void paintContentBorderLeftEdge(Graphics g, int tabPlacement, int selectedIndex, int x, int y, int w, int h) {
  190.       g.setColor(super.tabDarkShadow);
  191.       g.drawLine(x, y, x, y + (h - 1));
  192.    }
  193.  
  194.    protected void paintContentBorderRightEdge(Graphics g, int tabPlacement, int selectedIndex, int x, int y, int w, int h) {
  195.       g.setColor(super.tabDarkShadow);
  196.       g.drawLine(x + (w - 1), y, x + (w - 1), y + (h - 1));
  197.    }
  198.  
  199.    protected void paintContentBorderTopEdge(Graphics g, int tabPlacement, int selectedIndex, int x, int y, int width, int height) {
  200.       g.setColor(super.tabDarkShadow);
  201.       g.drawLine(x - 1, y, width - 2, y);
  202.    }
  203.  
  204.    protected void paintFocusIndicator(Graphics g, JTabbedPane pane, int tabPlacement, Rectangle[] rects, int tabIndex, Rectangle iconRect, Rectangle textRect, boolean isSelected) {
  205.       if (((JComponent)pane).hasFocus() && isSelected) {
  206.          Rectangle tabRect = rects[tabIndex];
  207.          g.setColor(super.focus);
  208.          g.translate(tabRect.x, tabRect.y);
  209.          int right = tabRect.width - 1;
  210.          int bottom = tabRect.height - 1;
  211.          switch (tabPlacement) {
  212.             case 1:
  213.             case 2:
  214.             default:
  215.                g.drawLine(2, 6, 6, 2);
  216.                g.drawLine(2, 6, 2, bottom - 1);
  217.                g.drawLine(6, 2, right, 2);
  218.                g.drawLine(right, 2, right, bottom - 1);
  219.                g.drawLine(2, bottom - 1, right, bottom - 1);
  220.                break;
  221.             case 3:
  222.                g.drawLine(2, bottom - 6, 6, bottom - 2);
  223.                g.drawLine(6, bottom - 2, right, bottom - 2);
  224.                g.drawLine(2, 0, 2, bottom - 6);
  225.                g.drawLine(2, 0, right, 0);
  226.                g.drawLine(right, 0, right, bottom - 2);
  227.                break;
  228.             case 4:
  229.                g.drawLine(right - 6, 2, right - 2, 6);
  230.                g.drawLine(1, 2, right - 6, 2);
  231.                g.drawLine(right - 2, 6, right - 2, bottom);
  232.                g.drawLine(1, 2, 1, bottom);
  233.                g.drawLine(1, bottom, right - 2, bottom);
  234.          }
  235.  
  236.          g.translate(-tabRect.x, -tabRect.y);
  237.       }
  238.  
  239.    }
  240.  
  241.    protected void paintHighlightBelowTab(JTabbedPane pane) {
  242.    }
  243.  
  244.    protected void paintLeftTabBorder(JTabbedPane pane, int tabIndex, Graphics g, int x, int y, int w, int h, int btm, int rght, boolean isSelected) {
  245.       int currentRun = ((BasicTabbedPaneUI)this).getRunForTab(pane, pane.getTabCount(), tabIndex);
  246.       int lastIndex = ((BasicTabbedPaneUI)this).lastIndexInRun(currentRun, pane.getTabCount());
  247.       int firstIndex = super.tabRuns[currentRun];
  248.       g.translate(x, y);
  249.       int bottom = h - 1;
  250.       int right = w - 1;
  251.       if (tabIndex != firstIndex) {
  252.          g.setColor(pane.getSelectedIndex() == tabIndex - 1 ? this.selectColor : pane.getBackgroundAt(tabIndex - 1));
  253.          g.fillRect(2, 0, 4, 3);
  254.          g.drawLine(2, 3, 2, 3);
  255.       }
  256.  
  257.       g.setColor(isSelected ? this.selectHighlight : super.tabHighlight);
  258.       g.drawLine(1, 6, 6, 1);
  259.       g.drawLine(6, 1, right, 1);
  260.       g.drawLine(1, 6, 1, bottom);
  261.       if (tabIndex != firstIndex) {
  262.          g.setColor(pane.getSelectedIndex() == tabIndex - 1 ? this.selectHighlight : super.tabHighlight);
  263.          g.drawLine(1, 0, 1, 4);
  264.       }
  265.  
  266.       g.setColor(super.tabDarkShadow);
  267.       g.drawLine(1, 5, 6, 0);
  268.       g.drawLine(6, 0, right, 0);
  269.       if (tabIndex != firstIndex) {
  270.          g.drawLine(0, 0, 0, bottom);
  271.       } else {
  272.          g.drawLine(0, 6, 0, bottom);
  273.       }
  274.  
  275.       if (tabIndex == lastIndex) {
  276.          g.drawLine(0, bottom, right, bottom);
  277.       }
  278.  
  279.       g.translate(-x, -y);
  280.    }
  281.  
  282.    protected void paintRightTabBorder(JTabbedPane pane, int tabIndex, Graphics g, int x, int y, int w, int h, int btm, int rght, boolean isSelected) {
  283.       int currentRun = ((BasicTabbedPaneUI)this).getRunForTab(pane, pane.getTabCount(), tabIndex);
  284.       int lastIndex = ((BasicTabbedPaneUI)this).lastIndexInRun(currentRun, pane.getTabCount());
  285.       int firstIndex = super.tabRuns[currentRun];
  286.       g.translate(x, y);
  287.       int bottom = h - 1;
  288.       int right = w - 1;
  289.       if (tabIndex != firstIndex) {
  290.          g.setColor(pane.getSelectedIndex() == tabIndex - 1 ? this.selectColor : pane.getBackgroundAt(tabIndex - 1));
  291.          g.fillRect(right - 5, 0, 5, 3);
  292.          g.fillRect(right - 2, 3, 2, 2);
  293.       }
  294.  
  295.       g.setColor(isSelected ? this.selectHighlight : super.tabHighlight);
  296.       g.drawLine(right - 6, 1, right - 1, 6);
  297.       g.drawLine(0, 1, right - 6, 1);
  298.       g.drawLine(0, 1, 0, bottom);
  299.       g.setColor(super.tabDarkShadow);
  300.       g.drawLine(right - 6, 0, right, 6);
  301.       g.drawLine(0, 0, right - 6, 0);
  302.       if (tabIndex != firstIndex) {
  303.          g.drawLine(right, 0, right, bottom);
  304.       } else {
  305.          g.drawLine(right, 6, right, bottom);
  306.       }
  307.  
  308.       if (tabIndex == lastIndex) {
  309.          g.drawLine(0, bottom, right, bottom);
  310.       }
  311.  
  312.       g.translate(-x, -y);
  313.    }
  314.  
  315.    protected void paintTabBackground(Graphics g, JTabbedPane pane, int tabPlacement, int tabIndex, int x, int y, int w, int h, boolean isSelected) {
  316.       int var10000 = h / 2;
  317.       if (isSelected) {
  318.          g.setColor(this.selectColor);
  319.       } else {
  320.          g.setColor(pane.getBackgroundAt(tabIndex));
  321.       }
  322.  
  323.       switch (tabPlacement) {
  324.          case 1:
  325.          default:
  326.             g.fillRect(x + 4, y + 2, w - 1 - 3, h - 1 - 1);
  327.             g.fillRect(x + 2, y + 5, 2, h - 5);
  328.             break;
  329.          case 2:
  330.             g.fillRect(x + 5, y + 1, w - 5, h - 1);
  331.             g.fillRect(x + 2, y + 4, 3, h - 4);
  332.             break;
  333.          case 3:
  334.             g.fillRect(x + 2, y, w - 2, h - 3);
  335.             g.fillRect(x + 5, y + (h - 1) - 3, w - 5, 3);
  336.             break;
  337.          case 4:
  338.             g.fillRect(x + 1, y + 1, w - 5, h - 1);
  339.             g.fillRect(x + (w - 1) - 3, y + 5, 3, h - 1);
  340.       }
  341.  
  342.    }
  343.  
  344.    protected void paintTabBorder(Graphics g, JTabbedPane pane, int tabPlacement, int tabIndex, int x, int y, int w, int h, boolean isSelected) {
  345.       int bottom = y + (h - 1);
  346.       int right = x + (w - 1);
  347.       switch (tabPlacement) {
  348.          case 1:
  349.          default:
  350.             this.paintTopTabBorder(pane, tabIndex, g, x, y, w, h, bottom, right, isSelected);
  351.             break;
  352.          case 2:
  353.             this.paintLeftTabBorder(pane, tabIndex, g, x, y, w, h, bottom, right, isSelected);
  354.             break;
  355.          case 3:
  356.             this.paintBottomTabBorder(pane, tabIndex, g, x, y, w, h, bottom, right, isSelected);
  357.             break;
  358.          case 4:
  359.             this.paintRightTabBorder(pane, tabIndex, g, x, y, w, h, bottom, right, isSelected);
  360.       }
  361.  
  362.    }
  363.  
  364.    protected void paintTopTabBorder(JTabbedPane pane, int tabIndex, Graphics g, int x, int y, int w, int h, int btm, int rght, boolean isSelected) {
  365.       int currentRun = ((BasicTabbedPaneUI)this).getRunForTab(pane, pane.getTabCount(), tabIndex);
  366.       int lastIndex = ((BasicTabbedPaneUI)this).lastIndexInRun(currentRun, pane.getTabCount());
  367.       int firstIndex = super.tabRuns[currentRun];
  368.       if (this.shouldFillGap(pane, currentRun, tabIndex, x, y)) {
  369.          g.translate(x, y);
  370.          g.setColor(this.getColorForGap(pane, currentRun, x, y + 1));
  371.          g.fillRect(1, 0, 5, 3);
  372.          g.fillRect(1, 3, 2, 2);
  373.          g.translate(-x, -y);
  374.       }
  375.  
  376.       g.translate(x, y);
  377.       int bottom = h - 1;
  378.       int right = w - 1;
  379.       g.setColor(super.tabDarkShadow);
  380.       g.drawLine(1, 5, 6, 0);
  381.       g.drawLine(6, 0, right, 0);
  382.       if (tabIndex == lastIndex) {
  383.          g.drawLine(right, 1, right, bottom);
  384.       }
  385.  
  386.       if (tabIndex != super.tabRuns[super.runCount - 1]) {
  387.          g.drawLine(0, 0, 0, bottom);
  388.       } else {
  389.          g.drawLine(0, 6, 0, bottom);
  390.       }
  391.  
  392.       g.setColor(isSelected ? this.selectHighlight : super.tabHighlight);
  393.       g.drawLine(1, 6, 6, 1);
  394.       g.drawLine(6, 1, right, 1);
  395.       g.drawLine(1, 6, 1, bottom);
  396.       if (tabIndex == firstIndex && tabIndex != super.tabRuns[super.runCount - 1]) {
  397.          g.setColor(pane.getSelectedIndex() == super.tabRuns[currentRun + 1] ? this.selectHighlight : super.tabHighlight);
  398.          g.drawLine(1, 0, 1, 4);
  399.       }
  400.  
  401.       g.translate(-x, -y);
  402.    }
  403.  
  404.    protected void rotateTabRuns(JTabbedPane pane, int tabPlacement, int selectedRun) {
  405.    }
  406.  
  407.    protected boolean shouldFillGap(JTabbedPane pane, int currentRun, int tabIndex, int x, int y) {
  408.       boolean result = false;
  409.       if (currentRun == super.runCount - 2) {
  410.          Rectangle lastTabBounds = ((BasicTabbedPaneUI)this).getTabBounds(pane, pane.getTabCount() - 1);
  411.          int lastTabRight = lastTabBounds.x + lastTabBounds.width - 1;
  412.          if (lastTabRight > ((BasicTabbedPaneUI)this).getTabBounds(pane, tabIndex).x + 2) {
  413.             return true;
  414.          }
  415.       } else {
  416.          result = currentRun != super.runCount - 1;
  417.       }
  418.  
  419.       return result;
  420.    }
  421.  
  422.    protected boolean shouldPadRun(JTabbedPane pane, int tabPlacement, int run) {
  423.       return super.runCount > 1 && run < super.runCount - 1;
  424.    }
  425. }
  426.