ButtonTabComponent.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. public ButtonTabComponent(final JTabbedPane pane) {
  44. //unset default FlowLayout' gaps
  45. super(new FlowLayout(FlowLayout.LEFT, 0, 0));
  46. if (pane == null) {
  47. throw new NullPointerException("TabbedPane is null");
  48. }
  49. this.pane = pane;
  50. setOpaque(false);
  51. //make JLabel read titles from JTabbedPane
  52. JLabel label = new JLabel() {
  53. public String getText() {
  54. int i = pane.indexOfTabComponent(ButtonTabComponent.this);
  55. if (i != -1) {
  56. return pane.getTitleAt(i);
  57. }
  58. return null;
  59. }
  60. };
  61. add(label);
  62. //add more space between the label and the button
  63. label.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 5));
  64. //tab button
  65. JButton button = new TabButton();
  66. add(button);
  67. //add more space to the top of the component
  68. setBorder(BorderFactory.createEmptyBorder(2, 0, 0, 0));
  69. }
  70. private class TabButton extends JButton implements ActionListener {
  71. public TabButton() {
  72. int size = 17;
  73. setPreferredSize(new Dimension(size, size));
  74. setToolTipText("close this tab");
  75. //Make the button looks the same for all Laf's
  76. setUI(new BasicButtonUI());
  77. //Make it transparent
  78. setContentAreaFilled(false);
  79. //No need to be focusable
  80. setFocusable(false);
  81. setBorder(BorderFactory.createEtchedBorder());
  82. setBorderPainted(false);
  83. //Making nice rollover effect
  84. //we use the same listener for all buttons
  85. addMouseListener(buttonMouseListener);
  86. setRolloverEnabled(true);
  87. //Close the proper tab by clicking the button
  88. addActionListener(this);
  89. }
  90. public void actionPerformed(ActionEvent e) {
  91. int i = pane.indexOfTabComponent(ButtonTabComponent.this);
  92. if (i != -1) {
  93. pane.remove(i);
  94. }
  95. }
  96. //we don't want to update UI for this button
  97. public void updateUI() {
  98. }
  99. //paint the cross
  100. protected void paintComponent(Graphics g) {
  101. super.paintComponent(g);
  102. Graphics2D g2 = (Graphics2D) g.create();
  103. //shift the image for pressed buttons
  104. if (getModel().isPressed()) {
  105. g2.translate(1, 1);
  106. }
  107. g2.setStroke(new BasicStroke(2));
  108. g2.setColor(Color.BLACK);
  109. if (getModel().isRollover()) {
  110. g2.setColor(Color.MAGENTA);
  111. }
  112. int delta = 6;
  113. g2.drawLine(delta, delta, getWidth() - delta - 1, getHeight() - delta - 1);
  114. g2.drawLine(getWidth() - delta - 1, delta, delta, getHeight() - delta - 1);
  115. g2.dispose();
  116. }
  117. }
  118. private final static MouseListener buttonMouseListener = new MouseAdapter() {
  119. public void mouseEntered(MouseEvent e) {
  120. Component component = e.getComponent();
  121. if (component instanceof AbstractButton) {
  122. AbstractButton button = (AbstractButton) component;
  123. button.setBorderPainted(true);
  124. }
  125. }
  126. public void mouseExited(MouseEvent e) {
  127. Component component = e.getComponent();
  128. if (component instanceof AbstractButton) {
  129. AbstractButton button = (AbstractButton) component;
  130. button.setBorderPainted(false);
  131. }
  132. }
  133. };
  134. }