BrightSide Workbench Full Report + Source Code
PhaseControl.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2022 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.phase;
20 
21 import java.util.ArrayList;
22 import java.util.SortedSet;
23 import org.turro.elephant.util.Images;
24 import org.turro.i18n.I_;
25 import org.turro.zkoss.label.LabelTypes;
26 import org.zkoss.zk.ui.Component;
27 import org.zkoss.zk.ui.event.Event;
28 import org.zkoss.zk.ui.event.Events;
29 import org.zkoss.zul.Hlayout;
30 import org.zkoss.zul.Label;
31 import org.zkoss.zul.Space;
32 import org.zkoss.zul.Toolbarbutton;
33 
38 public class PhaseControl extends Hlayout {
39 
40  private Phase phase;
41  private final ArrayList<Component> comps = new ArrayList<>();
42 
43  public Phase getPhase() {
44  return phase;
45  }
46 
47  public void setPhase(Phase phase) {
48  this.phase = phase;
49  addComponents();
50  }
51 
52  private void addComponents() {
53  if(phase == null) return;
54  setSclass("z-valign-middle");
55  setValign("middle");
56  for(Component c : comps) {
57  c.detach();
58  }
59  comps.clear();
60  comps.add(LabelTypes.getSoftLabel(I_.get("Phase")));
61  comps.add(new Label(I_.get(phase.getCurrent().getName())));
62  SortedSet<PhaseDefinition> possible = phase.getPossiblePhases();
63  if(!possible.isEmpty()) {
64  comps.add(new Space());
65  comps.add(LabelTypes.getSoftLabel(I_.get("Change to")));
66  }
67  for(final PhaseDefinition e : possible) {
68  if(phase.canChangeTo(e)) {
69  Toolbarbutton button = new Toolbarbutton(
70  I_.get(e.getName()), Images.getImage("forward"));
71  button.addEventListener(Events.ON_CLICK, (Event event) -> {
72  phase.changeTo(e);
73  if(phase.changed) {
74  Events.postEvent(new Event(Events.ON_CHANGE));
75  setPhase(phase);
76  }
77  });
78  comps.add(button);
79  }
80  }
81  Component first = getFirstChild();
82  for(Component c : comps) {
83  insertBefore(c, first);
84  }
85  }
86 
87 }
void setPhase(Phase phase)
PhaseDefinition getCurrent()
Definition: Phase.java:38
SortedSet< PhaseDefinition > getPossiblePhases()
Definition: Phase.java:91
boolean canChangeTo(PhaseDefinition newPhase)
Definition: Phase.java:76