ckan-metadata-publisher-widget/src/main/java/org/gcube/portlets/widgets/ckandatapublisherwidget/client/ui/AddedResourcesSummary.java

135 lines
3.5 KiB
Java

package org.gcube.portlets.widgets.ckandatapublisherwidget.client.ui;
import java.util.ArrayList;
import java.util.List;
import org.gcube.portlets.widgets.ckandatapublisherwidget.client.events.AddResourceEvent;
import org.gcube.portlets.widgets.ckandatapublisherwidget.client.events.AddResourceEventHandler;
import org.gcube.portlets.widgets.ckandatapublisherwidget.shared.ResourceBean;
import com.github.gwtbootstrap.client.ui.Button;
import com.github.gwtbootstrap.client.ui.constants.ButtonType;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
/**
* A summary of the added resources by the user.
* @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it)
*/
public class AddedResourcesSummary extends Composite{
private static AddedResourcesSummaryUiBinder uiBinder = GWT
.create(AddedResourcesSummaryUiBinder.class);
interface AddedResourcesSummaryUiBinder extends
UiBinder<Widget, AddedResourcesSummary> {
}
// Event bus
private HandlerManager eventBus;
// list of added resources
List<ResourceBean> addedResources;
@UiField VerticalPanel addResourcesPanel;
public AddedResourcesSummary(HandlerManager eventBus) {
initWidget(uiBinder.createAndBindUi(this));
// save bus
this.eventBus = eventBus;
// bind on add resource event
bind();
// init list
addedResources = new ArrayList<ResourceBean>();
}
private void bind() {
// when a new resource is added
eventBus.addHandler(AddResourceEvent.TYPE, new AddResourceEventHandler() {
@Override
public void onAddedResource(AddResourceEvent addResourceEvent) {
// get the resource
final ResourceBean justAddedResource = addResourceEvent.getResource();
// check if a resource with this id already exists
for (ResourceBean resource : addedResources){
if(resource.getId().equals(justAddedResource.getId())){
// clear list and rebuild
rebuildSummary();
return;
}
}
Button associatedButton = new Button();
associatedButton.setType(ButtonType.LINK);
associatedButton.setText("-" + justAddedResource.getName());
// add to the list
addedResources.add(justAddedResource);
// add to the panel
addResourcesPanel.add(associatedButton);
// add handler to swap tab on click
associatedButton.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
// TODO show information below this link and swap panel
}
});
}
});
}
/**
* Rebuild the summary list
*/
protected void rebuildSummary() {
addResourcesPanel.clear();
for (final ResourceBean resource : addedResources){
Button associatedButton = new Button();
associatedButton.setType(ButtonType.LINK);
associatedButton.setText("-" + resource.getName());
// add to the list
addedResources.add(resource);
// add to the panel
addResourcesPanel.add(associatedButton);
// add handler to swap tab on click
associatedButton.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
// TODO show information below this link and swap panel
}
});
}
}
}