dangsn
2024-12-26 4e9ff2ce6a830bb2340d7c8612c72eea0c5a553e
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
package com.vci.client.logon.client;
 
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Composite;
import java.awt.FontMetrics;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.LayoutManager;
import java.awt.Polygon;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Area;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
 
import javax.swing.JComponent;
import javax.swing.JTabbedPane;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicTabbedPaneUI;
 
import com.sun.java.swing.plaf.windows.WindowsTabbedPaneUI;
 
public class VCITabbedPaneUI extends BasicTabbedPaneUI {
 
    private static final Insets NO_INSETS = new Insets(0, 0, 0, 0);
    private ColorSet selectedColorSet;
    private ColorSet defaultColorSet;
    private ColorSet hoverColorSet;
    private boolean contentTopBorderDrawn = true;
    private Color lineColor = new Color(158, 158, 158);
    private Color dividerColor = new Color(200, 200, 200);
    private Insets contentInsets = new Insets(1, 1, 1, 1);
    private int lastRollOverTab = -1;
 
    public static ComponentUI createUI(JComponent c) {
        return new VCITabbedPaneUI(40);
    }
 
    protected LayoutManager createLayoutManager() {
        if (tabPane.getTabLayoutPolicy() == JTabbedPane.SCROLL_TAB_LAYOUT) {
            return super.createLayoutManager();
        } else { /* WRAP_TAB_LAYOUT */
            return new TabbedPaneLayout();
        }
    }
 
    // protected void installComponents() {
    // super.installComponents();
    // int counts = tabPane.getComponents().length;
    // for (int i = 0; i < counts; i++) {
    // if (tabPane.getComponentAt(i) instanceof JPanel) {
    // ((JPanel) tabPane.getComponentAt(i)).setLayout(new FlowLayout(
    // 10, 0, FlowLayout.LEFT));
    // ((JPanel) tabPane.getComponentAt(i)).updateUI();
    // }
    // }
    //
    // }
//    public VCITabbedPaneUI() {
//        this(41);
//    }
    
    public VCITabbedPaneUI(int maxTabHeight) {
 
        selectedColorSet = new ColorSet();
        selectedColorSet.topGradColor1 = new Color(233, 237, 248);
        selectedColorSet.topGradColor2 = new Color(158, 199, 240);
 
        selectedColorSet.bottomGradColor1 = new Color(112, 173, 239);
        selectedColorSet.bottomGradColor2 = new Color(183, 244, 253);
 
        defaultColorSet = new ColorSet();
        defaultColorSet.topGradColor1 = new Color(253, 253, 253);
        defaultColorSet.topGradColor2 = new Color(237, 237, 237);
        defaultColorSet.bottomGradColor1 = new Color(222, 222, 222);
        defaultColorSet.bottomGradColor2 = new Color(255, 255, 255);
//        defaultColorSet.topGradColor1 = new Color(100, 113, 186);
//        defaultColorSet.topGradColor2 = new Color(84, 122, 184);
//        defaultColorSet.bottomGradColor1 = new Color(65, 106, 170);
//        defaultColorSet.bottomGradColor2 = new Color(114, 166, 206);
 
        hoverColorSet = new ColorSet();
        hoverColorSet.topGradColor1 = new Color(244, 244, 244);
        hoverColorSet.topGradColor2 = new Color(223, 223, 223);
 
        hoverColorSet.bottomGradColor1 = new Color(211, 211, 211);
        hoverColorSet.bottomGradColor2 = new Color(235, 235, 235);
 
        this.maxTabHeight = maxTabHeight;
 
        setContentInsets(1);
    }
 
    public void setContentTopBorderDrawn(boolean b) {
        contentTopBorderDrawn = b;
    }
 
    public void setContentInsets(Insets i) {
        contentInsets = i;
    }
 
    public void setContentInsets(int i) {
        contentInsets = new Insets(i, i, i, i);
    }
 
    // 返回当前运行的用于显示选项卡的选项卡数
    public int getTabRunCount(JTabbedPane pane) {
        return 1;
    }
 
    protected void installDefaults() {
        super.installDefaults();
 
        RollOverListener l = new RollOverListener();
        tabPane.addMouseListener(l);
        tabPane.addMouseMotionListener(l);
 
        tabAreaInsets = NO_INSETS; // tab色insets边框
        tabInsets = new Insets(0, 0, 50, 50);
    }
 
