BrightSide Workbench Full Report + Source Code
zkoss/menu/ElephantMenuBar.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 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 package org.turro.zkoss.menu;
19 
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import org.turro.about.AboutTurro;
23 import org.turro.auth.Authentication;
24 import org.turro.command.Command;
25 import org.turro.elephant.context.Application;
26 import org.turro.elephant.util.Images;
27 import org.turro.elephant.zkoss.Modal;
28 import org.turro.elephant.zkoss.RoleMap;
29 import org.turro.i18n.I_;
30 import org.turro.zul.frame.Framework;
31 import org.zkoss.zk.ui.Component;
32 import org.zkoss.zk.ui.Executions;
33 import org.zkoss.zk.ui.event.Event;
34 import org.zkoss.zk.ui.event.EventListener;
35 import org.zkoss.zk.ui.event.Events;
36 import org.zkoss.zk.ui.util.Clients;
37 import org.zkoss.zul.*;
38 import org.zkoss.zul.theme.Themes;
39 
44 @Deprecated
45 public class ElephantMenuBar extends Menubar {
46 
47  private Application app = Application.getApplication();
48  private ElephantToolbar toolBar;
49 
50  public ElephantMenuBar(String orient, ElephantToolbar toolBar) {
51  super(orient);
52  setAutodrop(false);
53  this.toolBar = toolBar;
54  addMenus();
55  }
56 
57  public ElephantMenuBar(ElephantToolbar toolBar) {
58  super();
59  setAutodrop(false);
60  this.toolBar = toolBar;
61  addMenus();
62  }
63 
64  public Menu addMenu(String label) {
65  return addMenu(label, null);
66  }
67 
68  public Menu addMenu(String label, String src) {
69  Menu m = src == null ? new Menu(label) : new Menu(label, src);
70  //m.setParent(this);
71  appendChild(m);
72  return m;
73  }
74 
75  public Menu addMenu(Menupopup mp, String label) {
76  Menu m = new Menu(label);
77  //m.setParent(mp);
78  mp.appendChild(m);
79  return m;
80  }
81 
82  public Menu getMenuByLabel(String label) {
83  for(Component m : getChildren()) {
84  if(label.equals(((Menu)m).getLabel())) {
85  return (Menu) m;
86  }
87  }
88  return null;
89  }
90 
91  public Menupopup addMenupopup(Menu menu) {
92  Menupopup mp = new Menupopup();
93  //mp.setParent(menu);
94  menu.appendChild(mp);
95  return mp;
96  }
97 
98  public Menuitem addMenuitem(Menupopup popup, String label, final String include) {
99  return addMenuitem(popup, label, null, include);
100  }
101 
102  public Menuitem addMenuitem(Menupopup popup, String label, EventListener listener) {
103  return addMenuitem(popup, label, null, listener);
104  }
105 
106  public Menuitem addMenuitem(Menupopup popup, String label, final String include, boolean toolButton) {
107  return addMenuitem(popup, label, null, include, toolButton);
108  }
109 
110  public Menuitem addMenuitem(Menupopup popup, String label, EventListener listener, boolean toolButton) {
111  return addMenuitem(popup, label, null, listener, toolButton);
112  }
113 
114  public Menuitem addMenuitem(Menupopup popup, final String label, final String src, final String include) {
115  return addMenuitem(popup, label, src, include, false);
116  }
117 
118  public Menuitem addMenuitem(Menupopup popup, final String label, final String src, EventListener listener) {
119  return addMenuitem(popup, label, src, listener, false);
120  }
121 
122  public Menuitem addMenuitem(Menupopup popup, final String label, final String src, final String include, boolean toolButton) {
123  EventListener listener = null;
124  if(include != null) {
125  listener = new EventListener() {
126  @Override
127  public void onEvent(Event event) throws Exception {
128  Framework.getCurrent().addContent(label, include);
129  }
130  };
131  }
132  return addMenuitem(popup, label, src, listener, toolButton);
133  }
134 
135  public Menuitem addMenuitem(Menupopup popup, final String label, final String src, EventListener listener, boolean toolButton) {
136  if(toolButton && toolBar != null) {
137  Toolbarbutton button = new Toolbarbutton();
138  toolBar.addComponent(button);
139  button.setTooltiptext(label);
140  if(src != null) {
141  button.setImage(src);
142  }
143  if(listener != null) {
144  button.addEventListener(Events.ON_CLICK, listener);
145  }
146  }
147  Menuitem mi = new Menuitem(label);
148  mi.setImage(src);
149  //mi.setParent(popup);
150  popup.appendChild(mi);
151  if(listener != null) {
152  mi.addEventListener(Events.ON_CLICK, listener);
153  }
154  return mi;
155  }
156 
157  public void addMenuSeparator(Menupopup popup) {
158  if(!popup.getChildren().isEmpty() && !(popup.getLastChild() instanceof Menuseparator)) {
159  Menuseparator ms = new Menuseparator();
160  //ms.setParent(popup);
161  popup.appendChild(ms);
162  }
163  }
164 
165  public void addToolbarSeparator(boolean bar) {
166  toolBar.addSeparator(false);
167  }
168 
169  protected void addMenus() {
170  Collection<ElephantMenu> externals = new ArrayList<>(); //ExternalMenus.externalMenus();//getExternals();
171  RoleMap rm = new RoleMap(app);
172  Menu m = addMenu(I_.get("Session"));
173  Menupopup mp = addMenupopup(m);
174  for(ElephantMenu em : externals) {
175  if(em != null) em.addToSessionMenu(this, mp);
176  }
177  addMenuitem(mp, I_.get("Calendar"), Images.getImage("calendar"), "/WEB-INF/_zul/calendar/calendar.zul", true);
178  addMenuSeparator(mp);
179  addToolbarSeparator(true);
180  addMenuitem(mp, I_.get("Profile"), "/_zul/images/contact.png", new EventListener() {
181  @Override
182  public void onEvent(Event event) throws Exception {
183  Modal.doModal("/WEB-INF/_zul/www/profile.zul", (Command) null);
184  }
185  });
186  addMenuSeparator(mp);
187  //addMenuitem(mp, I_.get("Welcome"), null, "/WEB-INF/_zul/welcome/welcome.zul");
188  addMenuitem(mp, I_.get("Web home page"), "/_zul/images/home.png", new EventListener() {
189  @Override
190  public void onEvent(Event event) throws Exception {
191  Clients.evalJavaScript("exitWanted=true;");
192  Executions.sendRedirect("/");
193  }
194  }, true);
195  addMenuitem(mp, I_.get("User home page"), "/_zul/images/user-home.png", new EventListener() {
196  @Override
197  public void onEvent(Event event) throws Exception {
198  Clients.evalJavaScript("exitWanted=true;");
199  Executions.sendRedirect("/user");
200  }
201  }, true);
202  //Framework.getCurrent().getPortalWindows().addPortalMenu(this, mp);
203  addMenuitem(mp, I_.get("Logout"), "/_zul/images/exit.png", new EventListener() {
204  @Override
205  public void onEvent(Event event) throws Exception {
206  Clients.evalJavaScript("exitWanted=true;");
208  }
209  }, true);
210  addToolbarSeparator(true);
211  for(ElephantMenu em : externals) {
212  em.addMenus(this);
213  }
214  m = addMenu(I_.get("Tools"));
215  mp = addMenupopup(m);
216  for(ElephantMenu em : externals) {
217  em.addToToolsMenu(this, mp);
218  }
219  addMenuSeparator(mp);
220  addToolbarSeparator(true);
221  addMenuitem(mp, I_.get("Reload content"), "/_zul/images/reload.png", new EventListener() {
222  @Override
223  public void onEvent(Event event) throws Exception {
225  }
226  }, true);
227  addMenuitem(mp, I_.get("Password generator"), "/_zul/images/locked.png", new EventListener() {
228  @Override
229  public void onEvent(Event event) throws Exception {
230  Modal.doModal("/WEB-INF/_zul/www/passgen.zul", (Command) null);
231  }
232  });
233  //addThemes(mp);
234  addMenuSeparator(mp);
235  addMenuitem(mp, I_.get("About") + "...", "/_zul/images/brightside.png", new EventListener() {
236  @Override
237  public void onEvent(Event event) throws Exception {
238  new AboutTurro().show(getPage(), null);
239  }
240  });
241  }
242 
243 // private Set<ElephantMenu> getExternals() {
244 // Set<ElephantMenu> lem = new TreeSet<ElephantMenu>();
245 // IConstructor constructor = app.getConstructor();
246 // Iterator it = ContextFactory.getImplementationNamesByType("IMenu").iterator();
247 // while(it.hasNext()) {
248 // lem.add((ElephantMenu) ContextFactory.getImplementation(constructor,(String) it.next()));
249 // }
250 // return lem;
251 // }
252 
253  private void addThemes(Menupopup mp) {
254  Menu mm = addMenu(mp, "Themes");
255  Menupopup mip = addMenupopup(mm);
256  addMenuitem(mip, "Breeze", new EventListener() {
257  @Override
258  public void onEvent(Event event) throws Exception {
259  Themes.setTheme(Executions.getCurrent(), "Breeze");
260  Executions.sendRedirect(null);
261  }
262  });
263  addMenuitem(mip, "Saphire", new EventListener() {
264  @Override
265  public void onEvent(Event event) throws Exception {
266  Themes.setTheme(Executions.getCurrent(), "Sapphire");
267  Executions.sendRedirect(null);
268  }
269  });
270  addMenuitem(mip, "Silvertail", new EventListener() {
271  @Override
272  public void onEvent(Event event) throws Exception {
273  Themes.setTheme(Executions.getCurrent(), "Silvertail");
274  Executions.sendRedirect(null);
275  }
276  });
277  }
278 
279 }
static void doLogout(String redir)
static String getImage(String image)
Definition: Images.java:36
static int doModal(String file)
Definition: Modal.java:38
static String get(String msg)
Definition: I_.java:41
Menu addMenu(Menupopup mp, String label)
Menuitem addMenuitem(Menupopup popup, String label, EventListener listener, boolean toolButton)
Menuitem addMenuitem(Menupopup popup, final String label, final String src, final String include, boolean toolButton)
Menuitem addMenuitem(Menupopup popup, final String label, final String src, EventListener listener, boolean toolButton)
Menuitem addMenuitem(Menupopup popup, String label, final String include, boolean toolButton)
Menuitem addMenuitem(Menupopup popup, String label, EventListener listener)
Menuitem addMenuitem(Menupopup popup, String label, final String include)
Menuitem addMenuitem(Menupopup popup, final String label, final String src, final String include)
Menuitem addMenuitem(Menupopup popup, final String label, final String src, EventListener listener)
ElephantMenuBar(String orient, ElephantToolbar toolBar)
void addContent(String label, String src)
Definition: Framework.java:69
static Framework getCurrent()
Definition: Framework.java:204