package org.argeo.cms.swt.widgets;
-import java.util.function.Consumer;
-
+import org.argeo.api.cms.ux.CmsIcon;
+import org.argeo.cms.swt.CmsSwtTheme;
import org.argeo.cms.swt.CmsSwtUtils;
+import org.argeo.cms.ux.widgets.Column;
import org.argeo.cms.ux.widgets.TabularPart;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
-import org.eclipse.swt.events.SelectionListener;
+import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
+import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
/** {@link TabularPart} implementation based on a {@link Table}. */
-public class SwtTabularPart implements TabularPart {
- private Composite area;
-
+public class SwtTabularPart<INPUT, T> extends AbstractSwtPart<INPUT, T> {
+ private static final long serialVersionUID = -1114155772446357750L;
private final Table table;
+ private TabularPart<INPUT, T> tabularPart;
+
+ private CmsSwtTheme theme;
- private Consumer<Object> onSelected;
- private Consumer<Object> onAction;
+ public SwtTabularPart(Composite parent, int style, TabularPart<INPUT, T> tabularPart) {
+ super(parent, style, tabularPart);
+ theme = CmsSwtUtils.getCmsTheme(parent);
- public SwtTabularPart(Composite parent, int style) {
- area = new Composite(parent, style);
- area.setLayout(CmsSwtUtils.noSpaceGridLayout());
- table = new Table(area, SWT.VIRTUAL | SWT.BORDER);
+ table = new Table(this, SWT.VIRTUAL | SWT.BORDER);
+ table.setLinesVisible(true);
+ table.setLayoutData(CmsSwtUtils.fillAll());
+
+ this.tabularPart = tabularPart;
}
@Override
TableItem item = (TableItem) event.item;
refreshItem(item);
});
- table.setItemCount(getItemCount());
+ table.setItemCount(tabularPart.getItemCount());
+ for (int i = 0; i < tabularPart.getColumnCount(); i++) {
+ TableColumn swtColumn = new TableColumn(table, SWT.NONE);
+ swtColumn.setWidth(tabularPart.getColumn(i).getWidth());
+ }
CmsSwtUtils.fill(table);
- table.addSelectionListener(new SelectionListener() {
- private static final long serialVersionUID = -5225905921522775948L;
-
- @Override
- public void widgetSelected(SelectionEvent e) {
- if (onSelected != null)
- onSelected.accept(e.item.getData());
- }
-
- @Override
- public void widgetDefaultSelected(SelectionEvent e) {
- if (onAction != null)
- onAction.accept(e.item.getData());
- }
- });
+ table.addSelectionListener(selectionListener);
}
- @Override
- public void setInput(Object data) {
- area.setData(data);
- refresh();
- }
-
- @Override
- public Object getInput() {
- return area.getData();
+ protected Object getDataFromEvent(SelectionEvent e) {
+ Object data = e.item.getData();
+ if (data == null)
+ data = tabularPart.getData(getTable().indexOf((TableItem) e.item));
+ return data;
}
protected void refreshItem(TableItem item) {
-
- }
-
- protected int getItemCount() {
- return 0;
+ int row = getTable().indexOf(item);
+ for (int i = 0; i < tabularPart.getColumnCount(); i++) {
+ Column<T> column = tabularPart.getColumn(i);
+ T data = tabularPart.getData(row);
+ item.setData(data);
+ String text = data != null ? column.getText(data) : "";
+ if (text != null)
+ item.setText(i, text);
+ CmsIcon icon = column.getIcon(data);
+ if (icon != null) {
+ Image image = theme.getSmallIcon(icon);
+ item.setImage(i, image);
+ }
+ }
}
protected Table getTable() {
return table;
}
- public void onSelected(Consumer<Object> onSelected) {
- this.onSelected = onSelected;
- }
-
- public void onAction(Consumer<Object> onAction) {
- this.onAction = onAction;
- }
-
}