Skip to main content
summaryrefslogtreecommitdiffstats
blob: 64b266d8ad9b6fd38bd0255b12385d5741146069 (plain) (blame)
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/**
 *                                                                            
 *  Copyright (c) 2011, 2016 - Loetz GmbH&Co.KG (69115 Heidelberg, Germany) 
 *                                                                            
 *  All rights reserved. This program and the accompanying materials           
 *  are made available under the terms of the Eclipse Public License v1.0       
 *  which accompanies this distribution, and is available at                  
 *  http://www.eclipse.org/legal/epl-v10.html                                 
 *                                                                            
 *  Contributors:                                                      
 * 	   Christophe Loetz (Loetz GmbH&Co.KG) - initial implementation
 * 
 */
package org.eclipse.osbp.xtext.table.common;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.TreeMap;

import org.eclipse.osbp.ui.api.datamart.DerivedOlapException;
import org.eclipse.osbp.ui.api.themes.IThemeResourceService;
import org.eclipse.osbp.ui.api.themes.IThemeResourceService.ThemeResourceType;
import org.eclipse.osbp.xtext.datamart.common.olap.DerivedCellSet;
import org.eclipse.osbp.xtext.datamart.common.olap.DerivedMember;
import org.eclipse.osbp.xtext.datamart.common.olap.DerivedPosition;
import org.tepi.filtertable.FilterTable;

import com.vaadin.server.Resource;
import com.vaadin.ui.CustomTable;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Notification;

class DbleComparator implements Comparator<Double> {
	@Override
	public int compare(Double arg0, Double arg1) {
		return arg0.compareTo(arg1);
	}
}

class DteComparator implements Comparator<Date> {
	@Override
	public int compare(Date o1, Date o2) {
		return o1.compareTo(o2);
	}
}

@Deprecated
public class ScorecardCellGenerator implements FilterTable.ColumnGenerator {
	private static final long serialVersionUID = -3687322995582002747L;
	DerivedCellSet cellSet = null;
	DerivedPosition rows = null;
	List<Integer> coordinate = null;
	String format = null;
	DateFormat dateFormat = null;
	int memberColumnId = -1;
	boolean hideLabel = false;
	boolean iconFirst = false;
	boolean styleDiscreteValues = false;
	boolean resourceDiscreteValues = false;
	Map<Date, Resource> dateResourceMap = null;
	Map<Date, String> dateStyleMap = null;
	Map<Double, Resource> doubleResourceMap = null;
	Map<Double, String> doubleStyleMap = null;
	Map<String, Resource> stringResourceMap = null;
	Map<String, String> stringStyleMap = null;
	private Locale locale = new Locale("de", "DE");

	private final IThemeResourceService themeResourceService;

	public ScorecardCellGenerator(IThemeResourceService themeResourceService, DerivedCellSet cellSet, DerivedPosition rows, List<Integer> coordinate) {
		this(themeResourceService, cellSet, rows, coordinate, -1);
	}

	public ScorecardCellGenerator(IThemeResourceService themeResourceService, DerivedCellSet cellSet, DerivedPosition rows, List<Integer> coordinate, int memberColumnId) {
		this.cellSet = cellSet;
		this.rows = rows;
		this.coordinate = coordinate;
		this.memberColumnId = memberColumnId;
		this.themeResourceService = themeResourceService;
	}

	/**
	 * @return the hideLabel
	 */
	public boolean isHideLabel() {
		return hideLabel;
	}

	/**
	 * @param hideLabel
	 *            the hideLabel to set
	 */
	public void setHideLabel(boolean hideLabel) {
		this.hideLabel = hideLabel;
	}

	public boolean isIconFirst() {
		return iconFirst;
	}

	public void setIconFirst(boolean iconFirst) {
		this.iconFirst = iconFirst;
	}

	public String getFormat() {
		return format;
	}

	public void setFormat(String format) {
		this.format = format;
	}

	/**
	 * @param until
	 * @param resourceName
	 */
	public void addResourceInterval(Date until, String resourceName) {
		Resource resource = themeResourceService.getThemeResource(resourceName, ThemeResourceType.ICON);
		if (dateResourceMap == null) {
			dateResourceMap = new TreeMap<Date, Resource>(new DteComparator());
		}
		dateResourceMap.put(until, resource);
	}

	public void addStyleInterval(Date until, String styleName) {
		if (dateStyleMap == null) {
			dateStyleMap = new TreeMap<Date, String>(new DteComparator());
		}
		dateStyleMap.put(until, styleName);
	}

	public void addResourceLookup(Date value, String resourceName) {
		resourceDiscreteValues = true;
		addResourceInterval(value, resourceName);
	}

	public void addStyleLookup(Date value, String styleName) {
		styleDiscreteValues = true;
		addResourceInterval(value, styleName);
	}

	public void addResourceLookup(String value, String resourceName) {
		Resource resource = themeResourceService.getThemeResource(resourceName, ThemeResourceType.ICON);
		if (stringResourceMap == null) {
			stringResourceMap = new HashMap<String, Resource>();
		}
		stringResourceMap.put(value, resource);
	}

