2014-01-13 15:52:52 +01:00
package org.gcube.portlets.user.workspace.client.view.trash ;
2014-01-14 14:26:41 +01:00
import java.util.ArrayList ;
2014-01-13 15:52:52 +01:00
import java.util.Arrays ;
import java.util.List ;
import org.gcube.portlets.user.workspace.client.ConstantsExplorer ;
import org.gcube.portlets.user.workspace.client.interfaces.GXTFolderItemTypeEnum ;
import org.gcube.portlets.user.workspace.client.model.FileModel ;
import org.gcube.portlets.user.workspace.client.resources.Resources ;
import com.extjs.gxt.ui.client.Style.ButtonScale ;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment ;
2014-01-14 14:26:41 +01:00
import com.extjs.gxt.ui.client.data.BaseModelData ;
import com.extjs.gxt.ui.client.dnd.GridDropTarget ;
2014-01-13 15:52:52 +01:00
import com.extjs.gxt.ui.client.event.ButtonEvent ;
2014-01-14 14:26:41 +01:00
import com.extjs.gxt.ui.client.event.DNDEvent ;
2014-01-13 15:52:52 +01:00
import com.extjs.gxt.ui.client.event.SelectionListener ;
import com.extjs.gxt.ui.client.store.GroupingStore ;
import com.extjs.gxt.ui.client.store.ListStore ;
import com.extjs.gxt.ui.client.store.Record ;
import com.extjs.gxt.ui.client.widget.ContentPanel ;
import com.extjs.gxt.ui.client.widget.LayoutContainer ;
import com.extjs.gxt.ui.client.widget.button.ToggleButton ;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig ;
import com.extjs.gxt.ui.client.widget.grid.ColumnData ;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel ;
import com.extjs.gxt.ui.client.widget.grid.Grid ;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer ;
import com.extjs.gxt.ui.client.widget.grid.GroupingView ;
import com.extjs.gxt.ui.client.widget.grid.filters.GridFilters ;
import com.extjs.gxt.ui.client.widget.grid.filters.StringFilter ;
import com.extjs.gxt.ui.client.widget.layout.FitLayout ;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar ;
import com.google.gwt.i18n.client.NumberFormat ;
public class TrashInfoContainer extends LayoutContainer {
/ * *
*
* /
2014-01-14 14:26:41 +01:00
2014-01-13 15:52:52 +01:00
private Grid < FileModel > grid ;
private ContentPanel cp ;
private GroupingStore < FileModel > store = new GroupingStore < FileModel > ( ) ;
private boolean groupingEnabled ;
private ListStore < FileModel > typeStoreOperation = new ListStore < FileModel > ( ) ;
private NumberFormat number = ConstantsExplorer . numberFormatterKB ;
2014-01-14 14:26:41 +01:00
private GridDropTarget gridDropTarget ;
2014-01-13 15:52:52 +01:00
public TrashInfoContainer ( ) {
initContentPanel ( ) ;
initGrid ( ) ;
createToolBar ( ) ;
2014-01-14 14:26:41 +01:00
initDropTarget ( ) ;
}
/ * *
*
* /
private void initDropTarget ( ) {
this . gridDropTarget = new GridDropTarget ( grid ) {
/ * ( non - Javadoc )
* @see com . extjs . gxt . ui . client . dnd . GridDropTarget # onDragDrop ( com . extjs . gxt . ui . client . event . DNDEvent )
* /
@Override
protected void onDragDrop ( DNDEvent e ) {
// TODO Auto-generated method stub
List < FileModel > listFileModel = getDragSource ( e ) ;
for ( FileModel fileModel : listFileModel ) {
fileModel . setIcon ( ) ;
}
System . out . println ( " ** Trash Event move handleInsertDrop is completed " ) ;
super . onDragDrop ( e ) ;
}
private List < FileModel > getDragSource ( DNDEvent event ) {
List < FileModel > listDraggedFile = new ArrayList < FileModel > ( ) ;
if ( event . getData ( ) ! = null ) {
List < BaseModelData > listItemsSource = event . getData ( ) ;
System . out . println ( " Trash - Number of move " + listItemsSource . size ( ) ) ;
FileModel sourceFileModel = null ; //for print
for ( int i = 0 ; i < listItemsSource . size ( ) ; i + + ) {
/ *
if ( listItemsSource . get ( i ) instanceof TreeStoreModel ) {
System . out . println ( " qui 1 " ) ;
TreeStoreModel itemSource = ( TreeStoreModel ) listItemsSource . get ( i ) ;
listDraggedFile . add ( ( FileModel ) itemSource . getModel ( ) ) ;
sourceFileModel = ( FileModel ) itemSource . getModel ( ) ;
if ( sourceFileModel . getParentFileModel ( ) ! = null )
System . out . println ( " Trash Source Name " + sourceFileModel . getName ( ) + " id " + sourceFileModel . getIdentifier ( ) + " end drag " + " Parent Name: " + sourceFileModel . getParentFileModel ( ) . getName ( ) + " id " + sourceFileModel . getParentFileModel ( ) . getIdentifier ( ) ) ;
else
System . out . println ( " Trash Source Name " + sourceFileModel . getName ( ) + " id " + sourceFileModel . getIdentifier ( ) + " end drag " ) ;
} else
{
System . out . println ( " qui 2 " ) ;
System . out . println ( " class " + listItemsSource . get ( i ) . getClass ( ) ) ;
} * /
// System.out.println("Trash Child count: " + itemSource.getChildCount());
}
}
return listDraggedFile ;
}
} ;
2014-01-13 15:52:52 +01:00
}
private void initContentPanel ( ) {
setLayout ( new FitLayout ( ) ) ;
getAriaSupport ( ) . setPresentation ( true ) ;
cp = new ContentPanel ( ) ;
cp . setHeaderVisible ( false ) ;
cp . setBodyBorder ( true ) ;
cp . setLayout ( new FitLayout ( ) ) ;
cp . setButtonAlign ( HorizontalAlignment . CENTER ) ;
// cp.getHeader().setIconAltText("Grid Icon");
// cp.setSize(550, 280);
add ( cp ) ;
}
private void createToolBar ( ) {
ToolBar bar = new ToolBar ( ) ;
final ToggleButton buttonGrouping = new ToggleButton ( " " , Resources . getIconGridView ( ) ) ;
buttonGrouping . setToolTip ( " Grouping by Type " ) ;
buttonGrouping . setScale ( ButtonScale . SMALL ) ;
buttonGrouping . toggle ( true ) ;
buttonGrouping . addSelectionListener ( new SelectionListener < ButtonEvent > ( ) {
@Override
public void componentSelected ( ButtonEvent ce ) {
if ( buttonGrouping . isPressed ( ) )
enableGrouping ( ) ;
else
disableGrouping ( ) ;
}
} ) ;
2014-01-14 14:26:41 +01:00
// bar.add(buttonGrouping);
2014-01-13 15:52:52 +01:00
cp . setTopComponent ( bar ) ;
}
public void initGrid ( ) {
2014-01-14 14:26:41 +01:00
// store.groupBy(ConstantsExplorer.SHORTCUTCATEGORY);
// groupingEnabled = true;
2014-01-13 15:52:52 +01:00
2014-01-14 14:26:41 +01:00
ColumnConfig icon = new ColumnConfig ( ConstantsExplorer . ICON , " " , 25 ) ;
2014-01-13 15:52:52 +01:00
ColumnConfig name = createSortableColumnConfig ( ConstantsExplorer . NAME , ConstantsExplorer . NAME , 300 ) ;
ColumnConfig type = createSortableColumnConfig ( ConstantsExplorer . TYPE , ConstantsExplorer . TYPE , 50 ) ;
2014-01-14 14:26:41 +01:00
// ColumnConfig category = createSortableColumnConfig(ConstantsExplorer.SHORTCUTCATEGORY, ConstantsExplorer.SHORTCUTCATEGORY, 100);
2014-01-13 15:52:52 +01:00
ColumnConfig ownerFullName = createSortableColumnConfig ( ConstantsExplorer . OWNERFULLNAME , ConstantsExplorer . OWNER , 100 ) ;
2014-01-14 14:26:41 +01:00
ColumnModel cm = new ColumnModel ( Arrays . asList ( icon , name , type , ownerFullName ) ) ;
2014-01-13 15:52:52 +01:00
final ColumnModel columnModel = cm ;
grid = new Grid < FileModel > ( this . store , cm ) ;
GroupingView view = new GroupingView ( ) ;
view . setShowGroupedColumn ( true ) ;
this . grid . setView ( view ) ;
2014-01-14 14:26:41 +01:00
// view.setGroupRenderer(new GridGroupRenderer() {
// public String render(GroupColumnData data) {
// String f = columnModel.getColumnById(data.field).getHeader();
// String l = data.models.size() == 1 ? "Item" : "Items";
// return f + ": " + data.group + " (" + data.models.size() + " "
// + l + ")";
// }
// });
2014-01-13 15:52:52 +01:00
2014-01-14 14:26:41 +01:00
GridCellRenderer < FileModel > folderRender = new GridCellRenderer < FileModel > ( ) {
2014-01-13 15:52:52 +01:00
@Override
2014-01-14 14:26:41 +01:00
public String render ( FileModel model , String property , ColumnData config , int rowIndex , int colIndex , ListStore < FileModel > store , Grid < FileModel > grid ) {
2014-01-13 15:52:52 +01:00
String val = model . get ( property ) ;
String color = " black " ;
if ( val ! = null & & val . equals ( GXTFolderItemTypeEnum . FOLDER . toString ( ) ) ) {
// color = "#EEC900";
return " <span qtitle=' " + columnModel . getColumnById ( property ) . getHeader ( ) + " ' qtip=' " + val + " ' style='font-weight: bold;color: " + color + " '> " + val + " </span> " ;
} else {
if ( val = = null )
val = " " ;
return " <span qtitle=' " + columnModel . getColumnById ( property ) . getHeader ( ) + " ' qtip=' " + val + " ' style='color: " + color + " '> " + val + " </span> " ;
}
}
} ;
type . setRenderer ( folderRender ) ;
GridFilters filters = new GridFilters ( ) ;
filters . setLocal ( true ) ;
StringFilter nameFilter = new StringFilter ( ConstantsExplorer . NAME ) ;
StringFilter authorFilter = new StringFilter ( ConstantsExplorer . TYPE ) ;
filters . addFilter ( nameFilter ) ;
filters . addFilter ( authorFilter ) ;
grid . addPlugin ( filters ) ;
grid . setBorders ( true ) ;
grid . setStripeRows ( true ) ;
grid . getView ( ) . setAutoFill ( true ) ;
grid . setColumnLines ( true ) ;
grid . setColumnReordering ( true ) ;
grid . setStyleAttribute ( " borderTop " , " none " ) ;
cp . add ( grid ) ;
2014-01-14 14:26:41 +01:00
2014-01-13 15:52:52 +01:00
}
public void setPanelSize ( int width , int height ) {
if ( width > 0 & & height > 0 & & grid ! = null ) {
cp . setSize ( width , height ) ;
// grid.setSize(width, height);
}
}
public TrashInfoContainer ( List < FileModel > trashFiles ) {
initContentPanel ( ) ;
initGrid ( ) ;
updateTrash ( trashFiles ) ;
}
public void disableGrouping ( ) {
GroupingStore < FileModel > groupingStore = null ;
if ( store instanceof GroupingStore ) {
groupingStore = ( GroupingStore < FileModel > ) store ;
if ( groupingStore ! = null ) {
groupingStore . clearGrouping ( ) ;
}
this . groupingEnabled = false ;
}
}
public void enableGrouping ( ) {
GroupingStore < FileModel > groupingStore = null ;
if ( store instanceof GroupingStore ) {
groupingStore = ( GroupingStore < FileModel > ) store ;
if ( groupingStore ! = null ) {
2014-01-14 14:26:41 +01:00
groupingStore . groupBy ( ConstantsExplorer . SHORTCUTCATEGORY ) ;
2014-01-13 15:52:52 +01:00
}
this . groupingEnabled = true ;
}
}
public void updateTrash ( List < FileModel > trashFiles ) {
store . removeAll ( ) ;
typeStoreOperation . removeAll ( ) ;
store . add ( trashFiles ) ;
}
private void updateStore ( ListStore < FileModel > store ) {
resetStore ( ) ;
this . grid . getStore ( ) . add ( store . getModels ( ) ) ;
}
public void resetStore ( ) {
this . grid . getStore ( ) . removeAll ( ) ;
}
public ColumnConfig createSortableColumnConfig ( String id , String name ,
int width ) {
ColumnConfig columnConfig = new ColumnConfig ( id , name , width ) ;
columnConfig . setSortable ( true ) ;
return columnConfig ;
}
public void setHeaderTitle ( String title ) {
cp . setHeading ( title ) ;
// cp.layout();
}
public boolean isGroupingEnabled ( ) {
return groupingEnabled ;
}
public void setGroupingEnabled ( boolean groupingEnabled ) {
this . groupingEnabled = groupingEnabled ;
}
/ * *
*
* @param identifier
* @return
* /
public FileModel getFileModelByIdentifier ( String identifier ) {
return store . findModel ( ConstantsExplorer . IDENTIFIER , identifier ) ;
}
/ * *
*
* @param identifier ( MANDATORY )
* @return
* /
public boolean deleteItem ( String identifier ) {
FileModel fileTarget = getFileModelByIdentifier ( identifier ) ;
if ( fileTarget ! = null ) {
return deleteItem ( fileTarget ) ;
}
else
System . out . println ( " Delete Error: file target with " + identifier + " identifier not exist in store " ) ;
return false ;
}
/ * *
*
* @param fileTarget ( MANDATORY )
* @return
* /
private boolean deleteItem ( FileModel fileTarget ) {
Record record = store . getRecord ( fileTarget ) ;
if ( record ! = null ) {
FileModel item = ( FileModel ) record . getModel ( ) ;
store . remove ( item ) ;
return true ;
} else
System . out . println ( " Record Error: file target with "
+ fileTarget . getIdentifier ( )
+ " identifier not exist in store " ) ;
return false ;
}
}