1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.cosylab.gui.components.table.renderers;
21
22 import com.cosylab.gui.components.table.cells.PatternCell;
23 import com.cosylab.gui.components.util.PaintHelper;
24 import com.cosylab.util.BitSetUtilities;
25
26 import java.awt.Color;
27 import java.awt.Component;
28 import java.awt.Graphics;
29 import java.awt.Point;
30 import java.awt.event.MouseEvent;
31
32 import javax.swing.JTable;
33 import javax.swing.ToolTipManager;
34
35
36
37
38
39
40
41
42
43
44
45 public class PatternCellRenderer extends DefaultTableCellRenderer
46 {
47 private static final long serialVersionUID = 1L;
48 protected TimeoutBorder border = new TimeoutBorder();
49 private Point position = null;
50 private int size;
51 private int h;
52 private int w;
53 private long value;
54 private Color[] colorsOn;
55 private Color[] colorsOff;
56 private String[] descriptions;
57 public static final Color DEFAULT_ON = Color.GREEN;
58 public static final Color DEFAULT_OFF = Color.GRAY;
59
60
61
62
63 public PatternCellRenderer()
64 {
65 super(true, false);
66 setToolTipText("");
67 w = getWidth();
68 ToolTipManager.sharedInstance().setInitialDelay(0);
69 ToolTipManager.sharedInstance().setDismissDelay(1000);
70
71
72 }
73
74
75
76
77
78 public Component getTableCellRendererComponent(JTable table,
79 Object patternCell, boolean isSelected, boolean hasFocus, int row,
80 int column)
81 {
82 super.getTableCellRendererComponent(table, patternCell, isSelected,
83 hasFocus, row, column);
84
85 if (patternCell instanceof PatternCell) {
86 PatternCell cell = (PatternCell)patternCell;
87 descriptions = cell.getDescriptions();
88 value = cell.getLongValue();
89
90 if (descriptions == null) {
91 size = BitSetUtilities.forLong(value).length();
92 if (size == 0) size = 1;
93 } else {
94 size = descriptions.length;
95 }
96
97 colorsOn = cell.getColorsWhenOn();
98 colorsOff = cell.getColorsWhenOff();
99
100
101
102 setText("");
103 }
104 return this;
105 }
106
107
108
109
110
111
112
113
114 public String getToolTipText(MouseEvent e)
115 {
116 position = e.getPoint();
117
118 for (int i = 0; i < size; i++) {
119 if (position.getX() > (i * w) / size
120 && position.getX() < ((i + 1) * w) / size) {
121 if (descriptions != null) {
122 return descriptions[i];
123 } else {
124 return String.valueOf(i);
125 }
126 }
127 }
128
129 return null;
130 }
131
132
133
134
135
136
137 public void paintComponent(Graphics g)
138 {
139 paintSuperComponent(g);
140
141 if (active) {
142 w = getWidth() - 1;
143 h = getHeight() - 1;
144
145 for (int i = 0; i < size; i++) {
146 if ((value & 1) == 1) {
147 if (colorsOn != null && colorsOn.length > i) {
148 g.setColor(colorsOn[i]);
149 } else {
150 g.setColor(DEFAULT_ON);
151 }
152 } else {
153 if (colorsOn != null && colorsOn.length > i) {
154 g.setColor(colorsOff[i]);
155 } else {
156 g.setColor(DEFAULT_OFF);
157 }
158 }
159
160 value >>= 1;
161 int bin= w/size+1;
162 g.fillRect((i * w) / size, 0, bin, h);
163 g.setColor(Color.WHITE);
164 g.drawRect((i * w) / size, 0, bin, h);
165 }
166 }
167
168 paintDecorations(g);
169
170 if (alarm) {
171 PaintHelper.paintRectangle(g, 2, 2, getWidth()-5, getHeight()-5,
172 Color.WHITE, 1);
173 }
174 }
175 }
176
177