/platform/platform-impl/src/com/intellij/openapi/wm/impl/content/ContentTabLabel.java

https://bitbucket.org/nbargnesi/idea · Java · 112 lines · 80 code · 17 blank · 15 comment · 10 complexity · 7cbfe14c3129babe9fb6f5879f4d74d3 MD5 · raw file

  1. /*
  2. * Copyright 2000-2009 JetBrains s.r.o.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.intellij.openapi.wm.impl.content;
  17. import com.intellij.ui.EngravedTextGraphics;
  18. import com.intellij.ui.Gray;
  19. import com.intellij.ui.content.Content;
  20. import com.intellij.ui.content.ContentManager;
  21. import com.intellij.util.ui.BaseButtonBehavior;
  22. import com.intellij.util.ui.TimedDeadzone;
  23. import javax.swing.*;
  24. import javax.swing.border.EmptyBorder;
  25. import java.awt.*;
  26. import java.awt.event.MouseEvent;
  27. class ContentTabLabel extends BaseLabel {
  28. Content myContent;
  29. private final BaseButtonBehavior myBehavior;
  30. private final TabContentLayout myLayout;
  31. public ContentTabLabel(final Content content, TabContentLayout layout) {
  32. super(layout.myUi, true);
  33. myLayout = layout;
  34. myContent = content;
  35. update();
  36. myBehavior = new BaseButtonBehavior(this) {
  37. protected void execute(final MouseEvent e) {
  38. final ContentManager mgr = contentManager();
  39. if (mgr.getIndexOfContent(myContent) >= 0) {
  40. mgr.setSelectedContent(myContent, true);
  41. }
  42. }
  43. };
  44. myBehavior.setActionTrigger(MouseEvent.MOUSE_PRESSED);
  45. myBehavior.setMouseDeadzone(TimedDeadzone.NULL);
  46. }
  47. public void update() {
  48. if (!myLayout.isToDrawTabs()) {
  49. setHorizontalAlignment(JLabel.LEFT);
  50. setBorder(null);
  51. } else {
  52. setHorizontalAlignment(JLabel.CENTER);
  53. setBorder(new EmptyBorder(0, 8, 0, 8));
  54. }
  55. updateTextAndIcon(myContent, isSelected());
  56. }
  57. @Override
  58. protected boolean allowEngravement() {
  59. return isSelected() || (myUi != null && myUi.myWindow.isActive());
  60. }
  61. @Override
  62. protected Color getActiveFg(boolean selected) {
  63. if (contentManager().getContentCount() > 1) {
  64. return selected ? Color.white : super.getActiveFg(selected);
  65. }
  66. return super.getActiveFg(selected);
  67. }
  68. @Override
  69. protected Color getPassiveFg(boolean selected) {
  70. if (contentManager().getContentCount() > 1) {
  71. return selected ? Gray._255 : super.getPassiveFg(selected);
  72. }
  73. return super.getPassiveFg(selected);
  74. }
  75. protected void paintComponent(final Graphics g) {
  76. super.paintComponent(g);
  77. }
  78. public boolean isSelected() {
  79. return contentManager().isSelected(myContent);
  80. }
  81. @Override
  82. protected Graphics _getGraphics(Graphics2D g) {
  83. if (isSelected() && contentManager().getContentCount() > 1) {
  84. return new EngravedTextGraphics(g, 1, 1, myUi.myWindow.isActive() ? new Color(0, 0, 0, 120) : new Color(0, 0, 0, 130));
  85. }
  86. return super._getGraphics(g);
  87. }
  88. private ContentManager contentManager() {
  89. return myUi.myWindow.getContentManager();
  90. }
  91. @Override
  92. public Content getContent() {
  93. return myContent;
  94. }
  95. }