/*******************************************************************************
* Copyright (c) 2009, 2010 Innovation Gate GmbH.
* 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:
* Innovation Gate GmbH - initial API and implementation
******************************************************************************/
package de.innovationgate.eclipse.utils.ui;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import de.innovationgate.eclipse.utils.SWTBotIDs;
import de.innovationgate.eclipse.utils.ui.model.BeanListTableModelCellModifier;
import de.innovationgate.eclipse.utils.ui.model.BeanListTableModelContentProvider;
import de.innovationgate.eclipse.utils.ui.model.BeanListTableModelLabelProvider;
import de.innovationgate.wga.model.BeanListTableModel;
import de.innovationgate.wga.model.BeanListTableModelListener;
public class EditableTableControl<T> extends Composite {
private Table _table;
private String[] _columnNames;
private TableColumn[] _columns;
private BeanListTableModel<T> _model;
private TableViewer _tableViewer;
private BeanListTableModelCellModifier _cellModifier;
protected GridData _btnLayout;
private Composite _buttonArea;
protected Map<String, Button> _buttons = new HashMap<String, Button>();
public static final String BUTTON_ADD = "BUTTON_ADD";
public static final String BUTTON_REMOVE = "BUTTON_REMOVE";
private BeanListTableModelListener _modelListener;
private int _tableStyle = SWT.BORDER;
public EditableTableControl(Composite parent, int style) {
super(parent, style);
}
public EditableTableControl(Composite parent, int style, int tableStyle) {
super(parent, style);
_tableStyle = tableStyle;
}
public void init(String[] columnNames, BeanListTableModel<T> model) {
_columnNames = columnNames;
_model = model;
_btnLayout = new GridData(GridData.FILL, GridData.VERTICAL_ALIGN_FILL, true , false);
createControls();
_modelListener = new BeanListTableModelListener() {
public void add(Object bean) {
updateTableItem(bean);
packColumns();
}
public void refresh(List beans) {
updateTableItems(beans);
packColumns();
}
public void remove(Object bean) {
packColumns();
}
public void update(Object bean) {
updateTableItem(bean);
packColumns();
}
};
_model.addListener(_modelListener);
updateTableItems(_model.getBeans());
}
protected void updateTableItems(List beans) {
for (Object bean : beans) {
updateTableItem(bean);
}
}
protected void updateTableItem(Object bean) {
TableItem items[] = getTable().getItems();
for (TableItem item : items) {
if (item.getData().equals(bean)) {
if (_model.isBeanEditable((T)bean)) {
item.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_WIDGET_FOREGROUND));
} else {
item.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_GRAY));
}
}
}
}
protected void packColumns() {
for (TableColumn column : getColumns()) {
column.pack();
}
}
private void createControls() {
GridLayout layout = new GridLayout();
layout.numColumns = 2;
layout.marginHeight = 0;
layout.marginWidth = 0;
this.setLayout(layout);
GridData tblLayoutData = new GridData(SWT.FILL, SWT.FILL, true, true);
//tblLayoutData.verticalSpan = 2;
_table = new Table(this, _tableStyle);
_table.setHeaderVisible(true);
_table.setLayoutData(tblLayoutData);
createColumns();
_tableViewer = new TableViewer(_table);
_cellModifier = new BeanListTableModelCellModifier(_tableViewer, _model);
_tableViewer.setCellModifier(_cellModifier);
_tableViewer.setContentProvider(new BeanListTableModelContentProvider());
_tableViewer.setLabelProvider(new BeanListTableModelLabelProvider(_model));
_tableViewer.setInput(_model);
if (isButtonAreaNeeded()) {
_buttonArea = new Composite(this, SWT.NONE);
layout = new GridLayout();
layout.numColumns = 1;
layout.marginHeight = 0;
layout.marginWidth = 0;
_buttonArea.setLayout(layout);
_buttonArea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, true));
createButtonArea(_buttonArea);
}
packColumns();
}
protected boolean isButtonAreaNeeded() {
return true;
}
private void createColumns() {
_columns = new TableColumn[_columnNames.length];
for (int i=0; i < _columnNames.length; i++) {
TableColumn column = new TableColumn(_table, SWT.NONE);
column.setText(_columnNames[i]);
_columns[i] = column;
}
}
protected void createButtonArea(Composite area) {
Button btnAdd = new Button(area, SWT.PUSH);
btnAdd.setText("add");
btnAdd.setLayoutData(GridDataFactory.copyData(_btnLayout));
btnAdd.pack();
btnAdd.setData(SWTBotIDs.WIDGET_KEY, SWTBotIDs.EDITABLE_TABLE_CONTROL_BUTTON_ADD);
_buttons.put(BUTTON_ADD, btnAdd);
Button btnRemove = new Button(area, SWT.PUSH);
btnRemove.setText("remove");
btnRemove.setLayoutData(GridDataFactory.copyData(_btnLayout));
btnRemove.pack();
btnRemove.setData(SWTBotIDs.WIDGET_KEY, SWTBotIDs.EDITABLE_TABLE_CONTROL_BUTTON_REMOVE);
_buttons.put(BUTTON_REMOVE, btnRemove);
}
public Table getTable() {
return _table;
}
public String[] getColumnNames() {
return _columnNames;
}
public TableColumn[] getColumns() {
return _columns;
}
public BeanListTableModel<T> getModel() {
return _model;
}
public TableViewer getTableViewer() {
return _tableViewer;
}
public BeanListTableModelCellModifier getCellModifier() {
return _cellModifier;
}
public Button getButton(String id) {
return _buttons.get(id);
}
@Override
public void dispose() {
if (_model != null && _modelListener != null) {
_model.removeListener(_modelListener);
}
}
}