package org.gcube.portlets.user.reportgenerator.client.model; import java.io.Serializable; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import org.gcube.portlets.d4sreporting.common.shared.ComponentType; import org.gcube.portlets.d4sreporting.common.shared.Metadata; import org.gcube.portlets.d4sreporting.common.shared.SerializableAttribute; import org.gcube.portlets.d4sreporting.common.shared.SerializableAttributeArea; import org.gcube.portlets.d4sreporting.common.shared.SerializableComponent; import org.gcube.portlets.d4sreporting.common.shared.SerializableRepeatableSequence; import org.gcube.portlets.d4sreporting.common.shared.SerializableTable; import org.gcube.portlets.d4sreporting.common.shared.SerializableTimeSeries; import org.gcube.portlets.user.reportgenerator.client.Presenter.Presenter; import org.gcube.portlets.user.reportgenerator.client.targets.AttributeArea; import org.gcube.portlets.user.reportgenerator.client.targets.BasicTextArea; import org.gcube.portlets.user.reportgenerator.client.targets.D4sRichTextarea; import org.gcube.portlets.user.reportgenerator.client.targets.DropImageListener; import org.gcube.portlets.user.reportgenerator.client.targets.DropTSListener; import org.gcube.portlets.user.reportgenerator.client.targets.DroppingArea; import org.gcube.portlets.user.reportgenerator.client.targets.GenericTable; import org.gcube.portlets.user.reportgenerator.client.targets.GroupingDelimiterArea; import org.gcube.portlets.user.reportgenerator.client.targets.GroupingInnerArea; import org.gcube.portlets.user.reportgenerator.client.targets.ImageArea; import org.gcube.portlets.user.reportgenerator.client.targets.RepeatableSequence; import org.gcube.portlets.user.reportgenerator.client.targets.ReportTextArea; import org.gcube.portlets.user.reportgenerator.client.targets.TSArea; import com.google.gwt.core.client.GWT; import com.google.gwt.dom.client.Style.Unit; import com.google.gwt.user.client.ui.CheckBox; import com.google.gwt.user.client.ui.HTML; import com.google.gwt.user.client.ui.Image; import com.google.gwt.user.client.ui.Widget; /** * * This class represent all the possible template components IN THE MODEL * @author Massimiliano Assante, ISTI-CNR - massimiliano.assante@isti.cnr.it * @version July 2011 (3.0) */ public class TemplateComponent { /** * */ public final static String DEFAULT_IMAGE_PATH= GWT.getModuleBaseURL() + "../images/organization_logo.jpg"; private int x; private int y; private int width; private int height; private TemplateModel myModel; private int templatePage; private String idInBasket = null; private ComponentType type; private boolean locked; private boolean doubleColLayout; /** * holds the metadata(s) for the sections */ private List metadata; /** * the paramName for assigning it a value when exporting to pdf, valid only for Dynamic Content */ private String paramName; //what is in the template component may vary depending on its type private Widget content; /** * Creates a empty TemplateComponent */ public TemplateComponent() { super(); } /** * Creates a TemplateComponent with the given charactheristics in double column * * @param myModel . * @param x . * @param y . * @param width . * @param height . * @param templatePage . * @param content the inserted widget * @param type the type of the inserted widget * @param paramName for assigning it a value when exporting to pdf, valid only for Dynamic Content * @param doubleColLayout to specify that its layout is double columned */ public TemplateComponent(TemplateModel myModel, int x, int y, int width, int height, int templatePage, ComponentType type, String paramName, Widget content, boolean doubleColLayout) { this.myModel = myModel; this.x = x; this.y = y; this.width = width; this.height = height; this.templatePage = templatePage; this.content = content; this.type = type; this.paramName = paramName; this.doubleColLayout = doubleColLayout; this.metadata = new LinkedList(); } /** * * Creates a TemplateComponent with the given charactheristics and single column * * @param myModel . * @param x . * @param y . * @param width . * @param height . * @param templatePage . * @param content the inserted widget * @param type the type of the inserted widget * @param paramName for assigning it a value when exporting to pdf, valid only for Dynamic Content */ public TemplateComponent(TemplateModel myModel, int x, int y, int width, int height, int templatePage, ComponentType type, String paramName, Widget content) { this.myModel = myModel; this.x = x; this.y = y; this.width = width; this.height = height; this.templatePage = templatePage; this.content = content; this.type = type; this.paramName = paramName; this.doubleColLayout = false; this.metadata = new LinkedList(); } /** * create a template component which is displayable (create an actual Widget in the content field) * @param myModel the model * @param sc the serialiazble to convert * @param presenter . */ public TemplateComponent(TemplateModel myModel, SerializableComponent sc, Presenter presenter) { this.myModel = myModel; // //Coords start = new Coords(x, y); int width = sc.getWidth(); int height = sc.getHeight(); this.x = sc.getX(); this.y = sc.getY(); this.width = sc.getWidth(); this.height = sc.getHeight(); this.templatePage = sc.getTemplatePage(); this.type = sc.getType(); this.paramName = sc.getParamName(); this.doubleColLayout = sc.isDoubleColLayout(); this.locked = sc.isLocked(); this.metadata = sc.getMetadata(); switch (sc.getType()) { case DYNA_IMAGE: DroppingArea dp; String possibelContent = ((String) sc.getPossibleContent()); if (possibelContent.startsWith("http")) { dp = new DroppingArea(presenter, width, height,possibelContent); dp.showImage(new Image(possibelContent)); } else if (possibelContent.startsWith("/")) dp = new DroppingArea(presenter, width, height, ""); else if (sc.getPossibleContent().equals(DEFAULT_IMAGE_PATH)) { dp = new DroppingArea(presenter, width, height, ""); } else { dp = new DroppingArea(presenter, width, height, possibelContent); } dp.setPixelSize(width, height); @SuppressWarnings("unused") DropImageListener dropListener = new DropImageListener(dp); this.content = dp; break; case STATIC_IMAGE: String imagePath = (String) sc.getPossibleContent(); ImageArea img = new ImageArea(presenter, imagePath, myModel.getTemplateName(), true, width, height); img.setPixelSize(width, height); this.content = img; break; case HEADING_1: case HEADING_2: case HEADING_3: case HEADING_4: case HEADING_5: case TITLE: if (sc.isLocked()) { HTML area = new HTML(); area.setStyleName(getStyle(sc.getType())); area.getElement().getStyle().setMarginLeft(25, Unit.PX); area.getElement().getStyle().setMarginTop(15, Unit.PX); area.setPixelSize(width, 25); area.setText((String) sc.getPossibleContent()); this.content = area; } else { BasicTextArea bToAdd = new BasicTextArea(sc.getType(), presenter, sc.getX(), sc.getY(), width, 25, getUserComments() != null); bToAdd.setText((String) sc.getPossibleContent()); this.content = bToAdd; } break; case BODY: if (sc.isLocked()) { HTML area = new HTML(); area.setStyleName("d4sFrame"); area.addStyleName("fixedTextArea"); area.addStyleName("hasRichTextToolbar"); area.setPixelSize(width, height); area.setHTML((String) sc.getPossibleContent()); this.content = area; } else { D4sRichTextarea ta = new D4sRichTextarea(sc.getType(), presenter, sc.getX(), sc.getY(),width, height, getUserComments() != null); ta.setHTML((String) sc.getPossibleContent()); //ta.setStyleName("cw-RichText"); ta.setPixelSize(width, height); this.content = ta; } this.setLocked(sc.isLocked()); break; case TOC: ReportTextArea dp2 = new ReportTextArea(sc.getType(), presenter, sc.getX(), sc.getY(), width, height, getUserComments() != null); dp2.addStyleName("tocArea"); this.content = dp2; break; case BIBLIO: ReportTextArea dp3 = new ReportTextArea(sc.getType(), presenter, sc.getX(), sc.getY(), width, height, getUserComments() != null); dp3.addStyleName("biblioArea"); this.content = dp3; break; case PAGEBREAK: ReportTextArea dp4 = new ReportTextArea(sc.getType(), presenter, sc.getX(), sc.getY(), width, height, getUserComments() != null); dp4.addStyleName("pagebreak"); this.content = dp4; break; case FLEX_TABLE: SerializableTable st = (SerializableTable) sc.getPossibleContent(); GenericTable table = new GenericTable(st, presenter, sc.getX(), sc.getY(), TemplateModel.TEMPLATE_WIDTH - 50, 200); this.content = table; break; case ATTRIBUTE: AttributeArea ta = null; if (sc.getPossibleContent() instanceof SerializableAttributeArea) { SerializableAttributeArea sata = (SerializableAttributeArea) sc.getPossibleContent(); ta = new AttributeArea(presenter, sc.getX(), sc.getY(), width, height, sata); } else { ta = new AttributeArea(presenter, sc.getX(), sc.getY(), width, height, sc.getPossibleContent().toString()); } this.content = ta; break; case COMMENT: HTML comment = new HTML(); comment.setStyleName("commentArea"); comment.setHTML((String) sc.getPossibleContent()); this.content = comment; break; case INSTRUCTION: HTML instr = new HTML(); instr.setStyleName("instructionArea"); instr.setHTML((String) sc.getPossibleContent()); this.content = instr; break; case TIME_SERIES: TSArea tsa; SerializableTimeSeries sts = null; try { sts = ((SerializableTimeSeries) sc.getPossibleContent()); } catch (ClassCastException e) { } //need to reset the filter when loading a TS sts.setFilter(null); tsa = new TSArea(presenter, width, 155, sts); tsa.setPixelSize(width, 155); @SuppressWarnings("unused") DropTSListener dropTSListener = new DropTSListener(tsa); this.content = tsa; break; case REPEAT_SEQUENCE_DELIMITER: GroupingDelimiterArea gp = new GroupingDelimiterArea(); this.content = gp; break; case REPEAT_SEQUENCE_INNER: GroupingInnerArea spacer = new GroupingInnerArea(); this.content = spacer; break; case REPEAT_SEQUENCE: GWT.log("FOUND SEQUENCE trying getGroup"); SerializableRepeatableSequence serializableRepeatableSequence = (SerializableRepeatableSequence) sc.getPossibleContent(); GWT.log("getGroup: " + serializableRepeatableSequence.toString()); RepeatableSequence rps = new RepeatableSequence(presenter, serializableRepeatableSequence); this.content = rps; } } /** * * @param type * @return */ private String getStyle(ComponentType type) { switch (type) { case TITLE: return "titleArea"; case HEADING_1: return "headgin1Area"; case HEADING_2: return "headgin2Area"; case HEADING_3: return "headgin3Area"; case HEADING_4: return "headgin4Area"; case HEADING_5: return "headgin5Area"; default: return ""; } } /** * return a template Component which is serializable * @return . */ public SerializableComponent getSerializable() { Serializable content = ""; String id = ""; switch (this.getType()) { case DYNA_IMAGE: DroppingArea da = (DroppingArea) this.content; content = da.getDroppedImage().getUrl(); id = da.getIdInBasket(); idInBasket = id; if (((String) content).compareTo("") == 0) content = DEFAULT_IMAGE_PATH; break; case STATIC_IMAGE: ImageArea tmp = ((ImageArea) this.content); String imageName = tmp.getImageName(); content = tmp.getUrl(); break; case BODY: if (this.isLocked()) { content = ((HTML) this.content).getHTML(); } else { content = ((D4sRichTextarea) this.content).getHTML(); } break; case HEADING_1: case HEADING_2: case HEADING_3: case TITLE: if (this.isLocked()) { content = ((HTML) this.content).getText(); } else content = ((BasicTextArea) this.content).getText(); break; case TIME_SERIES: GWT.log("Found Time Series", null); TSArea tsa = (TSArea) this.content; content = tsa.getSts(); break; case FLEX_TABLE: GenericTable gt = (GenericTable) this.content; SerializableTable st = gt.getSerializable(); content = st; break; case ATTRIBUTE: AttributeArea att = (AttributeArea) this.content; ArrayList values = new ArrayList(); for (CheckBox box : att.getBoxes()) { values.add(new SerializableAttribute(box.getText().trim(), box.getValue())); } SerializableAttributeArea sat= new SerializableAttributeArea(att.getAttrName().trim(), values); content = sat; break; case COMMENT: content = ((HTML) this.content).getHTML(); break; case INSTRUCTION: content = ((HTML) this.content).getHTML(); break; } return new SerializableComponent(x, y, width, height, templatePage, type, idInBasket, "param empty", content, this.doubleColLayout, isLocked(), metadata); } /** * @return . */ public int getHeight() {return height;} /** * @param height . */ public void setHeight(int height) { this.height = height; } /** * @return . */ public ComponentType getType() {return type;} /** * @param type . */ public void setType(ComponentType type) { this.type = type;} /** * @return . */ public int getWidth() { return width;} /** * @param width . */ public void setWidth(int width) {this.width = width;} /** * @return . */ public int getX() { return x;} /** * @param x . */ public void setX(int x) {this.x = x;} /** * @return . */ public int getY() {return y;} /** * @param y . */ public void setY(int y) {this.y = y;} /** * @return . */ public int getTemplatePage() { return templatePage; } /** * @param templatePage . */ public void setTemplatePage(int templatePage) { this.templatePage = templatePage;} /** * @return . */ public Widget getContent() {return content; } /** * @param content . */ public void setContent(Widget content) { this.content = content; } /** * * @return . */ public String getParamName() { return paramName; } /** * * @param paramName . */ public void setParamName(String paramName) { this.paramName = paramName; } /** * * @return . */ public boolean isLocked() { return locked; } /** * * @param locked . */ public void setLocked(boolean locked) { this.locked = locked; } /** * * @param attr . * @param value . */ public void addMetadata(String attr, String value) { if (attr != null && value != null) { metadata.add(new Metadata(attr, value)); } else throw new NullPointerException(); } /** * * @return . */ public List getAllMetadata() { if (metadata == null) { new LinkedList(); } return metadata; } /** * * @return */ public String getUserComments() { if (metadata == null) return null; for (Metadata md : metadata) { if (md.getAttribute().equals(TemplateModel.USER_COMMENT)) return md.getValue(); } return null; } /** * * @return . */ public String getIdInBasket() { return idInBasket; } /** * * @param idInBasket . */ public void setIdInBasket(String idInBasket) { this.idInBasket = idInBasket; } /** * * @return . */ public boolean isDoubleColLayout() { return doubleColLayout; } }