use of com.codename1.io.File 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.io.File in project CodenameOne by codenameone.
the class CSSTheme method updateResources.
public void updateResources() {
for (String id : elements.keySet()) {
if (!isModified(id)) {
continue;
}
String currToken = "";
try {
Element el = elements.get(id);
Map<String, LexicalUnit> unselectedStyles = el.getUnselected().getFlattenedStyle();
Map<String, LexicalUnit> selectedStyles = el.getSelected().getFlattenedStyle();
Map<String, LexicalUnit> pressedStyles = el.getPressed().getFlattenedStyle();
Map<String, LexicalUnit> disabledStyles = el.getDisabled().getFlattenedStyle();
Element selected = el.getSelected();
String selId = id + ".sel";
String unselId = id;
String pressedId = id + ".press";
String disabledId = id + ".dis";
currToken = "padding";
res.setThemeProperty(themeName, unselId + ".padding", el.getThemePadding(unselectedStyles));
currToken = "padUnit";
res.setThemeProperty(themeName, unselId + ".padUnit", el.getThemePaddingUnit(unselectedStyles));
currToken = "selected padding";
res.setThemeProperty(themeName, selId + "#padding", el.getThemePadding(selectedStyles));
currToken = "selected padUnit";
res.setThemeProperty(themeName, selId + "#padUnit", el.getThemePaddingUnit(selectedStyles));
currToken = "pressed padding";
res.setThemeProperty(themeName, pressedId + "#padding", el.getThemePadding(pressedStyles));
currToken = "pressed padUnit";
res.setThemeProperty(themeName, pressedId + "#padUnit", el.getThemePaddingUnit(pressedStyles));
currToken = "disabled padding";
res.setThemeProperty(themeName, disabledId + "#padding", el.getThemePadding(disabledStyles));
currToken = "disabled padUnit";
res.setThemeProperty(themeName, disabledId + "#padUnit", el.getThemePaddingUnit(disabledStyles));
currToken = "margin";
res.setThemeProperty(themeName, unselId + ".margin", el.getThemeMargin(unselectedStyles));
currToken = "marUnit";
res.setThemeProperty(themeName, unselId + ".marUnit", el.getThemeMarginUnit(unselectedStyles));
currToken = "selected margin";
res.setThemeProperty(themeName, selId + "#margin", el.getThemeMargin(selectedStyles));
currToken = "selected marUnit";
res.setThemeProperty(themeName, selId + "#marUnit", el.getThemeMarginUnit(selectedStyles));
currToken = "pressed margin";
res.setThemeProperty(themeName, pressedId + "#margin", el.getThemeMargin(pressedStyles));
currToken = "pressed marUnit";
res.setThemeProperty(themeName, pressedId + "#marUnit", el.getThemeMarginUnit(pressedStyles));
currToken = "disabled margin";
res.setThemeProperty(themeName, disabledId + "#margin", el.getThemeMargin(disabledStyles));
currToken = "disabled marUnit";
res.setThemeProperty(themeName, disabledId + "#marUnit", el.getThemeMarginUnit(disabledStyles));
currToken = "elevation";
if (unselectedStyles.containsKey("elevation")) {
res.setThemeProperty(themeName, unselId + ".elevation", el.getThemeElevation(unselectedStyles));
}
currToken = "selected elevation";
if (selectedStyles.containsKey("elevation")) {
res.setThemeProperty(themeName, selId + "#elevation", el.getThemeElevation(selectedStyles));
}
currToken = "pressed elevation";
if (pressedStyles.containsKey("elevation")) {
res.setThemeProperty(themeName, pressedId + "#elevation", el.getThemeElevation(pressedStyles));
}
currToken = "disabled elevation";
if (disabledStyles.containsKey("elevation")) {
res.setThemeProperty(themeName, disabledId + "#elevation", el.getThemeElevation(disabledStyles));
}
currToken = "iconGap";
float gap = el.getThemeIconGap(unselectedStyles);
if (gap < 0) {
res.setThemeProperty(themeName, unselId + ".iconGap", null);
currToken = "selected iconGap";
res.setThemeProperty(themeName, selId + "#iconGap", null);
currToken = "pressed iconGap";
res.setThemeProperty(themeName, pressedId + "#iconGap", null);
currToken = "disabled iconGap";
res.setThemeProperty(themeName, disabledId + "#iconGap", null);
currToken = "iconGapUnit";
res.setThemeProperty(themeName, unselId + ".iconGapUnit", null);
currToken = "selected iconGapUnit";
res.setThemeProperty(themeName, selId + "#iconGapUnit", null);
currToken = "pressed iconGapUnit";
res.setThemeProperty(themeName, pressedId + "#iconGapUnit", null);
currToken = "disabled iconGapUnit";
res.setThemeProperty(themeName, disabledId + "#iconGapUnit", null);
} else {
res.setThemeProperty(themeName, unselId + ".iconGap", gap);
currToken = "selected iconGap";
res.setThemeProperty(themeName, selId + "#iconGap", gap);
currToken = "pressed iconGap";
res.setThemeProperty(themeName, pressedId + "#iconGap", gap);
currToken = "disabled iconGap";
res.setThemeProperty(themeName, disabledId + "#iconGap", gap);
currToken = "iconGapUnit";
byte gapUnit = el.getThemeIconGapUnit(unselectedStyles);
res.setThemeProperty(themeName, unselId + ".iconGapUnit", gapUnit);
currToken = "selected iconGapUnit";
res.setThemeProperty(themeName, selId + "#iconGapUnit", gapUnit);
currToken = "pressed iconGapUnit";
res.setThemeProperty(themeName, pressedId + "#iconGapUnit", gapUnit);
currToken = "disabled iconGapUnit";
res.setThemeProperty(themeName, disabledId + "#iconGapUnit", gapUnit);
}
currToken = "surface";
if (unselectedStyles.containsKey("surface")) {
res.setThemeProperty(themeName, unselId + ".surface", el.getThemeSurface(unselectedStyles));
}
currToken = "selected surface";
if (selectedStyles.containsKey("surface")) {
res.setThemeProperty(themeName, selId + "#surface", el.getThemeSurface(selectedStyles));
}
currToken = "pressed surface";
if (pressedStyles.containsKey("surface")) {
res.setThemeProperty(themeName, pressedId + "#surface", el.getThemeSurface(pressedStyles));
}
currToken = "disabled surface";
if (disabledStyles.containsKey("surface")) {
res.setThemeProperty(themeName, disabledId + "#surface", el.getThemeSurface(disabledStyles));
}
currToken = "fgColor";
res.setThemeProperty(themeName, unselId + ".fgColor", el.getThemeFgColor(unselectedStyles));
currToken = "selected fgColor";
res.setThemeProperty(themeName, selId + "#fgColor", el.getThemeFgColor(selectedStyles));
currToken = "pressed fgColor";
res.setThemeProperty(themeName, pressedId + "#fgColor", el.getThemeFgColor(pressedStyles));
currToken = "disabled fgColor";
res.setThemeProperty(themeName, disabledId + "#fgColor", el.getThemeFgColor(disabledStyles));
currToken = "fgAlpha";
res.setThemeProperty(themeName, unselId + ".fgAlpha", el.getThemeFgAlpha(unselectedStyles));
currToken = "selected fgAlpha";
res.setThemeProperty(themeName, selId + "#fgAlpha", el.getThemeFgAlpha(selectedStyles));
currToken = "pressed fgAlpha";
res.setThemeProperty(themeName, pressedId + "#fgAlpha", el.getThemeFgAlpha(pressedStyles));
currToken = "disabled fgAlpha";
res.setThemeProperty(themeName, disabledId + "#fgAlpha", el.getThemeFgAlpha(disabledStyles));
currToken = "bgColor";
res.setThemeProperty(themeName, unselId + ".bgColor", el.getThemeBgColor(unselectedStyles));
currToken = "selected bgColor";
res.setThemeProperty(themeName, selId + "#bgColor", el.getThemeBgColor(selectedStyles));
currToken = "pressed bgColor";
res.setThemeProperty(themeName, pressedId + "#bgColor", el.getThemeBgColor(pressedStyles));
currToken = "disabled bgColor";
res.setThemeProperty(themeName, disabledId + "#bgColor", el.getThemeBgColor(disabledStyles));
currToken = "transparency";
res.setThemeProperty(themeName, unselId + ".transparency", el.getThemeTransparency(unselectedStyles));
currToken = "selected transparency";
res.setThemeProperty(themeName, selId + "#transparency", el.getThemeTransparency(selectedStyles));
currToken = "pressed transparency";
res.setThemeProperty(themeName, pressedId + "#transparency", el.getThemeTransparency(pressedStyles));
currToken = "disabled transparency";
res.setThemeProperty(themeName, disabledId + "#transparency", el.getThemeTransparency(disabledStyles));
currToken = "align";
res.setThemeProperty(themeName, unselId + ".align", el.getThemeAlignment(unselectedStyles));
currToken = "selected align";
res.setThemeProperty(themeName, selId + "#align", el.getThemeAlignment(selectedStyles));
currToken = "pressed align";
res.setThemeProperty(themeName, pressedId + "#align", el.getThemeAlignment(pressedStyles));
currToken = "disabled align";
res.setThemeProperty(themeName, disabledId + "#align", el.getThemeAlignment(disabledStyles));
currToken = "font";
res.setThemeProperty(themeName, unselId + ".font", el.getThemeFont(unselectedStyles));
currToken = "selected font";
res.setThemeProperty(themeName, selId + "#font", el.getThemeFont(selectedStyles));
currToken = "pressed font";
res.setThemeProperty(themeName, pressedId + "#font", el.getThemeFont(pressedStyles));
currToken = "disabled font";
res.setThemeProperty(themeName, disabledId + "#font", el.getThemeFont(disabledStyles));
currToken = "textDecoration";
res.setThemeProperty(themeName, unselId + ".textDecoration", el.getThemeTextDecoration(unselectedStyles));
currToken = "selected textDecoration";
res.setThemeProperty(themeName, selId + "#textDecoration", el.getThemeTextDecoration(selectedStyles));
currToken = "pressed textDecoration";
res.setThemeProperty(themeName, pressedId + "#textDecoration", el.getThemeTextDecoration(pressedStyles));
currToken = "disabled textDecoration";
res.setThemeProperty(themeName, disabledId + "#textDecoration", el.getThemeTextDecoration(disabledStyles));
currToken = "bgGradient";
res.setThemeProperty(themeName, unselId + ".bgGradient", el.getThemeBgGradient(unselectedStyles));
currToken = "selected bgGradient";
res.setThemeProperty(themeName, selId + "#bgGradient", el.getThemeBgGradient(selectedStyles));
currToken = "pressed bgGradient";
res.setThemeProperty(themeName, pressedId + "#bgGradient", el.getThemeBgGradient(pressedStyles));
currToken = "disabled bgGradient";
res.setThemeProperty(themeName, disabledId + "#bgGradient", el.getThemeBgGradient(disabledStyles));
currToken = "bgType";
res.setThemeProperty(themeName, unselId + ".bgType", el.getThemeBgType(unselectedStyles));
currToken = "selected bgType";
res.setThemeProperty(themeName, selId + "#bgType", el.getThemeBgType(selectedStyles));
currToken = "pressed bgType";
res.setThemeProperty(themeName, pressedId + "#bgType", el.getThemeBgType(pressedStyles));
currToken = "disabled bgType";
res.setThemeProperty(themeName, disabledId + "#bgType", el.getThemeBgType(disabledStyles));
currToken = "derive";
res.setThemeProperty(themeName, unselId + ".derive", el.getThemeDerive(unselectedStyles, ""));
currToken = "selected derive";
res.setThemeProperty(themeName, selId + "#derive", el.getThemeDerive(selectedStyles, ".sel"));
currToken = "pressed derive";
res.setThemeProperty(themeName, pressedId + "#derive", el.getThemeDerive(pressedStyles, ".press"));
currToken = "disabled derive";
res.setThemeProperty(themeName, disabledId + "#derive", el.getThemeDerive(disabledStyles, ".dis"));
currToken = "opacity";
res.setThemeProperty(themeName, unselId + ".opacity", el.getThemeOpacity(unselectedStyles));
currToken = "selected opacity";
res.setThemeProperty(themeName, selId + "#opacity", el.getThemeOpacity(selectedStyles));
currToken = "pressed opacity";
res.setThemeProperty(themeName, pressedId + "#opacity", el.getThemeOpacity(pressedStyles));
currToken = "disabled opacity";
res.setThemeProperty(themeName, disabledId + "#opacity", el.getThemeOpacity(disabledStyles));
currToken = "bgImage";
if (el.hasBackgroundImage(unselectedStyles) && !el.requiresBackgroundImageGeneration(unselectedStyles) && !el.requiresImageBorder(unselectedStyles)) {
Image[] imageId = getBackgroundImages(unselectedStyles);
if (imageId != null && imageId.length > 0) {
res.setThemeProperty(themeName, unselId + ".bgImage", imageId[0]);
}
}
currToken = "selected bgImage";
if (el.hasBackgroundImage(selectedStyles) && !el.requiresBackgroundImageGeneration(selectedStyles) && !el.requiresImageBorder(selectedStyles)) {
Image[] imageId = getBackgroundImages(selectedStyles);
if (imageId != null && imageId.length > 0) {
res.setThemeProperty(themeName, selId + "#bgImage", imageId[0]);
}
}
currToken = "pressed bgImage";
if (el.hasBackgroundImage(pressedStyles) && !el.requiresBackgroundImageGeneration(pressedStyles) && !el.requiresImageBorder(pressedStyles)) {
Image[] imageId = getBackgroundImages(pressedStyles);
if (imageId != null && imageId.length > 0) {
res.setThemeProperty(themeName, pressedId + "#bgImage", imageId[0]);
}
}
currToken = "disabled bgImage";
if (el.hasBackgroundImage(disabledStyles) && !el.requiresBackgroundImageGeneration(disabledStyles) && !el.requiresImageBorder(disabledStyles)) {
Image[] imageId = getBackgroundImages(disabledStyles);
if (imageId != null && imageId.length > 0) {
res.setThemeProperty(themeName, disabledId + "#bgImage", imageId[0]);
}
}
currToken = "border";
if (!el.requiresImageBorder(unselectedStyles) && !el.requiresBackgroundImageGeneration(unselectedStyles)) {
res.setThemeProperty(themeName, unselId + ".border", el.getThemeBorder(unselectedStyles));
}
currToken = "selected border";
if (!el.requiresImageBorder(selectedStyles) && !el.requiresBackgroundImageGeneration(selectedStyles)) {
res.setThemeProperty(themeName, selId + "#border", el.getThemeBorder(selectedStyles));
}
currToken = "pressed border";
if (!el.requiresImageBorder(pressedStyles) && !el.requiresBackgroundImageGeneration(pressedStyles)) {
res.setThemeProperty(themeName, pressedId + "#border", el.getThemeBorder(pressedStyles));
}
currToken = "disabled border";
if (!el.requiresImageBorder(disabledStyles) && !el.requiresBackgroundImageGeneration(disabledStyles)) {
res.setThemeProperty(themeName, disabledId + "#border", el.getThemeBorder(disabledStyles));
}
} catch (RuntimeException t) {
System.err.println("An error occurred while updating resources for UIID " + id + ". Processing property " + currToken);
throw t;
}
}
for (String constantKey : constants.keySet()) {
try {
LexicalUnit lu = constants.get(constantKey);
if (lu.getLexicalUnitType() == LexicalUnit.SAC_STRING_VALUE || lu.getLexicalUnitType() == LexicalUnit.SAC_IDENT) {
if (constantKey.endsWith("Image")) {
// We have an image
Image im = res.getImage(lu.getStringValue());
if (im == null) {
im = getResourceImage(lu.getStringValue());
}
if (im == null) {
System.err.println("Error processing file " + this.baseURL);
throw new RuntimeException("Failed to set constant value " + constantKey + " to value " + lu.getStringValue() + " because no such image was found in the resource file");
}
res.setThemeProperty(themeName, "@" + constantKey, im);
} else {
res.setThemeProperty(themeName, "@" + constantKey, lu.getStringValue());
}
} else if (lu.getLexicalUnitType() == LexicalUnit.SAC_INTEGER) {
res.setThemeProperty(themeName, "@" + constantKey, String.valueOf(((ScaledUnit) lu).getIntegerValue()));
}
} catch (RuntimeException t) {
System.err.println("\nAn error occurred processing constant key " + constantKey);
throw t;
}
}
imagesMetadata.store(res);
Map<String, Object> theme = res.getTheme(themeName);
HashSet<String> keys = new HashSet<String>();
keys.addAll(theme.keySet());
Set<String> deletedIds = getDeletedElements();
for (String key : keys) {
if (key.startsWith("Default.")) {
res.setThemeProperty(themeName, key.substring(key.indexOf(".") + 1), theme.get(key));
} else {
for (String delId : deletedIds) {
if (key.startsWith(delId + ".") || key.startsWith(delId + "#")) {
res.setThemeProperty(themeName, key, null);
}
}
}
}
// Get rid of unused images now
deleteUnusedImages();
}
use of com.codename1.io.File in project CodenameOne by codenameone.
the class CSSTheme method getResourceImage.
public Image getResourceImage(String imageName) {
if (new File(cssFile, "res").exists()) {
File res = new File(cssFile, "res");
File preferredThemeDir = new File(res, themeName);
if (preferredThemeDir.exists()) {
Image i = null;
try {
i = getResourceImage(imageName, preferredThemeDir);
if (i != null) {
return i;
}
} catch (Throwable t) {
t.printStackTrace();
}
}
for (File d : res.listFiles()) {
if (d.isDirectory()) {
Image i = null;
try {
i = getResourceImage(imageName, d);
if (i != null) {
return i;
}
} catch (Throwable t) {
t.printStackTrace();
}
}
}
}
if (new File(cssFile.getParentFile(), "res").exists()) {
File res = new File(cssFile.getParentFile(), "res");
File preferredThemeDir = new File(res, themeName);
if (preferredThemeDir.exists()) {
Image i = null;
try {
i = getResourceImage(imageName, preferredThemeDir);
if (i != null) {
return i;
}
} catch (Throwable t) {
t.printStackTrace();
}
}
for (File d : res.listFiles()) {
if (d.isDirectory()) {
Image i = null;
try {
i = getResourceImage(imageName, d);
if (i != null) {
return i;
}
} catch (Throwable t) {
t.printStackTrace();
}
}
}
}
if (new File(cssFile.getParentFile().getParentFile(), "res").exists()) {
File res = new File(cssFile.getParentFile().getParentFile(), "res");
File preferredThemeDir = new File(res, themeName);
if (preferredThemeDir.exists()) {
Image i = null;
try {
i = getResourceImage(imageName, preferredThemeDir);
if (i != null) {
return i;
}
} catch (Throwable t) {
t.printStackTrace();
}
}
for (File d : res.listFiles()) {
if (d.isDirectory()) {
Image i = null;
try {
i = getResourceImage(imageName, d);
if (i != null) {
return i;
}
} catch (Throwable t) {
t.printStackTrace();
}
}
}
}
return null;
}
use of com.codename1.io.File in project CodenameOne by codenameone.
the class EditableResources method save.
/**
* Allows us to store a modified resource file
*/
public void save(OutputStream out) throws IOException {
if (overrideFile != null) {
overrideResource.save(new FileOutputStream(overrideFile));
}
// disable override for the duration of the save so stuff from the override doesn't
// get into the main resource file
File overrideFileBackup = overrideFile;
EditableResources overrideResourceBackup = overrideResource;
overrideResource = null;
overrideFile = null;
try {
DataOutputStream output = new DataOutputStream(out);
String[] resourceNames = getResourceNames();
keyOffset = 0;
if (currentPassword != null) {
output.writeShort(resourceNames.length + 2);
output.writeByte(MAGIC_PASSWORD);
output.writeUTF("" + ((char) encode('l')) + ((char) encode('w')));
output.writeByte(encode(MAGIC_HEADER & 0xff));
} else {
output.writeShort(resourceNames.length + 1);
// write the header of the resource file
output.writeByte(MAGIC_HEADER);
}
output.writeUTF("");
// the size of the header
output.writeShort(6);
output.writeShort(MAJOR_VERSION);
output.writeShort(MINOR_VERSION);
// currently resource file meta-data isn't supported
output.writeShort(0);
for (int iter = 0; iter < resourceNames.length; iter++) {
// write the magic number
byte magic = getResourceType(resourceNames[iter]);
switch(magic) {
case MAGIC_TIMELINE:
case MAGIC_ANIMATION_LEGACY:
case MAGIC_IMAGE_LEGACY:
case MAGIC_INDEXED_IMAGE_LEGACY:
magic = MAGIC_IMAGE;
break;
case MAGIC_THEME_LEGACY:
magic = MAGIC_THEME;
break;
case MAGIC_FONT_LEGACY:
magic = MAGIC_FONT;
break;
}
if (currentPassword != null) {
output.writeByte(encode(magic & 0xff));
char[] chars = resourceNames[iter].toCharArray();
for (int i = 0; i < chars.length; i++) {
chars[i] = (char) encode(chars[i] & 0xffff);
}
output.writeUTF(new String(chars));
} else {
output.writeByte(magic);
output.writeUTF(resourceNames[iter]);
}
switch(magic) {
case MAGIC_IMAGE:
Object o = getResourceObject(resourceNames[iter]);
if (!(o instanceof MultiImage)) {
o = null;
}
saveImage(output, getImage(resourceNames[iter]), (MultiImage) o, BufferedImage.TYPE_INT_ARGB);
continue;
case MAGIC_THEME:
saveTheme(output, getTheme(resourceNames[iter]), magic == MAGIC_THEME_LEGACY);
continue;
case MAGIC_FONT:
saveFont(output, false, resourceNames[iter]);
continue;
case MAGIC_DATA:
{
InputStream i = getData(resourceNames[iter]);
ByteArrayOutputStream outArray = new ByteArrayOutputStream();
int val = i.read();
while (val != -1) {
outArray.write(val);
val = i.read();
}
byte[] data = outArray.toByteArray();
output.writeInt(data.length);
output.write(data);
continue;
}
case MAGIC_UI:
{
InputStream i = getUi(resourceNames[iter]);
ByteArrayOutputStream outArray = new ByteArrayOutputStream();
int val = i.read();
while (val != -1) {
outArray.write(val);
val = i.read();
}
byte[] data = outArray.toByteArray();
output.writeInt(data.length);
output.write(data);
continue;
}
case MAGIC_L10N:
// we are getting the theme which allows us to acces the l10n data
saveL10N(output, getTheme(resourceNames[iter]));
continue;
default:
throw new IOException("Corrupt theme file unrecognized magic number: " + Integer.toHexString(magic & 0xff));
}
}
modified = false;
updateModified();
undoQueue.clear();
redoQueue.clear();
} finally {
overrideFile = overrideFileBackup;
overrideResource = overrideResourceBackup;
}
}
use of com.codename1.io.File in project CodenameOne by codenameone.
the class EditableResources method openFileWithXMLSupport.
public void openFileWithXMLSupport(File f) throws IOException {
if (xmlEnabled && f.getParentFile().getName().equals("src")) {
File res = new File(f.getParentFile().getParentFile(), "res");
if (res.exists()) {
File xml = new File(res, f.getName().substring(0, f.getName().length() - 3) + "xml");
if (xml.exists()) {
loadingMode = true;
com.codename1.ui.Font.clearBitmapCache();
clear();
try {
File resDir = new File(res, f.getName().substring(0, f.getName().length() - 4));
// open the XML file...
JAXBContext ctx = JAXBContext.newInstance(ResourceFileXML.class);
ResourceFileXML xmlData = (ResourceFileXML) ctx.createUnmarshaller().unmarshal(xml);
boolean normalize = xmlData.getMajorVersion() > 1 || xmlData.getMinorVersion() > 5;
majorVersion = (short) xmlData.getMajorVersion();
minorVersion = (short) xmlData.getMinorVersion();
xmlUI = xmlData.isUseXmlUI();
if (xmlData.getData() != null) {
for (Data d : xmlData.getData()) {
setResource(d.getName(), MAGIC_DATA, readFile(resDir, d.getName(), normalize));
}
}
if (xmlData.getLegacyFont() != null) {
for (LegacyFont d : xmlData.getLegacyFont()) {
String name = d.getName();
if (normalize) {
name = normalizeFileName(name);
}
DataInputStream fi = new DataInputStream(new FileInputStream(new File(resDir, name)));
setResource(d.getName(), MAGIC_FONT, loadFont(fi, d.getName(), false));
fi.close();
}
}
if (xmlData.getImage() != null) {
for (com.codename1.ui.util.xml.Image d : xmlData.getImage()) {
if (d.getType() == null) {
// standara JPG or PNG
String name = d.getName();
if (normalize) {
name = normalizeFileName(name);
}
FileInputStream fi = new FileInputStream(new File(resDir, name));
EncodedImage e = EncodedImage.create(fi);
fi.close();
setResource(d.getName(), MAGIC_IMAGE, e);
continue;
}
if ("svg".equals(d.getType())) {
setResource(d.getName(), MAGIC_IMAGE, Image.createSVG(d.getType(), false, readFile(resDir, d.getName(), normalize)));
continue;
}
if ("timeline".equals(d.getType())) {
String name = d.getName();
if (normalize) {
name = normalizeFileName(name);
}
DataInputStream fi = new DataInputStream(new FileInputStream(new File(resDir, name)));
setResource(d.getName(), MAGIC_IMAGE, readTimeline(fi));
fi.close();
continue;
}
if ("multi".equals(d.getType())) {
String name = d.getName();
if (normalize) {
name = normalizeFileName(name);
}
File multiImageDir = new File(resDir, name);
File hd4k = new File(multiImageDir, "4k.png");
File hd2 = new File(multiImageDir, "2hd.png");
File hd560 = new File(multiImageDir, "560.png");
File hd = new File(multiImageDir, "hd.png");
File veryhigh = new File(multiImageDir, "veryhigh.png");
File high = new File(multiImageDir, "high.png");
File medium = new File(multiImageDir, "medium.png");
File low = new File(multiImageDir, "low.png");
File veryLow = new File(multiImageDir, "verylow.png");
Map<Integer, EncodedImage> images = new HashMap<Integer, EncodedImage>();
if (hd4k.exists()) {
images.put(new Integer(Display.DENSITY_4K), EncodedImage.create(readFileNoNormal(hd4k)));
}
if (hd2.exists()) {
images.put(new Integer(Display.DENSITY_2HD), EncodedImage.create(readFileNoNormal(hd2)));
}
if (hd560.exists()) {
images.put(new Integer(Display.DENSITY_560), EncodedImage.create(readFileNoNormal(hd560)));
}
if (hd.exists()) {
images.put(new Integer(Display.DENSITY_HD), EncodedImage.create(readFileNoNormal(hd)));
}
if (veryhigh.exists()) {
images.put(new Integer(Display.DENSITY_VERY_HIGH), EncodedImage.create(readFileNoNormal(veryhigh)));
}
if (high.exists()) {
images.put(new Integer(Display.DENSITY_HIGH), EncodedImage.create(readFileNoNormal(high)));
}
if (medium.exists()) {
images.put(new Integer(Display.DENSITY_MEDIUM), EncodedImage.create(readFileNoNormal(medium)));
}
if (low.exists()) {
images.put(new Integer(Display.DENSITY_LOW), EncodedImage.create(readFileNoNormal(low)));
}
if (veryLow.exists()) {
images.put(new Integer(Display.DENSITY_VERY_LOW), EncodedImage.create(readFileNoNormal(veryLow)));
}
int[] dpis = new int[images.size()];
EncodedImage[] imageArray = new EncodedImage[images.size()];
int count = 0;
for (Map.Entry<Integer, EncodedImage> m : images.entrySet()) {
dpis[count] = m.getKey().intValue();
imageArray[count] = m.getValue();
count++;
}
MultiImage result = new MultiImage();
result.setDpi(dpis);
result.setInternalImages(imageArray);
setResource(d.getName(), MAGIC_IMAGE, result);
continue;
}
}
}
if (xmlData.getL10n() != null) {
for (L10n d : xmlData.getL10n()) {
Hashtable<String, Hashtable<String, String>> l10n = new Hashtable<String, Hashtable<String, String>>();
for (Lang l : d.getLang()) {
Hashtable<String, String> language = new Hashtable<String, String>();
if (l != null && l.getEntry() != null) {
for (Entry e : l.getEntry()) {
language.put(e.getKey(), e.getValue());
}
}
l10n.put(l.getName(), language);
}
setResource(d.getName(), MAGIC_L10N, l10n);
}
}
if (xmlData.getTheme() != null) {
for (Theme d : xmlData.getTheme()) {
Hashtable<String, Object> theme = new Hashtable<String, Object>();
theme.put("uninitialized", Boolean.TRUE);
if (d.getVal() != null) {
for (Val v : d.getVal()) {
String key = v.getKey();
if (key.endsWith("align") || key.endsWith("textDecoration") || key.endsWith(Style.ELEVATION)) {
theme.put(key, Integer.valueOf(v.getValue()));
continue;
}
if (key.endsWith(Style.BACKGROUND_TYPE) || key.endsWith(Style.BACKGROUND_ALIGNMENT) || key.endsWith(Style.ICON_GAP_UNIT)) {
theme.put(key, Byte.valueOf(v.getValue()));
continue;
}
if (key.endsWith(Style.SURFACE)) {
theme.put(key, Boolean.valueOf(v.getValue()));
continue;
}
if (key.endsWith(Style.ICON_GAP)) {
theme.put(key, Float.valueOf(v.getValue()));
continue;
}
// padding and or margin type
if (key.endsWith("Unit")) {
String[] s = v.getValue().split(",");
theme.put(key, new byte[] { Byte.parseByte(s[0]), Byte.parseByte(s[1]), Byte.parseByte(s[2]), Byte.parseByte(s[3]) });
continue;
}
theme.put(key, v.getValue());
}
}
if (d.getBorder() != null) {
for (com.codename1.ui.util.xml.Border b : d.getBorder()) {
if ("empty".equals(b.getType())) {
theme.put(b.getKey(), Border.createEmpty());
continue;
}
if ("round".equals(b.getType())) {
RoundBorder rb = RoundBorder.create();
rb = rb.opacity(b.getOpacity());
rb = rb.color(b.getRoundBorderColor());
rb = rb.rectangle(b.isRectangle());
rb = rb.shadowBlur(b.getShadowBlur());
rb = rb.shadowOpacity(b.getShadowOpacity());
rb = rb.shadowSpread((int) b.getShadowSpread(), b.isShadowMM());
rb = rb.shadowX(b.getShadowX());
rb = rb.shadowY(b.getShadowY());
rb = rb.stroke(b.getStrokeThickness(), b.isStrokeMM());
rb = rb.strokeColor(b.getStrokeColor());
rb = rb.strokeOpacity(b.getStrokeOpacity());
theme.put(b.getKey(), rb);
continue;
}
if ("css".equals(b.getType())) {
CSSBorder cb = new CSSBorder(this, b.getCSS());
theme.put(b.getKey(), cb);
continue;
}
if ("roundRect".equals(b.getType())) {
RoundRectBorder rb = RoundRectBorder.create();
rb = rb.shadowBlur(b.getShadowBlur());
rb = rb.shadowOpacity(b.getShadowOpacity());
rb = rb.shadowSpread(b.getShadowSpread());
rb = rb.shadowX(b.getShadowX());
rb = rb.shadowY(b.getShadowY());
rb = rb.stroke(b.getStrokeThickness(), b.isStrokeMM());
rb = rb.strokeColor(b.getStrokeColor());
rb = rb.strokeOpacity(b.getStrokeOpacity());
rb = rb.bezierCorners(b.isBezierCorners());
if (b.isTopOnlyMode()) {
rb.topOnlyMode(true);
} else if (b.isBottomOnlyMode()) {
rb.bottomOnlyMode(true);
}
rb = rb.cornerRadius(b.getCornerRadius());
theme.put(b.getKey(), rb);
continue;
}
if ("line".equals(b.getType())) {
if (b.getColor() == null) {
if (b.isMillimeters()) {
theme.put(b.getKey(), Border.createLineBorder(b.getThickness().floatValue()));
} else {
theme.put(b.getKey(), Border.createLineBorder(b.getThickness().intValue()));
}
} else {
if (b.isMillimeters()) {
theme.put(b.getKey(), Border.createLineBorder(b.getThickness().floatValue(), b.getColor().intValue()));
} else {
theme.put(b.getKey(), Border.createLineBorder(b.getThickness().intValue(), b.getColor().intValue()));
}
}
continue;
}
if ("underline".equals(b.getType())) {
if (b.getColor() == null) {
theme.put(b.getKey(), Border.createUndelineBorder(b.getThickness().intValue()));
} else {
theme.put(b.getKey(), Border.createUnderlineBorder(b.getThickness().intValue(), b.getColor().intValue()));
}
continue;
}
if ("rounded".equals(b.getType())) {
if (b.getColor() == null) {
theme.put(b.getKey(), Border.createRoundBorder(b.getArcW().intValue(), b.getArcH().intValue()));
} else {
theme.put(b.getKey(), Border.createRoundBorder(b.getArcW().intValue(), b.getArcH().intValue(), b.getColor().intValue()));
}
continue;
}
if ("etchedRaised".equals(b.getType())) {
if (b.getColor() == null) {
theme.put(b.getKey(), Border.createEtchedRaised());
} else {
theme.put(b.getKey(), Border.createEtchedRaised(b.getColor().intValue(), b.getColorB().intValue()));
}
continue;
}
if ("etchedLowered".equals(b.getType())) {
if (b.getColor() == null) {
theme.put(b.getKey(), Border.createEtchedLowered());
} else {
theme.put(b.getKey(), Border.createEtchedLowered(b.getColor().intValue(), b.getColorB().intValue()));
}
continue;
}
if ("bevelLowered".equals(b.getType())) {
if (b.getColor() == null) {
theme.put(b.getKey(), Border.createBevelLowered());
} else {
theme.put(b.getKey(), Border.createBevelLowered(b.getColor().intValue(), b.getColorB().intValue(), b.getColorC().intValue(), b.getColorD().intValue()));
}
continue;
}
if ("bevelRaised".equals(b.getType())) {
if (b.getColor() == null) {
theme.put(b.getKey(), Border.createBevelRaised());
} else {
theme.put(b.getKey(), Border.createBevelRaised(b.getColor().intValue(), b.getColorB().intValue(), b.getColorC().intValue(), b.getColorD().intValue()));
}
continue;
}
if ("image".equals(b.getType())) {
int imageCount = 2;
if (b.getI9() != null) {
imageCount = 9;
} else {
if (b.getI8() != null) {
imageCount = 8;
} else {
if (b.getI3() != null) {
imageCount = 3;
}
}
}
String[] borderInstance;
switch(imageCount) {
case 2:
borderInstance = new String[] { b.getI1(), b.getI2() };
break;
case 3:
borderInstance = new String[] { b.getI1(), b.getI2(), b.getI3() };
break;
case 8:
borderInstance = new String[] { b.getI1(), b.getI2(), b.getI3(), b.getI4(), b.getI5(), b.getI6(), b.getI7(), b.getI8() };
break;
default:
borderInstance = new String[] { b.getI1(), b.getI2(), b.getI3(), b.getI4(), b.getI5(), b.getI6(), b.getI7(), b.getI8(), b.getI9() };
break;
}
theme.put(b.getKey(), borderInstance);
continue;
}
if ("imageH".equals(b.getType())) {
theme.put(b.getKey(), new String[] { "h", b.getI1(), b.getI2(), b.getI3() });
continue;
}
if ("imageV".equals(b.getType())) {
theme.put(b.getKey(), new String[] { "v", b.getI1(), b.getI2(), b.getI3() });
continue;
}
}
}
if (d.getFont() != null) {
for (com.codename1.ui.util.xml.Font b : d.getFont()) {
if ("ttf".equals(b.getType())) {
com.codename1.ui.Font system = com.codename1.ui.Font.createSystemFont(b.getFace().intValue(), b.getStyle().intValue(), b.getSize().intValue());
EditorTTFFont t;
if (b.getName().startsWith("native:")) {
t = new EditorTTFFont(b.getName(), b.getSizeSettings().intValue(), b.getActualSize().floatValue(), system);
} else {
t = new EditorTTFFont(new File(f.getParentFile(), b.getName()), b.getSizeSettings().intValue(), b.getActualSize().floatValue(), system);
}
theme.put(b.getKey(), t);
continue;
}
if ("system".equals(b.getType())) {
com.codename1.ui.Font system = com.codename1.ui.Font.createSystemFont(b.getFace().intValue(), b.getStyle().intValue(), b.getSize().intValue());
theme.put(b.getKey(), system);
continue;
}
// bitmap fonts aren't supported right now
}
}
if (d.getGradient() != null) {
for (com.codename1.ui.util.xml.Gradient b : d.getGradient()) {
theme.put(b.getKey(), new Object[] { b.getColor1(), b.getColor2(), b.getPosX(), b.getPosY(), b.getRadius() });
}
}
setResource(d.getName(), MAGIC_THEME, theme);
}
}
// we load the UI last since it might depend on images or other elements in the future
if (xmlData.getUi() != null) {
if (xmlData.isUseXmlUI()) {
ArrayList<ComponentEntry> guiElements = new ArrayList<ComponentEntry>();
// place renderers first
final ArrayList<String> renderers = new ArrayList<String>();
for (Ui d : xmlData.getUi()) {
JAXBContext componentContext = JAXBContext.newInstance(ComponentEntry.class);
File uiFile = new File(resDir, normalizeFileName(d.getName()) + ".ui");
ComponentEntry uiXMLData = (ComponentEntry) componentContext.createUnmarshaller().unmarshal(uiFile);
guiElements.add(uiXMLData);
uiXMLData.findRendererers(renderers);
}
Collections.sort(guiElements, new Comparator<ComponentEntry>() {
private final ArrayList<String> entries1 = new ArrayList<String>();
private final ArrayList<String> entries2 = new ArrayList<String>();
@Override
public int compare(ComponentEntry o1, ComponentEntry o2) {
if (renderers.contains(o1.getName())) {
return -1;
}
if (renderers.contains(o2.getName())) {
return 1;
}
entries1.clear();
entries2.clear();
o1.findEmbeddedDependencies(entries1);
o2.findEmbeddedDependencies(entries2);
if (entries1.size() == 0) {
if (entries2.size() == 0) {
return 0;
}
return -1;
} else {
if (entries2.size() == 0) {
return 1;
}
}
for (String e : entries1) {
if (e.equals(o2.getName())) {
return 1;
}
}
for (String e : entries2) {
if (e.equals(o1.getName())) {
return -1;
}
}
return 0;
}
});
for (ComponentEntry uiXMLData : guiElements) {
UIBuilderOverride uib = new UIBuilderOverride();
com.codename1.ui.Container cnt = uib.createInstance(uiXMLData, this);
// encountered an error loading the component fallback to loading with the binary types
if (cnt == null) {
for (Ui ui : xmlData.getUi()) {
setResource(uiXMLData.getName(), MAGIC_UI, readFile(resDir, ui.getName(), normalize));
}
break;
} else {
byte[] data = UserInterfaceEditor.persistContainer(cnt, this);
setResource(uiXMLData.getName(), MAGIC_UI, data);
}
}
} else {
for (Ui d : xmlData.getUi()) {
setResource(d.getName(), MAGIC_UI, readFile(resDir, d.getName(), normalize));
}
}
}
loadingMode = false;
modified = false;
updateModified();
// can occure when a resource file is opened via the constructor
if (undoQueue != null) {
undoQueue.clear();
redoQueue.clear();
}
return;
} catch (JAXBException err) {
err.printStackTrace();
}
}
}
}
openFile(new FileInputStream(f));
}
Aggregations