Table context menu different with multiselect

Hi,

Im trying to create a Table where you have different options in the context menu depending on if you have selected a single row or multiple rows.

It took me a while to do this but now i have a working solution. The problem is that is feel that its not good coding practice and I would gladly take any advice in how to perhaps split my “function” into smaller classes or functions. Feel free to comment and advice!


	    Table contactList = new Table("Test table");
	    contactList.addListener(new Property.ValueChangeListener(){
	    	public void valueChange(ValueChangeEvent event){    		
	    		Set<?> value = (Set<?>) event.getProperty().getValue();
	    		if(value == null || value.size() == 0){
	    			getMainWindow().showNotification("NULL or 0");
	    		}else if(value.size() == 1){
	    			contactList.removeAllActionHandlers();
	    			contactList.addActionHandler(new Action.Handler(){
	    		    	public Action[] getActions(Object target, Object sender){	    		    		
	    		    		return ACTIONS_EDIT;    		    		
	    		    	}	    		    	
	    		    	public void handleAction(Action action, Object sender, Object target){		    		    		
	    		    		getMainWindow().showNotification("ACTION_EDIT");	    		    			
	    		    	}
	    		    }); 
	    		}else{
	    			contactList.removeAllActionHandlers();
	    			contactList.addActionHandler(new Action.Handler(){
	    		    	public Action[] getActions(Object target, Object sender){	    		    		
	    		    		return ACTIONS_EDIT_ALL;    		    		
	    		    	}	    		    	
	    		    	public void handleAction(Action action, Object sender, Object target){		    		    		
	    		    		getMainWindow().showNotification("ACTION_EDIT_ALL");	    		    			
	    		    	}
	    		    });    	
	    		}
	    	}
	    });

Thx for any help!

Well yes, the anonymous class looks a bit crammed. I sense some genericity in the task, so I guess you could make it a proper public class that implements the listener, say SelectionNumberListener or something. You could make it abstract and require the user to implement methods like singleSelection() and multipleSelection(). Then implement just the valueChange() method, which should be just a switchboard for the abstract methods.

You could then make the class specific to the menu handling case - or make a generic implementation of the above abstract class for that purpose. For example, MultiplicityContextMenuManagerThing or something, which takes the two action handlers in the constructor as parameters.

Thanks once again for your help.
I did almost as you adviced.

I posted my result at stackoverflow. Sry but i currently dont have the code so I have to refer anyone who whants to see the end result to
stackoverflow

I do apologize for redirecting any users away from this forum!

Best Regards
Marthin