19 package org.turro.visual;
22 import java.io.IOException;
23 import java.io.Writer;
24 import java.util.HashMap;
26 import java.util.TreeSet;
27 import java.util.logging.Level;
28 import java.util.logging.Logger;
29 import org.turro.elephant.context.ElephantContext;
30 import org.turro.elephant.impl.util.FileUtil;
31 import org.turro.file.FileWatch;
32 import org.turro.plugin.contacts.IContact;
33 import org.turro.reflection.JSONSerializer;
39 public abstract class VisualElements<F
extends VisualElements, E, V extends
VisualElement<F, V, E>> extends TreeSet<V> {
41 public boolean allows(E entity,
IContact contact, String name) {
43 if(ve.getName().equals(name) && ve.check(entity, contact))
return true;
48 public TreeSet<V> allowed(E entity,
IContact contact) {
49 TreeSet<V> allowed =
new TreeSet<>();
51 if(ve.check(entity, contact)) {
58 public V getElement(String name, E entity,
IContact contact) {
59 for(V ve : allowed(entity, contact)) {
60 if(ve.getName().equals(name)) {
67 public V getElement(String name) {
69 if(ve.getName().equals(name)) {
77 public boolean add(V e) {
78 if(e.getOrder() == 0) {
79 e.setOrder(size() + 1);
84 public V addElement(String name) {
85 V v = createElement(name);
86 return add(v) ? v :
null;
89 protected void setDefaults() {
90 addElement(
"Default");
93 protected abstract F createSubElements();
94 protected abstract V createElement(String name);
98 private static final Map<String, VisualElements> _ves =
new HashMap<>();
99 private static final Map<String, Long> _lastLoads =
new HashMap<>();
101 protected static <F extends VisualElements> F doLoad(String visualFile, F visuals) {
102 if(_ves.get(visualFile) ==
null || FileWatch.isNewer(convert(visualFile), _lastLoads.get(visualFile))
103 || !FileWatch.exists(convert(visualFile))) {
104 VisualElements ves =
null;
106 JSONSerializer ser =
new JSONSerializer(
true);
107 ves = ser.fromJson(
FileUtil.
getContent(
new File(convert(visualFile))), visuals.getClass());
108 }
catch (IOException ex) {
109 Logger.getLogger(VisualElements.class.getName()).log(Level.SEVERE,
ElephantContext.
logMsg(
null), ex);
114 VisualElements.save(visualFile, ves);
116 _ves.put(visualFile, ves);
117 _lastLoads.put(visualFile, FileWatch.getTime(convert(visualFile)));
119 return (F) _ves.get(visualFile);
122 private static <F extends VisualElements>
void save(String visualFile, F ves) {
124 JSONSerializer ser =
new JSONSerializer(ves,
true);
125 writer.write(ser.toJson());
126 }
catch (IOException ex) {
127 Logger.getLogger(VisualElements.class.getName()).log(Level.SEVERE,
ElephantContext.
logMsg(
null), ex);
131 private static String convert(String visualFile) {
132 if(!visualFile.contains(
"/")) {
133 visualFile =
"/WEB-INF/elephant/conf/" + visualFile +
"-visual.json";
static String getRealPath(String path)
static String logMsg(String msg)
static OutputStreamWriter getFileWriter(String folder, String file)
static String getContent(File file)