ComponentWithBrowseButton - optional remove listener on hide
[fedora-idea.git] / platform / platform-impl / src / com / intellij / openapi / wm / impl / StripeButtonUI.java
blob864da4b6af6c57a51381a55738844f5bdca9ad73
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;
18 import com.intellij.openapi.wm.ToolWindowAnchor;
20 import javax.swing.*;
21 import javax.swing.plaf.ComponentUI;
22 import javax.swing.plaf.basic.BasicGraphicsUtils;
23 import javax.swing.plaf.metal.MetalToggleButtonUI;
24 import java.awt.*;
25 import java.awt.geom.AffineTransform;
27 /**
28 * @author Vladimir Kondratyev
30 final class StripeButtonUI extends MetalToggleButtonUI{
31 private static final StripeButtonUI ourInstance=new StripeButtonUI();
33 private static final Rectangle ourIconRect=new Rectangle();
34 private static final Rectangle ourTextRect=new Rectangle();
35 private static final Rectangle ourViewRect=new Rectangle();
36 private static Insets ourViewInsets=new Insets(0,0,0,0);
38 private StripeButtonUI(){}
40 /** Invoked by reflection */
41 public static ComponentUI createUI(final JComponent c){
42 return ourInstance;
45 public Dimension getPreferredSize(final JComponent c){
46 final StripeButton button=(StripeButton)c;
47 final Dimension dim=super.getPreferredSize(button);
49 dim.width=(int)(4+dim.width*1.1f);
50 dim.height+=4;
52 final ToolWindowAnchor anchor=button.getWindowInfo().getAnchor();
53 if(ToolWindowAnchor.LEFT==anchor||ToolWindowAnchor.RIGHT==anchor){
54 return new Dimension(dim.height,dim.width);
55 } else{
56 return dim;
60 public void paint(final Graphics g,final JComponent c){
61 final StripeButton button=(StripeButton)c;
63 final String text=button.getText();
64 final Icon icon=(button.isEnabled()) ? button.getIcon() : button.getDisabledIcon();
66 if((icon==null)&&(text==null)){
67 return;
70 final FontMetrics fm=button.getFontMetrics(button.getFont());
71 ourViewInsets=c.getInsets(ourViewInsets);
73 ourViewRect.x=ourViewInsets.left;
74 ourViewRect.y=ourViewInsets.top;
76 final ToolWindowAnchor anchor=button.getWindowInfo().getAnchor();
78 // Use inverted height & width
79 if(ToolWindowAnchor.RIGHT==anchor||ToolWindowAnchor.LEFT==anchor){
80 ourViewRect.height=c.getWidth()-(ourViewInsets.left+ourViewInsets.right);
81 ourViewRect.width=c.getHeight()-(ourViewInsets.top+ourViewInsets.bottom);
82 } else{
83 ourViewRect.height=c.getHeight()-(ourViewInsets.left+ourViewInsets.right);
84 ourViewRect.width=c.getWidth()-(ourViewInsets.top+ourViewInsets.bottom);
87 ourIconRect.x=ourIconRect.y=ourIconRect.width=ourIconRect.height=0;
88 ourTextRect.x=ourTextRect.y=ourTextRect.width=ourTextRect.height=0;
90 final String clippedText=SwingUtilities.layoutCompoundLabel(
91 c,fm,text,icon,
92 button.getVerticalAlignment(),button.getHorizontalAlignment(),
93 button.getVerticalTextPosition(),button.getHorizontalTextPosition(),
94 ourViewRect,ourIconRect,ourTextRect,
95 button.getText()==null ? 0 : button.getIconTextGap()
98 // Paint button's background
100 final Graphics2D g2=(Graphics2D)g;
101 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
102 g2.setRenderingHint(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY);
104 final ButtonModel model=button.getModel();
106 final Color background = button.getBackground();
108 boolean toFill = true; // button.isSelected() || !button.getWindowInfo().isSplit(); // do not draw split button bg
109 boolean toBorder = true; // button.isSelected() || !button.getWindowInfo().isSplit();
111 if (model.isArmed() && model.isPressed() || model.isSelected()) {
112 final Graphics2D g2d = (Graphics2D) g;
113 final GradientPaint paint;
114 if (ToolWindowAnchor.TOP == anchor || ToolWindowAnchor.BOTTOM == anchor) {
115 paint = new GradientPaint(0, 0, background.darker(), 0, button.getHeight(), background.brighter());
117 else {
118 paint = new GradientPaint(0, 0, background.darker(), button.getWidth(), 0, background.brighter());
120 g2d.setPaint(paint);
122 if (toFill) {
123 g2d.fillRoundRect(3, 3, button.getWidth() - 6, button.getHeight() - 6, 5, 5);
126 if (toBorder) {
127 g.setColor(Color.black);
128 g.drawRoundRect(3, 3, button.getWidth() - 6, button.getHeight() - 6, 5, 5);
131 else {
132 if (toFill) {
133 g.setColor(background);
134 g.fillRoundRect(3, 3, button.getWidth() - 6, button.getHeight() - 6, 5, 5);
137 if (toBorder) {
138 g.setColor(Color.GRAY);
139 g.drawRoundRect(3, 3, button.getWidth() - 6, button.getHeight() - 6, 5, 5);
143 if (model.isRollover()) {
144 if (!model.isArmed() && !model.isPressed() && !model.isSelected()) {
145 final Graphics2D g2d = (Graphics2D) g;
146 final GradientPaint paint;
147 if (ToolWindowAnchor.TOP == anchor || ToolWindowAnchor.BOTTOM == anchor) {
148 paint = new GradientPaint(0, 0, background, 0, button.getHeight(), Color.white);
150 else {
151 paint = new GradientPaint(0, 0, background, button.getWidth(), 0, Color.white);
153 g2d.setPaint(paint);
154 g2d.fillRoundRect(3, 3, button.getWidth() - 6, button.getHeight() - 6, 5, 5);
157 if (toBorder) {
158 g.setColor(Color.darkGray);
159 g.drawRoundRect(3, 3, button.getWidth() - 6, button.getHeight() - 6, 5, 5);
163 AffineTransform tr=null;
164 if(ToolWindowAnchor.RIGHT==anchor||ToolWindowAnchor.LEFT==anchor){
165 tr=g2.getTransform();
166 if(ToolWindowAnchor.RIGHT==anchor){
167 if(icon != null){ // do not rotate icon
168 icon.paintIcon(c, g2, ourIconRect.y, ourIconRect.x);
170 g2.rotate(Math.PI/2);
171 g2.translate(0,-c.getWidth());
172 } else {
173 if(icon != null){ // do not rotate icon
174 icon.paintIcon(c, g2, ourIconRect.y, c.getHeight() - ourIconRect.x - icon.getIconHeight());
176 g2.rotate(-Math.PI/2);
177 g2.translate(-c.getHeight(),0);
180 else{
181 if(icon!=null){
182 icon.paintIcon(c,g2,ourIconRect.x,ourIconRect.y);
186 // paint text
188 if(text!=null){
189 if(model.isEnabled()){
190 if(model.isArmed()&&model.isPressed()||model.isSelected()){
191 g.setColor(background);
192 } else{
193 g.setColor(button.getForeground());
195 } else{
196 g.setColor(background.darker());
198 /* Draw the Text */
199 if(model.isEnabled()){
200 /*** paint the text normally */
201 g.setColor(button.getForeground());
202 BasicGraphicsUtils.drawString(g,clippedText,button.getMnemonic2(),ourTextRect.x,ourTextRect.y+fm.getAscent());
203 } else{
204 /*** paint the text disabled ***/
205 if(model.isSelected()){
206 g.setColor(c.getBackground());
207 } else{
208 g.setColor(getDisabledTextColor());
210 BasicGraphicsUtils.drawString(g,clippedText,button.getMnemonic2(),ourTextRect.x,ourTextRect.y+fm.getAscent());
213 if(ToolWindowAnchor.RIGHT==anchor||ToolWindowAnchor.LEFT==anchor){
214 g2.setTransform(tr);