BrightSide Workbench Full Report + Source Code
StatusControl.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.status;
20 
21 import java.util.ArrayList;
22 import java.util.Set;
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.EventListener;
29 import org.zkoss.zk.ui.event.Events;
30 import org.zkoss.zul.Hlayout;
31 import org.zkoss.zul.Label;
32 import org.zkoss.zul.Space;
33 import org.zkoss.zul.Toolbarbutton;
34 
39 public class StatusControl extends Hlayout {
40 
41  private Status status;
42  private final ArrayList<Component> comps = new ArrayList<Component>();
43 
44  public Status getStatus() {
45  return status;
46  }
47 
48  public void setStatus(Status status) {
49  this.status = status;
50  addComponents();
51  }
52 
53  private void addComponents() {
54  if(status == null) return;
55  setSclass("z-valign-middle");
56  setValign("middle");
57  for(Component c : comps) {
58  c.detach();
59  }
60  comps.clear();
61  comps.add(LabelTypes.getSoftLabel(I_.get("Status")));
62  comps.add(new Label(I_.byKey(status.getName())));
63  Set possible = status.getPossibleStatus();
64  if(!possible.isEmpty()) {
65  comps.add(new Space());
66  comps.add(LabelTypes.getSoftLabel(I_.get("Change to")));
67  }
68  for(final Object e : possible) {
69  if(status.canChangeTo((Enum) e)) {
70  Toolbarbutton button = new Toolbarbutton(
71  I_.get(e.toString()), Images.getImage("forward"));
72  button.addEventListener(Events.ON_CLICK, new EventListener() {
73  @Override
74  public void onEvent(Event event) throws Exception {
75  status.changeTo((Enum) e);
76  if(status.changed) {
77  Events.postEvent(new Event(Events.ON_CHANGE));
78  setStatus(status);
79  }
80  }
81  });
82  comps.add(button);
83  }
84  }
85  Component first = getFirstChild();
86  for(Component c : comps) {
87  insertBefore(c, first);
88  }
89  }
90 
91 }
void setStatus(Status status)