BrightSide Workbench Full Report + Source Code
SectionsGrid.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2018 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.newsletter;
20 
21 import java.util.TreeSet;
22 import org.turro.string.Strings;
23 import org.turro.elephant.util.Images;
24 import org.turro.i18n.I_;
25 import org.turro.jpa.entity.EntityCollections;
26 import org.turro.publication.entity.NewsSection;
27 import org.turro.publication.entity.NewsSectionType;
28 import org.turro.publication.entity.Newsletter;
29 import org.turro.zkoss.label.LabelTypes;
30 import org.turro.zkoss.layout.GroupboxArrow;
31 import org.zkoss.zk.ui.HtmlBasedComponent;
32 import org.zkoss.zk.ui.event.Event;
33 import org.zkoss.zk.ui.event.EventListener;
34 import org.zkoss.zk.ui.event.Events;
35 import org.zkoss.zul.Button;
36 import org.zkoss.zul.Hlayout;
37 import org.zkoss.zul.Html;
38 import org.zkoss.zul.Image;
39 import org.zkoss.zul.Include;
40 import org.zkoss.zul.Label;
41 import org.zkoss.zul.Vlayout;
42 
47 public class SectionsGrid extends Vlayout {
48 
49  private Newsletter newsletter;
50 
51  public void setNewsletter(Newsletter newsletter) {
52  this.newsletter = newsletter;
53  addRows();
54  }
55 
56  private void addRows() {
57  getChildren().clear();
58  int idx = 1;
59  for(NewsSection newsSection : new TreeSet<NewsSection>(newsletter.getNewsSections())) {
60  newsSection.setNewsOrder(idx++);
61  GroupboxArrow gba = new GroupboxArrow() {
62  @Override
63  protected void doFillContent() {
64  Include sectionBand = new Include("/WEB-INF/_zul/publication/sectionBand.zul");
65  sectionBand.setDynamicProperty("entity", newsSection);
66  sectionBand.setDynamicProperty("grid", SectionsGrid.this);
67  appendChild(sectionBand);
68  if(NewsSectionType.NEWS_SECTION_TEXT.equals(newsSection.getType())) {
69  Include textBand = new Include("/WEB-INF/_zul/publication/textBand.zul");
70  textBand.setDynamicProperty("entity", newsSection);
71  appendChild(textBand);
72  } else if(NewsSectionType.NEWS_SECTION_IMAGE.equals(newsSection.getType())) {
73  Include imageBand = new Include("/WEB-INF/_zul/publication/imageBand.zul");
74  imageBand.setDynamicProperty("entity", newsSection);
75  appendChild(imageBand);
76  } else if(NewsSectionType.NEWS_SECTION_EXTERNAL.equals(newsSection.getType())) {
77  Include externalBand = new Include("/WEB-INF/_zul/publication/externalBand.zul");
78  externalBand.setDynamicProperty("entity", newsSection);
79  appendChild(externalBand);
80  }
81  }
82  };
83  Hlayout hbox = new Hlayout();
84  gba.setCaption(hbox);
85  hbox.setSclass("z-valign-middle");
86  hbox.setValign("middle");
87  hbox.appendChild(new Image(Images.getImage(newsSection.isNewColumn() ? "table_column" : "table_row")));
88  hbox.appendChild(LabelTypes.getSoftLabel(I_.byKey(newsSection.getType().toString())));
89  hbox.appendChild(getLabel(newsSection));
90  gba.setOpen(false);
91  appendChild(gba);
92  }
93  Hlayout buttonBox = new Hlayout();
94  buttonBox.setSpacing("20px");
95  buttonBox.setStyle("margin:20px;");
96  Button addImage = new Button(I_.get("Add") + " " + I_.get("Image section"));
97  addImage.addEventListener(Events.ON_CLICK, new EventListener<Event>() {
98  @Override
99  public void onEvent(Event event) throws Exception {
100  createSection(NewsSectionType.NEWS_SECTION_IMAGE);
101  }
102  });
103  buttonBox.appendChild(addImage);
104  Button addText = new Button(I_.get("Add") + " " + I_.get("Text section"));
105  addText.addEventListener(Events.ON_CLICK, new EventListener<Event>() {
106  @Override
107  public void onEvent(Event event) throws Exception {
108  createSection(NewsSectionType.NEWS_SECTION_TEXT);
109  }
110  });
111  buttonBox.appendChild(addText);
112  Button addExternal = new Button(I_.get("Add") + " " + I_.get("External section"));
113  addExternal.addEventListener(Events.ON_CLICK, new EventListener<Event>() {
114  @Override
115  public void onEvent(Event event) throws Exception {
116  createSection(NewsSectionType.NEWS_SECTION_EXTERNAL);
117  }
118  });
119  buttonBox.appendChild(addExternal);
120  appendChild(buttonBox);
121  }
122 
123  public void deleteSection(NewsSection newsSection) {
124  EntityCollections.entities(newsletter.getNewsSections()).remove(newsSection);
125  newsSection.setNewsletter(null);
126  setNewsletter(newsletter);
127  }
128 
129  public void moveUp(NewsSection newsSection) {
130  for(NewsSection ns : newsletter.getNewsSections()) {
131  if(ns.getNewsOrder() == newsSection.getNewsOrder() - 1) {
132  ns.setNewsOrder(newsSection.getNewsOrder());
133  newsSection.setNewsOrder(newsSection.getNewsOrder() - 1);
134  setNewsletter(newsletter);
135  break;
136  }
137  }
138  }
139 
140  public void moveDown(NewsSection newsSection) {
141  for(NewsSection ns : newsletter.getNewsSections()) {
142  if(ns.getNewsOrder() == newsSection.getNewsOrder() + 1) {
143  ns.setNewsOrder(newsSection.getNewsOrder());
144  newsSection.setNewsOrder(newsSection.getNewsOrder() + 1);
145  setNewsletter(newsletter);
146  break;
147  }
148  }
149  }
150 
151  private void createSection(NewsSectionType type) {
152  NewsSection e = new NewsSection();
153  e.setNewsletter(newsletter);
154  e.setType(type);
155  e.setNewsOrder(newsletter.getNewsSections().size() + 1);
156  newsletter.getNewsSections().add(e);
157  setNewsletter(newsletter);
158  }
159 
160  private HtmlBasedComponent getLabel(NewsSection newsSection) {
161  if(newsSection.isEmpty()) {
162  return new Label(I_.get("Empty"));
163  } else {
164  if(NewsSectionType.NEWS_SECTION_TEXT.equals(newsSection.getType())) {
165  return new Label(Strings.truncateAndWarn(newsSection.getWiki(), 100));
166  } else if(NewsSectionType.NEWS_SECTION_IMAGE.equals(newsSection.getType())) {
167  return new Html("<div style='max-width:23px;max-height:23px;'>" + newsSection.getBody() + "</div>");
168  } else if(NewsSectionType.NEWS_SECTION_EXTERNAL.equals(newsSection.getType())) {
169  return new Label(Strings.truncateAndWarn(newsSection.getBody(), 100));
170  } else {
171  return new Label(I_.get("Empty"));
172  }
173  }
174  }
175 
176 }
static EntityCollections entities(Collection values)
void moveDown(NewsSection newsSection)
void moveUp(NewsSection newsSection)
void setNewsletter(Newsletter newsletter)
void deleteSection(NewsSection newsSection)
void setNewsletter(Newsletter newsletter)
void setType(NewsSectionType type)