BrightSide Workbench Full Report + Source Code
DescribeItComposer.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2013 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.zul.describeit;
20 
21 import java.util.Date;
22 import java.util.List;
23 import java.util.logging.Level;
24 import java.util.logging.Logger;
25 import org.turro.auth.Authentication;
26 import org.turro.composer.EntityPathComposer;
27 import org.turro.contacts.Contact;
28 import org.turro.contacts.DescribeIt;
29 import org.turro.contacts.db.ContactsPU;
30 import org.turro.describeit.DescribeItUtil;
31 import org.turro.elephant.context.ElephantContext;
32 import org.turro.i18n.I_;
33 import org.turro.jpa.Dao;
34 import org.turro.parser.wiki.WikiCompiler;
35 import org.turro.zkoss.dialog.PreviewHtml;
36 import org.turro.zkoss.text.WikiEditor;
37 import org.zkoss.lang.Strings;
38 import org.zkoss.zk.ui.Component;
39 import org.zkoss.zk.ui.event.EventListener;
40 import org.zkoss.zk.ui.event.Events;
41 import org.zkoss.zk.ui.event.InputEvent;
42 import org.zkoss.zk.ui.select.annotation.Listen;
43 import org.zkoss.zk.ui.select.annotation.Wire;
44 import org.zkoss.zul.Tab;
45 import org.zkoss.zul.Tabbox;
46 import org.zkoss.zul.Tabpanel;
47 
52 public class DescribeItComposer extends EntityPathComposer {
53 
54  private List<DescribeIt> list;
55  private int currentIndex = 0;
56 
57  @Wire("#dieditors")
58  private Tabbox dieditors;
59 
60  @Listen("onClick = #preview")
61  public void onPreview() {
62  try {
63  PreviewHtml.preview(list.get(currentIndex).getBody());
64  } catch (InterruptedException ex) {
65  Logger.getLogger(DescribeItComposer.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
66  }
67  }
68 
69  @Listen("onClick = #save")
70  public void onSave() {
71  Dao dao = new ContactsPU();
72  DescribeIt di = list.get(currentIndex);
73  if(!Strings.isBlank(di.getBody())) {
74  di = dao.saveObject(di);
75  list.set(currentIndex, di);
76  Tabpanel tp = (Tabpanel) dieditors.getTabpanels().getChildren().get(currentIndex);
77  tp.getChildren().clear();
78  tp.appendChild(createWiki(di));
79  } else if(di.getId() != null) {
80  dao.deleteObject(di);
81  }
82  }
83 
84  @Override
85  public void doAfterCompose(Component comp) throws Exception {
86  super.doAfterCompose(comp);
87  list = (List<DescribeIt>) DescribeItUtil.descriptions(entityPath);
88  if(list == null || list.isEmpty()) {
89  DescribeIt di = new DescribeIt();
91  di.setDateCreation(new Date());
92  di.setPath(entityPath);
94  list.add(di);
95  }
96  currentIndex = 0;
97  for(DescribeIt di : list) {
98  String label = DescribeItUtil.DEFAULT_ID.equals(di.getDescribeId()) ?
100  di.getDescribeId();
101  dieditors.getTabs().appendChild(new Tab(label));
102  Tabpanel tp = new Tabpanel();
103  tp.appendChild(createWiki(di));
104  dieditors.getTabpanels().appendChild(tp);
105  }
106  }
107 
108  private WikiEditor createWiki(final DescribeIt describeIt) {
109  WikiEditor wiki = new WikiEditor();
110  wiki.setWidth("100%");
111  wiki.setHeight("100%");
112  wiki.setValue(describeIt.getWiki());
113  wiki.setImageFolder("/_internal/files" + describeIt.getPath());
114  wiki.setFileFolder("/_internal/files" + describeIt.getPath());
115  wiki.setReadonly(false);
116  wiki.addEventListener(Events.ON_CHANGE, new EventListener<InputEvent>() {
117  @Override
118  public void onEvent(InputEvent event) throws Exception {
119  describeIt.setWiki(event.getValue());
120  describeIt.setBody(WikiCompiler.source(describeIt.getWiki()).html());
121  }
122  });
123  return wiki;
124  }
125 
126 }
void setPath(String path)
Definition: DescribeIt.java:86
void setDescribeId(String describeId)
Definition: DescribeIt.java:78
void setDateCreation(Date dateCreation)
void setCreator(Contact creator)
Definition: DescribeIt.java:94
static Collection< DescribeIt > descriptions(Object entity)
static String get(String msg)
Definition: I_.java:41
void deleteObject(Object obj)
Definition: Dao.java:162
static void preview(String html)
void setReadonly(boolean value)
void setImageFolder(String folder)
void setValue(String value)
Definition: WikiEditor.java:98
void setFileFolder(String folder)