Added GridAndCellSelectionModel

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/portlets/user/tabular-data-widgetx@93572 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Giancarlo Panichi 2014-03-26 13:57:56 +00:00 committed by Giancarlo Panichi
parent 69078a132b
commit f4f99ddf82
2 changed files with 268 additions and 76 deletions

View File

@ -11,6 +11,8 @@ import java.util.Map;
import org.gcube.portlets.user.td.widgetcommonevent.client.event.GridHeaderColumnMenuItemEvent;
import org.gcube.portlets.user.td.widgetcommonevent.client.type.GridHeaderColumnMenuItemType;
import org.gcube.portlets.user.td.widgetcommonevent.shared.CellData;
import org.gcube.portlets.user.tdwx.client.config.GridAndCellSelectionModel;
import org.gcube.portlets.user.tdwx.client.config.Row;
import org.gcube.portlets.user.tdwx.client.config.TableViewConfig;
import org.gcube.portlets.user.tdwx.client.config.TabularDataGridViewConfig;
@ -56,7 +58,6 @@ import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer.VerticalLayoutData;
import com.sencha.gxt.widget.core.client.event.HeaderContextMenuEvent;
import com.sencha.gxt.widget.core.client.event.HeaderContextMenuEvent.HeaderContextMenuHandler;
import com.sencha.gxt.widget.core.client.grid.CellSelectionModel;
import com.sencha.gxt.widget.core.client.grid.ColumnConfig;
import com.sencha.gxt.widget.core.client.grid.ColumnModel;
import com.sencha.gxt.widget.core.client.grid.Grid;
@ -219,35 +220,64 @@ public class TabularDataXGridPanel extends ContentPanel {
*
* @return
*/
public String getSelectedCell() {
public CellData getSelectedCell() {
if (grid != null && grid.getSelectionModel() != null) {
CellSelection<DataRow> cell=((CellSelectionModel<DataRow>)grid.getSelectionModel()).getSelectCell();
CellSelection<DataRow> cell = ((GridAndCellSelectionModel<DataRow>) grid
.getSelectionModel()).getCellSelected();
if (cell != null) {
DataRow dataRow = cell.getModel();
Row row = new Row(dataRow, keys);
ColumnModel<DataRow> columnModel = grid
.getColumnModel();
ColumnModel<DataRow> columnModel = grid.getColumnModel();
List<ColumnConfig<DataRow, ?>> columns = columnModel
.getColumns();
ColumnConfig<DataRow, ?> col=columns.get(cell.getCell());
DataRowColumnConfig<?> c = (DataRowColumnConfig<?>) col;
ColumnDefinition cd = c.getDefinition();
// Retrive ColumnId
DataRowColumnConfig<?> columnId = null;
boolean columnIdRetrieved = false;
for (ColumnConfig<DataRow, ?> col : columns) {
columnId = (DataRowColumnConfig<?>) col;
ColumnType ctype = columnId.getDefinition().getType();
if (ctype == ColumnType.COLUMNID) {
columnIdRetrieved = true;
break;
}
}
if (columnIdRetrieved) {
Log.debug("ColumnId Retrieved");
ColumnDefinition columnIdSelected = columnId.getDefinition();
// Retrive ColumnSelected
ColumnConfig<DataRow, ?> colSelected = columns.get(cell
.getCell());
DataRowColumnConfig<?> cSelected = (DataRowColumnConfig<?>) colSelected;
ColumnDefinition cdSelected = cSelected.getDefinition();
String cellValue = row
.getFieldAsText(cdSelected.getLabel());
String rowS = row.getFieldAsText(cd.getLabel());
String columnIdValue = row
.getFieldAsText(columnIdSelected.getLabel());
String cellS="Selected Cell: ["+cell.getRow()+", "+cell.getCell()+"] "+rowS;
Log.debug(cellS);
CellData tdCell=new CellData(cellValue, cdSelected.getId(), cdSelected.getColumnLocalId(),
cdSelected.getLabel(), columnIdValue, cell.getRow(),cell.getCell());
return rowS;
Log.debug("Selected Cell: "+tdCell);
return tdCell;
} else {
Log.debug("No ColumnId Retrieved");
return null;
}
} else {
Log.debug("No cell selected");
}
} else {
Log.debug("No gridSelectionModel set");
}
return null;
}
/**
* getSelectedRowsId
*
@ -255,11 +285,9 @@ public class TabularDataXGridPanel extends ContentPanel {
*/
public ArrayList<String> getSelectedRowsId() {
if (grid != null) {
ColumnModel<DataRow> columnModel = grid
.getColumnModel();
ColumnModel<DataRow> columnModel = grid.getColumnModel();
List<ColumnConfig<DataRow, ?>> columns = columnModel
.getColumns();
List<ColumnConfig<DataRow, ?>> columns = columnModel.getColumns();
DataRowColumnConfig<?> c = null;
boolean columnIdRetrieved = false;
for (ColumnConfig<DataRow, ?> col : columns) {
@ -273,10 +301,9 @@ public class TabularDataXGridPanel extends ContentPanel {
if (columnIdRetrieved) {
ColumnDefinition cd = c.getDefinition();
Log.debug("CD - Definition:" + cd.getId()
+ " ColumnLocalId:" + cd.getColumnLocalId()
+ " Label:" + cd.getLabel() + " Key:"
+ cd.getKey());
Log.debug("CD - Definition:" + cd.getId() + " ColumnLocalId:"
+ cd.getColumnLocalId() + " Label:" + cd.getLabel()
+ " Key:" + cd.getKey());
List<Row> rowsSelected = getSelectedRows();
Log.debug("Retriving rows selected");
String rowS = "";
@ -302,8 +329,6 @@ public class TabularDataXGridPanel extends ContentPanel {
}
/**
*
*/
@ -410,18 +435,21 @@ public class TabularDataXGridPanel extends ContentPanel {
// liveGridView.setRowHeight(19);
grid = new Grid<DataRow>(store, columnModel);
grid.setSelectionModel(new CellSelectionModel<DataRow>());
GridAndCellSelectionModel<DataRow> sm = new GridAndCellSelectionModel<DataRow>();
grid.setSelectionModel(sm);
grid.setLoadMask(true);
grid.setLoader(loader);
grid.setView(liveGridView);
// TODO disabled editing rows
/*editing = new GridInlineEditing<DataRow>(grid);
editing.setClicksToEdit(ClicksToEdit.TWO);
for (ColumnConfig<DataRow, ?> c : columnsConfig) {
ColumnConfigGenerator.setEditor(editing, c);
}*/
/*
* editing = new GridInlineEditing<DataRow>(grid);
* editing.setClicksToEdit(ClicksToEdit.TWO);
*
* for (ColumnConfig<DataRow, ?> c : columnsConfig) {
* ColumnConfigGenerator.setEditor(editing, c); }
*/
// numberer.initPlugin(grid);
@ -463,12 +491,12 @@ public class TabularDataXGridPanel extends ContentPanel {
// TODO Disabled editing rows
/*
editing = new GridInlineEditing<DataRow>(grid);
editing.setClicksToEdit(ClicksToEdit.TWO);
for (ColumnConfig<DataRow, ?> c : columnsConfig) {
ColumnConfigGenerator.setEditor(editing, c);
}*/
* editing = new GridInlineEditing<DataRow>(grid);
* editing.setClicksToEdit(ClicksToEdit.TWO);
*
* for (ColumnConfig<DataRow, ?> c : columnsConfig) {
* ColumnConfigGenerator.setEditor(editing, c); }
*/
// TODO workaround: the loader is called only if the scroll bar is
// in the middle
@ -573,7 +601,6 @@ public class TabularDataXGridPanel extends ContentPanel {
return columnId;
}
/**
*
* @param rows
@ -581,26 +608,21 @@ public class TabularDataXGridPanel extends ContentPanel {
public void addRow(ArrayList<String> rows) {
// TODO Disabled AddROW
/*
if (grid != null && editing != null) {
List<ColumnKey> keys = tableDefinition.getKeys();
DataRow dataRow = new DataRow(keys.size());
for (ColumnKey key : keys) {
dataRow.set(key, null);
}
editing.cancelEditing();
int pos = 0;
if (rows != null && rows.size() > 0) {
pos = new Integer(rows.get(0));
}
store.add(pos, dataRow);
int row = store.indexOf(dataRow);
editing.startEditing(new GridCell(row, pos));
}*/
* if (grid != null && editing != null) {
*
* List<ColumnKey> keys = tableDefinition.getKeys();
*
* DataRow dataRow = new DataRow(keys.size()); for (ColumnKey key :
* keys) { dataRow.set(key, null); }
*
* editing.cancelEditing(); int pos = 0; if (rows != null && rows.size()
* > 0) { pos = new Integer(rows.get(0)); }
*
* store.add(pos, dataRow);
*
* int row = store.indexOf(dataRow); editing.startEditing(new
* GridCell(row, pos)); }
*/
}
protected class ExtendedLiveGridView extends LiveGridView<DataRow> {

View File

@ -0,0 +1,170 @@
package org.gcube.portlets.user.tdwx.client.config;
import java.util.Collections;
import com.google.gwt.dom.client.Element;
import com.google.gwt.user.client.Event;
import com.sencha.gxt.core.client.Style.SelectionMode;
import com.sencha.gxt.widget.core.client.event.RowClickEvent;
import com.sencha.gxt.widget.core.client.event.RowMouseDownEvent;
import com.sencha.gxt.widget.core.client.event.XEvent;
import com.sencha.gxt.widget.core.client.grid.GridSelectionModel;
import com.sencha.gxt.widget.core.client.selection.CellSelection;
/**
* Extends the GridSelectionModel to retrieve the selected cell.
* CellSelectionModel is not suitable.
*
* @author "Giancarlo Panichi"
* <a href="mailto:g.panichi@isti.cnr.it">g.panichi@isti.cnr.it</a>
*
* @param <M>
*/
public class GridAndCellSelectionModel<M> extends GridSelectionModel<M> {
protected CellSelection<M> cellSelected;
public GridAndCellSelectionModel() {
super();
}
/**
* Handles a row click event. The row click event is responsible for adding
* to a selection in multiple selection mode.
*
* @param event
* the row click event
*/
@Override
protected void handleRowClick(RowClickEvent event) {
if (Element.is(event.getEvent().getEventTarget())
&& !grid.getView().isSelectableTarget(
Element.as(event.getEvent().getEventTarget()))) {
return;
}
if (isLocked()) {
return;
}
if (fireSelectionChangeOnClick) {
fireSelectionChange();
fireSelectionChangeOnClick = false;
}
XEvent xe = event.getEvent().<XEvent> cast();
int rowIndex = event.getRowIndex();
int colIndex = event.getColumnIndex();
if (rowIndex == -1) {
deselectAll();
return;
}
if (selectionMode == SelectionMode.MULTI) {
M sel = listStore.get(rowIndex);
cellSelected=new CellSelection<M>(sel, rowIndex,colIndex);
if (xe.getCtrlOrMetaKey() && isSelected(sel)) {
doDeselect(Collections.singletonList(sel), false);
} else if (xe.getCtrlOrMetaKey()) {
grid.getView().focusCell(rowIndex, colIndex, false);
doSelect(Collections.singletonList(sel), true, false);
} else if (isSelected(sel) && !event.getEvent().getShiftKey()
&& !xe.getCtrlOrMetaKey() && selected.size() > 1) {
grid.getView().focusCell(rowIndex, colIndex, false);
doSelect(Collections.singletonList(sel), false, false);
}
}
}
/**
* Handles a row mouse down event. The row mouse down event is responsible
* for initiating a selection.
*
* @param event
* the row mouse down event
*/
@Override
protected void handleRowMouseDown(RowMouseDownEvent event) {
if (Element.is(event.getEvent().getEventTarget())
&& !grid.getView().isSelectableTarget(
Element.as(event.getEvent().getEventTarget()))) {
return;
}
if (isLocked()) {
return;
}
int rowIndex = event.getRowIndex();
int colIndex = event.getColumnIndex();
if (rowIndex == -1) {
return;
}
mouseDown = true;
XEvent e = event.getEvent().<XEvent> cast();
if (event.getEvent().getButton() == Event.BUTTON_RIGHT) {
if (selectionMode != SelectionMode.SINGLE
&& isSelected(listStore.get(rowIndex))) {
M sel=listStore.get(rowIndex);
cellSelected=new CellSelection<M>(sel, rowIndex,colIndex);
return;
}
grid.getView().focusCell(rowIndex, colIndex, false);
select(rowIndex, false);
} else {
M sel = listStore.get(rowIndex);
if (selectionMode == SelectionMode.SIMPLE) {
if (!isSelected(sel)) {
grid.getView().focusCell(rowIndex, colIndex, false);
cellSelected=new CellSelection<M>(sel, rowIndex,colIndex);
select(sel, true);
}
} else if (selectionMode == SelectionMode.SINGLE) {
if (e.getCtrlOrMetaKey() && isSelected(sel)) {
deselect(sel);
} else if (!isSelected(sel)) {
grid.getView().focusCell(rowIndex, colIndex, false);
cellSelected=new CellSelection<M>(sel, rowIndex,colIndex);
select(sel, false);
}
} else if (!e.getCtrlOrMetaKey()) {
if (event.getEvent().getShiftKey() && lastSelected != null) {
int last = listStore.indexOf(lastSelected);
int index = rowIndex;
grid.getView().focusCell(index, colIndex, false);
select(last, index, e.getCtrlOrMetaKey());
} else if (!isSelected(sel)) {
grid.getView().focusCell(rowIndex, colIndex, false);
cellSelected=new CellSelection<M>(sel, rowIndex,colIndex);
doSelect(Collections.singletonList(sel), false, false);
}
} else {
// EXTGWT-2019 when inline editing for grid and tree grid with
// row based
// selection model focus is not
// being moved to grid when clicking on another cell in same row
// as
// active edit and therefore
// field is not bluring and firing change event
grid.getView().focus();
}
}
mouseDown = false;
}
public CellSelection<M> getCellSelected() {
return cellSelected;
}
public void setCellSelected(CellSelection<M> cellSelected) {
this.cellSelected = cellSelected;
}
}