BrightSide Workbench Full Report + Source Code
ElephantMenuBar.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 org.turro.about.AboutTurro;
23 import org.turro.action.ExternalMenus;
24 import org.turro.auth.Authentication;
25 import org.turro.command.Command;
26 import org.turro.elephant.zkoss.Modal;
27 import org.turro.security.Permission;
28 import org.turro.zul.frame.Framework;
29 import org.turro.zul.portal.PortalWindows;
30 import org.zkoss.zk.ui.Executions;
31 import org.zkoss.zk.ui.event.Event;
32 import org.zkoss.zk.ui.event.EventListener;
33 import org.zkoss.zk.ui.util.Clients;
34 import org.zkoss.zul.Menubar;
35 
40 public class ElephantMenuBar extends Menubar {
41 
42  private ElephantToolbar toolbar;
43 
44  public ElephantMenuBar(ElephantToolbar toolbar) {
45  super();
46  initComponents(toolbar);
47  addMenus();
48  }
49 
50  public ElephantMenu addMenu(String label) {
51  return addMenu(label, null, null);
52  }
53 
54  public ElephantMenu addMenu(String label, String src) {
55  return addMenu(label, src, null);
56  }
57 
58  public ElephantMenu addMenu(String label, Permission permission) {
59  return addMenu(label, null, permission);
60  }
61 
62  public ElephantMenu addMenu(String label, String src, Permission permission) {
63  ElephantMenu m = src == null ? new ElephantMenu(label) : new ElephantMenu(label, src);
64  appendChild(m);
65  if(permission != null) m.setVisible(permission.check());
66  return m;
67  }
68 
70  return toolbar;
71  }
72 
73  public ElephantMenuBar addExternalsToMenu(Collection<ElephantMenu> externals) {
74  externals.forEach((esm) -> {
75  esm.addToMenu(this);
76  });
77  return this;
78  }
79 
80  private void addMenus() {
81  Collection<ElephantMenu> externals = ExternalMenus.externalMenus();
83  addMenu("Session")
84  .addSubmenu()
85  .addExternalsToSession(externals)
86  .addMenuitem("Calendar", "calendar", "/calendar/calendar.zul", true)
88  .addToolbarSeparator(true)
89  .addMenuitem("Profile", "contact", (EventListener) (Event event) -> {
90  Modal.doModal(ElephantMenu.makeInclude("/www/profile.zul"), (Command) null);
91  })
92  .addMenuSeparator()
93  //.addMenuitem("Welcome", "/welcome/welcome.zul");
94  .addMenuitem("Web home page", "home", (EventListener) (Event event) -> {
95  Clients.evalJavaScript("exitWanted=true;");
96  Executions.sendRedirect("/");
97  }, true)
98  .addMenuitem("User home page", "user-home", (EventListener) (Event event) -> {
99  Clients.evalJavaScript("exitWanted=true;");
100  Executions.sendRedirect("/user");
101  }, true)
102  .addMenu("Dashboards", Permission.from(() -> {
103  return !Framework.getCurrent().getPortalWindows().isEmpty();
104  }))
105  .addSubmenu()
106  .addCollecction(portalWindows.keySet(), (esm, key) -> {
107  esm.addMenuitem(portalWindows.get(key).getLabel(), (EventListener) (Event event) -> {
108  Framework.getCurrent().addContent(portalWindows.get(key));
109  });
110  })
111  .getParentSubmenu()
112  .addMenuitem("Logout", "exit", new EventListener() {
113  @Override
114  public void onEvent(Event event) throws Exception {
115  Clients.evalJavaScript("exitWanted=true;");
116  Authentication.doLogout("/");
117  }
118  }, true)
119  .addToolbarSeparator(true)
120  .getMenuBar()
121  .addExternalsToMenu(externals)
122  .addMenu("Tools")
123  .addSubmenu()
124  .addExternalsToTools(externals)
126  .addToolbarSeparator(true)
127  .addMenuitem("Reload content", "reload", (EventListener) (Event event) -> {
128  Framework.getCurrent().invalidateSelected();
129  }, true)
130  .addMenuitem("Password generator", "locked", (EventListener) (Event event) -> {
131  Modal.doModal(ElephantMenu.makeInclude("/www/passgen.zul"), (Command) null);
132  })
133  //addThemes(mp);
134  .addMenuSeparator()
135  .addMenuitem("About", "brightside", (EventListener) (Event event) -> {
136  new AboutTurro().show();
137  });
138  }
139 
140  private void initComponents(ElephantToolbar toolbar) {
141  setAutodrop(false);
142  this.toolbar = toolbar;
143  }
144 
145 }
static Collection< ElephantMenu > externalMenus()
static int doModal(String file)
Definition: Modal.java:38
ElephantMenuBar(ElephantToolbar toolbar)
ElephantMenu addMenu(String label)
ElephantMenu addMenu(String label, String src, Permission permission)
ElephantMenu addMenu(String label, String src)
ElephantMenu addMenu(String label, Permission permission)
ElephantMenuBar addExternalsToMenu(Collection< ElephantMenu > externals)
static String makeInclude(String include)
ElephantSubmenu addSubmenu()
ElephantSubmenu addMenuitem(String label, String include)
ElephantSubmenu addToolbarSeparator(boolean bar)
ElephantSubmenu addExternalsToTools(Collection< ElephantMenu > externals)
ElephantSubmenu addExternalsToSession(Collection< ElephantMenu > externals)
static Framework getCurrent()
Definition: Framework.java:203
PortalWindows getPortalWindows()
Definition: Framework.java:128