package org.gcube.portlets.user.reportgenerator.client.targets; import java.util.ArrayList; import org.gcube.portlets.d4sreporting.common.shared.BasicComponent; import org.gcube.portlets.d4sreporting.common.shared.ComponentType; import org.gcube.portlets.d4sreporting.common.shared.RepeatableSequence; import org.gcube.portlets.d4sreporting.common.shared.Tuple; import org.gcube.portlets.user.reportgenerator.client.Presenter.Presenter; import org.gcube.portlets.user.reportgenerator.client.model.TemplateComponent; import org.gcube.portlets.user.reportgenerator.client.model.TemplateModel; import org.gcube.portlets.user.reportgenerator.shared.VMETypeIdentifier; import com.google.gwt.core.shared.GWT; import com.google.gwt.dom.client.Style.Unit; import com.google.gwt.user.client.ui.Composite; import com.google.gwt.user.client.ui.VerticalPanel; /** * * @author massi * */ public class ClientReportReference extends Composite implements ClientSequence { private final int indentationValue = 20; protected ArrayList groupedComponents = new ArrayList(); private VerticalPanel myPanel = new VerticalPanel(); private String refType; private ArrayList tupleList; private SequenceWidget first; private Presenter p; private boolean singleRelation; //allow or not to add new entries /** * s * @param p * @param ms */ public ClientReportReference(Presenter p, String refType, ArrayList tupleList, boolean singleRelation) { this.p = p; this.refType = refType; this.tupleList = tupleList; this.singleRelation = singleRelation; shrinkComponents(tupleList); //(beacuse the ref is indented for (Tuple seq : tupleList) { if (myPanel.getWidgetCount()==0) { // if is the first one first = getMasterSequence(seq); if (isSingleRelation()) first.removeAddAnotherButton(); myPanel.add(first); } else { SequenceWidget seqW = getRefSequence(p, this, seq, false, false); myPanel.add(seqW); } } initWidget(myPanel); } /** * to make the components less in width (beacuse the ref is indented) */ private void shrinkComponents(ArrayList tupleList) { for (Tuple seq : tupleList) { for (BasicComponent bc : seq.getGroupedComponents()) { if (bc.getWidth() >= (TemplateModel.TEMPLATE_WIDTH - 50)) bc.setWidth(bc.getWidth()-(indentationValue+5)); } } } /** * the first seq has to behave differently */ private SequenceWidget getMasterSequence(RepeatableSequence seq) { SequenceWidget toReturn = getRefSequence(p, this, seq, true, false); if (tupleList.size() == 1) { toReturn.enableClear(); if (isTupleEmpty(tupleList.get(0))) { toReturn.clearAssociation(); } } return toReturn; } private boolean isTupleEmpty(Tuple toCheck) { boolean toReturn = true; for (BasicComponent bc : toCheck.getGroupedComponents()) { if ((bc.getType() == ComponentType.BODY_NOT_FORMATTED ||bc.getType() == ComponentType.BODY) && bc.getPossibleContent() != null) return false; } return toReturn; } public ArrayList getTupleList() { return tupleList; } public void setTupleList(ArrayList tupleList) { this.tupleList = tupleList; } public boolean isSingleRelation() { return singleRelation; } @Override public ArrayList getGroupedComponents() { return groupedComponents; } @Override public boolean add(String id, RepeatableSequence sequence) { //(beacuse the ref is indented for (BasicComponent bc : sequence.getGroupedComponents()) { bc.setWidth(bc.getWidth()-(indentationValue+5)); } if (first.isSelectNewEnabled()) { first = getRefSequence(p, this, sequence, true, false); myPanel.clear(); myPanel.add(first); tupleList.clear(); } else { SequenceWidget seqW = getRefSequence(p, this, sequence, false, true); myPanel.add(seqW); first.hideClearAssociationButton(); } //needed for the model Tuple toAdd = new Tuple(id, sequence.getGroupedComponents()); tupleList.add(toAdd); return true; } @Override public SequenceWidget remove(SequenceWidget toRemove) { myPanel.remove(toRemove); for (TemplateComponent tc : toRemove.getSeqGroupedComponents()) { groupedComponents.remove(tc); } tupleList.remove(toRemove.getSequence()); if (tupleList.size() == 1) first.enableClear(); GWT.log("List Tuple Size = "+tupleList.size()); return toRemove; } @Override public void AddButtonClicked(RepeatableSequence sequence) { VMETypeIdentifier type2Pass = null; try { type2Pass = p.getTypeIdFromString(refType); } catch (Exception e) { e.printStackTrace(); } p.showVMERefAssociateDialog(type2Pass); p.setClientSequenceSelected(this); //important! } public String getRefType() { return refType; } public void clear() { first.clearAssociation(); } /** * A Ref is a seqeunce with diffrent style (intendedm white background and less wide) * */ private SequenceWidget getRefSequence(Presenter p, ClientSequence owner, RepeatableSequence repSequence, boolean notRemovable, boolean isNew) { SequenceWidget toReturn = new SequenceWidget(p, this, repSequence, notRemovable, isNew); toReturn.getElement().getStyle().setMarginLeft(indentationValue, Unit.PX); toReturn.addStyleName("seqWidget-shadow"); toReturn.alignButtonsLeft(); return toReturn; } }