ButtonTabComponent.java 5.1 KB

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