BrightSide Workbench Full Report + Source Code
Windows.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.Html;
34 import org.zkoss.zul.Include;
35 import org.zkoss.zul.Space;
36 import org.zkoss.zul.Window;
37 
42 public class Windows extends Window {
43 
44  private final List<HtmlBasedComponent> components;
45 
46  public Windows page(Page page) {
47  setPage(page);
48  return this;
49  }
50 
51  public Windows position(String position) {
52  setPosition(position);
53  return this;
54  }
55 
56  public Windows closeable() {
57  setClosable(true);
58  return this;
59  }
60 
61  public Windows width(String width) {
62  setWidth(width);
63  return this;
64  }
65 
66  public Windows height(String height) {
67  setHeight(height);
68  return this;
69  }
70 
71  public Windows sizeable() {
72  setSizable(true);
73  return this;
74  }
75 
76  public Windows scrollable() {
77  setContentStyle("overflow-y:auto");
78  return this;
79  }
80 
81  public Windows onClose(Consumer<Windows> onClose) {
82  if(onClose != null) {
83  EventListener el = (EventListener) (Event event) -> {
84  onClose.accept(Windows.this);
85  detach();
86  };
87  addEventListener(Events.ON_CLOSE, el);
88  }
89  return this;
90  }
91 
92  public Windows addSpace() {
93  components.add(new Space());
94  return this;
95  }
96 
97  public Windows addComponent(HtmlBasedComponent component) {
98  components.add(component);
99  return this;
100  }
101 
102  public Windows addComponent(String uri, Map args) {
103  Include include = new Include("/WEB-INF/_zul" + uri);
104  if(args != null) {
105  args.forEach((k, v) -> include.setDynamicProperty((String) k, v));
106  }
107  components.add(include);
108  return this;
109  }
110 
111  public Windows adaptWidth(int width) {
112  return width((width + WIDTH_ADAPT) + "px");
113  }
114 
115  public Windows adaptHeight(int height) {
116  return height((height + HEIGHT_ADAPT) + "px");
117  }
118 
119  public void show() {
120  createComponents();
121  doModal();
122  }
123 
124  /* Factory */
125 
126  public static void showHtml(String title, String html) {
127  Html component = new Html(html);
129  .addComponent(component)
130  .width("900px")
131  .height("60%")
132  .sizeable()
133  .scrollable()
134  .closeable()
135  .show();
136  }
137 
138  public static Windows title(String title) {
139  return new Windows(title);
140  }
141 
142  private static final int
143  WIDTH_ADAPT = 20,
144  HEIGHT_ADAPT = 54;
145 
146  private Windows( String title) {
147  super(title, "normal", false);
148  components = new ArrayList<>();
149  setPosition("center");
150  }
151 
152  /* Container */
153 
154  private void createComponents() {
155  if(getPage() == null) {
156  if(Framework.getCurrent() != null) {
157  setPage(Framework.getCurrent().getPage());
158  } else {
159  setPage(Executions.getCurrent().getDesktop().getFirstPage());
160  }
161  }
162  components.forEach(component -> {
163  appendChild(component);
164  if(component instanceof AfterCompose) {
165  ((AfterCompose) component).afterCompose();
166  }
167  });
168  }
169 
170 }
static Windows title(String title)
Definition: Windows.java:138
Windows addComponent(HtmlBasedComponent component)
Definition: Windows.java:97
Windows adaptHeight(int height)
Definition: Windows.java:115
Windows width(String width)
Definition: Windows.java:61
Windows position(String position)
Definition: Windows.java:51
static void showHtml(String title, String html)
Definition: Windows.java:126
Windows onClose(Consumer< Windows > onClose)
Definition: Windows.java:81
Windows adaptWidth(int width)
Definition: Windows.java:111
Windows page(Page page)
Definition: Windows.java:46
Windows addComponent(String uri, Map args)
Definition: Windows.java:102
Windows height(String height)
Definition: Windows.java:66