Debian Codegraph @

src/com/jidesoft/plaf/basic/MeterProgressBarUI.java

raw
1 /*
2 * MeterProgressBarUI.java
3 *
4 * Created on 2007-10-1, 16:42:13
5 *
6 * To change this template, choose Tools | Templates
7 * and open the template in the editor.
8 */
9
10 package com.jidesoft.plaf.basic;
11
12 import com.jidesoft.plaf.UIDefaultsLookup;
13 import com.jidesoft.swing.JideSwingUtilities;
14 import com.jidesoft.swing.MeterProgressBar;
15
16 import javax.swing.*;
17 import javax.swing.plaf.basic.BasicProgressBarUI;
18 import javax.swing.plaf.ComponentUI;
19 import java.awt.*;
20 import java.beans.PropertyChangeEvent;
21 import java.beans.PropertyChangeListener;
22
23 public class MeterProgressBarUI extends BasicProgressBarUI {
24
25 protected Color _cellBackground;
26 protected Color _cellForeground;
27 protected int _cellLength;
28 protected int _cellSpacing;
29 private PropertyChangeListener _propertyChangeListener;
30
31 public static ComponentUI createUI(JComponent c) {
32 return new MeterProgressBarUI();
33 }
34
35 @Override
36 protected void installDefaults() {
37 super.installDefaults();
38 LookAndFeel.installBorder(progressBar, "MeterProgressBar.border");
39 LookAndFeel.installColors(progressBar, "MeterProgressBar.background", "MeterProgressBar.foreground");
40 _cellForeground = UIDefaultsLookup.getColor("MeterProgressBar.cellForeground");
41 _cellBackground = UIDefaultsLookup.getColor("MeterProgressBar.cellBackground");
42 _cellLength = UIDefaultsLookup.getInt("MeterProgressBar.cellLength");
43 _cellSpacing = UIDefaultsLookup.getInt("MeterProgressBar.cellSpacing");
44 }
45
46
47 @Override
48 protected void uninstallDefaults() {
49 super.uninstallDefaults();
50 _cellBackground = null;
51 _cellForeground = null;
52 }
53
54 @Override
55 protected void installListeners() {
56 super.installListeners();
57 progressBar.addPropertyChangeListener(
58 _propertyChangeListener = new PropertyChangeHandler());
59 }
60
61 @Override
62 protected void uninstallListeners() {
63 progressBar.removePropertyChangeListener(_propertyChangeListener);
64 super.uninstallListeners();
65 }
66
67 @Override
68 public void paint(Graphics g, JComponent c) {
69 Insets b = progressBar.getInsets(); // area for border
70 int barRectWidth = progressBar.getWidth() - (b.right + b.left);
71 int barRectHeight = progressBar.getHeight() - (b.top + b.bottom);
72
73 // amount of progress to draw
74 int amountFull = getAmountFull(b, barRectWidth, barRectHeight);
75 int orientation = progressBar.getOrientation();
76 float width = orientation == JProgressBar.HORIZONTAL ? barRectHeight : barRectWidth;
77
78 Graphics2D g2 = (Graphics2D) g.create();
79
80 //paint cell background
81 g2.setColor(_cellBackground);
82 g2.setStroke(new BasicStroke(width, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL));
83 if (orientation == JProgressBar.HORIZONTAL) {
84 g2.drawLine(b.left, barRectHeight / 2 + b.top,
85 b.left + barRectWidth, barRectHeight / 2 + b.top);
86 }
87 else {
88 g2.drawLine(barRectWidth / 2 + b.left, b.top + barRectHeight,
89 barRectWidth / 2 + b.left, b.top);
90 }
91
92 //paint cell foreground
93 if (((MeterProgressBar) c).getStyle() == MeterProgressBar.STYLE_PLAIN) {
94 g2.setColor(_cellForeground);
95 if (orientation == JProgressBar.HORIZONTAL) {
96 if (c.getComponentOrientation().isLeftToRight()) {
97 g2.drawLine(b.left, barRectHeight / 2 + b.top,
98 amountFull + b.left, barRectHeight / 2 + b.top);
99 }
100 else {
101 g2.drawLine(barRectWidth + b.left, barRectHeight / 2 + b.top,
102 barRectWidth + b.left - amountFull, barRectHeight / 2 + b.top);
103 }
104 }
105 else {
106 g2.drawLine(barRectWidth / 2 + b.left, b.top + barRectHeight,
107 barRectWidth / 2 + b.left, b.top + barRectHeight - amountFull);
108 }
109 }
110 else {
111 g2.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL));
112 if (orientation == JProgressBar.HORIZONTAL) {
113 Rectangle rect = new Rectangle(b.left, b.top, amountFull, barRectHeight / 2);
114 if (!c.getComponentOrientation().isLeftToRight()) {
115 rect.x += barRectWidth - amountFull;
116 }
117 JideSwingUtilities.fillGradient(g2, rect, _cellForeground, _cellBackground, true);
118 rect.y += barRectHeight / 2;
119 JideSwingUtilities.fillGradient(g2, rect, _cellBackground, _cellForeground, true);
120 }
121 else {
122 Rectangle rect = new Rectangle(b.left, b.top + barRectHeight - amountFull,
123 barRectWidth / 2, amountFull);
124 JideSwingUtilities.fillGradient(g2, rect, _cellForeground, _cellBackground, false);
125 rect.x += barRectWidth / 2;
126 JideSwingUtilities.fillGradient(g2, rect, _cellBackground, _cellForeground, false);
127 }
128 }
129
130 //paint backgound
131 g2.setColor(progressBar.getBackground());
132 g2.setStroke(new BasicStroke(width, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL,
133 0.f, new float[]{_cellLength, _cellSpacing}, 0.f));
134 // draw each individual cell
135 if (orientation == JProgressBar.HORIZONTAL) {
136 g2.drawLine(b.left - _cellSpacing, barRectHeight / 2 + b.top,
137 b.left + barRectWidth, barRectHeight / 2 + b.top);
138 }
139 else {
140 g2.drawLine(barRectWidth / 2 + b.left, b.top + barRectHeight + _cellSpacing,
141 barRectWidth / 2 + b.left, b.top);
142 }
143
144 g2.dispose();
145 }
146
147 private class PropertyChangeHandler implements PropertyChangeListener {
148
149 public void propertyChange(PropertyChangeEvent evt) {
150 if (MeterProgressBar.PROPERTY_STYLE.equals(evt.getPropertyName())) {
151 progressBar.repaint();
152 }
153 }
154
155 }
156
157 }