tool window combo - show content action
[fedora-idea.git] / platform / platform-impl / src / com / intellij / openapi / wm / impl / content / ContentComboLabel.java
blob901c6b3b717fb8cb27a823089a6dfe6516956987
1 /*
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;
27 import javax.swing.*;
28 import javax.swing.border.Border;
29 import javax.swing.border.EmptyBorder;
30 import java.awt.*;
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() {
38 @Override
39 public Rectangle getIconRec() {
40 return new Rectangle(getWidth() - getIconWidth(), 0, getIconWidth(), getHeight());
43 @Override
44 public boolean isActive() {
45 return myUi.myWindow.isActive();
48 private ComboContentLayout myLayout;
50 public ContentComboLabel(ComboContentLayout layout) {
51 super(layout.myUi, true);
52 myLayout = layout;
53 addMouseListener(new MouseAdapter(){});
56 @Override
57 protected void processMouseEvent(MouseEvent e) {
58 super.processMouseEvent(e);
60 if (UIUtil.isActionClick(e)) {
61 myUi.showContentPopup(e);
65 void update() {
66 if (isToDrawCombo()) {
67 setBorder(new EmptyBorder(0, 8, 0, 8));
68 } else {
69 setBorder(null);
72 updateTextAndIcon(myUi.myManager.getSelectedContent(), true);
75 @Override
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);
81 } else {
82 super.paintComponent(g);
86 @Override
87 public Dimension getPreferredSize() {
88 if (!isToDrawCombo()) {
89 return super.getPreferredSize();
92 int width = 0;
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();
114 @Override
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);
128 @Override
129 public Content getContent() {
130 return myUi.myManager.getSelectedContent();