ButtonTabComponent.java 4.9 KB

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