2 * Copyright 2000-2009 JetBrains s.r.o.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package com
.intellij
.openapi
.wm
.impl
.content
;
18 import com
.intellij
.openapi
.ui
.popup
.JBPopupFactory
;
19 import com
.intellij
.openapi
.ui
.popup
.ListPopup
;
20 import com
.intellij
.openapi
.ui
.popup
.PopupStep
;
21 import com
.intellij
.openapi
.ui
.popup
.util
.BaseListPopupStep
;
22 import com
.intellij
.ui
.content
.Content
;
23 import com
.intellij
.ui
.content
.ContentManager
;
24 import com
.intellij
.util
.ui
.UIUtil
;
25 import org
.jetbrains
.annotations
.NotNull
;
28 import javax
.swing
.border
.Border
;
29 import javax
.swing
.border
.EmptyBorder
;
31 import java
.awt
.event
.MouseAdapter
;
32 import java
.awt
.event
.MouseEvent
;
33 import java
.util
.Arrays
;
35 public class ContentComboLabel
extends BaseLabel
{
37 private ComboIcon myComboIcon
= new ComboIcon() {
39 public Rectangle
getIconRec() {
40 return new Rectangle(getWidth() - getIconWidth(), 0, getIconWidth(), getHeight());
44 public boolean isActive() {
45 return myUi
.myWindow
.isActive();
48 private ComboContentLayout myLayout
;
50 public ContentComboLabel(ComboContentLayout layout
) {
51 super(layout
.myUi
, true);
53 addMouseListener(new MouseAdapter(){});
57 protected void processMouseEvent(MouseEvent e
) {
58 super.processMouseEvent(e
);
60 if (UIUtil
.isActionClick(e
)) {
61 myUi
.showContentPopup(e
);
66 if (isToDrawCombo()) {
67 setBorder(new EmptyBorder(0, 8, 0, 8));
72 updateTextAndIcon(myUi
.myManager
.getSelectedContent(), true);
76 protected void paintComponent(Graphics g
) {
77 if (isToDrawCombo()) {
78 g
.translate(0, -TAB_SHIFT
);
79 super.paintComponent(g
);
80 g
.translate(0, TAB_SHIFT
);
82 super.paintComponent(g
);
87 public Dimension
getPreferredSize() {
88 if (!isToDrawCombo()) {
89 return super.getPreferredSize();
93 for (int i
= 0; i
< myUi
.myManager
.getContentCount(); i
++) {
94 String text
= myUi
.myManager
.getContent(i
).getDisplayName();
95 int eachTextWidth
= getFontMetrics(getFont()).stringWidth(text
);
96 width
= Math
.max(eachTextWidth
, width
);
99 Border border
= getBorder();
100 if (border
!= null) {
101 Insets insets
= border
.getBorderInsets(this);
102 width
+= (insets
.left
+ insets
.right
);
105 width
+= myComboIcon
.getIconWidth();
107 return new Dimension(width
, super.getPreferredSize().height
);
110 private boolean isToDrawCombo() {
111 return myLayout
.isToDrawCombo();
115 protected void paintChildren(Graphics g
) {
116 super.paintChildren(g
);
117 if (isToDrawCombo()) {
118 myComboIcon
.paintIcon(this, g
);
119 g
.setColor(new Color(255, 255, 255, 100));
120 int x
= myComboIcon
.getIconRec().x
- 3;
121 int yTop
= myComboIcon
.getIconRec().y
;
122 int yBottom
= yTop
+ myComboIcon
.getIconHeight();
123 g
.drawLine(x
, yTop
+ 1, x
, yBottom
- 3);
129 public Content
getContent() {
130 return myUi
.myManager
.getSelectedContent();