public code v1
This commit is contained in:
@@ -0,0 +1,189 @@
|
||||
package flintstones.helper.html;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import org.eclipse.swt.SWT;
|
||||
import org.eclipse.swt.browser.Browser;
|
||||
import org.eclipse.swt.layout.FillLayout;
|
||||
import org.eclipse.swt.widgets.Composite;
|
||||
import org.osgi.framework.FrameworkUtil;
|
||||
|
||||
import flintstones.helper.FileHelper;
|
||||
import flintstones.helper.html.tags.Body;
|
||||
import flintstones.helper.html.tags.Div;
|
||||
import flintstones.helper.html.tags.HtmlTag;
|
||||
|
||||
/**
|
||||
* The Class HtmlWidget.
|
||||
*/
|
||||
public abstract class HtmlWidget {
|
||||
|
||||
/** The base. */
|
||||
Composite base;
|
||||
|
||||
/** The browser. */
|
||||
protected Browser browser;
|
||||
protected HtmlTag disabled = new Div();
|
||||
protected HtmlTag body = new Body();
|
||||
|
||||
boolean debug = false;
|
||||
boolean isDisabled = false;
|
||||
|
||||
private HashMap<String, HashMap<String, String>> userCss = new HashMap<>();
|
||||
|
||||
/**
|
||||
* Instantiates a new html widget.
|
||||
*
|
||||
* @param base the base
|
||||
*/
|
||||
public HtmlWidget(Composite base) {
|
||||
|
||||
this.base = base;
|
||||
|
||||
// If the composite to draw had an HtmlWidget, it just delete it so this new
|
||||
// widget will replace it.
|
||||
// It the composite have any other item, exception.
|
||||
if (base.getChildren().length > 0) {
|
||||
Arrays.stream(base.getChildren()).forEach(k -> {
|
||||
if (k instanceof Browser) {
|
||||
k.dispose();
|
||||
} else {
|
||||
throw new RuntimeException("Composite must have a HTMLWidget or be empty");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
base.setLayout(new FillLayout());
|
||||
|
||||
browser = new Browser(base, SWT.NONE);
|
||||
browser.setText("");
|
||||
|
||||
base.layout();
|
||||
|
||||
body.add(disabled);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Render.
|
||||
*/
|
||||
public void render() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("<html><!DOCTYPE html>");
|
||||
sb.append(getResult());
|
||||
|
||||
// Style
|
||||
sb.append("<style>");
|
||||
sb.append(getBaseStyle());
|
||||
sb.append(getStyle());
|
||||
sb.append(getUserStyle());
|
||||
sb.append("</style>");
|
||||
|
||||
// Script
|
||||
sb.append("<script>");
|
||||
sb.append(getBaseScript());
|
||||
sb.append(getScript());
|
||||
sb.append("</script>");
|
||||
|
||||
sb.append("</html>");
|
||||
|
||||
browser.setText(sb.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the result.
|
||||
*
|
||||
* @return the result
|
||||
*/
|
||||
protected abstract String getResult();
|
||||
|
||||
/**
|
||||
* Gets the style.
|
||||
*
|
||||
* @return the style
|
||||
*/
|
||||
protected String getStyle() {
|
||||
return "";
|
||||
};
|
||||
|
||||
/**
|
||||
* Gets the script.
|
||||
*
|
||||
* @return the script
|
||||
*/
|
||||
String getScript() {
|
||||
return "";
|
||||
};
|
||||
|
||||
/**
|
||||
* Gets the base style.
|
||||
*
|
||||
* @return the base style
|
||||
*/
|
||||
private String getBaseStyle() {
|
||||
return FileHelper.readFile("./assets/css/widget.css", FrameworkUtil.getBundle(getClass()));
|
||||
}
|
||||
|
||||
private String getBaseScript() {
|
||||
if (!debug)
|
||||
return "";
|
||||
return FileHelper.readFile("./assets/js/debug.js", FrameworkUtil.getBundle(getClass()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the parent.
|
||||
*
|
||||
* @return the parent
|
||||
*/
|
||||
public Composite getParent() {
|
||||
return base;
|
||||
}
|
||||
|
||||
public void setCss(String selector, String key, String value) {
|
||||
HashMap<String, String> selectorMap = userCss.get(selector);
|
||||
if (selectorMap == null)
|
||||
selectorMap = new HashMap<>();
|
||||
|
||||
selectorMap.put(key, value);
|
||||
userCss.put(selector, selectorMap);
|
||||
}
|
||||
|
||||
private String getUserStyle() {
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (Entry<String, HashMap<String, String>> selectorEntry : userCss.entrySet()) {
|
||||
String selector = selectorEntry.getKey();
|
||||
sb.append(selector);
|
||||
sb.append("{ ");
|
||||
for (Entry<String, String> propertyEntry : selectorEntry.getValue().entrySet()) {
|
||||
|
||||
String key = propertyEntry.getKey();
|
||||
String val = propertyEntry.getValue();
|
||||
|
||||
sb.append(key);
|
||||
sb.append(":");
|
||||
sb.append(val);
|
||||
sb.append(";");
|
||||
|
||||
}
|
||||
sb.append("}");
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public Browser getBrowser() {
|
||||
return browser;
|
||||
}
|
||||
|
||||
public void setEnabled(boolean isEnabled) {
|
||||
if (isEnabled)
|
||||
disabled.deleteClass("disabled");
|
||||
else
|
||||
disabled.addClass("disabled");
|
||||
|
||||
render();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user