use of com.codename1.designer.css.CSSTheme.WebViewProvider in project CodenameOne by codenameone.
the class CN1CSSCLI method compile.
private static void compile(File inputFile, File outputFile) throws IOException {
File baseDir = inputFile.getParentFile().getParentFile();
File checksumsFile = getChecksumsFile(baseDir);
if (!checksumsFile.exists()) {
saveChecksums(baseDir, new HashMap<String, String>());
}
if (!checksumsFile.exists()) {
throw new RuntimeException("Failed to create checksums file");
}
FileChannel channel = new RandomAccessFile(checksumsFile, "rw").getChannel();
FileLock lock = channel.lock();
try {
Map<String, String> checksums = loadChecksums(baseDir);
if (outputFile.exists() && !isMavenProject(inputFile)) {
String outputFileChecksum = getMD5Checksum(outputFile.getAbsolutePath());
String previousChecksum = checksums.get(inputFile.getName());
if (previousChecksum == null || !previousChecksum.equals(outputFileChecksum)) {
File backups = new File(inputFile.getParentFile(), ".backups");
backups.mkdirs();
File bak = new File(backups, outputFile.getName() + "." + System.currentTimeMillis() + ".bak");
Files.copy(outputFile.toPath(), bak.toPath(), StandardCopyOption.REPLACE_EXISTING);
System.out.println(outputFile + " has been modified since it was last compiled. Making copy at " + bak);
outputFile.delete();
}
}
if (outputFile.exists() && inputFile.lastModified() <= outputFile.lastModified()) {
System.out.println("File has not changed since last compile.");
return;
}
try {
URL url = inputFile.toURI().toURL();
// CSSTheme theme = CSSTheme.load(CSSTheme.class.getResource("test.css"));
CSSTheme theme = CSSTheme.load(url);
theme.cssFile = inputFile;
theme.resourceFile = outputFile;
JavaSEPort.setBaseResourceDir(outputFile.getParentFile());
WebViewProvider webViewProvider = new WebViewProvider() {
@Override
public BrowserComponent getWebView() {
if (web == null) {
if (!CN1Bootstrap.isCEFLoaded()) /* && !CN1Bootstrap.isJavaFXLoaded()*/
{
// to output the correct bounds so we are killing FX support. CEF only.
throw new MissingNativeBrowserException();
}
if (!CN.isEdt()) {
CN.callSerially(() -> {
getWebView();
});
int counter = 0;
while (web == null && counter++ < 50) {
Util.sleep(100);
}
return web;
}
web = new BrowserComponent();
ComponentSelector.select("*", web).add(web, true).selectAllStyles().setBgTransparency(0).setMargin(0).setPadding(0).setBorder(Border.createEmpty()).each(new ComponentClosure() {
@Override
public void call(Component c) {
c.setOpaque(false);
}
});
web.setOpaque(false);
Form f = new Form();
f.getContentPane().getStyle().setBgColor(0xff0000);
f.getContentPane().getStyle().setBgTransparency(0xff);
if (f.getToolbar() == null) {
f.setToolbar(new com.codename1.ui.Toolbar());
}
f.getToolbar().hideToolbar();
f.setLayout(new com.codename1.ui.layouts.BorderLayout());
f.add(CN.CENTER, web);
f.show();
}
return web;
}
};
File cacheFile = new File(theme.cssFile.getParentFile(), theme.cssFile.getName() + ".checksums");
if (outputFile.exists() && cacheFile.exists()) {
theme.loadResourceFile();
theme.loadSelectorCacheStatus(cacheFile);
}
theme.createImageBorders(webViewProvider);
theme.updateResources();
theme.save(outputFile);
theme.saveSelectorChecksums(cacheFile);
String checksum = getMD5Checksum(outputFile.getAbsolutePath());
checksums.put(inputFile.getName(), checksum);
saveChecksums(baseDir, checksums);
} catch (MalformedURLException ex) {
Logger.getLogger(CN1CSSCLI.class.getName()).log(Level.SEVERE, null, ex);
}
} finally {
if (lock != null) {
lock.release();
}
if (channel != null) {
channel.close();
}
}
}
use of com.codename1.designer.css.CSSTheme.WebViewProvider in project CodenameOne by codenameone.
the class CSSTheme method createImageBorders.
public void createImageBorders(WebViewProvider webviewProvider) {
if (res == null) {
res = new EditableResourcesForCSS(resourceFile);
}
ArrayList<Border> borders = new ArrayList<Border>();
ResourcesMutator resm = new ResourcesMutator(res, Display.DENSITY_VERY_HIGH, minDpi, maxDpi);
resm.targetDensity = targetDensity;
List<Runnable> onComplete = new ArrayList<Runnable>();
for (String id : elements.keySet()) {
try {
if (!isModified(id)) {
continue;
}
Element e = (Element) elements.get(id);
Element unselected = e.getUnselected();
Map<String, LexicalUnit> unselectedStyles = (Map<String, LexicalUnit>) unselected.getFlattenedStyle();
Border b = unselected.createBorder(unselectedStyles);
Border unselectedBorder = b;
currentId = id;
if (e.requiresImageBorder(unselectedStyles)) {
if (!borders.contains(b)) {
borders.add(b);
resm.addImageProcessor(id, (img) -> {
Insets insets = unselected.getImageBorderInsets(unselectedStyles, img.getWidth(), img.getHeight());
resm.targetDensity = getSourceDensity(unselectedStyles, resm.targetDensity);
com.codename1.ui.plaf.Border border = resm.create9PieceBorder(img, id, (int) insets.top, (int) insets.right, (int) insets.bottom, (int) insets.left);
resm.put(id + ".border", border);
unselectedBorder.border = border;
resm.targetDensity = targetDensity;
});
} else {
onComplete.add(() -> {
resm.put(id + ".border", borders.get(borders.indexOf(unselectedBorder)).border);
});
}
} else if (e.requiresBackgroundImageGeneration(unselectedStyles)) {
if (!borders.contains(b)) {
borders.add(b);
resm.addImageProcessor(id, (img) -> {
int i = 1;
while (res.containsResource(id + "_" + i + ".png")) {
i++;
}
String prefix = id + "_" + i + ".png";
resm.targetDensity = getSourceDensity(unselectedStyles, resm.targetDensity);
Image im = resm.storeImage(EncodedImage.create(ResourcesMutator.toPngOrJpeg(img)), prefix, false);
unselectedBorder.image = im;
resm.put(id + ".bgImage", im);
resm.targetDensity = targetDensity;
// resm.put(id+".press#bgType", Style.B)
});
} else {
onComplete.add(() -> {
resm.put(id + ".bgImage", unselectedBorder.image);
});
}
}
Element selected = e.getSelected();
Map<String, LexicalUnit> selectedStyles = (Map<String, LexicalUnit>) selected.getFlattenedStyle();
b = selected.createBorder(selectedStyles);
Border selectedBorder = b;
if (e.requiresImageBorder(selectedStyles)) {
if (!borders.contains(b)) {
borders.add(b);
resm.addImageProcessor(id + ".sel", (img) -> {
Insets insets = selected.getImageBorderInsets(selectedStyles, img.getWidth(), img.getHeight());
resm.targetDensity = getSourceDensity(selectedStyles, resm.targetDensity);
com.codename1.ui.plaf.Border border = resm.create9PieceBorder(img, id, (int) insets.top, (int) insets.right, (int) insets.bottom, (int) insets.left);
resm.put(id + ".sel#border", border);
selectedBorder.border = border;
resm.targetDensity = targetDensity;
});
} else {
onComplete.add(() -> {
resm.put(id + ".sel#border", borders.get(borders.indexOf(selectedBorder)).border);
});
}
} else if (e.requiresBackgroundImageGeneration(selectedStyles)) {
if (!borders.contains(b)) {
borders.add(b);
resm.addImageProcessor(id + ".sel", (img) -> {
int i = 1;
while (res.containsResource(id + "_" + i + ".png")) {
i++;
}
String prefix = id + "_" + i + ".png";
resm.targetDensity = getSourceDensity(selectedStyles, resm.targetDensity);
Image im = resm.storeImage(EncodedImage.create(ResourcesMutator.toPngOrJpeg(img)), prefix, false);
selectedBorder.image = im;
resm.put(id + ".sel#bgImage", im);
// resm.put(id+".press#bgType", Style.B)
resm.targetDensity = targetDensity;
});
} else {
onComplete.add(() -> {
resm.put(id + ".sel#bgImage", selectedBorder.image);
});
}
}
Element pressed = e.getPressed();
Map<String, LexicalUnit> pressedStyles = (Map<String, LexicalUnit>) pressed.getFlattenedStyle();
b = pressed.createBorder(pressedStyles);
Border pressedBorder = b;
if (e.requiresImageBorder(pressedStyles)) {
if (!borders.contains(b)) {
borders.add(b);
resm.addImageProcessor(id + ".press", (img) -> {
Insets insets = pressed.getImageBorderInsets(pressedStyles, img.getWidth(), img.getHeight());
resm.targetDensity = getSourceDensity(pressedStyles, resm.targetDensity);
com.codename1.ui.plaf.Border border = resm.create9PieceBorder(img, id, (int) insets.top, (int) insets.right, (int) insets.bottom, (int) insets.left);
resm.put(id + ".press#border", border);
pressedBorder.border = border;
resm.targetDensity = targetDensity;
});
} else {
onComplete.add(() -> {
resm.put(id + ".press#border", borders.get(borders.indexOf(pressedBorder)).border);
});
}
} else if (e.requiresBackgroundImageGeneration(pressedStyles)) {
if (!borders.contains(b)) {
borders.add(b);
resm.addImageProcessor(id + ".press", (img) -> {
int i = 1;
while (res.containsResource(id + "_" + i + ".png")) {
i++;
}
String prefix = id + "_" + i + ".png";
resm.targetDensity = getSourceDensity(pressedStyles, resm.targetDensity);
Image im = resm.storeImage(EncodedImage.create(ResourcesMutator.toPngOrJpeg(img)), prefix, false);
pressedBorder.imageId = prefix;
resm.put(id + ".press#bgImage", im);
resm.targetDensity = targetDensity;
// resm.put(id+".press#bgType", Style.B)
});
} else {
onComplete.add(() -> {
resm.put(id + ".press#bgImage", res.findId(pressedBorder.imageId, true));
});
}
}
Element disabled = e.getDisabled();
Map<String, LexicalUnit> disabledStyles = (Map<String, LexicalUnit>) disabled.getFlattenedStyle();
b = disabled.createBorder(disabledStyles);
Border disabledBorder = b;
if (e.requiresImageBorder(disabledStyles)) {
if (!borders.contains(b)) {
borders.add(b);
resm.addImageProcessor(id + ".dis", (img) -> {
Insets disabledInsets = disabled.getImageBorderInsets(disabledStyles, img.getWidth(), img.getHeight());
resm.targetDensity = getSourceDensity(disabledStyles, resm.targetDensity);
com.codename1.ui.plaf.Border border = resm.create9PieceBorder(img, id, (int) disabledInsets.top, (int) disabledInsets.right, (int) disabledInsets.bottom, (int) disabledInsets.left);
disabledBorder.border = border;
resm.put(id + ".dis#border", border);
resm.targetDensity = targetDensity;
});
} else {
onComplete.add(() -> {
resm.put(id + ".dis#border", borders.get(borders.indexOf(disabledBorder)).border);
});
}
} else if (e.requiresBackgroundImageGeneration(disabledStyles)) {
if (!borders.contains(b)) {
borders.add(b);
resm.addImageProcessor(id + ".dis", (img) -> {
int i = 1;
while (res.containsResource(id + "_" + i + ".png")) {
i++;
}
String prefix = id + "_" + i + ".png";
resm.targetDensity = getSourceDensity(disabledStyles, resm.targetDensity);
Image im = resm.storeImage(EncodedImage.create(ResourcesMutator.toPngOrJpeg(img)), prefix, false);
disabledBorder.image = im;
resm.put(id + ".dis#bgImage", im);
resm.targetDensity = targetDensity;
// resm.put(id+".press#bgType", Style.B)
});
} else {
onComplete.add(() -> {
resm.put(id + ".dis#bgImage", disabledBorder.image);
});
}
}
} catch (Exception ex) {
throw new RuntimeException("An exception occurred while processing the image border for element " + id, ex);
}
}
if (requiresCaptureHtml()) {
resm.createScreenshots(webviewProvider.getWebView(), generateCaptureHtml(), this.baseURL.toExternalForm());
}
for (Runnable r : onComplete) {
r.run();
}
}
Aggregations