	public void addStyleLookup(String value, String styleName) {
		if (stringStyleMap == null) {
			stringStyleMap = new HashMap<String, String>();
		}
		stringStyleMap.put(value, styleName);
	}

	public void addResourceLookup(double value, String resourceName) {
		resourceDiscreteValues = true;
		addResourceInterval(value, resourceName);
	}

	public void addStyleLookup(double value, String styleName) {
		styleDiscreteValues = true;
		addResourceInterval(value, styleName);
	}

	public void addResourceInterval(double until, String resourceName) {
		Resource resource = themeResourceService.getThemeResource(resourceName, ThemeResourceType.ICON);
		if (doubleResourceMap == null) {
			doubleResourceMap = new TreeMap<Double, Resource>(new DbleComparator());
		}
		doubleResourceMap.put(until, resource);
	}

	public void addStyleInterval(double until, String styleName) {
		if (doubleStyleMap == null) {
			doubleStyleMap = new TreeMap<Double, String>(new DbleComparator());
		}
		doubleStyleMap.put(until, styleName);
	}

	protected double getDoubleValue(Object value) throws DerivedOlapException {
		if (value instanceof Double) {
			return (Double) value;
		} else if (value instanceof Float) {
			return (Float) value;
		} else if (value instanceof Long) {
			return (Long) value;
		} else if (value instanceof Integer) {
			return (Integer) value;
		} else if (value instanceof BigDecimal) {
			return ((BigDecimal) value).doubleValue();
		}
		throw new DerivedOlapException("unsupported type conversion");
	}

