1
2
3
4
5
6
7
8
9
10
11 package de.jaret.util.ui.table.renderer;
12
13 import java.beans.PropertyChangeEvent;
14 import java.beans.PropertyChangeListener;
15 import java.util.ArrayList;
16 import java.util.HashMap;
17 import java.util.List;
18 import java.util.Map;
19
20 import org.eclipse.swt.graphics.FontData;
21 import org.eclipse.swt.graphics.RGB;
22
23 import de.jaret.util.ui.table.model.IColumn;
24 import de.jaret.util.ui.table.model.IJaretTableCell;
25 import de.jaret.util.ui.table.model.IRow;
26 import de.jaret.util.ui.table.model.ITableViewState;
27 import de.jaret.util.ui.table.model.JaretTableCellImpl;
28 import de.jaret.util.ui.table.model.ITableViewState.HAlignment;
29 import de.jaret.util.ui.table.model.ITableViewState.VAlignment;
30
31 /***
32 * A Default implementation of a CellStyleProvider. It will register itself with every cell style as a property change
33 * listener.
34 *
35 * @author Peter Kliem
36 * @version $Id: DefaultCellStyleProvider.java 347 2007-04-07 15:01:10Z olk $
37 */
38 public class DefaultCellStyleProvider implements ICellStyleProvider, PropertyChangeListener {
39 /*** map storing the row cell styles. */
40 protected Map<IRow, ICellStyle> _rowMap = new HashMap<IRow, ICellStyle>();
41 /*** map storing the column cell styles. */
42 protected Map<IColumn, ICellStyle> _columnMap = new HashMap<IColumn, ICellStyle>();
43 /*** map combintaion storing the style of a cell . */
44 protected Map<IRow, Map<IColumn, ICellStyle>> _cellMap = new HashMap<IRow, Map<IColumn, ICellStyle>>();
45
46 /*** the listener list. */
47 protected List<ICellStyleListener> _listeners;
48
49 /*** the default cell style. */
50 protected ICellStyle _defaultCellStyle;
51 /*** the default cell style aligned right. */
52 protected ICellStyle _defaultCellStyleAlignRight;
53 /*** style stategy. */
54 protected IStyleStrategy _styleStrategy;
55
56 /***
57 * Constructor.
58 */
59 public DefaultCellStyleProvider() {
60 IBorderConfiguration borderConf = new DefaultBorderConfiguration(1, 1, 1, 1);
61 _defaultCellStyle = new DefaultCellStyle(null, null, borderConf, null);
62 _defaultCellStyle.addPropertyChangeListener(this);
63 _defaultCellStyleAlignRight = new DefaultCellStyle(null, null, borderConf, null);
64 _defaultCellStyleAlignRight.setHorizontalAlignment(ITableViewState.HAlignment.RIGHT);
65 _defaultCellStyleAlignRight.addPropertyChangeListener(this);
66 }
67
68 /***
69 * {@inheritDoc} TODO include a strategy for priority row/column.
70 */
71 public ICellStyle getCellStyle(IRow row, IColumn column) {
72 ICellStyle style = null;
73 style = getCellSpecificStyle(row, column, false);
74 if (style == null) {
75 style = _rowMap.get(row);
76 }
77 if (style == null) {
78 style = _columnMap.get(column);
79 }
80 if (style == null) {
81 Class<?> clazz = column.getContentClass(row);
82 if (clazz != null
83 && (clazz.equals(Double.class) || clazz.equals(Integer.class) || clazz.equals(Float.class)
84 || clazz.equals(Double.TYPE) || clazz.equals(Integer.TYPE) || clazz.equals(Float.TYPE))) {
85 style = _defaultCellStyleAlignRight;
86 } else {
87 style = _defaultCellStyle;
88 }
89 }
90 if (_styleStrategy != null) {
91 style = _styleStrategy.getCellStyle(row, column, style, _defaultCellStyle);
92 }
93
94 return style;
95 }
96
97 /***
98 * {@inheritDoc}
99 */
100 public void setRowCellStyle(IRow row, ICellStyle style) {
101 ICellStyle old = _rowMap.get(row);
102 if (old != null) {
103 old.removePropertyChangeListener(this);
104 }
105 _rowMap.put(row, style);
106 if (style != null) {
107 style.addPropertyChangeListener(this);
108 }
109 fireCellStyleChanged(row, null, style);
110 }
111
112 /***
113 * {@inheritDoc}
114 */
115 public ICellStyle getRowCellStyle(IRow row, boolean create) {
116 ICellStyle style = null;
117 style = _rowMap.get(row);
118 if (style != null) {
119 return style;
120 }
121
122 if (style == null && !create) {
123 return _defaultCellStyle;
124 } else {
125 style = _defaultCellStyle.copy();
126 setRowCellStyle(row, style);
127 return style;
128 }
129 }
130
131 /***
132 * {@inheritDoc}
133 */
134 public void setColumnCellStyle(IColumn column, ICellStyle style) {
135 ICellStyle old = _columnMap.get(column);
136 if (old != null) {
137 old.removePropertyChangeListener(this);
138 }
139 _columnMap.put(column, style);
140 if (style != null) {
141 style.addPropertyChangeListener(this);
142 }
143 fireCellStyleChanged(null, column, style);
144 }
145
146 /***
147 * {@inheritDoc}
148 */
149 public ICellStyle getColumnCellStyle(IColumn column, boolean create) {
150 ICellStyle style = null;
151 style = _columnMap.get(column);
152 if (style != null) {
153 return style;
154 }
155 if (style == null && !create) {
156 return _defaultCellStyle;
157 } else {
158
159 style = _defaultCellStyle.copy();
160 setColumnCellStyle(column, style);
161 return style;
162 }
163 }
164
165 /***
166 * {@inheritDoc}
167 */
168 public ICellStyle getCellSpecificStyle(IRow row, IColumn column, boolean create) {
169 ICellStyle style = null;
170 Map<IColumn, ICellStyle> cMap = _cellMap.get(row);
171 if (cMap != null) {
172 style = cMap.get(column);
173 }
174 if (style == null && create) {
175 style = _defaultCellStyle.copy();
176 setCellStyle(row, column, style);
177 }
178 return style;
179 }
180
181 /***
182 * {@inheritDoc}
183 */
184 public void setCellStyle(IRow row, IColumn column, ICellStyle style) {
185 ICellStyle oldStyle = getCellSpecificStyle(row, column, false);
186 if (oldStyle != null) {
187 oldStyle.removePropertyChangeListener(this);
188 }
189 Map<IColumn, ICellStyle> cMap = _cellMap.get(row);
190 if (cMap == null) {
191 cMap = new HashMap<IColumn, ICellStyle>();
192 _cellMap.put(row, cMap);
193 }
194 cMap.put(column, style);
195 style.addPropertyChangeListener(this);
196 }
197
198 /***
199 * {@inheritDoc}
200 */
201 public ICellStyle getDefaultCellStyle() {
202 return _defaultCellStyle;
203 }
204
205 /***
206 * {@inheritDoc}
207 */
208 public void setDefaultCellStyle(ICellStyle cellStyle) {
209 _defaultCellStyle.removePropertyChangeListener(this);
210 _defaultCellStyle = cellStyle;
211 cellStyle.addPropertyChangeListener(this);
212 }
213
214 /***
215 * {@inheritDoc}
216 */
217 public synchronized void addCellStyleListener(ICellStyleListener csl) {
218 if (_listeners == null) {
219 _listeners = new ArrayList<ICellStyleListener>();
220 }
221 _listeners.add(csl);
222 }
223
224 /***
225 * {@inheritDoc}
226 */
227 public void remCellStyleListener(ICellStyleListener csl) {
228 if (_listeners != null) {
229 _listeners.remove(csl);
230 }
231 }
232
233 /***
234 * Inform listeners about a cell style change.
235 *
236 * @param row row affected
237 * @param column olumn affected
238 * @param cellStyle new style
239 */
240 protected void fireCellStyleChanged(IRow row, IColumn column, ICellStyle cellStyle) {
241 if (_listeners != null) {
242 for (ICellStyleListener listener : _listeners) {
243 listener.cellStyleChanged(row, column, cellStyle);
244 }
245 }
246 }
247
248 /***
249 * Retrieve all cells that have a certain style. TODO check performance
250 *
251 * @param style the style to search
252 * @return list of cels the style applies to
253 */
254 protected List<IJaretTableCell> getStyleLocations(ICellStyle style) {
255 List<IJaretTableCell> result = new ArrayList<IJaretTableCell>();
256 if (_columnMap.containsValue(style)) {
257 for (IColumn col : _columnMap.keySet()) {
258 if (_columnMap.get(col) == style) {
259 result.add(new JaretTableCellImpl(null, col));
260 }
261 }
262 }
263 if (_rowMap.containsValue(style)) {
264 for (IRow row : _rowMap.keySet()) {
265 if (_rowMap.get(row) == style) {
266 result.add(new JaretTableCellImpl(row, null));
267 }
268 }
269 }
270
271 for (IRow row : _cellMap.keySet()) {
272 Map<IColumn, ICellStyle> cmap = _cellMap.get(row);
273 if (cmap != null) {
274 for (IColumn col : cmap.keySet()) {
275 ICellStyle cs = cmap.get(col);
276 if (style == cs) {
277 result.add(new JaretTableCellImpl(row, col));
278 }
279 }
280 }
281 }
282
283 return result;
284 }
285
286 /***
287 * {@inheritDoc} Listens to all styles and fires style changed for every location a style is used in.
288 */
289 public void propertyChange(PropertyChangeEvent event) {
290 ICellStyle style = (ICellStyle) event.getSource();
291 List<IJaretTableCell> locs = getStyleLocations(style);
292 for (IJaretTableCell loc : locs) {
293 fireCellStyleChanged(loc.getRow(), loc.getColumn(), (ICellStyle) style);
294 }
295 }
296
297 /***
298 * {@inheritDoc}
299 */
300 public IStyleStrategy getStyleStrategy() {
301 return _styleStrategy;
302 }
303
304 /***
305 * {@inheritDoc}
306 */
307 public void setStyleStrategy(IStyleStrategy startegy) {
308 _styleStrategy = startegy;
309 }
310
311
312 /***
313 * {@inheritDoc}
314 */
315 public void setBackground(IRow row, RGB background) {
316 ICellStyle style = getRowCellStyle(row, true);
317 style.setBackgroundColor(background);
318 setRowCellStyle(row, style);
319 }
320
321 /***
322 * {@inheritDoc}
323 */
324 public void setBackground(IColumn column, RGB background) {
325 ICellStyle style = getColumnCellStyle(column, true);
326 style.setBackgroundColor(background);
327 setColumnCellStyle(column, style);
328 }
329
330 /***
331 * {@inheritDoc}
332 */
333 public void setBackground(IRow row, IColumn column, RGB background) {
334 ICellStyle style = getCellSpecificStyle(row, column, true);
335 style.setBackgroundColor(background);
336 setCellStyle(row, column, style);
337 }
338
339 /***
340 * {@inheritDoc}
341 */
342 public void setForeground(IRow row, RGB foreground) {
343 ICellStyle style = getRowCellStyle(row, true);
344 style.setForegroundColor(foreground);
345 setRowCellStyle(row, style);
346 }
347
348 /***
349 * {@inheritDoc}
350 */
351 public void setForeground(IColumn column, RGB foreground) {
352 ICellStyle style = getColumnCellStyle(column, true);
353 style.setForegroundColor(foreground);
354 setColumnCellStyle(column, style);
355 }
356
357 /***
358 * {@inheritDoc}
359 */
360 public void setForeground(IRow row, IColumn column, RGB foreground) {
361 ICellStyle style = getCellSpecificStyle(row, column, true);
362 style.setForegroundColor(foreground);
363 setCellStyle(row, column, style);
364 }
365
366 /***
367 * {@inheritDoc}
368 */
369 public void setHorizontalAlignment(IRow row, HAlignment alignment) {
370 ICellStyle style = getRowCellStyle(row, true);
371 style.setHorizontalAlignment(alignment);
372 setRowCellStyle(row, style);
373 }
374
375 /***
376 * {@inheritDoc}
377 */
378 public void setHorizontalAlignment(IColumn column, HAlignment alignment) {
379 ICellStyle style = getColumnCellStyle(column, true);
380 style.setHorizontalAlignment(alignment);
381 setColumnCellStyle(column, style);
382 }
383
384 /***
385 * {@inheritDoc}
386 */
387 public void setHorizontalAlignment(IRow row, IColumn column, HAlignment alignment) {
388 ICellStyle style = getCellSpecificStyle(row, column, true);
389 style.setHorizontalAlignment(alignment);
390 setCellStyle(row, column, style);
391 }
392
393 /***
394 * {@inheritDoc}
395 */
396 public void setVerticalAlignment(IRow row, VAlignment alignment) {
397 ICellStyle style = getRowCellStyle(row, true);
398 style.setVerticalAlignment(alignment);
399 setRowCellStyle(row, style);
400 }
401
402 /***
403 * {@inheritDoc}
404 */
405 public void setVerticalAlignment(IColumn column, VAlignment alignment) {
406 ICellStyle style = getColumnCellStyle(column, true);
407 style.setVerticalAlignment(alignment);
408 setColumnCellStyle(column, style);
409 }
410
411 /***
412 * {@inheritDoc}
413 */
414 public void setVerticalAlignment(IRow row, IColumn column, VAlignment alignment) {
415 ICellStyle style = getCellSpecificStyle(row, column, true);
416 style.setVerticalAlignment(alignment);
417 setCellStyle(row, column, style);
418 }
419
420 /***
421 * {@inheritDoc}
422 */
423 public void setFont(IRow row, FontData fontdata) {
424 ICellStyle style = getRowCellStyle(row, true);
425 style.setFont(fontdata);
426 setRowCellStyle(row, style);
427 }
428
429 /***
430 * {@inheritDoc}
431 */
432 public void setFont(IColumn column, FontData fontdata) {
433 ICellStyle style = getColumnCellStyle(column, true);
434 style.setFont(fontdata);
435 setColumnCellStyle(column, style);
436 }
437
438 /***
439 * {@inheritDoc}
440 */
441 public void setFont(IRow row, IColumn column, FontData fontdata) {
442 ICellStyle style = getCellSpecificStyle(row, column, true);
443 style.setFont(fontdata);
444 setCellStyle(row, column, style);
445 }
446
447 }