BrightSide Workbench Full Report + Source Code
Dialogs.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.zkoss.dialog;
20 
21 import java.util.ArrayList;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.function.Consumer;
25 import org.turro.zul.frame.Framework;
26 import org.zkoss.zk.ui.Executions;
27 import org.zkoss.zk.ui.HtmlBasedComponent;
28 import org.zkoss.zk.ui.Page;
29 import org.zkoss.zk.ui.event.Event;
30 import org.zkoss.zk.ui.event.EventListener;
31 import org.zkoss.zk.ui.event.Events;
32 import org.zkoss.zk.ui.ext.AfterCompose;
33 import org.zkoss.zul.Borderlayout;
34 import org.zkoss.zul.Button;
35 import org.zkoss.zul.Center;
36 import org.zkoss.zul.Hlayout;
37 import org.zkoss.zul.Include;
38 import org.zkoss.zul.South;
39 import org.zkoss.zul.Vlayout;
40 import org.zkoss.zul.Window;
41 
46 public class Dialogs extends Window {
47 
48  private final DialogEvents events;
49  private final List<DialogField> fields;
50  private final List<HtmlBasedComponent> components;
51  private Borderlayout blayout;
52  private Vlayout fieldsContainer;
53  private Hlayout buttonsContainer;
54 
55  public Dialogs page(Page page) {
56  setPage(page);
57  return this;
58  }
59 
60  public Dialogs position(String position) {
61  setPosition(position);
62  return this;
63  }
64 
65  public Dialogs closeable() {
66  setClosable(true);
67  return this;
68  }
69 
70  public Dialogs width(String width) {
71  setWidth(width);
72  return this;
73  }
74 
75  public Dialogs height(String height) {
76  setHeight(height);
77  return this;
78  }
79 
80  public Dialogs sizeable() {
81  setSizable(true);
82  return this;
83  }
84 
85  public Dialogs onOk(Consumer<Dialogs> onOk) {
86  events.add(new DialogEvent(DialogButton.BUTTON_OK, onOk));
87  return this;
88  }
89 
90  public Dialogs onCancel(Consumer<Dialogs> onCancel) {
92  return this;
93  }
94 
95  public Dialogs emptyCancel() {
96  events.add(new DialogEvent(DialogButton.BUTTON_CANCEL, null));
97  return this;
98  }
99 
100  public Dialogs onClose(Consumer<Dialogs> onClose) {
101  events.add(new DialogEvent(DialogButton.BUTTON_CLOSE, onClose));
102  return this;
103  }
104 
105  public Dialogs emptyClose() {
106  events.add(new DialogEvent(DialogButton.BUTTON_CLOSE, null));
107  return this;
108  }
109 
110  public Dialogs addField(DialogField field) {
111  fields.add(field.initField());
112  return this;
113  }
114 
115  public Dialogs addComponent(HtmlBasedComponent component) {
116  components.add(component);
117  return this;
118  }
119 
120  public Dialogs addComponent(String uri, Map args) {
121  return addComponent(uri, "100%", args);
122  }
123 
124  public Dialogs addComponent(String uri, String height, Map args) {
125  Include include = new Include("/WEB-INF/_zul" + uri);
126  if(args != null) {
127  args.forEach((k, v) -> include.setDynamicProperty((String) k, v));
128  }
129  include.setHeight(height);
130  components.add(include);
131  return this;
132  }
133 
134  public void show() {
135  createContainer();
136  createComponents();
137  createFields();
138  createButtons();
139  if(getPage() == null) {
140  setPage(getMeAPage());
141  }
142  doModal();
143  }
144 
145  /* Fields */
146 
147  public <V> V getValue(String label, Class<V> javaClass) {
148  return (V) fields.stream().filter(f -> label.equals(f.getLabel())).findFirst().get().getEditorValue();
149  }
150 
151  public Object getValue(String label) {
152  return fields.stream().filter(f -> label.equals(f.getLabel())).findFirst().get().getEditorValue();
153  }
154 
155  public <E> E getEditor(String label) {
156  return (E) fields.stream().filter(f -> label.equals(f.getLabel())).findFirst().get().getEditor();
157  }
158 
159  /* Factory */
160 
161  public static Dialogs title(String title) {
162  return new Dialogs(title);
163  }
164 
165  private Dialogs( String title) {
166  super(title, "normal", false);
167  fields = new ArrayList<>();
168  components = new ArrayList<>();
169  events = new DialogEvents();
170  setPosition("center");
171  }
172 
173  /* Page */
174 
175  private Page getMeAPage() {
176  Page page = getPage();
177  if(page == null) {
178  if(Framework.getCurrent() != null) {
179  page = Framework.getCurrent().getPage();
180  } else {
181  page = Executions.getCurrent().getDesktop().getFirstPage();
182  }
183  }
184  return page;
185  }
186 
187  /* Container */
188 
189  private void createContainer() {
190  blayout = new Borderlayout();
191  blayout.setVflex("1");
192  blayout.setHflex("1");
193  Center center = new Center();
194  center.setBorder("none");
195  center.setStyle("overflow-y:auto");
196  fieldsContainer = new Vlayout();
197  fieldsContainer.setSclass("inputDialogContainer");
198  center.appendChild(fieldsContainer);
199  blayout.appendChild(center);
200  if(!events.isEmpty()) {
201  South south = new South();
202  south.setBorder("none");
203  south.setHeight("50px");
204  south.setStyle("padding-top:8px");
205  blayout.appendChild(south);
206  buttonsContainer = new Hlayout();
207  buttonsContainer.setSclass("inputDialogContainer");
208  south.appendChild(buttonsContainer);
209  }
210  appendChild(blayout);
211  }
212 
213  private void createFields() {
214  fields.forEach(field -> {
215  fieldsContainer.appendChild(field);
216  if(field.getEditor() instanceof AfterCompose) {
217  ((AfterCompose) field.getEditor()).afterCompose();
218  }
219  });
220  }
221 
222  private void createComponents() {
223  components.forEach(component -> {
224  fieldsContainer.appendChild(component);
225  if(component instanceof AfterCompose) {
226  ((AfterCompose) component).afterCompose();
227  }
228  });
229  }
230 
231  private boolean defaultDone = false;
232 
233  private void createButtons() {
234  if(!events.isEmpty()) {
235  Hlayout butCont = new Hlayout();
236  butCont.setSclass("buttonDialogContainer");
237  butCont.setHflex("true");
238  butCont.setStyle("padding:10px;text-align:right");
239  butCont.setSpacing("15px");
240  buttonsContainer.appendChild(butCont);
241  events.forEach(dialogEvent -> {
242  Button b = dialogEvent.getButton().getButton();
243  EventListener el = (EventListener) (Event event) -> {
244  if(dialogEvent.getOnClik() != null) dialogEvent.getOnClik().accept(Dialogs.this);
245  Events.postEvent(new Event(Events.ON_CLOSE, Dialogs.this));
246  };
247  b.addEventListener(Events.ON_CLICK, el);
248  if(!defaultDone) {
249  b.setStyle("font-weight:bold");
250  blayout.addEventListener(Events.ON_OK, el);
251  defaultDone = true;
252  }
253  butCont.appendChild(b);
254  });
255  }
256  }
257 
258 }
Dialogs width(String width)
Definition: Dialogs.java:70
Object getValue(String label)
Definition: Dialogs.java:151
Dialogs height(String height)
Definition: Dialogs.java:75
Dialogs onOk(Consumer< Dialogs > onOk)
Definition: Dialogs.java:85
Dialogs position(String position)
Definition: Dialogs.java:60
Dialogs page(Page page)
Definition: Dialogs.java:55
Dialogs addComponent(String uri, Map args)
Definition: Dialogs.java:120
Dialogs addComponent(String uri, String height, Map args)
Definition: Dialogs.java:124
Dialogs onClose(Consumer< Dialogs > onClose)
Definition: Dialogs.java:100
static Dialogs title(String title)
Definition: Dialogs.java:161
Dialogs addComponent(HtmlBasedComponent component)
Definition: Dialogs.java:115
Dialogs onCancel(Consumer< Dialogs > onCancel)
Definition: Dialogs.java:90
Dialogs addField(DialogField field)
Definition: Dialogs.java:110