BrightSide Workbench Full Report + Source Code
IssueRelatedTree.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.dossier.zul.relation;
19 
20 import java.util.List;
21 import java.util.Set;
22 import org.turro.dossier.entity.Issue;
23 import org.turro.dossier.entity.IssuePredecessor;
24 import org.turro.i18n.I_;
25 import org.turro.registry.ChangeCategory;
26 import org.turro.registry.Changes;
27 import org.turro.registry.UniqueString;
28 import org.zkoss.zk.ui.event.Event;
29 import org.zkoss.zk.ui.event.EventListener;
30 import org.zkoss.zk.ui.event.Events;
31 import org.zkoss.zul.*;
32 
37 public class IssueRelatedTree extends Tree {
38 
39  private Treechildren children;
40  private Toolbar toolbar;
41  private Toolbarbutton addRelation, editRelation, deleteRelation;
42  private Issue issue;
43  private Changes changes;
44 
45  public IssueRelatedTree() {
46  super();
47  addColumns();
48  addChildrenSpace();
49  }
50 
51  public Changes getChanges() {
52  return changes;
53  }
54 
55  public void setChanges(Changes changes) {
56  this.changes = changes;
57  }
58 
59  public Issue getIssue() {
60  return issue;
61  }
62 
63  public void setIssue(Issue issue) {
64  this.issue = issue;
65  removeChild(children);
66  addChildrenSpace();
67  addRoots(new IssueRelation(null, issue));
68  openChildrenWithIssue(children.getChildren());
69  }
70 
71  public void setAddToolbar(boolean addToolbar) {
72  if(addToolbar && toolbar == null) {
73  toolbar = new Toolbar();
74  toolbar.setStyle("width:100%");
75  getParent().appendChild(toolbar);
76  addToolbarButtons();
77  addEventListener(Events.ON_SELECT, new EventListener() {
78  @Override
79  public void onEvent(Event event) throws Exception {
80  updateButtons();
81  }
82  });
83  updateButtons();
84  }
85  }
86 
87  private void addColumns() {
88  Treecols cols = new Treecols();
89  cols.setSizable(true);
90  appendChild(cols);
91 
92  Treecol col = new Treecol();
93  col.setLabel("");
94  cols.appendChild(col);
95 
96  col = new Treecol();
97  col.setLabel(I_.get("Status"));
98  col.setWidth("120px");
99  cols.appendChild(col);
100 
101  col = new Treecol();
102  col.setLabel(I_.get("Responsible"));
103  col.setWidth("230px");
104  cols.appendChild(col);
105  }
106 
107  private void addChildrenSpace() {
108  children = new Treechildren();
109  appendChild(children);
110  }
111 
112  private void addRoots(IssueRelation current) {
113  Set<IssuePredecessor> ips = current.getIssue().getSources();
114  if(ips.isEmpty()) {
115  children.appendChild(current);
116  current.setOpen(true);
117  } else {
118  for(IssuePredecessor ip : ips) {
119  addRoots(new IssueRelation(null, ip.getSource()));
120  }
121  }
122  }
123 
124  private void addToolbarButtons() {
125  addRelation = new Toolbarbutton(
126  I_.get("Add relation"),
127  "/_zul/images/new.png"
128  );
129  addRelation.addEventListener(Events.ON_CLICK, new EventListener() {
130  @Override
131  public void onEvent(Event event) throws Exception {
132  if(IssueRelatedTree.this.getSelectedItem() instanceof IssueRelation) {
133  IssueRelation ir = (IssueRelation) IssueRelatedTree.this.getSelectedItem();
134  ir.doAddRelation(changes);
135  }
136  }
137  });
138  toolbar.appendChild(addRelation);
139  editRelation = new Toolbarbutton(
140  I_.get("Edit"),
141  "/_zul/images/edit.png"
142  );
143  editRelation.addEventListener(Events.ON_CLICK, new EventListener() {
144  @Override
145  public void onEvent(Event event) throws Exception {
146  if(IssueRelatedTree.this.getSelectedItem() instanceof IssueRelation) {
147  IssueRelation ir = (IssueRelation) IssueRelatedTree.this.getSelectedItem();
148  ir.doEditRelation(changes);
149  }
150  }
151  });
152  toolbar.appendChild(editRelation);
153  deleteRelation = new Toolbarbutton(
154  I_.get("Delete relation"),
155  "/_zul/images/edit-delete.png"
156  );
157  deleteRelation.addEventListener(Events.ON_CLICK, new EventListener() {
158  @Override
159  public void onEvent(Event event) throws Exception {
160  if(IssueRelatedTree.this.getSelectedItem() instanceof IssueRelation) {
161  IssueRelation ir = (IssueRelation) IssueRelatedTree.this.getSelectedItem();
162  if(changes != null) {
163  ChangeCategory cc = new ChangeCategory(3, I_.get("Relations"));
164  changes.addChange(new UniqueString(cc, "", "-" + ir.getPredecessor().getFullDescription()));
165  }
166  ir.doDeleteRelation();
167  }
168  }
169  });
170  toolbar.appendChild(deleteRelation);
171  }
172 
173  private void openChildrenWithIssue(List children) {
174  for(IssueRelation ir : (List<IssueRelation>) children) {
175  if(ir.getIssue() != null && ir.getIssue().getId() != issue.getId()) {
176  ir.setOpen(true);
177  openChildrenWithIssue(ir.getRelatedChildren());
178  break;
179  }
180  }
181  }
182 
183  private void updateButtons() {
184  if(toolbar != null) {
185  boolean isSelected = getSelectedItem() instanceof IssueRelation;
186  addRelation.setVisible(isSelected);
187  editRelation.setVisible(isSelected && ((IssueRelation) getSelectedItem()).canEdit());
188  deleteRelation.setVisible(isSelected && ((IssueRelation) getSelectedItem()).canDelete());
189  }
190  }
191 
192 }
static String get(String msg)
Definition: I_.java:41