BrightSide Workbench Full Report + Source Code
PredecessorsGrid.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2012 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.erp.task;
20 
21 import org.turro.command.Command;
22 import org.turro.command.Context;
23 import org.turro.elephant.context.Application;
24 import org.turro.elephant.util.ZkossUtils;
25 import org.turro.erp.entity.Predecessor;
26 import org.turro.erp.entity.PredecessorType;
27 import org.turro.erp.entity.Task;
28 import org.turro.jpa.entity.EntityCollectionUtil;
29 import org.turro.zkoss.dialog.InputDialog;
30 import org.turro.zkoss.dialog.InputField;
31 import org.turro.zkoss.grid.GroupExtended;
32 import org.turro.zkoss.grid.PagingGrid;
33 import org.turro.zul.frame.Framework;
34 import org.zkoss.zk.ui.HtmlBasedComponent;
35 import org.zkoss.zk.ui.event.Event;
36 import org.zkoss.zk.ui.event.EventListener;
37 import org.zkoss.zk.ui.event.Events;
38 import org.zkoss.zul.*;
39 
44 public class PredecessorsGrid extends PagingGrid {
45 
46  private Task task;
47  private PredecessorType lastType = null;
48 
49  public Task getTask() {
50  return task;
51  }
52 
53  public void setTask(Task task) {
54  this.task = task;
55  addColumns();
56  addRows();
57  }
58 
59  public void addRelation() {
60  final Predecessor p = new Predecessor();
61  p.setTask(task);
63  editRelation(p, new Command() {
64  @Override
65  public Object execute(Context context) {
66  task.getPredecessors().add(p);
67  addRows();
68  return null;
69  }
70  });
71  }
72 
73  private void editRelation(final Predecessor p, final Command command) {
75  Framework.getCurrent().getPage(),
76  Application.getString("lPredecessor"),
77  new InputField[] {
78  new InputField("lType", null, null, 0) {
79  @Override
80  protected HtmlBasedComponent createEditor() {
81  PredecessorTypeListbox ptl = new PredecessorTypeListbox();
82  ptl.setMold("select");
83  ptl.setObjectValue(p.getType());
84  return ptl;
85  }
86  },
87  new InputField("lTask", null, null, 0) {
88  @Override
89  protected HtmlBasedComponent createEditor() {
90  TaskListbox tl = new TaskListbox();
91  tl.setMold("select");
92  tl.setReference(task);
95  return tl;
96  }
97  },
98  new InputField("lLag", new Double(p.getLag()), null, 2)
99  }, new Command() {
100  @Override
101  public Object execute(Context context) {
102  InputField[] fields = (InputField[]) context.get("fields");
103  if(fields.length > 0) {
104  for(InputField f : fields) {
105  if("lType".equals(f.getLabel())) {
106  p.setType((PredecessorType) f.getValue());
107  } else if("lTask".equals(f.getLabel())) {
108  p.setPredecessor((Task) f.getValue());
109  } else if("lLag".equals(f.getLabel())) {
110  p.setLag((Double) f.getValue());
111  }
112  }
113  if(!p.isEmpty()) {
114  if(command != null) command.execute(context);
115  }
116  }
117  return null;
118  }
119  });
120  }
121 
122  private void addColumns() {
123  Columns cols = getColumns(true);
124  cols.getChildren().clear();
125 
126  Column col = new Column(null, null, "100px");
127  cols.appendChild(col);
128 
129  col = new Column(Application.getString("lTask"));
130  cols.appendChild(col);
131 
132  col = new Column(null, null, "40px");
133  cols.appendChild(col);
134  }
135 
136  private void addRows() {
137  Rows rows = getRows(true);
138  rows.getChildren().clear();
139 
140  for(final Predecessor p : task.getPredecessors()) {
141  if(lastType == null || !p.getType().equals(lastType)) {
142  GroupExtended g = new GroupExtended(Application.getString(p.getType().toString()));
143  rows.appendChild(g);
144  lastType = p.getType();
145  }
146  final Row row = new Row();
147  row.setValign("top");
148  rows.appendChild(row);
149  Button edit = new Button(Application.getString("lEdit"));
150  edit.addEventListener(Events.ON_CLICK, new EventListener() {
151  @Override
152  public void onEvent(Event event) throws Exception {
153  editRelation(p, new Command() {
154  @Override
155  public Object execute(Context context) {
156  addRows();
157  return null;
158  }
159  });
160  }
161  });
162  row.appendChild(edit);
163  row.appendChild(new Label(p.getPredecessor().getFullDescription() + " [" + p.getLag() + "]"));
164  Toolbarbutton del = new Toolbarbutton(null, "/_zul/images/edit-delete.png");
165  del.addEventListener(Events.ON_CLICK, new EventListener() {
166  @Override
167  public void onEvent(Event event) throws Exception {
168  ZkossUtils.confirmDeletion(null, new Command() {
169  @Override
170  public Object execute(Context context) {
171  EntityCollectionUtil.remove(task.getPredecessors(), p);
172  row.detach();
173  return null;
174  }
175  });
176  }
177  });
178  row.appendChild(del);
179  }
180  }
181 
182 }
static String getString(String key)
void setType(PredecessorType type)
void setPredecessor(Task predecessor)
static void getInput(Page page, String title, String label, Object value, String format, int scale, final Command onOk)
static Framework getCurrent()
Definition: Framework.java:203