}
});
store.load();
final GridPanel grid = new GridPanel( store,
cm );
grid.setStripeRows( true );
grid.addGridColumnListener( new GridColumnListener() {
public void onColumnMove(GridPanel grid,
int oldIndex,
int newIndex) {
if ( DecisionTableHandler.validateMove( dt,
oldIndex,
newIndex ) ) {
// Save any changes to the dt.data.
scrapeData( -1 );
DecisionTableHandler.moveColumn( dt,
oldIndex,
newIndex );
grid = doGrid();
} else {
// Refresh undoes the move.
refreshGrid();
ErrorPopup.showMessage( constants.CanNotMoveColumnsFromOneTypeGroupToAnother() );
}
}
public void onColumnResize(GridPanel grid,
int colIndex,
int newSize) {
// Nothing
}
} );
GroupingView gv = new GroupingView();
//to stretch it out
gv.setForceFit( true );
gv.setGroupTextTpl( "{text} ({[values.rs.length]} {[values.rs.length > 1 ? \"" //NON-NLS
+ constants.Items() + "\" : \"" + constants.Item() + "\"]})" );
grid.setView( gv );
grid.setStore( store );
int width = 900;
if ( cm.getColumnCount() > 10 ) {
width = 900 + (90 * (cm.getColumnCount() - 10));
}
grid.setWidth( width );
grid.setHeight( 500 );
//Add the cell listener for when the user wants to edit.
grid.addGridCellListener( new GridCellListenerAdapter() {
public void onCellDblClick(GridPanel grid,
int rowIndex,
int colIndex,
EventObject e) {
final String dataIdx = grid.getColumnModel().getDataIndex( colIndex );
final Record r = store.getAt( rowIndex );
String val = r.getAsString( dataIdx );
DTColumnConfig colConf = colMap.get( dataIdx );
String[] vals = dt.getValueList( colConf,
getSCE() );
if ( vals.length == 0 ) {
showTextEditor( e,
dataIdx,
r,
val,
colConf );
} else {
showDropDownEditor( e,
dataIdx,
r,
val,
vals );
}
}
} );
//remember any size changes
grid.addGridColumnListener( new GridColumnListenerAdapter() {
public void onColumnResize(GridPanel grid,
int colIndex,
int newSize) {
final String dta = grid.getColumnModel().getDataIndex( colIndex );
if ( dta.equals( "desc" ) ) { //NON-NLS
dt.descriptionWidth = newSize;
} else {
if ( colMap.containsKey( dta ) ) {
DTColumnConfig col = colMap.get( dta );
col.width = newSize;
}
}
}
} );
Toolbar tb = new Toolbar();
Menu menu = new Menu();
menu.addItem( new Item( constants.AddRow(),
new BaseItemListenerAdapter() {
public void onClick(BaseItem item,
EventObject e) {
Record r = recordDef.createRecord( new Object[recordDef.getFields().length] );
r.set( "num",
store.getRecords().length + 1 ); //NON-NLS
store.add( r );
}
} ) );
menu.addItem( new Item( constants.AddRowBeforeSelectedRow(),
new BaseItemListenerAdapter() {
public void onClick(BaseItem item,
EventObject e) {
Record[] selectedRows = grid.getSelectionModel().getSelections();
if ( selectedRows.length == 1 ) {
int selected = selectedRows[0].getAsInteger( "num" );
Record newRecord = recordDef.createRecord( new Object[recordDef.getFields().length] );
Record[] records = store.getRecords();
for ( int i = 0; i < records.length; i++ ) {
Record temp = records[i];
int num = temp.getAsInteger( "num" );
if ( num == selected ) {
newRecord.set( "num",
num ); //NON-NLS
temp.set( "num",
num + 1 ); //NON-NLS
store.addSorted( newRecord );
} else if ( num > selected ) {
temp.set( "num",
num + 1 ); //NON-NLS
}
}
} else {
ErrorPopup.showMessage( constants.PleaseSelectARow() );
}
}
} ) );
menu.addItem( new Item( constants.RemoveSelectedRowS(),
new BaseItemListenerAdapter() {
public void onClick(BaseItem item,
EventObject e) {
Record[] selected = grid.getSelectionModel().getSelections();
if ( com.google.gwt.user.client.Window.confirm( constants.AreYouSureYouWantToDeleteTheSelectedRowS() ) ) {
for ( int i = 0; i < selected.length; i++ ) {
store.remove( selected[i] );
}
renumber( store.getRecords() );
}
}
} ) );
menu.addItem( new Item( constants.CopySelectedRowS(),
new BaseItemListenerAdapter() {
public void onClick(BaseItem item,
EventObject e) {
Record[] selected = grid.getSelectionModel().getSelections();
for ( int i = 0; i < selected.length; i++ ) {
Record r = recordDef.createRecord( new Object[recordDef.getFields().length] );
Record orig = selected[i];
for ( int j = 0; j < fds.length; j++ ) {
r.set( fds[j].getName(),
orig.getAsString( fds[j].getName() ) );
}
store.add( r );
}
renumber( store.getRecords() );
}
} ) );
// Menu subMenu = new Menu();
// subMenu.addItem( new Item( "Move up",
// new BaseItemListenerAdapter() {
// public void onClick(BaseItem item,
// EventObject e) {
// Record[] selected = grid.getSelectionModel().getSelections();
// if ( selected.length == 1 ) {
// Record from = selected[0];
//
// grid.getSelectionModel().selectPrevious();
//
// selected = grid.getSelectionModel().getSelections();
// Record to = selected[0];
//
// changeRowPositions( from,
// to );
//
// } else {
// // TODO: Popup: Please select one.
// }
// }
// } ) );
// subMenu.addItem( new Item( "Move down",
// new BaseItemListenerAdapter() {
// public void onClick(BaseItem item,
// EventObject e) {
// Record[] selected = grid.getSelectionModel().getSelections();
// if ( selected.length == 1 ) {
// Record from = selected[0];
//
// grid.getSelectionModel().selectNext();
//
// selected = grid.getSelectionModel().getSelections();
// Record to = selected[0];
//
// changeRowPositions( from,
// to );
//
// } else {
// // TODO: Popup: Please select one.
// }
// }
// } ) );
// subMenu.addItem( new Item( "Switch selected",
// new BaseItemListenerAdapter() {
// public void onClick(BaseItem item,
// EventObject e) {
// Record[] selected = grid.getSelectionModel().getSelections();
// if ( selected.length == 2 ) {
//
// changeRowPositions( selected[0],
// selected[1] );
// } else {
// // TODO: Popup: Please select two.
// }
// }
// } ) );
// menu.addItem( new com.gwtext.client.widgets.menu.MenuItem( "Move",
// subMenu ) );
ToolbarMenuButton tbb = new ToolbarMenuButton( constants.Modify(),
menu );
tb.addButton( tbb );
grid.add( tb );
return grid;
}