| /** |
| * Copyright 2011-2019 Jonathan Nash and others |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| * |
| * Contributors: |
| * Jonathan Nash - initial implementation |
| * Compex Systemhaus GmbH - adaptions for OSBP |
| */ |
| package org.eclipse.osbp.xtext.table.common.export; |
| |
| import com.vaadin.data.Container; |
| import com.vaadin.data.Property; |
| import com.vaadin.ui.Grid; |
| import com.vaadin.ui.UI; |
| import org.apache.poi.ss.usermodel.CellStyle; |
| import org.tepi.filtertable.FilterTable; |
| |
| import java.util.Arrays; |
| import java.util.LinkedList; |
| import java.util.List; |
| |
| /** |
| * @author thomas |
| */ |
| public class DefaultTableHolder implements TableHolder { |
| |
| protected short defaultAlignment = CellStyle.ALIGN_LEFT; |
| |
| /** |
| * Whether the Container is a HierarchicalContainer or an extension thereof. |
| */ |
| private boolean hierarchical; |
| |
| private FilterTable heldTable; |
| private Grid heldGrid; |
| private Container container; |
| /** |
| * The Property ids of the Items in the FilterTable. |
| */ |
| private LinkedList<Object> propIds; |
| |
| public DefaultTableHolder(FilterTable table) { |
| this(table.getContainerDataSource()); |
| this.heldTable = table; |
| // The order and visibility of the columns are determined by the FilterTable |
| this.propIds = new LinkedList<Object>(Arrays.asList(table.getVisibleColumns())); |
| } |
| |
| public DefaultTableHolder(Grid grid) { |
| this(grid.getContainerDataSource()); |
| this.heldGrid = grid; |
| |
| List<Grid.Column> columns = grid.getColumns(); |
| this.propIds = new LinkedList<Object>(); |
| for (Grid.Column c: columns) { |
| propIds.add(c.getPropertyId()); |
| } |
| } |
| |
| public DefaultTableHolder(Container container) { |
| this.container = container; |
| // fixed issue pointed out by Mark Lillywhite in the forum and smorygo....@gmail.com on the issues page. |
| // Was comparing to HierarchicalContainer, should have been comparing to Container.Hierarchical. |
| if (Container.Hierarchical.class.isAssignableFrom(this.container.getClass())) { |
| setHierarchical(true); |
| } else { |
| setHierarchical(false); |
| } |
| // The order and visibility of the columns are determined by the Container unless later |
| // superseded by the FilterTable or the Grid. |
| this.propIds = new LinkedList<Object>(container.getContainerPropertyIds()); |
| this.heldTable = null; |
| this.heldGrid = null; |
| } |
| |
| @Override |
| public List<Object> getPropIds() { |
| return propIds; |
| } |
| |
| @Override |
| public boolean isHierarchical() { |
| return hierarchical; |
| } |
| |
| @Override |
| final public void setHierarchical(boolean hierarchical) { |
| this.hierarchical = hierarchical; |
| } |
| |
| @Override |
| public Short getCellAlignment(Object propId) { |
| if (null == heldTable) { |
| return defaultAlignment; |
| } |
| final FilterTable.Align vaadinAlignment = heldTable.getColumnAlignment(propId); |
| return vaadinAlignmentToCellAlignment(vaadinAlignment); |
| } |
| |
| private Short vaadinAlignmentToCellAlignment(final FilterTable.Align vaadinAlignment) { |
| if (FilterTable.Align.LEFT.equals(vaadinAlignment)) { |
| return CellStyle.ALIGN_LEFT; |
| } else if (FilterTable.Align.RIGHT.equals(vaadinAlignment)) { |
| return CellStyle.ALIGN_RIGHT; |
| } else { |
| return CellStyle.ALIGN_CENTER; |
| } |
| } |
| |
| @Override |
| public boolean isGeneratedColumn(final Object propId) throws IllegalArgumentException { |
| if (null == heldTable) { |
| return false; |
| } |
| return heldTable.getColumnGenerator(propId) != null; |
| } |
| |
| @Override |
| public Property getPropertyForGeneratedColumn(final Object propId, final Object rootItemId) throws IllegalArgumentException { |
| Property prop = null; |
| if (null != heldTable) { |
| final FilterTable.ColumnGenerator tcg = heldTable.getColumnGenerator(propId); |
| if (tcg instanceof ExportableColumnGenerator) { |
| prop = ((ExportableColumnGenerator) tcg).getGeneratedProperty(rootItemId, propId); |
| } |
| } |
| return prop; |
| } |
| |
| @Override |
| public Class<?> getPropertyTypeForGeneratedColumn(final Object propId) throws IllegalArgumentException { |
| Class<?> classType = String.class; |
| if (null != heldTable) { |
| final FilterTable.ColumnGenerator tcg = heldTable.getColumnGenerator(propId); |
| if (tcg instanceof ExportableColumnGenerator) { |
| classType = ((ExportableColumnGenerator) tcg).getType(); |
| } |
| } |
| return classType; |
| } |
| |
| @Override |
| public boolean isExportableFormattedProperty() { |
| if (null == heldTable) { |
| return false; |
| } |
| return heldTable instanceof ExportableFormattedProperty; |
| } |
| |
| @Override |
| public boolean isColumnCollapsed(Object propertyId) { |
| if (null == heldTable) { |
| return false; |
| } |
| return heldTable.isColumnCollapsed(propertyId); |
| } |
| |
| @Override |
| public UI getUI() { |
| if (null != heldTable) { |
| return heldTable.getUI(); |
| } |
| if (null != heldGrid) { |
| return heldGrid.getUI(); |
| } |
| return UI.getCurrent(); |
| } |
| |
| @Override |
| public String getColumnHeader(Object propertyId) { |
| if (null == heldTable) { |
| if (null != heldGrid) { |
| Grid.Column c = heldGrid.getColumn(propertyId); |
| return c.getHeaderCaption(); |
| } else { |
| return propertyId.toString(); |
| } |
| } |
| return heldTable.getColumnHeader(propertyId).replace(" ", "\n"); |
| } |
| |
| @Override |
| public Container getContainerDataSource() { |
| return this.container; |
| } |
| |
| @Override |
| public String getFormattedPropertyValue(Object rowId, Object colId, Property property) { |
| if (null == heldTable) { |
| return ""; |
| } |
| return ((ExportableFormattedProperty) heldTable).getFormattedPropertyValue(rowId, colId, property); |
| } |
| } |