BrightSide Workbench Full Report + Source Code
DefaultUserMenu.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2019 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.elephant.user.menu;
20 
21 import java.util.Set;
22 import java.util.TreeSet;
23 import java.util.function.Function;
24 import org.turro.elephant.context.ElephantContext;
25 import org.turro.file.Document;
26 import org.turro.string.Strings;
27 import org.turro.util.CompareUtil;
28 
33 public abstract class DefaultUserMenu implements IUserMenu, Comparable<IUserMenu> {
34 
35  protected IUserMenu parent;
36  protected Set<IUserMenu> children;
37  protected String id, label, image, link;
38  protected int order;
39  protected Function<IUserMenu, Boolean> command;
40 
41  public DefaultUserMenu(String label, String image) {
42  this(null, label, image, null, null, 0);
43  }
44 
45  public DefaultUserMenu(String label, String image, String link) {
46  this(null, label, image, link, null, 0);
47  }
48 
49  public DefaultUserMenu(String label, String image, Function<IUserMenu, Boolean> command) {
50  this(null, label, image, null, command, 0);
51  }
52 
53  public DefaultUserMenu(String label, String image, int order) {
54  this(null, label, image, null, null, order);
55  }
56 
57  public DefaultUserMenu(String label, String image, String link, int order) {
58  this(null, label, image, link, null, order);
59  }
60 
61  public DefaultUserMenu(String id, String label, String image, Function<IUserMenu, Boolean> command, int order) {
62  this(id, label, image, null, command, order);
63  }
64 
65  protected DefaultUserMenu(String id, String label, String image, String link, Function<IUserMenu, Boolean> command, int order) {
66  this.id = Strings.isBlank(id) ? (Strings.isBlank(label) ? Strings.asVar(image) : Strings.asVar(label)) : id;
67  this.label = label;
68  this.image = image;
69  this.link = link;
70  this.command = command;
71  this.order = order;
72  }
73 
74  @Override
75  public String getId() {
76  return id;
77  }
78 
79  @Override
80  public String getLabel() {
81  return label;
82  }
83 
84  @Override
85  public String getImage() {
86  return image;
87  }
88 
89  @Override
90  public int getOrder() {
91  return order;
92  }
93 
94  @Override
95  public String getLink() {
96  return link == null ? (command == null ? null : MenuCommand.createURL(this)) : link;
97  }
98 
99  @Override
100  public boolean isValid() {
101  return !Strings.isBlank(id) &&
102  !(Strings.isBlank(label) && Strings.isBlank(image));
103  }
104 
105  @Override
106  public boolean isCaption() {
107  return Strings.isBlank(link) && (command == null);
108  }
109 
110  @Override
111  public boolean isOnlyLabel() {
112  return Strings.isBlank(image);
113  }
114 
115  @Override
116  public boolean isOnlyImage() {
117  return Strings.isBlank(label);
118  }
119 
120  @Override
121  public boolean isActive() {
122  String currentPath = UserMenus.getActive();
123  return !Strings.isBlank(currentPath) &&
124  !Strings.isBlank(getLink()) &&
125  getLink().equals(currentPath);
126  }
127 
128  @Override
129  public IUserMenu getParent() {
130  return parent;
131  }
132 
133  @Override
134  public Set<IUserMenu> getChildren() {
135  return children;
136  }
137 
138  @Override
139  public IUserMenu addChild(IUserMenu menu) {
140  if(children == null) {
141  children = new TreeSet<>();
142  }
143  if(menu.isValid()) {
144  if((menu instanceof DefaultUserMenu) && menu.getOrder() == 0) {
145  ((DefaultUserMenu) menu).setOrder(children.size() + 1);
146  }
147  children.add(menu);
148  }
149  return menu;
150  }
151 
152  protected void setOrder(int order) {
153  this.order = order;
154  }
155 
156  @Override
157  public boolean execute() {
158  return command.apply(this);
159  }
160 
161  @Override
162  public void initMenu() {
163  // do nothing
164  }
165 
166  @Override
167  public int compareTo(IUserMenu o) {
168  int result = CompareUtil.compare(order, o.getOrder());
169  if(result == 0) {
170  result = CompareUtil.compare(label, o.getLabel());
171  }
172  if(result == 0) {
173  result = CompareUtil.compare(id, o.getId());
174  }
175  return result;
176  }
177 
178  @Override
179  public abstract boolean isInRole();
180 
181  @Override
182  public abstract Object getDetail();
183 
184  public boolean isHidden() {
185  return Document.from(ElephantContext.getRealPath("/WEB-INF/elephant/conf/hidden/" + this.getClass().getSimpleName())).exists();
186  }
187 
188 }
DefaultUserMenu(String label, String image, String link, int order)
DefaultUserMenu(String label, String image, int order)
DefaultUserMenu(String id, String label, String image, Function< IUserMenu, Boolean > command, int order)
Function< IUserMenu, Boolean > command
DefaultUserMenu(String label, String image, String link)
DefaultUserMenu(String id, String label, String image, String link, Function< IUserMenu, Boolean > command, int order)
DefaultUserMenu(String label, String image, Function< IUserMenu, Boolean > command)
static String createURL(IUserMenu userMenu)