BrightSide Workbench Full Report + Source Code
VariablesGrid.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.valuation;
20 
21 import java.util.TreeSet;
22 import org.turro.i18n.I_;
23 import org.turro.zkoss.grid.PagingGrid;
24 import org.turro.zkoss.input.Percentbox;
25 import org.turro.zkoss.label.LabelTip;
26 import org.zkoss.zk.ui.event.Event;
27 import org.zkoss.zk.ui.event.EventListener;
28 import org.zkoss.zk.ui.event.Events;
29 import org.zkoss.zul.*;
30 
35 public class VariablesGrid extends PagingGrid {
36 
37  protected ValuationModel model;
38  protected String currencyFormat = "#0.00",
39  unitFormat = "#0.000";
40 
41  public void setValuationModel(ValuationModel model) {
42  this.model = model;
43  addColumns();
44  addRows();
45  }
46 
47  protected void addRows() {
48  Rows rows = getRows(true);
49  rows.getChildren().clear();
50 
51  TreeSet<Variable> vars = new TreeSet<Variable>(new VariableOrderComparator());
52  vars.addAll(model.getVariables());
53 
54  for(Variable var : vars) {
55  processVariable(var);
56  }
57 
58  setRowCount(rows.getChildren().size());
59  }
60 
61  private void processVariable(final Variable var) {
62  Row row = new Row();
63  row.setValue(var);
64 
65  row.setSclass(var.getType() + "-var-row");
66 
67  if(var.isAlwaysApply()) {
68  final Checkbox apply = new Checkbox();
69  apply.setChecked(var.isApply());
70  apply.addEventListener(Events.ON_CHECK, new EventListener() {
71  @Override
72  public void onEvent(Event event) throws Exception {
73  var.setApply(apply.isChecked());
74  Events.postEvent(new Event(Events.ON_CHANGE, VariablesGrid.this));
75  }
76  });
77  row.appendChild(apply);
78  } else {
79  row.appendChild(new Space());
80  }
81 
82  Hlayout hbox = new Hlayout();
83  hbox.appendChild(new Image("/_zul/images/" + var.getType() + ".png"));
84  hbox.appendChild(new LabelTip(var.getName()));
85  row.appendChild(hbox);
86 
87  final Percentbox structure = new Percentbox();
88  structure.setInplace(true);
89  structure.setValue(var.getStructureMargin());
90  structure.addEventListener(Events.ON_CHANGE, new EventListener() {
91  @Override
92  public void onEvent(Event event) throws Exception {
93  var.setStructureMargin(structure.getValue());
94  Events.postEvent(new Event(Events.ON_CHANGE, VariablesGrid.this));
95  }
96  });
97  row.appendChild(structure);
98 
99  final Percentbox profit = new Percentbox();
100  profit.setInplace(true);
101  profit.setValue(var.getProfitMargin());
102  profit.addEventListener(Events.ON_CHANGE, new EventListener() {
103  @Override
104  public void onEvent(Event event) throws Exception {
105  var.setProfitMargin(profit.getValue());
106  Events.postEvent(new Event(Events.ON_CHANGE, VariablesGrid.this));
107  }
108  });
109  row.appendChild(profit);
110 
111  getRows().appendChild(row);
112  }
113 
114  protected void addColumns() {
115  Columns cols = new Columns();
116  cols.setSizable(true);
117  appendChild(cols);
118 
119  Column col = new Column();
120  col.setLabel("");
121  col.setWidth("50px");
122  cols.appendChild(col);
123 
124  col = new Column();
125  col.setLabel(I_.get("Variable"));
126  col.setHflex("1");
127  cols.appendChild(col);
128 
129  col = new Column();
130  col.setLabel(I_.get("Structure margin"));
131  col.setWidth("120px");
132  cols.appendChild(col);
133 
134  col = new Column();
135  col.setLabel(I_.get("Profit margin"));
136  col.setWidth("120px");
137  cols.appendChild(col);
138  }
139 
140 }
static String get(String msg)
Definition: I_.java:41
void setValuationModel(ValuationModel model)
Rows getRows(boolean create)