BrightSide Workbench Full Report + Source Code
ElephantSubmenu.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2020 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.menu;
20 
21 import java.util.Collection;
22 import java.util.function.BiConsumer;
23 import org.turro.string.Strings;
24 import org.turro.elephant.util.Components;
25 import org.turro.elephant.util.Images;
26 import org.turro.i18n.I_;
27 import org.turro.security.Permission;
28 import org.turro.zul.frame.Framework;
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.zul.Menuitem;
33 import org.zkoss.zul.Menupopup;
34 import org.zkoss.zul.Menuseparator;
35 import org.zkoss.zul.Toolbarbutton;
36 
41 public class ElephantSubmenu extends Menupopup {
42 
43  private boolean separator = false;
44 
46  return Components.from(this).parent(ElephantMenuBar.class);
47  }
48 
50  return Components.from(this).parent(ElephantMenu.class);
51  }
52 
54  return Components.from(this).parent(ElephantSubmenu.class);
55  }
56 
57  public ElephantMenu addMenu(String label) {
58  return addMenu(label, null, null);
59  }
60 
61  public ElephantMenu addMenu(String label, String image) {
62  return addMenu(label, image, null);
63  }
64 
65  public ElephantMenu addMenu(String label, Permission permission) {
66  return addMenu(label, null, permission);
67  }
68 
69  public ElephantMenu addMenu(String label, String image, Permission permission) {
70  checkSeparator();
71  ElephantMenu m = image == null ? new ElephantMenu(label) : new ElephantMenu(label, image);
72  appendChild(m);
73  if(permission != null) m.setVisible(permission.check());
74  return m;
75  }
76 
77  public ElephantSubmenu addMenuitem(String label, String include) {
78  return addMenuitem(label, null, include);
79  }
80 
81  public ElephantSubmenu addMenuitem(String label, EventListener listener) {
82  return addMenuitem(label, null, listener);
83  }
84 
85  public ElephantSubmenu addMenuitem(String label, EventListener listener, boolean toolButton) {
86  return addMenuitem(label, null, listener, toolButton);
87  }
88 
89  public ElephantSubmenu addMenuitem(String label, String image, String include) {
90  return addMenuitem(label, image, include, false);
91  }
92 
93  public ElephantSubmenu addMenuitem(String label, String image, EventListener listener) {
94  return addMenuitem(label, image, listener, false);
95  }
96 
97  public ElephantSubmenu addMenuitem(String label, String image, String include, boolean toolButton) {
98  return addMenuitem(label, image, include, toolButton, null);
99  }
100 
101  public ElephantSubmenu addMenuitem(String label, String image, EventListener listener, boolean toolButton) {
102  return addMenuitem(label, image, listener, toolButton, null);
103  }
104 
105  public ElephantSubmenu addMenuitem(String label, String include, Permission permission) {
106  return addMenuitem(label, null, include, permission);
107  }
108 
109  public ElephantSubmenu addMenuitem(String label, EventListener listener, Permission permission) {
110  return addMenuitem(label, null, listener, permission);
111  }
112 
113  public ElephantSubmenu addMenuitem(String label, String include, boolean toolButton, Permission permission) {
114  return addMenuitem(label, null, include, toolButton, permission);
115  }
116 
117  public ElephantSubmenu addMenuitem(String label, EventListener listener, boolean toolButton, Permission permission) {
118  return addMenuitem(label, null, listener, toolButton, permission);
119  }
120 
121  public ElephantSubmenu addMenuitem(String label, String image, String include, Permission permission) {
122  return addMenuitem(label, image, include, false, permission);
123  }
124 
125  public ElephantSubmenu addMenuitem(String label, String image, EventListener listener, Permission permission) {
126  return addMenuitem(label, image, listener, false, permission);
127  }
128 
129  public ElephantSubmenu addMenuitem(String label, String image, String include, boolean toolButton, Permission permission) {
130  if(permission == null || permission.check()) {
131  EventListener listener = null;
132  if(include != null) {
133  listener = (EventListener) (Event event) -> {
135  };
136  }
137  return addMenuitem(label, image, listener, toolButton, permission);
138  }
139  return this;
140  }
141 
142  public ElephantSubmenu addMenuitem(String label, String image, EventListener listener, boolean toolButton, Permission permission) {
143  if(permission == null || permission.check()) {
144  ElephantToolbar toolbar = getToolbar();
145  if(toolButton && toolbar != null) {
146  Toolbarbutton button = new Toolbarbutton();
147  toolbar.addComponent(button);
148  button.setTooltiptext(I_.get(label));
149  if(!Strings.isBlank(image)) {
150  button.setImage(Images.getImage(image));
151  }
152  if(listener != null) {
153  button.addEventListener(Events.ON_CLICK, listener);
154  }
155  }
156  checkSeparator();
157  Menuitem mi = new Menuitem(I_.get(label));
158  if(!Strings.isBlank(image)) {
159  mi.setImage(Images.getImage(image));
160  }
161  appendChild(mi);
162  if(listener != null) {
163  mi.addEventListener(Events.ON_CLICK, listener);
164  }
165  }
166  return this;
167  }
168 
170  if(!getChildren().isEmpty()) separator = true;
171  return this;
172  }
173 
174  public ElephantSubmenu addExternalsToSession(Collection<ElephantMenu> externals) {
175  for(ElephantMenu em : externals) {
176  em.addToSessionMenu(this);
177  }
178  return this;
179  }
180 
181  public ElephantSubmenu addExternalsToTools(Collection<ElephantMenu> externals) {
182  for(ElephantMenu em : externals) {
183  em.addToToolsMenu(this);
184  }
185  return this;
186  }
187 
188  public ElephantSubmenu addToolbarSeparator(boolean bar) {
189  getToolbar().addSeparator(false);
190  return this;
191  }
192 
193  public <T> ElephantSubmenu addCollecction(Collection<T> items, BiConsumer<ElephantSubmenu, T> consumer) {
194  items.stream().forEach((i) -> consumer.accept(ElephantSubmenu.this, i));
195  return this;
196  }
197 
198  public <T> ElephantSubmenu addObject(T item, BiConsumer<ElephantSubmenu, T> consumer) {
199  consumer.accept(ElephantSubmenu.this, item);
200  return this;
201  }
202 
203  private ElephantToolbar getToolbar() {
204  ElephantMenuBar emb = getMenuBar();
205  return emb != null ? emb.getToolbar() : null;
206  }
207 
208  private void checkSeparator() {
209  if(separator) {
210  appendChild(new Menuseparator());
211  separator = false;
212  }
213  }
214 
215 }
static Components from(Object component)
Definition: Components.java:46
static String getImage(String image)
Definition: Images.java:36
static String get(String msg)
Definition: I_.java:41
static String makeInclude(String include)
ElephantSubmenu addMenuitem(String label, String image, EventListener listener, boolean toolButton)
ElephantSubmenu addMenuitem(String label, String include, boolean toolButton, Permission permission)
ElephantMenu addMenu(String label)
ElephantSubmenu addMenuitem(String label, String include)
ElephantSubmenu addMenuitem(String label, EventListener listener, boolean toolButton)
ElephantSubmenu addMenuitem(String label, String image, String include, boolean toolButton, Permission permission)
ElephantSubmenu addToolbarSeparator(boolean bar)
ElephantSubmenu addMenuitem(String label, EventListener listener, boolean toolButton, Permission permission)
ElephantSubmenu addMenuitem(String label, String image, EventListener listener, boolean toolButton, Permission permission)
ElephantSubmenu addMenuitem(String label, String image, String include)
ElephantMenu addMenu(String label, Permission permission)
ElephantSubmenu addMenuitem(String label, String image, EventListener listener, Permission permission)
ElephantSubmenu addExternalsToTools(Collection< ElephantMenu > externals)
ElephantSubmenu addMenuitem(String label, String image, String include, Permission permission)
ElephantSubmenu addMenuitem(String label, String image, EventListener listener)
ElephantSubmenu addMenuitem(String label, String image, String include, boolean toolButton)
ElephantMenu addMenu(String label, String image, Permission permission)
ElephantSubmenu addExternalsToSession(Collection< ElephantMenu > externals)
ElephantMenu addMenu(String label, String image)
ElephantSubmenu addMenuitem(String label, String include, Permission permission)
ElephantSubmenu addMenuitem(String label, EventListener listener, Permission permission)
ElephantSubmenu addMenuitem(String label, EventListener listener)
void addComponent(Component child)
void addContent(String label, String src)
Definition: Framework.java:69
static Framework getCurrent()
Definition: Framework.java:203