package org.gcube.portlets.user.reportgenerator.client.targets; import org.gcube.portlets.d4sreporting.common.shared.Attribute; import org.gcube.portlets.d4sreporting.common.shared.AttributeArea; import org.gcube.portlets.d4sreporting.common.shared.ComponentType; import org.gcube.portlets.user.reportgenerator.client.Presenter.Presenter; import com.google.gwt.core.client.GWT; import com.google.gwt.dom.client.Style.Unit; import com.google.gwt.user.client.ui.Composite; import com.google.gwt.user.client.ui.FlowPanel; import com.google.gwt.user.client.ui.HTML; import com.google.gwt.user.client.ui.HasHorizontalAlignment; import com.google.gwt.user.client.ui.HasVerticalAlignment; import com.google.gwt.user.client.ui.HorizontalPanel; import com.google.gwt.user.client.ui.RadioButton; /** * AttributeArea class * * @author Massimiliano Assante, ISTI-CNR - massimiliano.assante@isti.cnr.it */ public class AttributeSingleSelection extends Composite { private final String RADIO_NAME = "radio_unique"; private HorizontalPanel myPanel; private String attrName; private RadioButton[] boxes; /** * Coming form a template constructor */ public AttributeSingleSelection(final Presenter presenter, int left, int top, int width, final int height, String textToDisplay) { myPanel = new HorizontalPanel(); myPanel.setTitle("Attribute Area"); myPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_BOTTOM); myPanel.setPixelSize(width, 20); myPanel.addStyleName("attributeArea"); attrName = getAttributeName(textToDisplay); HTML attrNameLabel = new HTML(attrName+":"); attrNameLabel.getElement().getStyle().setMarginRight(5, Unit.PX); FlowPanel boxesPanel = new FlowPanel(); boxesPanel.add(attrNameLabel); myPanel.add(boxesPanel); myPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT); boxes = getRadioboxes(textToDisplay); for (int i = 0; i < boxes.length; i++) { boxesPanel.add(boxes[i]); } initWidget(myPanel); } /** * Coming form a report constructor */ public AttributeSingleSelection(final Presenter presenter, int left, int top, int width, final int height, AttributeArea sata) { myPanel = new HorizontalPanel(); myPanel.setTitle("Attribute Area"); myPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_BOTTOM); myPanel.setPixelSize(width, 20); myPanel.addStyleName("attributeArea"); attrName = sata.getAttrName(); HTML attrNameLabel = new HTML(attrName+":"); attrNameLabel.getElement().getStyle().setMarginRight(5, Unit.PX); FlowPanel boxesPanel = new FlowPanel(); boxesPanel.add(attrNameLabel); myPanel.add(boxesPanel); myPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT); int values = sata.getValues().size(); boxes = new RadioButton[values]; int j = 0; for (Attribute attr: sata.getValues()) { RadioButton toAdd = new RadioButton(RADIO_NAME, attr.getName()); toAdd.setStyleName("checkAttribute"); toAdd.setValue(attr.getValue()); boxes[j] = toAdd; j++; } //adding it to the panel for (int i = 0; i < boxes.length; i++) { boxesPanel.add(boxes[i]); } initWidget(myPanel); } /** * * @param toParse * @return */ private String getAttributeName(String toParse) { if (toParse == null) return ""; String toReturn = ""; try { toReturn = toParse.substring(0, toParse.indexOf(":")); } catch (StringIndexOutOfBoundsException e) { //GWT.log("Could not find : returning empty"); } return toReturn; } /** * * @param toParse * @return */ private RadioButton[] getRadioboxes(String toParse) { String toSplit = toParse.substring(toParse.indexOf(":")+1, toParse.length()); String[] values = toSplit.split("\\|"); // GWT.log("toSplit" + toSplit); // GWT.log("values" + values.length); RadioButton[] boxes = new RadioButton[values.length]; for (int i = 0; i < values.length; i++) { boxes[i] = new RadioButton(RADIO_NAME, values[i].trim()); boxes[i].setStyleName("checkAttribute"); } return boxes; } /** * * @return */ public ComponentType getType() { return ComponentType.ATTRIBUTE_MULTI; } /** * * @return */ public RadioButton[] getBoxes() { return boxes; } /** * * @return */ public String getAttrName() { return attrName; } }