Hi,
I'm working with an application intended for use with a touch screen. Each
time a row in a table is "picked" should it be selected or deselected
depending on the previous state, but all other rows should be as is. This is
the behavior if CTRL is pressed at the same time as the row is "picked". So
what we want to do is to handle every select event as if CTRL is pressed.
After some elaboration is our conclusion that the best way to a achieve such
behavior is to subclass Table and override all methods handling selection
and deselection. You can see our first implementation at the end of the
mail. It's not tested that much so it may contain errors. Anyway the reason
for this mail and my question is why is Table (and other composites) not
intended to be subclassed?
"IMPORTANT: This class is <em>not</em> intended to be subclassed."
I can't see the reason and this seams to be the best (and probably only)
solution to our issue.
Sincerely
/Robert
package my.viewer;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.SWT;
import java.util.List;
import java.util.ArrayList;
/**
* MultiSelectTable implement a table acting as if CTRL is pressed each time
* a row is selected in the table making it easier to work with a touch
screen.
*/
public class MultiSelectTable extends Table {
public final static int COLOR_NORMAL_BACKGROUND =
SWT.COLOR_LIST_BACKGROUND;
public final static int COLOR_NORMAL_TEXT = SWT.COLOR_LIST_FOREGROUND;
public final static int COLOR_SELECTED_BACKGROUND =
SWT.COLOR_LIST_SELECTION;
public final static int COLOR_SELCTED_TEXT =
SWT.COLOR_LIST_SELECTION_TEXT;
public MultiSelectTable(Composite composite, int i) {
super(composite, i);
}
public void deselect(int index) {
if (index>=0 && index<getItemCount())
deselectTableItem(getItem(index));
}
public void deselect(TableItem tableItem) {
deselectTableItem(tableItem);
}
publi c void deselect(int start, int end) {
for(int i=start;i<=end;i++) {
deselect(i);
}
}
public void deselect(int[] indices) {
for (int i = 0; i < indices.length; i++) {
int index = indices[i];
deselect(index);
}
}
public void deselectAll() {
TableItem[] tableItems =this.getItems();
for (int i = 0; i < tableItems.length; i++) {
TableItem tableItem = tableItems[i];
if
(tableItem.getBackground().equals(this.getDisplay().getSystemColor(COLOR_SELECTED_BACKGROUND)))
deselectTableItem(tableItem);
}
super.deselectAll();
}
public TableItem[] getSelection() {
TableItem[] allTableItems =this.getItems();
ArrayList tableItems=new ArrayList();
int cnt=0;
for (int i = 0; i < allTableItems.length; i++) {
TableItem tableItem = allTableItems[i];
if (isSelected(tableItem))
tableItems.add(tableItem);
}
return (TableItem[]) tableItems.toArray(new TableItem[0]);
}
public int getSelectionCount() {
TableItem[] tableItems =this.getItems();
int cnt=0;
for (int i = 0; i < tableItems.length; i++) {
TableItem tableItem = tableItems[i];
if (isSelected(tableItem))
cnt++;
}
return cnt;
}
public int[] getSelectionIndices() {
return super.getSelectionIndices();
}
public int getSelectionIndex() {
return super.getSelectionIndex();
}
public void selectAll() {
TableItem[] tableItems = getItems();
select(tableItems);
}
public void select(TableItem[] tableItems) {
for (int i = 0; i < tableItems.length; i++) {
TableItem tableItem = tableItems[i];
selectTableItem(tableItem);
}
}
public void select(int index) {
if (index>=0 && index<getItemCount())
selectTableItem(getItem(index));
}
public void select(TableItem tableItem) {
selectTableItem(tableItem);
}
public void select(int start, int end) {
for(int i=start;i<=end;i++){
select(i);
}
}
public void select(int [] indices) {
for (int i = 0; i < indices.length; i++) {
int index = indices[i];
select(index);
}
}
public boolean isSelected(int i) {
return isSelected(getItem(i));
}
public void setSelection(int index) {
deselectAll();
select(index);
}
public void setSelection(int start, int end) {
deselectAll();
select(start,end);
}
public void setSelection(int[] indices) {
deselectAll();
select(indices);
}
public void setSelection(TableItem[] tableItems) {
deselectAll();
select(tableItems);
}
public void showSelection() {
TableItem tableItem= getFirstSelectedItem();
if (tableItem!=null)
showItem(tableItem);
}
private TableItem getFirstSelectedItem(){
TableItem firstItem=null;
TableItem[] tableItems =this.getItems();
int cnt=0;
for (int i = 0; i < tableItems.length; i++) {
TableItem tableItem = tableItems[i];
if (isSelected(tableItem)){
firstItem=tableItem;
break;
}
}
return firstItem;
}
public boolean isSelected(TableItem tableItem){
return
tableItem.getBackground().equals(this.getDisplay().getSystemColor(COLOR_SELECTED_BACKGROUND));
}
private void selectTableItem(TableItem tableItem){
tableItem.setBackground(tableItem.getDisplay().getSystemColor(COLOR_SELECTED_BACKGROUND));
tableItem.setForeground(tableItem.getDisplay().getSystemColor(COLOR_SELCTED_TEXT));
super.deselectAll();
}
private void deselectTableItem(TableItem tableItem){
tableItem.setBackground(tableItem.getDisplay().getSystemColor(COLOR_NORMAL_BACKGROUND));
tableItem.setForeground(tableItem.getDisplay().getSystemColor(COLOR_NORMAL_TEXT));
super.deselectAll();
}
// To shortcircuit the check done in the super class
protected void checkSubclass () {
}
}