wangting
2024-09-27 a3e87f78ee262ca9bb7d9b0c997639d5f3295890
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package com.vci.client.uif.engine.client.tableArea;
 
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.LinearGradientPaint;
import java.awt.Point;
import java.awt.Rectangle;
import java.util.HashMap;
import java.util.Map;
 
import javax.swing.AbstractButton;
import javax.swing.ButtonModel;
import javax.swing.Icon;
import javax.swing.JComponent;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.metal.MetalButtonUI;
 
import com.vci.client.ui.swing.VCISwingUtil;
 
import sun.swing.SwingUtilities2;
 
/**
 * 查询区域标题按钮ButtonUI
 * @author VCI-STGK006
 *
 */
public class JXToggleButtonUI extends MetalButtonUI {
    
    protected static JXToggleButtonUI tbui = new JXToggleButtonUI();
 
    public static ComponentUI createUI(JComponent c) {
        return tbui;
    }
    
    @Override
    protected void paintText(Graphics g, JComponent c, Rectangle textRect,
            String text) {
        AbstractButton b = (AbstractButton) c;
        
        /* recount the letter position */
        int width = b.getWidth();
        textRect.x = textRect.x + (width - textRect.x - textRect.width) - 10;
        
        int textWidth = 0;
        ButtonModel model = b.getModel();
        FontMetrics fm = SwingUtilities2.getFontMetrics(c, g);
        int mnemIndex = b.getDisplayedMnemonicIndex();
 
        /* Draw the Text */
        if (model.isEnabled()) {
            /*** paint the text normally */
            g.setColor(b.getForeground());
        } else {
            /*** paint the text disabled ***/
            g.setColor(getDisabledTextColor());
        }
        SwingUtilities2.drawStringUnderlineCharAt(c, g, text, mnemIndex,
                textRect.x, textRect.y + fm.getAscent());
    }
    
    @Override
    protected void paintIcon(Graphics g, JComponent c, Rectangle iconRect) {
        /* draw the background*/
        drawBackground(g, c);
        
        AbstractButton b = (AbstractButton) c;
        
        /* recount the icon position */
        iconRect.x = iconRect.x + iconRect.x - 10;
        
        ButtonModel model = b.getModel();
        Icon icon = b.getIcon();
        Icon tmpIcon = null;
 
        if (icon == null) {
            return;
        }
 
        Icon selectedIcon = null;
 
        /* the fallback icon should be based on the selected state */
        if (model.isSelected()) {
            selectedIcon = (Icon) b.getSelectedIcon();
            if (selectedIcon != null) {
                icon = selectedIcon;
            }
        }
 
        if (!model.isEnabled()) {
            if (model.isSelected()) {
                tmpIcon = (Icon) b.getDisabledSelectedIcon();
                if (tmpIcon == null) {
                    tmpIcon = selectedIcon;
                }
            }
 
            if (tmpIcon == null) {
                tmpIcon = (Icon) b.getDisabledIcon();
            }
        } else if (model.isPressed() && model.isArmed()) {
            tmpIcon = (Icon) b.getPressedIcon();
            if (tmpIcon != null) {
                // revert back to 0 offset
                clearTextShiftOffset();
            }
        } else if (b.isRolloverEnabled() && model.isRollover()) {
            if (model.isSelected()) {
                tmpIcon = (Icon) b.getRolloverSelectedIcon();
                if (tmpIcon == null) {
                    tmpIcon = selectedIcon;
                }
            }
 
            if (tmpIcon == null) {
                tmpIcon = (Icon) b.getRolloverIcon();
            }
        }
 
        if (tmpIcon != null) {
            icon = tmpIcon;
        }
 
        if (model.isPressed() && model.isArmed()) {
            icon.paintIcon(c, g, iconRect.x + getTextShiftOffset(), iconRect.y
                    + getTextShiftOffset());
        } else {
            icon.paintIcon(c, g, iconRect.x, iconRect.y);
        }
    }
    
    /* Do nothing */
    @Override
    protected void paintFocus(Graphics g, AbstractButton b, Rectangle viewRect,
            Rectangle textRect, Rectangle iconRect) {
//        Rectangle focusRect = new Rectangle();
//        String text = b.getText();
//        boolean isIcon = b.getIcon() != null;
//
//        // If there is text
//        if (text != null && !text.equals("")) {
//            if (!isIcon) {
//                focusRect.setBounds(textRect);
//            } else {
//                focusRect.setBounds(iconRect.union(textRect));
//            }
//        }
//        // If there is an icon and no text
//        else if (isIcon) {
//            focusRect.setBounds(iconRect);
//        }
//        /* r=245,g=165,b=16 */
//        g.setColor(getFocusColor());
//        g.drawRect((focusRect.x - 1), (focusRect.y - 1), focusRect.width + 1,
//                focusRect.height + 1);
    }
    
    @Override
    protected void paintButtonPressed(Graphics g, AbstractButton b) {
        if (b.isContentAreaFilled()) {
            Dimension size = b.getSize();
            /* r=189,g=190,b=176 */
            g.setColor(getSelectColor());
            g.fillRect(0, 0, size.width, size.height);
        }
    }
    
    private void drawBackground(Graphics g, JComponent c) {
        JXToggleButton b = (JXToggleButton) c;
        String title = b.getTitle();
        String iconUrl = b.getIconUrl();
        
        Graphics2D g2 = (Graphics2D)g;
        Point start = new Point(0, 0);
        Point end = new Point(0, c.getHeight());
        float[] dist = { 0.0F, 0.45F, 0.5F, 1.0F };
        Color[] colors = { VCISwingUtil.TBC1, VCISwingUtil.TBC2, VCISwingUtil.TBC3, VCISwingUtil.TBC4 };
        LinearGradientPaint lgp = new LinearGradientPaint(start, end, dist, 
          colors);
        g2.setPaint(lgp);
        g2.fillRect(0, 0, c.getWidth(), c.getHeight());
        g2.setColor(VCISwingUtil.FRAMECOLOR);
        g2.drawRect(0, 0, c.getWidth() - 1, c.getHeight() - 1);
        if (iconUrl != null && !iconUrl.equals("")) {
          Image img = VCISwingUtil.createImageIcon(iconUrl).getImage();
          if (img != null) {
            g2.drawImage(img, 8, 4, 16, 16, null);
          }
        }
        g2.setColor(VCISwingUtil.TEXTCOLOR);
        g2.setFont(VCISwingUtil.Font12B);
        g2.drawString(title, 30, 17);
    }
}