ButtonTabComponent.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package ui.view;
  2. /*
  3. * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * - Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * - Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * - Neither the name of Oracle or the names of its
  17. * contributors may be used to endorse or promote products derived
  18. * from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  21. * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  22. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  24. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  28. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  30. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. import javax.swing.*;
  33. import javax.swing.plaf.basic.BasicButtonUI;
  34. import java.awt.*;
  35. import java.awt.event.*;
  36. /**
  37. * Component to be used as tabComponent;
  38. * Contains a JLabel to show the text and
  39. * a JButton to close the tab it belongs to
  40. */
  41. public class ButtonTabComponent extends JPanel {
  42. private final JTabbedPane pane;
  43. private final JTabbedPane pane2;
  44. public ButtonTabComponent(final JTabbedPane pane, final JTabbedPane pane2) {
  45. //unset default FlowLayout' gaps
  46. super(new FlowLayout(FlowLayout.LEFT, 0, 0));
  47. if (pane == null) {
  48. throw new NullPointerException("TabbedPane is null");
  49. }
  50. this.pane = pane;
  51. this.pane2 = pane2;
  52. setOpaque(false);
  53. //make JLabel read titles from JTabbedPane
  54. JLabel label = new JLabel() {
  55. public String getText() {
  56. int i = pane.indexOfTabComponent(ButtonTabComponent.this);
  57. if (i != -1) {
  58. return pane.getTitleAt(i);
  59. }
  60. return null;
  61. }
  62. };
  63. add(label);
  64. //add more space between the label and the button
  65. label.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 5));
  66. //tab button
  67. JButton button = new TabButton();
  68. add(button);
  69. //add more space to the top of the component
  70. setBorder(BorderFactory.createEmptyBorder(2, 0, 0, 0));
  71. }
  72. private class TabButton extends JButton implements ActionListener {
  73. public TabButton() {
  74. int size = 17;
  75. setPreferredSize(new Dimension(size, size));
  76. setToolTipText("close this tab");
  77. //Make the button looks the same for all Laf's
  78. setUI(new BasicButtonUI());
  79. //Make it transparent
  80. setContentAreaFilled(false);
  81. //No need to be focusable
  82. setFocusable(false);
  83. setBorder(BorderFactory.createEtchedBorder());
  84. setBorderPainted(false);
  85. //Making nice rollover effect
  86. //we use the same listener for all buttons
  87. addMouseListener(buttonMouseListener);
  88. setRolloverEnabled(true);
  89. //Close the proper tab by clicking the button
  90. addActionListener(this);
  91. }
  92. public void actionPerformed(ActionEvent e) {
  93. int i = pane.indexOfTabComponent(ButtonTabComponent.this);
  94. if (i != -1) {
  95. pane.remove(i);
  96. pane2.remove(i);
  97. }
  98. }
  99. //we don't want to update UI for this button
  100. public void updateUI() {
  101. }
  102. //paint the cross
  103. protected void paintComponent(Graphics g) {
  104. super.paintComponent(g);
  105. Graphics2D g2 = (Graphics2D) g.create();
  106. //shift the image for pressed buttons
  107. if (getModel().isPressed()) {
  108. g2.translate(1, 1);
  109. }
  110. g2.setStroke(new BasicStroke(2));
  111. g2.setColor(Color.BLACK);
  112. if (getModel().isRollover()) {
  113. g2.setColor(Color.MAGENTA);
  114. }
  115. int delta = 6;
  116. g2.drawLine(delta, delta, getWidth() - delta - 1, getHeight() - delta - 1);
  117. g2.drawLine(getWidth() - delta - 1, delta, delta, getHeight() - delta - 1);
  118. g2.dispose();
  119. }
  120. }
  121. private final static MouseListener buttonMouseListener = new MouseAdapter() {
  122. public void mouseEntered(MouseEvent e) {
  123. Component component = e.getComponent();
  124. if (component instanceof AbstractButton) {
  125. AbstractButton button = (AbstractButton) component;
  126. button.setBorderPainted(true);
  127. }
  128. }
  129. public void mouseExited(MouseEvent e) {
  130. Component component = e.getComponent();
  131. if (component instanceof AbstractButton) {
  132. AbstractButton button = (AbstractButton) component;
  133. button.setBorderPainted(false);
  134. }
  135. }
  136. };
  137. }