BrightSide Workbench Full Report + Source Code
TemplatesMailListbox.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2018 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.newsletter;
20 
21 import java.io.File;
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import org.turro.string.Strings;
25 import org.turro.elephant.context.ElephantContext;
26 import org.turro.i18n.I_;
27 import org.turro.zkoss.input.CollectionListbox;
28 
33 public class TemplatesMailListbox extends CollectionListbox<String> {
34 
35  private String prefix, suffix, path;
36 
37  public void setPrefix(String prefix) {
38  this.prefix = prefix;
39  }
40 
41  public void setSuffix(String suffix) {
42  this.suffix = suffix;
43  }
44 
45  public void setPath(String path) {
46  this.path = path;
47  }
48 
49  @Override
50  protected String convertToString(String v) {
51  return Strings.isBlank(v) ? ("<" + I_.get("Default") + ">") : v;
52  }
53 
54  private Collection<String> createCollection() {
55  ArrayList<String> list = new ArrayList<>();
56  File rootTmp = new File(ElephantContext.getRealPath("/WEB-INF/elephant/templates-mail" + path));
57  if(rootTmp.exists()) {
58  for(File file : rootTmp.listFiles((File dir, String name) -> name.matches(getMatchExpression()))) {
59  list.add(Strings.extract(file.getName(), getMatchExpression()));
60  }
61  }
62  return list;
63  }
64 
65  @Override
66  public void afterCompose() {
67  setCollection(createCollection());
68  super.afterCompose();
69  }
70 
71  private String getMatchExpression() {
72  return (Strings.isBlank(prefix) ? "" : prefix) +
73  "([a-zA-Z-0-9]*)" +
74  (Strings.isBlank(suffix) ? "Template.html" : suffix);
75  }
76 
77 }
static String get(String msg)
Definition: I_.java:41
void setCollection(Collection< V > collection)