    protected boolean scrollableTabLayoutEnabled() {
        return false;
    }
 
    protected Insets getContentBorderInsets(int tabPlacement) {
        return contentInsets;
    }
 
    // protected Insets getTabAreaInsets(int tabPlacement) {
    //        
    // return contentInsets;
    // }
 
    protected int calculateTabHeight(int tabPlacement, int tabIndex,
            int fontHeight) {
        return maxTabHeight;
    }
 
    protected int calculateTabWidth(int tabPlacement, int tabIndex,
            FontMetrics metrics) {
        int w = super.calculateTabWidth(tabPlacement, tabIndex, metrics);
        int wid = metrics.charWidth('M');
        w += wid * 2;
        return w;
    }
 
    protected int calculateMaxTabHeight(int tabPlacement) {
        return maxTabHeight;
    }
 
    protected void paintTabArea(Graphics g, int tabPlacement, int selectedIndex) {
        Graphics2D g2d = (Graphics2D) g;
        
        int height2 = maxTabHeight / 2;
        g2d.setPaint(new GradientPaint(0, 0, defaultColorSet.topGradColor1, 0,
                height2, defaultColorSet.topGradColor2));
        g2d.fillRect(0, 0, tabPane.getWidth(), height2);
 
        g2d.setPaint(new GradientPaint(0, height2, defaultColorSet.bottomGradColor1,
                0, maxTabHeight, defaultColorSet.bottomGradColor2));
        g2d.fillRect(0, height2, tabPane.getWidth(), height2 + 1);
 
        super.paintTabArea(g, tabPlacement, selectedIndex);
 
//        if (contentTopBorderDrawn) {
//            g2d.setColor(lineColor);
//            g2d.drawLine(0, 20 , tabPane.getWidth() - 1, 20);
//        }
    }
 
    protected void paintTabBackground(Graphics g, int tabPlacement,
            int tabIndex, int x, int y, int w, int h, boolean isSelected) {
        Graphics2D g2d = (Graphics2D) g;
        ColorSet colorSet;
 
        Rectangle rect = rects[tabIndex];
 
        if (isSelected) {
            colorSet = selectedColorSet;
        } else if (getRolloverTab() == tabIndex) {
            colorSet = hoverColorSet;
        } else {
            colorSet = defaultColorSet;
        }
 
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
 
        int width = rect.width;
        int xpos = rect.x;
        int yPos = rect.y;
        if (tabIndex > -1) {
            width--;
            xpos++;
            yPos += 2;
        }
 
        g2d.setPaint(new GradientPaint(xpos, 0, colorSet.topGradColor1, xpos,
                h / 2, colorSet.topGradColor2));
        g2d.fill(this.getUpArea(xpos, yPos, width, h - 2));
 
        g2d.setPaint(new GradientPaint(0, h / 2, colorSet.bottomGradColor1, 0,
                h, colorSet.bottomGradColor2));
        g2d.fill(this.getDownArea(xpos, yPos, width, h - 2));
 
//        if (contentTopBorderDrawn) {
//            g2d.setColor(lineColor);
//            g2d.drawLine(rect.x, 20, rect.x + rect.width - 1, 20);
//        }
    }
 
    private Shape getArea(int x, int y, int w, int h) {
        RoundRectangle2D rect = new RoundRectangle2D.Float(x, y, w, h, 15, 15);
        Area a = new Area(rect);
        Rectangle2D rec = new Rectangle2D.Float(x, y + h / 2, w, h / 2);
        Area b = new Area(rec);
        a.add(b);
        return a;
    }
 
    /**
     * 上半部分
     * 
     * @param x
     * @param y
     * @param w
     * @param h
     * @return
     */
    private Shape getUpArea(int x, int y, int w, int h) {
        Rectangle2D rec = new Rectangle2D.Float(x, y, w, h / 2 + 1);
        Area a = new Area(rec);
        RoundRectangle2D rect = new RoundRectangle2D.Float(x, y, w, h, 15, 15);
        Area b = new Area(rect);
        a.intersect(b);
        return a;
    }
 
    /**
     * 下半部分
     * 
     * @param x
     * @param y
     * @param w
     * @param h
     * @return
     */
    private Shape getDownArea(int x, int y, int w, int h) {
        Rectangle2D rec = new Rectangle2D.Float(x, y + h / 2, w, h / 2 + 1);
        Area a = new Area(rec);
        RoundRectangle2D rect = new RoundRectangle2D.Float(x, y, w, h, 15, 15);
        // Area b = new Area(rect);
        // a.intersect(b);
 
        return a;
    }
 
