BrightSide Workbench Full Report + Source Code
bsfinancials-www/src/main/java/org/turro/financials/relation/DocumentBox.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 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 package org.turro.financials.relation;
19 
20 import org.turro.string.Strings;
21 import org.turro.elephant.util.DateFormats;
22 import org.turro.elephant.util.DecimalFormats;
23 import org.turro.elephant.util.Messages;
24 import org.turro.financials.db.FinancialsPU;
25 import org.turro.financials.entity.Document;
26 import org.turro.financials.entity.DocumentRelation;
27 import org.turro.financials.menu.FinancialsMenu;
28 import org.turro.zkoss.label.LabelTypes;
29 import org.zkoss.zk.ui.Component;
30 import org.zkoss.zk.ui.event.Event;
31 import org.zkoss.zk.ui.event.EventListener;
32 import org.zkoss.zk.ui.event.Events;
33 import org.zkoss.zul.*;
34 
39 public class DocumentBox extends Groupbox {
40 
41  private Document document;
42 
43  public DocumentBox(Document document) {
44  this.document = document;
45  setMold("3d");
46  updateBox();
47  }
48 
49  public Document getDocument() {
50  return document;
51  }
52 
53  private void updateBox() {
54  getChildren().clear();
55  Caption caption = new Caption();
56  appendChild(caption);
57  Toolbarbutton docButton = new Toolbarbutton(
58  (Strings.isBlank(document.getDocumentNumber()) ? "*" : document.getDocumentNumber()) +
59  " - " +
60  DateFormats.format(document.getDocumentDate(), true));
61  docButton.addEventListener(Events.ON_CLICK, new EventListener() {
62  @Override
63  public void onEvent(Event event) throws Exception {
64  FinancialsMenu.showDocument(document.getId());
65  }
66  });
67  caption.appendChild(docButton);
68  Hbox hbox = new Hbox();
69  hbox.setWidth("100%");
70  appendChild(hbox);
71  Cell cconnectors = new Cell();
72  cconnectors.setHflex("1");
73  hbox.appendChild(cconnectors);
74  Vlayout connectors = new Vlayout();
75  connectors.setStyle("border-right: solid 1px #ddd");
76  cconnectors.appendChild(connectors);
77  for(DocumentRelation dr : document.getAncestors()) {
78  connectors.appendChild(getDelButton(dr.getId()));
79  }
80  Cell ccontent = new Cell();
81  ccontent.setHflex("4");
82  hbox.appendChild(ccontent);
83  Vlayout content = new Vlayout();
84  content.setStyle("padding-left:5px");
85  ccontent.appendChild(content);
86  if(document.getForcedView() != null) {
87  content.appendChild(LabelTypes.getSoftLabel(document.getForcedView().getName()));
88  }
89  content.appendChild(new Label(DecimalFormats.format(document.getTotalAmount())));
90  cconnectors = new Cell();
91  cconnectors.setHflex("1");
92  hbox.appendChild(cconnectors);
93  connectors = new Vlayout();
94  connectors.setStyle("border-left: solid 1px #ddd");
95  cconnectors.appendChild(connectors);
96  for(DocumentRelation dr : document.getDescendants()) {
97  if(dr.getId() > 0) {
98  connectors.appendChild(getDelButton(dr.getId()));
99  }
100  }
101  }
102 
103  private void notifyChange() {
104  ((RelationshipViewer) getParent().getParent()).notifyChange();
105  }
106 
107  private Component getDelButton(final long id) {
108  Toolbarbutton tb = new Toolbarbutton(id + "", "/_zul/images/12/mini_del.png");
109  tb.setSclass("tinyLabel");
110  tb.setOrient("vertical");
111  tb.addEventListener(Events.ON_CLICK, new EventListener() {
112  @Override
113  public void onEvent(Event event) throws Exception {
114  Messages.confirmDeletion().show(() -> {
115  killRelation(id);
116  new FinancialsPU().executeUpdate(
117  "delete from DocumentRelation where id = ?",
118  new Object[] { id });
119  notifyChange();
120  });
121  }
122  });
123  return tb;
124  }
125 
126  private boolean killRelation(long id) {
127  Document doc = ((RelationshipColumn) getParent()).getDocument();
128  for(DocumentRelation dr : doc.getAncestors()) {
129  if(dr.getId() == id) {
130  doc.getAncestors().remove(dr);
131  return true;
132  }
133  }
134  for(DocumentRelation dr : doc.getDescendants()) {
135  if(dr.getId() == id) {
136  doc.getDescendants().remove(dr);
137  return true;
138  }
139  }
140  return false;
141  }
142 }
static final String format(Date d, boolean dateOnly)
Set< DocumentRelation > getAncestors()
Definition: Document.java:107
Set< DocumentRelation > getDescendants()
Definition: Document.java:139