BrightSide Workbench Full Report + Source Code
WebLink.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.elephant.web.actions;
20 
21 import org.turro.i18n.I_;
22 import org.turro.icon.Icons;
23 import org.turro.string.Strings;
24 
29 public class WebLink {
30 
31  private final WebActionType type;
32  private final String caption, url, icon;
33 
34  public WebLink(WebActionType type, String caption, String url) {
35  this(type, caption, url, null);
36  }
37 
38  public WebLink(WebActionType type, String caption, String url, String icon) {
39  this.type = type;
40  this.caption = caption;
41  this.url = url;
42  this.icon = icon;
43  }
44 
46  return type;
47  }
48 
49  public String getCaption() {
50  if(Strings.isBlank(caption)) return switch(type) {
51  case NAVIGATE, ACTION, TAG, URL -> url;
52  case MAIL -> I_.get("Email");
53  case TEL -> I_.get("Call");
54  case WHATS -> "Whatsapp";
55  case TGRAM -> "Telegram";
56  case WRONG -> "Wrong";
57  };
58  return caption;
59  }
60 
61  public String getUrl() {
62  return url;
63  }
64 
65  public String getIcon() {
66  if(Strings.isBlank(icon)) return switch(type) {
67  case NAVIGATE, URL, TAG -> "globe";
68  case MAIL -> "envelope";
69  case TEL -> "phone";
70  case ACTION -> switch(url) {
71  case "singup" -> "sign in alternate";
72  case "tellsomeone" -> "satellite dish";
73  default -> "bolt";
74  };
75  case WHATS -> "whatsapp";
76  case TGRAM -> "telegram";
77  case WRONG -> "warning";
78  };
79  return icon;
80  }
81 
82  public String getHelp() {
83  return switch(type) {
84  case NAVIGATE, TAG, ACTION, URL -> url;
85  case MAIL, TEL -> Strings.pastFirst(url, ":");
86  case WHATS -> Strings.pastLast(url, "=");
87  case TGRAM -> Strings.pastLast(url, "/");
88  case WRONG -> "Wrong action found";
89  };
90  }
91 
92  public Icons getIcons() {
93  return Icons.from(getIcon());
94  }
95 
96  public boolean isExternal() {
97  return url.matches("(http|https|ftp|sftp|ftps):.+");
98  }
99 
100  public boolean isEmpty() {
101  return type == null || Strings.isBlank(caption) || Strings.isBlank(url);
102  }
103 
104  private String pastProtocol() {
105  if(url.contains(":")) {
106  return url.substring(url.indexOf(":") + 1);
107  }
108  return url;
109  }
110 
111  /* Factory */
112 
113  public static WebLink from(WebLink webLink, String newCaption) {
114  if(webLink.getType() == WebActionType.WRONG) return webLink;
115  return new WebLink(webLink.getType(), newCaption, webLink.getUrl(), webLink.getIcon());
116  }
117 
118 }
static String get(String msg)
Definition: I_.java:41