@ -1,10 +1,13 @@
package org.gcube.portlets.widgets.pickitem.client.dialog ;
import java.util.ArrayList ;
import java.util.List ;
import org.gcube.portlets.widgets.pickitem.client.bundle.CssAndImages ;
import org.gcube.portlets.widgets.pickitem.client.events.PickedItemEvent ;
import org.gcube.portlets.widgets.pickitem.client.events.PickedItemEventHandler ;
import org.gcube.portlets.widgets.pickitem.client.oracle.UserOracle ;
import org.gcube.portlets.widgets.pickitem.client.oracle.UserSuggestion ;
import org.gcube.portlets.widgets.pickitem.client.uibinder.NoPhotoTemplate ;
import org.gcube.portlets.widgets.pickitem.client.uibinder.SelectableItem ;
import org.gcube.portlets.widgets.pickitem.client.uibinder.WithPhotoTemplate ;
@ -55,7 +58,7 @@ public class PickItemsDialog extends PopupPanel {
private int itemCursorIndexStart = - 1 ;
boolean handleNonCharKeys = false ;
private final MultiWordSuggestOracle oracle = new MultiWordSuggestOracle( ) ;
private MultiWordSuggestOracle oracle = new UserOracle( ) ; //by default we use the async version;
private int displayIndexSelected ;
@ -70,7 +73,7 @@ public class PickItemsDialog extends PopupPanel {
private boolean includeTriggerChar ;
//to explain
private boolean stopListening = true ;
static {
CssAndImages . INSTANCE . css ( ) . ensureInjected ( ) ;
}
@ -84,6 +87,31 @@ public class PickItemsDialog extends PopupPanel {
* @param includeTriggerChar true if your suggestions start with the trigger char ( e . g . # anHashTag triggered by # ) false otherwise
* /
public PickItemsDialog ( char triggerChar , ArrayList < ItemBean > beans , final HandlerManager eventBus , int widthInPixel ) {
this ( triggerChar , eventBus , widthInPixel ) ;
this . beans = beans ;
oracle = new MultiWordSuggestOracle ( ) ; //not async version
GWT . log ( "new constructor" ) ;
//add the user fill names to the oracle
for ( ItemBean bean : beans ) {
oracle . add ( bean . getAlternativeName ( ) ) ;
// if it is a team, set the avatar
if ( bean . isItemGroup ( ) )
bean . setThumbnailURL ( CssAndImages . INSTANCE . iconTeam ( ) . getSafeUri ( ) . toString ( ) ) ;
}
}
/ * *
* Use this constructor for users , the list of the users is automatically loaded form the VRE members if in a VRE or from the union of the current ' s user VRE members if at root level
* Note that you must declare the pick item widget servlet in this case
* @param triggerChar the ' single char ' used to trigger the items list show , e . g . '@' , '#' . . . .
* @param eventBus the event bus on where the widget will fire the selected user event
* @param widthInPixel the desired width ( grater than 199 pixel )
* @param hasPhoto tell of you have want to show photo for the item or not
* @param includeTriggerChar true if your suggestions start with the trigger char ( e . g . # anHashTag triggered by # ) false otherwise
* /
public PickItemsDialog ( char triggerChar , final HandlerManager eventBus , int widthInPixel ) {
super ( true , false ) ;
if ( widthInPixel < 200 ) {
throw new IllegalArgumentException ( "width must be greater than 199" ) ;
@ -92,23 +120,17 @@ public class PickItemsDialog extends PopupPanel {
this . triggerChar = triggerChar ;
this . includeTriggerChar = false ;
this . hasPhoto = false ;
this . beans = beans ;
this . beans = new ArrayList < > ( ) ;
focusPanel . setWidth ( widthInPixel + "px" ) ;
mainPanel . setWidth ( widthInPixel + "px" ) ;
setWidth ( widthInPixel + "px" ) ;
focusPanel . add ( mainPanel ) ;
setWidget ( focusPanel ) ;
setStyleName ( "pickDialog" ) ;
//add the user fill names to the oracle
for ( ItemBean bean : beans ) {
oracle . add ( bean . getAlternativeName ( ) ) ;
// if it is a team, set the avatar
if ( bean . isItemGroup ( ) )
bean . setThumbnailURL ( CssAndImages . INSTANCE . iconTeam ( ) . getURL ( ) ) ;
}
setStyleName ( "pickDialog" ) ;
if ( oracle instanceof UserOracle ) {
UserOracle asyncOracle = ( UserOracle ) oracle ;
asyncOracle . addContacts ( getAllUsers ( ) ) ;
}
//remove the first selected when hovering
focusPanel . addMouseOverHandler ( new MouseOverHandler ( ) {
@ -133,15 +155,21 @@ public class PickItemsDialog extends PopupPanel {
handleMouseDown ( ) ;
}
} ) ;
eventBus . addHandler ( PickedItemEvent . TYPE , new PickedItemEventHandler ( ) {
@Override
public void onSelectedItem ( PickedItemEvent event ) {
GWT . log ( "GOT @ CAZZ EVENT! "+ event . getSelectedItem ( ) . getAlternativeName ( ) ) ;
GWT . log ( "GOT EVENT "+ event . getSelectedItem ( ) . getAlternativeName ( ) ) ;
stopListening = true ;
}
} ) ;
}
//just used in devlopment
private List < ItemBean > getAllUsers ( ) {
List < ItemBean > toReturn = new ArrayList < > ( ) ;
toReturn . add ( new ItemBean ( "andrea.rossi" , "andrea.rossi" , "Andrea Rossi" , "m.assante@gmail.com" ) ) ;
return toReturn ;
}
/ * *
* use if you have want to show a photo for the item or not , remember to provide it in { @link ItemBean } instances
* /
@ -181,10 +209,10 @@ public class PickItemsDialog extends PopupPanel {
this . itemCursorIndexStart = cursorPos ;
}
else {
itemCursorIndexEnd = cursorPos ;
currText = currText . substring ( itemCursorIndexStart , cursorPos ) + ch ;
if ( currText . contains ( "" + triggerChar ) & & currText . length ( ) > 1 & & ! stopListening ) {
if ( pickingUser ( currText . substring ( 1 ) ) ) {
handleNonCharKeys = true ;
@ -216,6 +244,7 @@ public class PickItemsDialog extends PopupPanel {
* @param currText the text being typed
* /
private boolean pickingUser ( String currText ) {
GWT . log ( "ci arriva" ) ;
if ( currText . trim ( ) . length ( ) > 0 ) {
if ( includeTriggerChar )
showSuggestions ( triggerChar + currText ) ;
@ -293,14 +322,20 @@ public class PickItemsDialog extends PopupPanel {
}
} ) ;
}
}
private ItemBean getUserModelBySuggestion ( Suggestion suggestion ) {
for ( ItemBean bean : beans ) {
if ( suggestion . getReplacementString ( ) . compareTo ( bean . getAlternativeName ( ) ) = = 0 )
return bean ;
if ( suggestion instanceof UserSuggestion ) {
UserSuggestion us = ( UserSuggestion ) suggestion ;
return us . getUser ( ) ;
} else { //the user were preloaded client side
for ( ItemBean bean : beans ) {
if ( suggestion . getReplacementString ( ) . compareTo ( bean . getAlternativeName ( ) ) = = 0 )
return bean ;
}
return new ItemBean ( "no-match" , "no-match" , "no-match" , "no-match" ) ;
}
return new ItemBean ( "no-match" , "no-match" , "no-match" , "no-match" ) ;
}
private Widget getUserTemplate ( ItemBean user , int displayIndex , boolean hasPhoto ) {