PageRenderTime 37ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/bundles/plugins-trunk/BufferTabs/buffertabs/BufferTabComponent.java

#
Java | 131 lines | 95 code | 17 blank | 19 comment | 3 complexity | 3d3b1157569048be2c7c12d48e617aa0 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. /*
  2. * :tabSize=8:indentSize=8:noTabs=false:
  3. * :folding=explicit:collapseFolds=1:
  4. *
  5. * Copyright (C) 2009 Shlomy Reinstein
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or any later version.
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. */
  20. package buffertabs;
  21. import java.awt.Color;
  22. import java.awt.Component;
  23. import java.awt.Dimension;
  24. import java.awt.FlowLayout;
  25. import java.awt.Graphics;
  26. import java.awt.event.MouseAdapter;
  27. import java.awt.event.MouseEvent;
  28. import javax.swing.Icon;
  29. import javax.swing.JLabel;
  30. import javax.swing.JPanel;
  31. import org.gjt.sp.jedit.EditPane;
  32. import org.gjt.sp.jedit.jEdit;
  33. @SuppressWarnings("serial")
  34. class BufferTabComponent extends JPanel
  35. {
  36. private static CloseIcon icon = new CloseIcon();
  37. private static Dimension iconDimension =
  38. new Dimension(icon.getIconWidth(), icon.getIconHeight());
  39. private BufferTabs pane;
  40. BufferTabComponent(BufferTabs bufferTabs) {
  41. super(new FlowLayout(FlowLayout.CENTER, 5, 0));
  42. pane = bufferTabs;
  43. setOpaque(false);
  44. JLabel l = new BufferTabLabel(this);
  45. add(l);
  46. JLabel close = new JLabel(icon);
  47. close.setPreferredSize(iconDimension);
  48. close.setForeground(Color.BLACK);
  49. add(close);
  50. close.addMouseListener(new BufferTabCloseButtonListener(this));
  51. }
  52. private static class CloseIcon implements Icon {
  53. private static final int width = 9;
  54. private static final int height = 11;
  55. private static final int top = 3;
  56. public void paintIcon(Component c, Graphics g, int x, int y) {
  57. g.drawLine(0, top, width - 2, height - 1);
  58. g.drawLine(1, top, width - 1, height - 1);
  59. g.drawLine(width - 1, top, 1, height - 1);
  60. g.drawLine(width - 2, top, 0, height - 1);
  61. }
  62. public int getIconWidth() {
  63. return width;
  64. }
  65. public int getIconHeight() {
  66. return height;
  67. }
  68. }
  69. private class BufferTabCloseButtonListener extends MouseAdapter {
  70. private BufferTabComponent component;
  71. BufferTabCloseButtonListener(BufferTabComponent component) {
  72. this.component = component;
  73. }
  74. @Override
  75. public void mouseEntered(MouseEvent e) {
  76. JLabel close = (JLabel)e.getSource();
  77. close.setForeground(Color.RED);
  78. }
  79. @Override
  80. public void mouseExited(MouseEvent e) {
  81. JLabel close = (JLabel)e.getSource();
  82. close.setForeground(Color.BLACK);
  83. }
  84. @Override
  85. public void mouseClicked(MouseEvent e) {
  86. int index = pane.indexOfTabComponent(component);
  87. if (index < 0)
  88. return;
  89. EditPane editPane = pane.getEditPane();
  90. jEdit.closeBuffer(editPane,
  91. editPane.getBufferSet().getBuffer(index));
  92. }
  93. }
  94. private class BufferTabLabel extends JLabel {
  95. private BufferTabComponent component;
  96. BufferTabLabel(BufferTabComponent component) {
  97. this.component = component;
  98. }
  99. @Override
  100. public Icon getIcon() {
  101. int index = pane.indexOfTabComponent(component);
  102. if (index < 0)
  103. return null;
  104. return pane.getIconAt(index);
  105. }
  106. @Override
  107. public String getText() {
  108. int index = pane.indexOfTabComponent(component);
  109. if (index < 0)
  110. return null;
  111. return pane.getTitleAt(index);
  112. }
  113. }
  114. }