    protected void paintTabBorder(Graphics g, int tabPlacement, int tabIndex,
            int x, int y, int w, int h, boolean isSelected) {
        Rectangle rect = getTabBounds(tabIndex, new Rectangle(x, y, w, h));
        // g.setColor(dividerColor);
        Graphics2D g2 = (Graphics2D) g;
        Composite old = g2.getComposite();
        AlphaComposite comp = AlphaComposite.getInstance(
                AlphaComposite.SRC_OVER, 0.0f);
        g2.setComposite(comp);
        g2.setColor(dividerColor);
        g2.drawLine(rect.x + rect.width, 0, rect.x + rect.width, 20);
        g2.setComposite(old);
    }
 
    protected void paintContentBorderTopEdge(Graphics g, int tabPlacement,
            int selectedIndex, int x, int y, int w, int h) {
    }
 
    protected void paintContentBorderRightEdge(Graphics g, int tabPlacement,
            int selectedIndex, int x, int y, int w, int h) {
        // Do nothing
    }
 
    protected void paintContentBorderLeftEdge(Graphics g, int tabPlacement,
            int selectedIndex, int x, int y, int w, int h) {
        // Do nothing
    }
 
    protected void paintContentBorderBottomEdge(Graphics g, int tabPlacement,
            int selectedIndex, int x, int y, int w, int h) {
        // Do nothing
    }
 
    protected void paintFocusIndicator(Graphics g, int tabPlacement,
            Rectangle[] rects, int tabIndex, Rectangle iconRect,
            Rectangle textRect, boolean isSelected) {
        // Do nothing
    }
 
    protected int getTabLabelShiftY(int tabPlacement, int tabIndex,
            boolean isSelected) {
        return 0;
    }
 
    private class ColorSet {
        Color topGradColor1;
        Color topGradColor2;
 
        Color bottomGradColor1;
        Color bottomGradColor2;
    }
 
    private class RollOverListener implements MouseMotionListener,
            MouseListener {
 
        public void mouseDragged(MouseEvent e) {
        }
 
        public void mouseMoved(MouseEvent e) {
            checkRollOver();
        }
 
        public void mouseClicked(MouseEvent e) {
        }
 
        public void mousePressed(MouseEvent e) {
        }
 
        public void mouseReleased(MouseEvent e) {
        }
 
        public void mouseEntered(MouseEvent e) {
            checkRollOver();
        }
 
        public void mouseExited(MouseEvent e) {
            tabPane.repaint();
        }
 
        private void checkRollOver() {
            int currentRollOver = getRolloverTab();
            if (currentRollOver != lastRollOverTab) {
                lastRollOverTab = currentRollOver;
                Rectangle tabsRect = new Rectangle(0, 0, tabPane.getWidth(), 20);
                tabPane.repaint(tabsRect);
            }
        }
    }
 
    public class TabbedPaneLayout extends BasicTabbedPaneUI.TabbedPaneLayout {
 
        public TabbedPaneLayout() {
            VCITabbedPaneUI.this.super();
 
        }
 
        protected void calculateTabRects(int tabPlacement, int tabCount) {
            super.calculateTabRects(tabPlacement, tabCount);
            for (int i = 0; i < rects.length; i++) {
                rects[i].x = rects[i].x + (5 * i);
            }
        }
 
        protected void padSelectedTab(int tabPlacement, int selectedIndex) {
            // don't pad only the selected tab, but all the tabs, to space them.
//            for (int i = 0; i < rects.length; i++) {
//                Rectangle selRect = rects[i];
//                Insets padInsets = getSelectedTabPadInsets(tabPlacement);
//                selRect.x += padInsets.left;
//                selRect.width -= (padInsets.left + padInsets.right);
//                selRect.y -= padInsets.top;
//                selRect.height += (padInsets.top + padInsets.bottom);
//            }
        }
 
//        protected void normalizeTabRuns(int tabPlacement, int tabCount,
//                int start, int max) {
//            if (tabPlacement == TOP || tabPlacement == BOTTOM) {
//                super.normalizeTabRuns(tabPlacement, tabCount, start, max);
//            }
//        }
//
//        // Don't rotate runs!
//        protected void rotateTabRuns(int tabPlacement, int selectedRun) {
//        }
 
    }
 
}