blob: 64b266d8ad9b6fd38bd0255b12385d5741146069 [file] [log] [blame]
/**
*
* 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;
}
}
}