NPE in ToolWindow combo when text may be null
[fedora-idea.git] / platform / platform-impl / src / com / intellij / openapi / wm / impl / content / ContentComboLabel.java
blobec4366d1936022b585496910f6d47aae6940e74c
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.ui.content.Content;
19 import com.intellij.util.ui.UIUtil;
21 import javax.swing.border.Border;
22 import javax.swing.border.EmptyBorder;
23 import java.awt.*;
24 import java.awt.event.MouseAdapter;
25 import java.awt.event.MouseEvent;
27 public class ContentComboLabel extends BaseLabel {
29 private ComboIcon myComboIcon = new ComboIcon() {
30 @Override
31 public Rectangle getIconRec() {
32 return new Rectangle(getWidth() - getIconWidth(), 0, getIconWidth(), getHeight());
35 @Override
36 public boolean isActive() {
37 return myUi.myWindow.isActive();
40 private ComboContentLayout myLayout;
42 public ContentComboLabel(ComboContentLayout layout) {
43 super(layout.myUi, true);
44 myLayout = layout;
45 addMouseListener(new MouseAdapter(){});
48 @Override
49 protected void processMouseEvent(MouseEvent e) {
50 super.processMouseEvent(e);
52 if (UIUtil.isActionClick(e)) {
53 myUi.showContentPopup(e);
57 void update() {
58 if (isToDrawCombo()) {
59 setBorder(new EmptyBorder(0, 8, 0, 8));
60 } else {
61 setBorder(null);
64 updateTextAndIcon(myUi.myManager.getSelectedContent(), true);
67 @Override
68 protected void paintComponent(Graphics g) {
69 if (isToDrawCombo()) {
70 g.translate(0, -TAB_SHIFT);
71 super.paintComponent(g);
72 g.translate(0, TAB_SHIFT);
73 } else {
74 super.paintComponent(g);
78 @Override
79 public Dimension getPreferredSize() {
80 if (!isToDrawCombo()) {
81 return super.getPreferredSize();
84 int width = 0;
85 for (int i = 0; i < myUi.myManager.getContentCount(); i++) {
86 String text = myUi.myManager.getContent(i).getDisplayName();
87 FontMetrics metrics = getFontMetrics(getFont());
88 int eachTextWidth = metrics.stringWidth(text != null ? text : "");
89 width = Math.max(eachTextWidth, width);
92 Border border = getBorder();
93 if (border != null) {
94 Insets insets = border.getBorderInsets(this);
95 width += (insets.left + insets.right);
98 width += myComboIcon.getIconWidth();
100 return new Dimension(width, super.getPreferredSize().height);
103 private boolean isToDrawCombo() {
104 return myLayout.isToDrawCombo();
107 @Override
108 protected void paintChildren(Graphics g) {
109 super.paintChildren(g);
110 if (isToDrawCombo()) {
111 myComboIcon.paintIcon(this, g);
112 g.setColor(new Color(255, 255, 255, 100));
113 int x = myComboIcon.getIconRec().x - 3;
114 int yTop = myComboIcon.getIconRec().y;
115 int yBottom = yTop + myComboIcon.getIconHeight();
116 g.drawLine(x, yTop + 1, x, yBottom - 3);
121 @Override
122 public Content getContent() {
123 return myUi.myManager.getSelectedContent();