	@Override
	public Object generateCell(CustomTable source, Object itemId, Object columnId) {
		Object val = null;
		String alignment = "";
		coordinate.set(0, rows.getOrdinal());
		coordinate.set(1, ((DerivedPosition) itemId).getOrdinal());
		try {
			// when generating a cell from axis columns, memberColId is -1
			if (memberColumnId == -1) {
				val = cellSet.getCell(coordinate).getValue();
			} else {
				// when generating a cell from axis rows, memberColId is an index in the qualified member name of the cellSet
				for (DerivedMember member : ((DerivedPosition) itemId).getMembers()) {
					val = member.getUniqueName().split("\\]\\.\\[")[memberColumnId].replace("]", "");
					break;
				}
			}
		} catch (DerivedOlapException e) {
			e.printStackTrace();
		}
		if (val == null) {
			return null;
		}
		String caption = null;
		if (val instanceof Double || val instanceof Float || val instanceof Long || val instanceof Integer || val instanceof BigDecimal) {
			alignment = "v-align-right";
			if (format == null) {
				try {
					caption = String.format("%f", getDoubleValue(val));
				} catch (DerivedOlapException e) {
					e.printStackTrace();
					return null;
				}
			} else {
				DecimalFormat df = null;
				try {
					df = new DecimalFormat(format, new DecimalFormatSymbols(locale));
				} catch (IllegalArgumentException e) {
					String msg = String.format("modeled formatter %s is invalid for decimal numbers: %s", format, e.getLocalizedMessage());
					Notification.show(msg, Notification.Type.ERROR_MESSAGE);
					return null;
				}
				try {
					caption = df.format(getDoubleValue(val));
				} catch (DerivedOlapException e) {
					e.printStackTrace();
					return null;
				}
			}
		} else if (val instanceof String) {
			alignment = "v-align-left";
			if (format == null) {
				caption = (String) val;
			} else {
				caption = String.format(format, (String) val);
			}
		} else if (val instanceof Date) {
			alignment = "v-align-center";
			if (format == null) {
				caption = ((Date) val).toString();
			} else {
				// SimpleDateFormat df = null;
				// try {
				// df = new SimpleDateFormat(format, new DateFormatSymbols(locale));
				// } catch (IllegalArgumentException e) {
				// String msg = String.format("modeled formatter %s is invalid for date: %s", format, e.getLocalizedMessage());
				// Notification.show(msg, Notification.Type.ERROR_MESSAGE);
				// return null;
				// }
				// caption = df.format((Date)val);
				if (format.equals("SHORTDATE")) {
					dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, locale);
				}
				if (format.equals("MEDIUMDATE")) {
					dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, locale);
				}
				if (format.equals("LONGDATE")) {
					dateFormat = DateFormat.getDateInstance(DateFormat.LONG, locale);
				}
				if (format.equals("FULLDATE")) {
					dateFormat = DateFormat.getDateInstance(DateFormat.FULL, locale);
				}
				if (format.equals("SHORTTIME")) {
					dateFormat = DateFormat.getTimeInstance(DateFormat.SHORT, locale);
				}
				if (format.equals("MEDIUMTIME")) {
					dateFormat = DateFormat.getTimeInstance(DateFormat.MEDIUM, locale);
				}
				if (format.equals("LONGTIME")) {
					dateFormat = DateFormat.getTimeInstance(DateFormat.LONG, locale);
				}
				if (format.equals("FULLTIME")) {
					dateFormat = DateFormat.getTimeInstance(DateFormat.FULL, locale);
				}
				if (format.equals("SHORTDATESHORTTIME")) {
					dateFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, locale);
				}
				if (format.equals("SHORTDATEMEDIUMTIME")) {
					dateFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM, locale);
				}
				if (format.equals("SHORTDATELONGTIME")) {
					dateFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG, locale);
				}
				if (format.equals("SHORTDATEFULLTIME")) {
					dateFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.FULL, locale);
				}
				if (format.equals("MEDIUMDATESHORTTIME")) {
					dateFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT, locale);
				}
				if (format.equals("MEDIUMDATEMEDIUMTIME")) {
					dateFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, locale);
				}
				if (format.equals("MEDIUMDATELONGTIME")) {
					dateFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.LONG, locale);
				}
				if (format.equals("MEDIUMDATEFULLTIME")) {
					dateFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.FULL, locale);
				}
				if (format.equals("LONGDATESHORTTIME")) {
					dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.SHORT, locale);
				}
				if (format.equals("LONGDATEMEDIUMTIME")) {
					dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.MEDIUM, locale);
				}
				if (format.equals("LONGDATELONGTIME")) {
					dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
				}
				if (format.equals("LONGDATEFULLTIME")) {
					dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.FULL, locale);
				}
				if (format.equals("FULLDATESHORTTIME")) {
					dateFormat = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.SHORT, locale);
				}
				if (format.equals("FULLDATEMEDIUMTIME")) {
					dateFormat = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.MEDIUM, locale);
				}
				if (format.equals("FULLDATELONGTIME")) {
					dateFormat = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.LONG, locale);
				}
				if (format.equals("FULLDATEFULLTIME")) {
					dateFormat = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, locale);
				}
				caption = dateFormat.format((Date) val);
			}
		}
		Label label = new Label(caption);
		label.addStyleName(alignment);
		if (memberColumnId != -1) {
			label.addStyleName("row-header");
		}
		if (dateStyleMap != null && val instanceof Date) {
			for (Date key : dateStyleMap.keySet()) {
				if ((styleDiscreteValues && ((Date) val).equals(key)) || (!styleDiscreteValues && ((Date) val).before(key))) {
					label.addStyleName(dateStyleMap.get(key));
					break;
				}
			}
		}
		if (doubleStyleMap != null) {
			double doubleValue = 0;
			try {
				doubleValue = getDoubleValue(val);
			} catch (DerivedOlapException e) {
				e.printStackTrace();
			}
			for (Double key : doubleStyleMap.keySet()) {
				if ((styleDiscreteValues && doubleValue == key) || (!styleDiscreteValues && doubleValue <= key)) {
					label.addStyleName(doubleStyleMap.get(key));
					break;
				}
			}
		}
		if (stringStyleMap != null && val instanceof String) {
			for (String key : stringStyleMap.keySet()) {
				if (key.equals((String) val)) {
					label.addStyleName(stringStyleMap.get(key));
					break;
				}
			}
		}
		HorizontalLayout hl = null;
		Embedded embedded = null;
		if (dateResourceMap != null && val instanceof Date) {
			hl = new HorizontalLayout();
			embedded = new Embedded();
			for (Date key : dateResourceMap.keySet()) {
				if ((resourceDiscreteValues && ((Date) val).equals(key)) || (!resourceDiscreteValues && ((Date) val).before(key))) {
					embedded.setSource(dateResourceMap.get(key));
					break;
				}
			}
		}
		if (doubleResourceMap != null) {
			double doubleValue = 0;
			try {
				doubleValue = getDoubleValue(val);
			} catch (DerivedOlapException e) {
				e.printStackTrace();
			}
			hl = new HorizontalLayout();
			embedded = new Embedded();
			for (Double key : doubleResourceMap.keySet()) {
				if ((resourceDiscreteValues && doubleValue == key) || (!resourceDiscreteValues && doubleValue <= key)) {
					embedded.setSource(doubleResourceMap.get(key));
					break;
				}
			}
		}
		if (stringResourceMap != null && val instanceof String) {
			hl = new HorizontalLayout();
			embedded = new Embedded();
			for (String key : stringResourceMap.keySet()) {
				if (key.equals((String) val)) {
					embedded.setSource(stringResourceMap.get(key));
					break;
				}
			}
		}
		if (hl != null) {
			hl.setSizeFull();
			embedded.setWidth("16px");
			if (iconFirst || hideLabel) {
				hl.addComponent(embedded);
				if (!hideLabel) {
					hl.addComponent(label);
					hl.setExpandRatio(label, 1.0f);
				} else {
					hl.setExpandRatio(embedded, 1.0f);
				}
			} else {
				hl.addComponent(label);
				hl.addComponent(embedded);
				hl.setExpandRatio(label, 1.0f);
			}
			hl.setSpacing(true);
			hl.setPrimaryStyleName("chartsview");
			hl.addStyleName("v-tablecell-combined");
			return hl;
		} else {
			return label;
		}
	}

}

Back to the top