2013-11-25 17:12:01 +01:00
package org.gcube.portlets.user.reportgenerator.client.dialog ;
import java.util.ArrayList ;
import java.util.List ;
import org.gcube.portlets.user.reportgenerator.client.ReportService ;
import org.gcube.portlets.user.reportgenerator.client.ReportServiceAsync ;
import org.gcube.portlets.user.reportgenerator.client.events.SelectedReportEvent ;
import org.gcube.portlets.user.reportgenerator.shared.VMEReportBean ;
import org.gcube.portlets.user.reportgenerator.shared.VMETypeIdentifier ;
import com.extjs.gxt.ui.client.event.ButtonEvent ;
2013-12-17 19:19:51 +01:00
import com.extjs.gxt.ui.client.event.Events ;
import com.extjs.gxt.ui.client.event.GridEvent ;
import com.extjs.gxt.ui.client.event.Listener ;
2013-11-25 17:12:01 +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.Store ;
import com.extjs.gxt.ui.client.widget.ContentPanel ;
2014-07-02 15:12:44 +02:00
import com.extjs.gxt.ui.client.widget.Html ;
2013-11-25 17:12:01 +01:00
import com.extjs.gxt.ui.client.widget.Window ;
import com.extjs.gxt.ui.client.widget.button.Button ;
import com.extjs.gxt.ui.client.widget.form.StoreFilterField ;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig ;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel ;
import com.extjs.gxt.ui.client.widget.grid.Grid ;
2014-01-28 17:31:13 +01:00
import com.extjs.gxt.ui.client.widget.grid.GridGroupRenderer ;
import com.extjs.gxt.ui.client.widget.grid.GroupColumnData ;
2013-11-25 17:12:01 +01:00
import com.extjs.gxt.ui.client.widget.grid.GroupingView ;
import com.extjs.gxt.ui.client.widget.layout.FitLayout ;
import com.google.gwt.core.client.GWT ;
import com.google.gwt.dom.client.Style.Unit ;
import com.google.gwt.event.shared.HandlerManager ;
import com.google.gwt.user.client.rpc.AsyncCallback ;
import com.google.gwt.user.client.ui.HasVerticalAlignment ;
import com.google.gwt.user.client.ui.HorizontalPanel ;
2014-01-28 17:31:13 +01:00
public class SelectVMEReportDialog extends Window {
2013-12-12 16:39:48 +01:00
private static final int WIDTH = 700 ;
2013-11-25 17:12:01 +01:00
private static final int HEIGHT = 450 ;
private ReportServiceAsync reportService = ( ReportServiceAsync ) GWT . create ( ReportService . class ) ;
private ContentPanel mainPanel = new ContentPanel ( ) ;
private HandlerManager eventBus ;
private HorizontalPanel hp = new HorizontalPanel ( ) ;
private StoreFilterField < VMEReportBean > filter ;
2013-11-25 19:11:03 +01:00
private VMETypeIdentifier type ;
2013-11-25 17:12:01 +01:00
2014-01-28 17:31:13 +01:00
public enum Action { SELECT , ASSOCIATE , DELETE }
2013-11-25 17:12:01 +01:00
/ * *
*
* @param eventBus the bus to fire events into
* /
2014-01-28 17:31:13 +01:00
public SelectVMEReportDialog ( final HandlerManager eventBus , VMETypeIdentifier type , final Action action ) {
2013-11-25 17:12:01 +01:00
this . eventBus = eventBus ;
2013-11-25 19:11:03 +01:00
this . type = type ;
2013-11-26 18:37:13 +01:00
setModal ( true ) ;
2013-12-17 19:19:51 +01:00
setResizable ( false ) ;
2013-11-25 17:12:01 +01:00
mainPanel . setHeaderVisible ( false ) ;
setWidth ( WIDTH ) ;
setHeight ( HEIGHT ) ;
mainPanel . setWidth ( WIDTH - 10 ) ;
mainPanel . setHeight ( HEIGHT - 50 ) ;
filter = new StoreFilterField < VMEReportBean > ( ) {
@Override
protected boolean doSelect ( Store < VMEReportBean > store , VMEReportBean parent , VMEReportBean record , String property , String filter ) {
String name = record . getName ( ) ;
name = name . toLowerCase ( ) ;
if ( name . contains ( filter . toLowerCase ( ) ) ) {
return true ;
}
return false ;
}
} ;
2013-12-12 16:39:48 +01:00
filter . setEmptyText ( " Filter by ... " ) ;
2013-11-25 17:12:01 +01:00
filter . setWidth ( " 250px " ) ;
hp . add ( filter ) ;
hp . setPixelSize ( 250 , 20 ) ;
hp . setVerticalAlignment ( HasVerticalAlignment . ALIGN_MIDDLE ) ;
add ( hp ) ;
add ( mainPanel ) ;
showLoading ( ) ;
2013-12-12 16:39:48 +01:00
2014-01-28 17:31:13 +01:00
setHeading ( getHeading ( type , action ) ) ;
2013-11-26 18:37:13 +01:00
if ( type = = VMETypeIdentifier . Vme ) {
2013-11-25 19:11:03 +01:00
reportService . listVMEReports ( new AsyncCallback < ArrayList < VMEReportBean > > ( ) {
@Override
2013-12-12 16:39:48 +01:00
public void onFailure ( Throwable caught ) {
2014-07-02 15:12:44 +02:00
mainPanel . unmask ( ) ;
showCommunicationError ( ) ;
2013-11-25 19:11:03 +01:00
}
2013-11-25 17:12:01 +01:00
2013-11-25 19:11:03 +01:00
@Override
2013-12-12 16:39:48 +01:00
public void onSuccess ( ArrayList < VMEReportBean > refReports ) {
mainPanel . unmask ( ) ;
2014-01-28 17:31:13 +01:00
showAvailableReports ( refReports , action ) ;
2013-11-25 17:12:01 +01:00
}
2013-12-12 16:39:48 +01:00
} ) ;
} else {
2013-11-25 19:11:03 +01:00
reportService . listVMEReportRefByType ( type , new AsyncCallback < ArrayList < VMEReportBean > > ( ) {
@Override
public void onFailure ( Throwable caught ) {
2014-07-02 15:12:44 +02:00
mainPanel . unmask ( ) ;
showCommunicationError ( ) ;
2013-11-25 19:11:03 +01:00
}
@Override
public void onSuccess ( ArrayList < VMEReportBean > refReports ) {
mainPanel . unmask ( ) ;
2014-01-28 17:31:13 +01:00
showAvailableReports ( refReports , action ) ;
2013-11-25 19:11:03 +01:00
}
} ) ;
}
}
2014-07-02 15:12:44 +02:00
private void showCommunicationError ( ) {
mainPanel . add ( new Html ( " "
+ " <div style= \" text-align: center; font-size: 16px; margin: 100px 20px; \" >We're sorry, it seems something is broken in the communication with the VMEs Repository. "
+ " </div> "
+ " <div style= \" text-align: center; font-size: 16px; margin: 50px 20px; \" > "
+ " In the meantime you may <a target= \" _blank \" href= \" https://support.d4science.research-infrastructures.eu \" >report the issue.<a/></div> "
+ " " ) ) ;
mainPanel . setLayout ( new FitLayout ( ) ) ;
mainPanel . layout ( ) ;
}
2013-11-25 17:12:01 +01:00
/ * *
2014-01-28 17:31:13 +01:00
*
* @param reports
* @param isAssociation
2013-11-25 17:12:01 +01:00
* /
2014-01-28 17:31:13 +01:00
public void showAvailableReports ( List < VMEReportBean > reports , final Action theAction ) {
2013-11-25 17:12:01 +01:00
ColumnModel cm = null ;
List < ColumnConfig > configs = new ArrayList < ColumnConfig > ( ) ;
ColumnConfig column = new ColumnConfig ( ) ;
2013-12-12 16:39:48 +01:00
2013-11-25 17:12:01 +01:00
column . setId ( " rfmo " ) ;
2013-12-06 16:57:55 +01:00
column . setHeader ( " Owner " ) ;
2013-12-12 16:39:48 +01:00
column . setWidth ( 15 ) ;
2013-12-17 19:19:51 +01:00
column . setHidden ( true ) ;
2013-11-25 17:12:01 +01:00
configs . add ( column ) ;
column = new ColumnConfig ( ) ;
column . setId ( " name " ) ;
2013-12-12 16:39:48 +01:00
column . setHeader ( type = = VMETypeIdentifier . Vme ? " VME Title " : " Identifier " ) ;
2013-11-25 17:12:01 +01:00
column . setWidth ( 150 ) ;
configs . add ( column ) ;
column = new ColumnConfig ( ) ;
2013-12-04 23:21:12 +01:00
column . setId ( " id " ) ;
column . setHeader ( " ID " ) ;
2013-12-12 16:39:48 +01:00
column . setWidth ( 6 ) ;
2013-11-25 17:12:01 +01:00
configs . add ( column ) ;
cm = new ColumnModel ( configs ) ;
/ * *
* load the grid data
* /
GroupingStore < VMEReportBean > store = new GroupingStore < VMEReportBean > ( ) ;
store . add ( reports ) ;
2013-12-17 19:19:51 +01:00
if ( type ! = VMETypeIdentifier . Rfmo )
store . groupBy ( " rfmo " ) ;
2013-11-25 17:12:01 +01:00
2013-12-12 16:39:48 +01:00
filter . setEmptyText ( type = = VMETypeIdentifier . Vme ? " Filter by Title " : " Filter by Identifier " ) ;
2013-11-25 17:12:01 +01:00
filter . bind ( store ) ;
final Grid < VMEReportBean > grid = new Grid < VMEReportBean > ( store , cm ) ;
grid . getElement ( ) . getStyle ( ) . setBorderWidth ( 0 , Unit . PX ) ;
grid . setAutoExpandColumn ( " name " ) ;
2013-11-25 19:11:03 +01:00
grid . setBorders ( false ) ;
2013-11-25 17:12:01 +01:00
grid . setStripeRows ( true ) ;
GroupingView view = new GroupingView ( ) ;
2013-12-04 23:21:12 +01:00
view . setShowGroupedColumn ( true ) ;
view . setStartCollapsed ( true ) ;
2013-11-25 17:12:01 +01:00
grid . setView ( view ) ;
2013-12-17 19:19:51 +01:00
grid . addListener ( Events . OnMouseOver , new Listener < GridEvent < VMEReportBean > > ( ) {
@Override
public void handleEvent ( GridEvent < VMEReportBean > ge ) {
if ( ge ! = null & & ge . getModel ( ) ! = null ) {
grid . setTitle ( ge . getModel ( ) . getName ( ) ) ;
}
}
} ) ;
2013-12-18 17:52:55 +01:00
grid . addListener ( Events . CellDoubleClick , new Listener < GridEvent < VMEReportBean > > ( ) {
@Override
public void handleEvent ( GridEvent < VMEReportBean > ge ) {
if ( ge ! = null & & ge . getModel ( ) ! = null ) {
hide ( ) ;
2014-01-28 17:31:13 +01:00
switch ( theAction ) {
case SELECT :
openSelected ( grid . getSelectionModel ( ) . getSelectedItem ( ) , type , theAction ) ;
break ;
case ASSOCIATE :
associateSelected ( grid . getSelectionModel ( ) . getSelectedItem ( ) , type , theAction ) ;
break ;
case DELETE :
deleteSelected ( grid . getSelectionModel ( ) . getSelectedItem ( ) , type , theAction ) ;
break ;
}
2013-12-18 17:52:55 +01:00
}
}
} ) ;
2014-01-28 17:31:13 +01:00
final ColumnModel finalCM = cm ;
view . setGroupRenderer ( new GridGroupRenderer ( ) {
public String render ( GroupColumnData data ) {
String f = finalCM . getColumnById ( data . field ) . getHeader ( ) ;
String l = data . models . size ( ) = = 1 ? " Item " : " Items " ;
return f + " : " + data . group + " ( " + data . models . size ( ) + " " + l + " ) " ;
}
} ) ;
2013-11-25 17:12:01 +01:00
view . setForceFit ( true ) ;
ContentPanel gridPanel = new ContentPanel ( new FitLayout ( ) ) ;
gridPanel . setHeaderVisible ( false ) ;
gridPanel . add ( grid ) ;
gridPanel . addButton ( new Button ( " Cancel " , new SelectionListener < ButtonEvent > ( ) {
@Override
public void componentSelected ( ButtonEvent ce ) {
hide ( ) ;
}
} ) ) ;
2014-01-28 17:31:13 +01:00
switch ( theAction ) {
case SELECT :
gridPanel . addButton ( new Button ( " Open Selected " , new SelectionListener < ButtonEvent > ( ) {
@Override
public void componentSelected ( ButtonEvent ce ) {
openSelected ( grid . getSelectionModel ( ) . getSelectedItem ( ) , type , theAction ) ;
hide ( ) ;
}
} ) ) ;
break ;
case ASSOCIATE :
2013-12-12 16:39:48 +01:00
gridPanel . addButton ( new Button ( " Associate Selected " , new SelectionListener < ButtonEvent > ( ) {
@Override
public void componentSelected ( ButtonEvent ce ) {
hide ( ) ;
2014-01-28 17:31:13 +01:00
associateSelected ( grid . getSelectionModel ( ) . getSelectedItem ( ) , type , theAction ) ;
2013-12-12 16:39:48 +01:00
}
} ) ) ;
2014-01-28 17:31:13 +01:00
break ;
case DELETE :
gridPanel . addButton ( new Button ( " Delete Selected from VME-DB " , new SelectionListener < ButtonEvent > ( ) {
2013-12-12 16:39:48 +01:00
@Override
public void componentSelected ( ButtonEvent ce ) {
2014-01-28 17:31:13 +01:00
hide ( ) ;
deleteSelected ( grid . getSelectionModel ( ) . getSelectedItem ( ) , type , theAction ) ;
2013-12-12 16:39:48 +01:00
}
} ) ) ;
2014-01-28 17:31:13 +01:00
break ;
}
2013-11-25 17:12:01 +01:00
mainPanel . add ( gridPanel ) ;
mainPanel . setLayout ( new FitLayout ( ) ) ;
mainPanel . layout ( ) ;
2013-11-25 19:11:03 +01:00
}
2014-01-28 17:31:13 +01:00
private void associateSelected ( VMEReportBean selectedItem , VMETypeIdentifier type , Action theAction ) {
eventBus . fireEvent ( new SelectedReportEvent ( selectedItem . getId ( ) , selectedItem . getName ( ) , type , theAction ) ) ;
}
private void openSelected ( VMEReportBean selectedItem , VMETypeIdentifier type , Action theAction ) {
eventBus . fireEvent ( new SelectedReportEvent ( selectedItem . getId ( ) , selectedItem . getName ( ) , type , theAction ) ) ;
2013-12-18 17:52:55 +01:00
}
2014-01-28 17:31:13 +01:00
private void deleteSelected ( VMEReportBean selectedItem , VMETypeIdentifier type , Action theAction ) {
eventBus . fireEvent ( new SelectedReportEvent ( selectedItem . getId ( ) , selectedItem . getName ( ) , type , theAction ) ) ;
2013-12-18 17:52:55 +01:00
}
2013-11-25 17:12:01 +01:00
/ * *
*
* /
private void showLoading ( ) {
mainPanel . mask ( " Asking for available Reports, please wait ... " , " loading-indicator " ) ;
}
2013-12-12 16:39:48 +01:00
/ * *
* the heading of the window
* @param type
* @param isAssociation
* @return
* /
2014-01-28 17:31:13 +01:00
private String getHeading ( VMETypeIdentifier type , Action theAction ) {
2013-12-12 16:39:48 +01:00
String toReturn = " " ;
switch ( type ) {
case Vme :
toReturn = " VME " ;
break ;
case FisheryAreasHistory :
toReturn = " fishing foot print " ;
break ;
case VMEsHistory :
toReturn = " regional history " ;
break ;
case InformationSource :
toReturn = " Information Source " ;
break ;
case GeneralMeasure :
toReturn = " General Measure " ;
break ;
case Rfmo :
toReturn = " RFMO " ;
2013-12-17 19:19:51 +01:00
break ;
2013-12-12 16:39:48 +01:00
default :
toReturn = " Unknown Category! " ;
break ;
}
2014-01-28 17:31:13 +01:00
switch ( theAction ) {
case SELECT :
toReturn = " Edit " + toReturn ;
break ;
case ASSOCIATE :
toReturn = " Associate " + toReturn ;
break ;
case DELETE :
toReturn = " Delete " + toReturn ;
break ;
}
2013-12-12 16:39:48 +01:00
return toReturn ;
}
2013-11-25 17:12:01 +01:00
}