use of com.codename1.ui.util.UIBuilderOverride in project CodenameOne by codenameone.
the class UserInterfaceEditor method createCustomComponentButton.
private void createCustomComponentButton(final CustomComponent c) {
try {
final JButton b = new JButton(c.getType());
b.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/jdesktop/swingx/resources/placeholder32.png")));
b.setHorizontalAlignment(SwingConstants.LEFT);
b.setBorder(null);
userComponents.add(b);
b.putClientProperty("CustomComponent", c);
final Class codenameOneBaseClass = c.getCls();
makeDraggable(b, codenameOneBaseClass, c.getType(), c);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (lockForDragging) {
lockForDragging = false;
return;
}
try {
if (c.isUiResource()) {
UIBuilderOverride u = new UIBuilderOverride();
com.codename1.ui.Component cmp = u.createContainer(res, c.getType());
String t = (String) cmp.getClientProperty(TYPE_KEY);
if (t == null) {
cmp.putClientProperty(TYPE_KEY, c.getType());
t = c.getType();
}
addComponentToContainer(cmp, t);
return;
}
com.codename1.ui.Component cmp = (com.codename1.ui.Component) codenameOneBaseClass.newInstance();
cmp.putClientProperty("CustomComponent", c);
cmp.putClientProperty(TYPE_KEY, c.getType());
initializeComponentText(cmp);
addComponentToContainer(cmp, c.getType());
} catch (Exception err) {
err.printStackTrace();
JOptionPane.showMessageDialog(UserInterfaceEditor.this, err.getClass().getName() + ": " + err, "Error", JOptionPane.ERROR_MESSAGE);
}
}
});
/*b.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if(BaseForm.isRightClick(e)) {
JPopupMenu p = new JPopupMenu();
AbstractAction deleteAction = new AbstractAction("Delete") {
public void actionPerformed(ActionEvent e) {
componentPalette.remove(b);
componentPalette.revalidate();
customComponents.remove(c);
res.setUi(name, persistContainer(containerInstance));
}
};
p.add(deleteAction);
p.show(b, e.getPoint().x, e.getPoint().y);
}
}
});*/
} catch (Exception err) {
err.printStackTrace();
JOptionPane.showMessageDialog(UserInterfaceEditor.this, err.getClass().getName() + ": " + err, "Error", JOptionPane.ERROR_MESSAGE);
}
}
use of com.codename1.ui.util.UIBuilderOverride in project CodenameOne by codenameone.
the class EditableResources method saveXMLFile.
private void saveXMLFile(File xml, File resourcesDir) throws IOException {
// 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 {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(xml), "UTF-8"));
String[] resourceNames = getResourceNames();
Arrays.sort(resourceNames, String.CASE_INSENSITIVE_ORDER);
bw.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n");
bw.write("<resource majorVersion=\"" + MAJOR_VERSION + "\" minorVersion=\"" + MINOR_VERSION + "\" useXmlUI=\"" + xmlUI + "\">\n");
for (int iter = 0; iter < resourceNames.length; iter++) {
String xResourceName = xmlize(resourceNames[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;
}
switch(magic) {
case MAGIC_IMAGE:
Object o = getResourceObject(resourceNames[iter]);
if (!(o instanceof MultiImage)) {
o = null;
}
bw.write(" <image name=\"" + xResourceName + "\" ");
com.codename1.ui.Image image = getImage(resourceNames[iter]);
MultiImage mi = (MultiImage) o;
int rType = getImageType(image, mi);
switch(rType) {
// PNG file
case 0xf1:
// JPEG File
case 0xf2:
if (image instanceof EncodedImage) {
byte[] data = ((EncodedImage) image).getImageData();
writeToFile(data, new File(resourcesDir, normalizeFileName(resourceNames[iter])));
} else {
FileOutputStream fo = new FileOutputStream(new File(resourcesDir, normalizeFileName(resourceNames[iter])));
BufferedImage buffer = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
buffer.setRGB(0, 0, image.getWidth(), image.getHeight(), image.getRGB(), 0, image.getWidth());
ImageIO.write(buffer, "png", fo);
fo.close();
}
break;
// SVG
case 0xf5:
// multiimage with SVG
case 0xf7:
SVG s = (SVG) image.getSVGDocument();
writeToFile(s.getSvgData(), new File(resourcesDir, normalizeFileName(resourceNames[iter])));
if (s.getBaseURL() != null && s.getBaseURL().length() > 0) {
bw.write("baseUrl=\"" + s.getBaseURL() + "\" ");
}
bw.write("type=\"svg\" ");
break;
case 0xF6:
File multiImageDir = new File(resourcesDir, normalizeFileName(resourceNames[iter]));
multiImageDir.mkdirs();
for (int imageIter = 0; imageIter < mi.getDpi().length; imageIter++) {
File f = null;
switch(mi.getDpi()[imageIter]) {
case Display.DENSITY_4K:
f = new File(multiImageDir, "4k.png");
break;
case Display.DENSITY_2HD:
f = new File(multiImageDir, "2hd.png");
break;
case Display.DENSITY_560:
f = new File(multiImageDir, "560.png");
break;
case Display.DENSITY_HD:
f = new File(multiImageDir, "hd.png");
break;
case Display.DENSITY_VERY_HIGH:
f = new File(multiImageDir, "veryhigh.png");
break;
case Display.DENSITY_HIGH:
f = new File(multiImageDir, "high.png");
break;
case Display.DENSITY_MEDIUM:
f = new File(multiImageDir, "medium.png");
break;
case Display.DENSITY_LOW:
f = new File(multiImageDir, "low.png");
break;
case Display.DENSITY_VERY_LOW:
f = new File(multiImageDir, "verylow.png");
break;
}
writeToFile(mi.getInternalImages()[imageIter].getImageData(), f);
}
bw.write("type=\"multi\" ");
break;
// Timeline
case MAGIC_TIMELINE:
File timeline = new File(resourcesDir, normalizeFileName(resourceNames[iter]));
DataOutputStream timelineOut = new DataOutputStream(new FileOutputStream(timeline));
writeTimeline(timelineOut, (Timeline) image);
timelineOut.close();
bw.write("type=\"timeline\" ");
break;
// Fail this is the wrong data type
default:
throw new IOException("Illegal type while creating image: " + Integer.toHexString(rType));
}
bw.write(" />\n");
continue;
case MAGIC_THEME:
Hashtable<String, Object> theme = getTheme(resourceNames[iter]);
theme.remove("name");
bw.write(" <theme name=\"" + xResourceName + "\">\n");
ArrayList<String> setOfKeys = new ArrayList<String>(theme.keySet());
Collections.sort(setOfKeys);
for (String key : setOfKeys) {
if (key.startsWith("@")) {
if (key.endsWith("Image")) {
bw.write(" <val key=\"" + key + "\" value=\"" + findId(theme.get(key), true) + "\" />\n");
} else {
bw.write(" <val key=\"" + key + "\" value=\"" + theme.get(key) + "\" />\n");
}
continue;
}
// if this is a simple numeric value
if (key.endsWith("Color")) {
bw.write(" <val key=\"" + key + "\" value=\"" + theme.get(key) + "\" />\n");
continue;
}
if (key.endsWith("align") || key.endsWith("textDecoration")) {
bw.write(" <val key=\"" + key + "\" value=\"" + ((Number) theme.get(key)).shortValue() + "\" />\n");
continue;
}
// if this is a short numeric value
if (key.endsWith("transparency")) {
bw.write(" <val key=\"" + key + "\" value=\"" + theme.get(key) + "\" />\n");
continue;
}
if (key.endsWith("opacity")) {
bw.write(" <val key=\"" + key + "\" value=\"" + theme.get(key) + "\" />\n");
continue;
}
// if this is a padding or margin then we will have the 4 values as bytes
if (key.endsWith("padding") || key.endsWith("margin")) {
bw.write(" <val key=\"" + key + "\" value=\"" + theme.get(key) + "\" />\n");
continue;
}
// padding and or margin type
if (key.endsWith("Unit")) {
byte[] b = (byte[]) theme.get(key);
bw.write(" <val key=\"" + key + "\" value=\"" + b[0] + "," + b[1] + "," + b[2] + "," + b[3] + "\" />\n");
continue;
}
if (key.endsWith("border")) {
Border border = (Border) theme.get(key);
if (border instanceof RoundBorder) {
RoundBorder rb = (RoundBorder) border;
bw.write(" <border key=\"" + key + "\" type=\"round\" " + "roundBorderColor=\"" + rb.getColor() + "\" " + "opacity=\"" + rb.getOpacity() + "\" " + "strokeColor=\"" + rb.getStrokeColor() + "\" " + "strokeOpacity=\"" + rb.getStrokeOpacity() + "\" " + "strokeThickness=\"" + rb.getStrokeThickness() + "\" " + "strokeMM=\"" + rb.isStrokeMM() + "\" " + "shadowSpread=\"" + rb.getShadowSpread() + "\" " + "shadowOpacity=\"" + rb.getShadowOpacity() + "\" " + "shadowX=\"" + rb.getShadowX() + "\" " + "shadowY=\"" + rb.getShadowY() + "\" " + "shadowBlur=\"" + rb.getShadowBlur() + "\" " + "shadowMM=\"" + rb.isShadowMM() + "\" " + "rectangle=\"" + rb.isRectangle() + "\" />\n");
continue;
}
if (border instanceof RoundRectBorder) {
RoundRectBorder rb = (RoundRectBorder) border;
bw.write(" <border key=\"" + key + "\" type=\"roundRect\" " + "strokeColor=\"" + rb.getStrokeColor() + "\" " + "strokeOpacity=\"" + rb.getStrokeOpacity() + "\" " + "strokeThickness=\"" + rb.getStrokeThickness() + "\" " + "strokeMM=\"" + rb.isStrokeMM() + "\" " + "shadowSpread=\"" + rb.getShadowSpread() + "\" " + "shadowOpacity=\"" + rb.getShadowOpacity() + "\" " + "shadowX=\"" + rb.getShadowX() + "\" " + "shadowY=\"" + rb.getShadowY() + "\" " + "shadowBlur=\"" + rb.getShadowBlur() + "\" " + "topOnlyMode=\"" + rb.isTopOnlyMode() + "\" " + "bottomOnlyMode=\"" + rb.isBottomOnlyMode() + "\" " + "cornerRadius=\"" + rb.getCornerRadius() + "\" " + "bezierCorners=\"" + rb.isBezierCorners() + "\" />\n");
continue;
}
int type = Accessor.getType(border);
switch(type) {
case BORDER_TYPE_EMPTY:
bw.write(" <border key=\"" + key + "\" type=\"empty\" />\n");
continue;
case BORDER_TYPE_LINE:
// use theme colors?
if (Accessor.isThemeColors(border)) {
bw.write(" <border key=\"" + key + "\" type=\"line\" millimeters=\"" + Accessor.isMillimeters(border) + "\" thickness=\"" + Accessor.getThickness(border) + "\" />\n");
} else {
bw.write(" <border key=\"" + key + "\" type=\"line\" millimeters=\"" + Accessor.isMillimeters(border) + "\" thickness=\"" + Accessor.getThickness(border) + "\" color=\"" + Accessor.getColorA(border) + "\" />\n");
}
continue;
case BORDER_TYPE_UNDERLINE:
// use theme colors?
if (Accessor.isThemeColors(border)) {
bw.write(" <border key=\"" + key + "\" type=\"underline\" millimeters=\"" + Accessor.isMillimeters(border) + "\" thickness=\"" + Accessor.getThickness(border) + "\" />\n");
} else {
bw.write(" <border key=\"" + key + "\" type=\"underline\" millimeters=\"" + Accessor.isMillimeters(border) + "\" thickness=\"" + Accessor.getThickness(border) + "\" color=\"" + Accessor.getColorA(border) + "\" />\n");
}
continue;
case BORDER_TYPE_ROUNDED:
if (Accessor.isThemeColors(border)) {
bw.write(" <border key=\"" + key + "\" type=\"rounded\" " + "thickness=\"" + Accessor.getThickness(border) + "\" arcW=\"" + Accessor.getArcWidth(border) + "\" arcH=\"" + Accessor.getArcHeight(border) + "\" />\n");
} else {
bw.write(" <border key=\"" + key + "\" type=\"rounded\" " + "thickness=\"" + Accessor.getThickness(border) + "\" arcW=\"" + Accessor.getArcWidth(border) + "\" arcH=\"" + Accessor.getArcHeight(border) + "\" color=\"" + Accessor.getColorA(border) + "\" />\n");
}
continue;
case BORDER_TYPE_ETCHED_RAISED:
// use theme colors?
if (Accessor.isThemeColors(border)) {
bw.write(" <border key=\"" + key + "\" type=\"etchedRaised\" " + "thickness=\"" + Accessor.getThickness(border) + "\" />\n");
} else {
bw.write(" <border key=\"" + key + "\" type=\"etchedRaised\" " + "thickness=\"" + Accessor.getThickness(border) + "\" color=\"" + Accessor.getColorA(border) + "\" colorB=\"" + Accessor.getColorB(border) + "\" />\n");
}
continue;
case BORDER_TYPE_ETCHED_LOWERED:
// use theme colors?
if (Accessor.isThemeColors(border)) {
bw.write(" <border key=\"" + key + "\" type=\"etchedLowered\" " + "thickness=\"" + Accessor.getThickness(border) + "\" />\n");
} else {
bw.write(" <border key=\"" + key + "\" type=\"etchedLowered\" " + "thickness=\"" + Accessor.getThickness(border) + "\" color=\"" + Accessor.getColorA(border) + "\" colorB=\"" + Accessor.getColorB(border) + "\" />\n");
}
continue;
case BORDER_TYPE_BEVEL_LOWERED:
// use theme colors?
if (Accessor.isThemeColors(border)) {
bw.write(" <border key=\"" + key + "\" type=\"bevelLowered\" " + "thickness=\"" + Accessor.getThickness(border) + "\" />\n");
} else {
bw.write(" <border key=\"" + key + "\" type=\"bevelLowered\" " + "thickness=\"" + Accessor.getThickness(border) + "\" color=\"" + Accessor.getColorA(border) + "\" colorB=\"" + Accessor.getColorB(border) + "\" colorC=\"" + Accessor.getColorC(border) + "\" colorD=\"" + Accessor.getColorD(border) + "\" />\n");
}
continue;
case BORDER_TYPE_BEVEL_RAISED:
if (Accessor.isThemeColors(border)) {
bw.write(" <border key=\"" + key + "\" type=\"bevelRaised\" " + "thickness=\"" + Accessor.getThickness(border) + "\" />\n");
} else {
bw.write(" <border key=\"" + key + "\" type=\"bevelRaised\" " + "thickness=\"" + Accessor.getThickness(border) + "\" color=\"" + Accessor.getColorA(border) + "\" colorB=\"" + Accessor.getColorB(border) + "\" colorC=\"" + Accessor.getColorC(border) + "\" colorD=\"" + Accessor.getColorD(border) + "\" />\n");
}
continue;
// case BORDER_TYPE_IMAGE_SCALED:
case BORDER_TYPE_IMAGE:
{
Image[] images = Accessor.getImages(border);
int resourceCount = 0;
for (int counter = 0; counter < images.length; counter++) {
if (images[counter] != null && findId(images[counter], true) != null) {
resourceCount++;
}
}
if (resourceCount != 2 && resourceCount != 3 && resourceCount != 8 && resourceCount != 9) {
System.out.println("Odd resource count for image border: " + resourceCount);
resourceCount = 2;
}
switch(resourceCount) {
case 2:
bw.write(" <border key=\"" + key + "\" type=\"image\" " + "i1=\"" + findId(images[0], true) + "\" " + "i2=\"" + findId(images[4], true) + "\" />\n");
break;
case 3:
bw.write(" <border key=\"" + key + "\" type=\"image\" " + "i1=\"" + findId(images[0], true) + "\" " + "i2=\"" + findId(images[4], true) + "\" " + "i3=\"" + findId(images[8], true) + "\" />\n");
break;
case 8:
bw.write(" <border key=\"" + key + "\" type=\"image\" " + "i1=\"" + findId(images[0], true) + "\" " + "i2=\"" + findId(images[1], true) + "\" " + "i3=\"" + findId(images[2], true) + "\" " + "i4=\"" + findId(images[3], true) + "\" " + "i5=\"" + findId(images[4], true) + "\" " + "i6=\"" + findId(images[5], true) + "\" " + "i7=\"" + findId(images[6], true) + "\" " + "i8=\"" + findId(images[7], true) + "\" />\n");
break;
case 9:
bw.write(" <border key=\"" + key + "\" type=\"image\" " + "i1=\"" + findId(images[0], true) + "\" " + "i2=\"" + findId(images[1], true) + "\" " + "i3=\"" + findId(images[2], true) + "\" " + "i4=\"" + findId(images[3], true) + "\" " + "i5=\"" + findId(images[4], true) + "\" " + "i6=\"" + findId(images[5], true) + "\" " + "i7=\"" + findId(images[6], true) + "\" " + "i8=\"" + findId(images[7], true) + "\" " + "i9=\"" + findId(images[8], true) + "\" />\n");
break;
}
continue;
}
case BORDER_TYPE_IMAGE_HORIZONTAL:
{
Image[] images = Accessor.getImages(border);
bw.write(" <border key=\"" + key + "\" type=\"imageH\" " + "i1=\"" + findId(images[0], true) + "\" " + "i2=\"" + findId(images[1], true) + "\" " + "i3=\"" + findId(images[2], true) + "\" />\n");
continue;
}
case BORDER_TYPE_IMAGE_VERTICAL:
{
Image[] images = Accessor.getImages(border);
bw.write(" <border key=\"" + key + "\" type=\"imageV\" " + "i1=\"" + findId(images[0], true) + "\" " + "i2=\"" + findId(images[1], true) + "\" " + "i3=\"" + findId(images[2], true) + "\" />\n");
continue;
}
}
continue;
}
// if this is a font
if (key.endsWith("font")) {
com.codename1.ui.Font f = (com.codename1.ui.Font) theme.get(key);
// is this a new font?
boolean newFont = f instanceof EditorFont;
if (newFont) {
bw.write(" <font key=\"" + key + "\" type=\"named\" " + "name=\"" + findId(f) + "\" />\n");
} else {
if (f instanceof EditorTTFFont && (((EditorTTFFont) f).getFontFile() != null || ((EditorTTFFont) f).getNativeFontName() != null)) {
EditorTTFFont ed = (EditorTTFFont) f;
String fname;
String ffName;
if (((EditorTTFFont) f).getNativeFontName() != null) {
fname = ((EditorTTFFont) f).getNativeFontName();
ffName = fname;
} else {
fname = ed.getFontFile().getName();
ffName = ((java.awt.Font) ed.getNativeFont()).getPSName();
}
bw.write(" <font key=\"" + key + "\" type=\"ttf\" " + "face=\"" + f.getFace() + "\" " + "style=\"" + f.getStyle() + "\" " + "size=\"" + f.getSize() + "\" " + "name=\"" + fname + "\" " + "family=\"" + ffName + "\" " + "sizeSettings=\"" + ed.getSizeSetting() + "\" " + "actualSize=\"" + ed.getActualSize() + "\" />\n");
} else {
bw.write(" <font key=\"" + key + "\" type=\"system\" " + "face=\"" + f.getFace() + "\" " + "style=\"" + f.getStyle() + "\" " + "size=\"" + f.getSize() + "\" />\n");
}
}
continue;
}
// if this is a background image
if (key.endsWith("bgImage")) {
bw.write(" <val key=\"" + key + "\" value=\"" + findId(theme.get(key), true) + "\" />\n");
continue;
}
if (key.endsWith("scaledImage")) {
bw.write(" <val key=\"" + key + "\" value=\"" + theme.get(key) + "\" />\n");
continue;
}
if (key.endsWith("derive")) {
bw.write(" <val key=\"" + key + "\" value=\"" + theme.get(key) + "\" />\n");
continue;
}
// if this is a background gradient
if (key.endsWith("bgGradient")) {
Object[] gradient = (Object[]) theme.get(key);
bw.write(" <gradient key=\"" + key + "\" color1=\"" + gradient[0] + "\"" + " color2=\"" + gradient[1] + "\"" + " posX=\"" + gradient[2] + "\"" + " posY=\"" + gradient[3] + "\"" + " radius=\"" + gradient[4] + "\" />\n");
continue;
}
if (key.endsWith(Style.BACKGROUND_TYPE) || key.endsWith(Style.BACKGROUND_ALIGNMENT)) {
bw.write(" <val key=\"" + key + "\" value=\"" + theme.get(key) + "\" />\n");
continue;
}
// thow an exception no idea what this is
throw new IOException("Error while trying to read theme property: " + key);
}
bw.write(" </theme>\n");
continue;
case MAGIC_FONT:
File legacyFont = new File(resourcesDir, normalizeFileName(resourceNames[iter]));
DataOutputStream legacyFontOut = new DataOutputStream(new FileOutputStream(legacyFont));
saveFont(legacyFontOut, false, resourceNames[iter]);
legacyFontOut.close();
bw.write(" <legacyFont name=\"" + xResourceName + "\" />\n");
continue;
case MAGIC_DATA:
{
File dataFile = new File(resourcesDir, normalizeFileName(resourceNames[iter]));
DataOutputStream dataFileOut = new DataOutputStream(new FileOutputStream(dataFile));
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();
dataFileOut.write(data);
dataFileOut.close();
bw.write(" <data name=\"" + xResourceName + "\" />\n");
continue;
}
case MAGIC_UI:
{
File uiXML = new File(resourcesDir, resourceNames[iter] + ".ui");
UIBuilderOverride u = new UIBuilderOverride();
com.codename1.ui.Container cnt = u.createContainer(this, resourceNames[iter]);
FileOutputStream fos = new FileOutputStream(uiXML);
writeUIXml(cnt, fos);
fos.close();
File ui = new File(resourcesDir, resourceNames[iter]);
DataOutputStream uiOut = new DataOutputStream(new FileOutputStream(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();
uiOut.write(data);
uiOut.close();
bw.write(" <ui name=\"" + xResourceName + "\" />\n");
continue;
}
case MAGIC_L10N:
// we are getting the theme which allows us to acces the l10n data
bw.write(" <l10n name=\"" + xResourceName + "\">\n");
Hashtable<String, Object> l10n = getTheme(resourceNames[iter]);
for (String locale : l10n.keySet()) {
bw.write(" <lang name=\"" + locale + "\">\n");
Hashtable<String, String> current = (Hashtable<String, String>) l10n.get(locale);
for (String key : current.keySet()) {
String val = current.get(key);
bw.write(" <entry key=\"" + xmlize(key) + "\" value=\"" + xmlize(val) + "\" />\n");
}
bw.write(" </lang>\n");
}
bw.write(" </l10n>\n");
continue;
default:
throw new IOException("Corrupt theme file unrecognized magic number: " + Integer.toHexString(magic & 0xff));
}
}
bw.write("</resource>\n");
bw.close();
} finally {
overrideFile = overrideFileBackup;
overrideResource = overrideResourceBackup;
}
}
use of com.codename1.ui.util.UIBuilderOverride in project CodenameOne by codenameone.
the class ResourceEditorApp method migrateGuiBuilder.
private static void migrateGuiBuilder(File projectDir, EditableResources res, String destPackageName) throws IOException {
File propertiesFile = new File(projectDir, "codenameone_settings.properties");
Properties props = new Properties();
FileInputStream pIn = new FileInputStream(propertiesFile);
props.load(pIn);
pIn.close();
if (props.getProperty("guiResource") == null) {
System.out.println("Not a legacy GUI builder project!\nConversion failed!");
System.exit(1);
return;
}
UserInterfaceEditor.exportToNewGuiBuilderMode = true;
String mainForm = props.getProperty("mainForm");
File stateMachineBase = new File(projectDir, "src" + File.separatorChar + "generated" + File.separator + "StateMachineBase.java");
StringBuilder stateMachineBaseSource = new StringBuilder("/**\n * This class was generated by the migration wizard, ultimately both it and the Statemachine can be removed.\n");
stateMachineBaseSource.append(" * This class is no longer updated automatically\n");
stateMachineBaseSource.append("*/\n");
stateMachineBaseSource.append("package generated;\n");
stateMachineBaseSource.append("\nimport com.codename1.ui.*;\n");
stateMachineBaseSource.append("import com.codename1.ui.util.*;\n");
stateMachineBaseSource.append("import com.codename1.ui.plaf.*;\n");
stateMachineBaseSource.append("import java.util.Hashtable;\n");
stateMachineBaseSource.append("import com.codename1.ui.events.*;\n\n");
stateMachineBaseSource.append("public abstract class StateMachineBase extends UIBuilder {\n");
stateMachineBaseSource.append(" private static final java.util.HashMap<String, Class> formNameToClassHashMap = new java.util.HashMap<String, Class>();");
stateMachineBaseSource.append(" public static StateMachineBase instance;");
stateMachineBaseSource.append(" protected void initVars() {}\n\n");
stateMachineBaseSource.append(" protected void initVars(Resources res) {}\n\n");
stateMachineBaseSource.append(" public StateMachineBase(Resources res, String resPath, boolean loadTheme) {\n instance = this;\n");
stateMachineBaseSource.append(" startApp(res, resPath, loadTheme);\n");
stateMachineBaseSource.append(" }\n\n\n");
stateMachineBaseSource.append(" public Container startApp(Resources res, String resPath, boolean loadTheme) {\n");
stateMachineBaseSource.append(" initVars();\n");
stateMachineBaseSource.append(" if(loadTheme) {\n");
stateMachineBaseSource.append(" if(res == null) {\n");
stateMachineBaseSource.append(" try {\n");
stateMachineBaseSource.append(" if(resPath.endsWith(\".res\")) {\n");
stateMachineBaseSource.append(" res = Resources.open(resPath);\n");
stateMachineBaseSource.append(" System.out.println(\"Warning: you should construct the state machine without the .res extension to allow theme overlays\");\n");
stateMachineBaseSource.append(" } else {\n");
stateMachineBaseSource.append(" res = Resources.openLayered(resPath);\n");
stateMachineBaseSource.append(" }\n");
stateMachineBaseSource.append(" } catch(java.io.IOException err) { err.printStackTrace(); }\n");
stateMachineBaseSource.append(" }\n");
stateMachineBaseSource.append(" initTheme(res);\n");
stateMachineBaseSource.append(" }\n");
stateMachineBaseSource.append(" if(res != null) {\n");
stateMachineBaseSource.append(" setResourceFilePath(resPath);\n");
stateMachineBaseSource.append(" setResourceFile(res);\n");
stateMachineBaseSource.append(" Resources.setGlobalResources(res);");
stateMachineBaseSource.append(" initVars(res);\n");
stateMachineBaseSource.append(" return showForm(getFirstFormName(), null);\n");
stateMachineBaseSource.append(" } else {\n");
stateMachineBaseSource.append(" Form f = (Form)createContainer(resPath, getFirstFormName());\n");
stateMachineBaseSource.append(" Resources.setGlobalResources(fetchResourceFile());");
stateMachineBaseSource.append(" initVars(fetchResourceFile());\n");
stateMachineBaseSource.append(" beforeShow(f);\n");
stateMachineBaseSource.append(" f.show();\n");
stateMachineBaseSource.append(" postShow(f);\n");
stateMachineBaseSource.append(" return f;\n");
stateMachineBaseSource.append(" }\n");
stateMachineBaseSource.append(" }\n\n\n");
stateMachineBaseSource.append(" protected String getFirstFormName() {\n");
stateMachineBaseSource.append(" return \"");
stateMachineBaseSource.append(mainForm);
stateMachineBaseSource.append("\";\n");
stateMachineBaseSource.append(" }\n\n\n");
stateMachineBaseSource.append(" protected void initTheme(Resources res) {\n");
stateMachineBaseSource.append(" String[] themes = res.getThemeResourceNames();\n");
stateMachineBaseSource.append(" Resources.setGlobalResources(res);\n");
stateMachineBaseSource.append(" if(themes != null && themes.length > 0) {\n");
stateMachineBaseSource.append(" UIManager.getInstance().setThemeProps(res.getTheme(themes[0]));\n");
stateMachineBaseSource.append(" }\n");
stateMachineBaseSource.append(" }\n\n\n");
stateMachineBaseSource.append(" public StateMachineBase() {\n instance = this;\n");
stateMachineBaseSource.append(" }\n\n");
stateMachineBaseSource.append(" public StateMachineBase(String resPath) {\n");
stateMachineBaseSource.append(" this(null, resPath, true);\n instance = this;\n");
stateMachineBaseSource.append(" }\n\n");
stateMachineBaseSource.append(" public StateMachineBase(Resources res) {\n");
stateMachineBaseSource.append(" this(res, null, true);\n instance = this;\n");
stateMachineBaseSource.append(" }\n\n");
stateMachineBaseSource.append(" public StateMachineBase(String resPath, boolean loadTheme) {\n");
stateMachineBaseSource.append(" this(null, resPath, loadTheme);\n instance = this;\n");
stateMachineBaseSource.append(" }\n\n");
stateMachineBaseSource.append(" public StateMachineBase(Resources res, boolean loadTheme) {\n");
stateMachineBaseSource.append(" this(res, null, loadTheme);\n instance = this;\n");
stateMachineBaseSource.append(" }\n\n");
stateMachineBaseSource.append(" public Form showForm(String resourceName, Command sourceCommand) {\n");
stateMachineBaseSource.append(" try {\n");
stateMachineBaseSource.append(" Form f = (Form)formNameToClassHashMap.get(resourceName).newInstance();\n");
stateMachineBaseSource.append(" Form current = Display.getInstance().getCurrent();\n");
stateMachineBaseSource.append(" if(current != null && isBackCommandEnabled() && allowBackTo(resourceName)) {\n");
stateMachineBaseSource.append(" f.putClientProperty(\"previousForm\", current);\n");
stateMachineBaseSource.append(" setBackCommand(f, new Command(getBackCommandText(current.getTitle())) {\n");
stateMachineBaseSource.append(" public void actionPerformed(ActionEvent evt) {\n");
stateMachineBaseSource.append(" back(null);\n");
stateMachineBaseSource.append(" }\n");
stateMachineBaseSource.append(" });\n");
stateMachineBaseSource.append(" }\n");
stateMachineBaseSource.append(" if(sourceCommand != null && current != null && current.getBackCommand() == sourceCommand) {\n");
stateMachineBaseSource.append(" f.showBack();\n");
stateMachineBaseSource.append(" } else {\n");
stateMachineBaseSource.append(" f.show();\n");
stateMachineBaseSource.append(" }\n");
stateMachineBaseSource.append(" return f;\n");
stateMachineBaseSource.append(" } catch(Exception err) {\n");
stateMachineBaseSource.append(" err.printStackTrace();\n");
stateMachineBaseSource.append(" throw new RuntimeException(\"Form not found: \" + resourceName);\n");
stateMachineBaseSource.append(" }\n");
stateMachineBaseSource.append(" }\n\n");
stateMachineBaseSource.append(" protected void beforeShow(Form f) {\n");
stateMachineBaseSource.append(" }\n\n");
stateMachineBaseSource.append(" public final void beforeShow__(Form f) {\n beforeShow(f);\n");
stateMachineBaseSource.append(" if(Display.getInstance().getCurrent() != null) {\n");
stateMachineBaseSource.append(" exitForm(Display.getInstance().getCurrent());\n");
stateMachineBaseSource.append(" invokeFormExit__(Display.getInstance().getCurrent());\n");
stateMachineBaseSource.append(" }\n");
stateMachineBaseSource.append(" }\n\n");
stateMachineBaseSource.append(" protected void exitForm(Form f) {\n");
stateMachineBaseSource.append(" }\n\n");
stateMachineBaseSource.append(" protected void postShow(Form f) {\n");
stateMachineBaseSource.append(" }\n\n");
stateMachineBaseSource.append(" public final void postShow__(Form f) {\n postShow(f);\n");
stateMachineBaseSource.append(" }\n\n");
stateMachineBaseSource.append(" private Container getRootComponent__(Component rootComponent) {\n");
stateMachineBaseSource.append(" if(rootComponent.getParent() != null) {\n");
stateMachineBaseSource.append(" return getRoot__(rootComponent.getParent());\n");
stateMachineBaseSource.append(" }\n");
stateMachineBaseSource.append(" return (Container)rootComponent;\n");
stateMachineBaseSource.append(" }\n\n");
stateMachineBaseSource.append(" private Container getRoot__(Container rootComponent) {\n");
stateMachineBaseSource.append(" Container p = rootComponent.getParent();\n");
stateMachineBaseSource.append(" while(p != null) {\n");
stateMachineBaseSource.append(" rootComponent = p;\n");
stateMachineBaseSource.append(" p = rootComponent.getParent();\n");
stateMachineBaseSource.append(" }\n");
stateMachineBaseSource.append(" return rootComponent;\n");
stateMachineBaseSource.append(" }\n\n");
stateMachineBaseSource.append(" public Component findByName(String componentName, Container rootComponent) {\n");
stateMachineBaseSource.append(" Container root = getRoot__(rootComponent);\n");
stateMachineBaseSource.append(" return findByName__(componentName, root);\n");
stateMachineBaseSource.append(" }\n\n");
stateMachineBaseSource.append(" public Component findByName__(String componentName, Container root) {\n");
stateMachineBaseSource.append(" int count = root.getComponentCount();\n");
stateMachineBaseSource.append(" for(int iter = 0 ; iter < count ; iter++) {\n");
stateMachineBaseSource.append(" Component c = root.getComponentAt(iter);\n");
stateMachineBaseSource.append(" String n = c.getName();\n");
stateMachineBaseSource.append(" if(n != null && n.equals(componentName)) {\n");
stateMachineBaseSource.append(" return c;\n");
stateMachineBaseSource.append(" }\n");
stateMachineBaseSource.append(" if(c instanceof Container && ((Container)c).getLeadComponent() == null) {\n");
stateMachineBaseSource.append(" c = findByName__(componentName, (Container)c);\n");
stateMachineBaseSource.append(" if(c != null) {\n");
stateMachineBaseSource.append(" return c;\n");
stateMachineBaseSource.append(" }\n");
stateMachineBaseSource.append(" }\n");
stateMachineBaseSource.append(" }\n");
stateMachineBaseSource.append(" return null;\n");
stateMachineBaseSource.append(" }\n\n");
stateMachineBaseSource.append(" protected void handleComponentAction(Component c, ActionEvent event) {\n");
stateMachineBaseSource.append(" }\n\n");
stateMachineBaseSource.append(" public void handleComponentAction__(Component c, ActionEvent event) {\n");
stateMachineBaseSource.append(" }\n\n");
stateMachineBaseSource.append(" public void processCommand__(ActionEvent ev, Command cmd) {\n");
stateMachineBaseSource.append(" processCommand(ev, cmd);\n");
stateMachineBaseSource.append(" }\n\n");
stateMachineBaseSource.append(" public void back() {\n");
stateMachineBaseSource.append(" back(null);\n");
stateMachineBaseSource.append(" }\n\n");
stateMachineBaseSource.append(" public void back(Component sourceComponent) {\n");
stateMachineBaseSource.append(" Form current = (Form)Display.getInstance().getCurrent().getClientProperty(\"previousForm\");\n");
stateMachineBaseSource.append(" current.showBack();\n");
stateMachineBaseSource.append(" }\n\n");
StringBuilder formNameMapBuilder = new StringBuilder("static {");
StringBuilder invokeFormExitBuilder = new StringBuilder(" private void invokeFormExit__(Form f) {\n");
UserInterfaceEditor.componentNames = new HashMap<String, Class>();
UserInterfaceEditor.commandList = new ArrayList<ActionCommand>();
for (String uiName : res.getUIResourceNames()) {
System.out.println("Processing: " + uiName);
String fileName = convertToVarName(uiName);
formNameMapBuilder.append(" formNameToClassHashMap.put(\"");
formNameMapBuilder.append(uiName);
formNameMapBuilder.append("\", ");
formNameMapBuilder.append(destPackageName);
formNameMapBuilder.append(".");
formNameMapBuilder.append(fileName);
formNameMapBuilder.append(".class);\n");
String normalizedUiName = ResourceEditorView.normalizeFormName(uiName);
if (RESEVERVED_WORDS.contains(fileName)) {
fileName += "X";
} else {
try {
if (Class.forName("java.lang." + fileName) != null) {
fileName += "X";
}
} catch (Throwable t) {
// passed...
}
}
File guiFile = new File(projectDir, "res" + File.separatorChar + "guibuilder" + File.separatorChar + destPackageName.replace('.', File.separatorChar) + File.separatorChar + fileName + ".gui");
guiFile.getParentFile().mkdirs();
File sourcePackageDir = new File(projectDir, "src" + File.separatorChar + destPackageName.replace('.', File.separatorChar));
sourcePackageDir.mkdirs();
File sourceFile = new File(sourcePackageDir, fileName + ".java");
UIBuilderOverride u = new UIBuilderOverride();
com.codename1.ui.Container cnt = u.createContainer(res, uiName);
FileOutputStream fos = new FileOutputStream(guiFile);
Writer w = new OutputStreamWriter(fos, "UTF-8");
w.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n");
StringBuilder bld = new StringBuilder();
UserInterfaceEditor.actionEventNames = new ArrayList<String>();
UserInterfaceEditor.listNames = new ArrayList<String>();
UserInterfaceEditor.persistToXML(cnt, cnt, bld, res, "");
w.write(bld.toString());
w.flush();
w.close();
fos = new FileOutputStream(sourceFile);
w = new OutputStreamWriter(fos, "UTF-8");
w.write("package ");
w.write(destPackageName);
w.write(";\n");
w.write("\n");
w.write("/**\n");
w.write(" * GUI builder created Form\n");
w.write(" */\n");
w.write("public class ");
w.write(fileName);
String prePostCode;
w.write(" extends com.codename1.ui.");
if (cnt instanceof com.codename1.ui.Form) {
invokeFormExitBuilder.append(" if(f.getName().equals(\"");
invokeFormExitBuilder.append(uiName);
invokeFormExitBuilder.append("\")) {\n");
invokeFormExitBuilder.append(" exit");
invokeFormExitBuilder.append(normalizedUiName);
invokeFormExitBuilder.append("(f);\n }\n");
stateMachineBaseSource.append(" protected void before");
stateMachineBaseSource.append(normalizedUiName);
stateMachineBaseSource.append("(Form f) {\n");
stateMachineBaseSource.append(" }\n\n");
stateMachineBaseSource.append(" public final void before");
stateMachineBaseSource.append(normalizedUiName);
stateMachineBaseSource.append("__(Form f) {\n before");
stateMachineBaseSource.append(normalizedUiName);
stateMachineBaseSource.append("(f);\n }\n\n");
stateMachineBaseSource.append(" protected void post");
stateMachineBaseSource.append(normalizedUiName);
stateMachineBaseSource.append("(Form f) {\n");
stateMachineBaseSource.append(" }\n\n");
stateMachineBaseSource.append(" public final void post");
stateMachineBaseSource.append(normalizedUiName);
stateMachineBaseSource.append("__(Form f) {\n post");
stateMachineBaseSource.append(normalizedUiName);
stateMachineBaseSource.append("(f);\n }\n\n");
stateMachineBaseSource.append(" protected void exit");
stateMachineBaseSource.append(normalizedUiName);
stateMachineBaseSource.append("(Form f) {\n");
stateMachineBaseSource.append(" }\n\n");
}
if (cnt instanceof com.codename1.ui.Dialog) {
w.write("Dialog");
prePostCode = "\n public void initComponent() {\n generated.StateMachineBase.instance.beforeShow__(this);\n";
prePostCode += " generated.StateMachineBase.instance.before";
prePostCode += normalizedUiName;
prePostCode += "__(this);\n }\n";
prePostCode = "\n public void onShow() {\n generated.StateMachineBase.instance.postShow__(this);\n";
prePostCode += " generated.StateMachineBase.instance.post";
prePostCode += normalizedUiName;
prePostCode += "__(this);\n }\n";
prePostCode += " protected void actionCommand(com.codename1.ui.Command cmd) {\n";
prePostCode += " generated.StateMachineBase.instance.processCommand__(new com.codename1.ui.events.ActionEvent(cmd), cmd);\n";
prePostCode += " }\n\n";
} else {
if (cnt instanceof com.codename1.ui.Form) {
w.write("Form");
prePostCode = "\n public void show() {\n generated.StateMachineBase.instance.beforeShow__(this);\n";
prePostCode += " generated.StateMachineBase.instance.before";
prePostCode += normalizedUiName;
prePostCode += "__(this);\n super.show();\n generated.StateMachineBase.instance.post";
prePostCode += normalizedUiName;
prePostCode += "__(this);\n }\n";
prePostCode += " protected void actionCommand(com.codename1.ui.Command cmd) {\n";
prePostCode += " generated.StateMachineBase.instance.processCommand__(new com.codename1.ui.events.ActionEvent(cmd), cmd);\n";
prePostCode += " }\n\n";
} else {
w.write("Container");
prePostCode = "";
}
}
w.write(" {\n public ");
w.write(fileName);
w.write("() {\n");
w.write(" this(com.codename1.ui.util.Resources.getGlobalResources());\n");
w.write(" }\n \n public ");
w.write(fileName);
w.write("(com.codename1.ui.util.Resources resourceObjectInstance) {\n");
w.write(" initGuiBuilderComponents(resourceObjectInstance);\n");
w.write(" }\n\n");
w.write("//-- DON'T EDIT BELOW THIS LINE!!!\n\n private void initGuiBuilderComponents(com.codename1.ui.util.Resources resourceObjectInstance) {}\n\n");
w.write("//-- DON'T EDIT ABOVE THIS LINE!!!\n");
for (String actionListenerNames : UserInterfaceEditor.actionEventNames) {
w.write("\n public void on");
w.write(actionListenerNames);
w.write("ActionEvent(com.codename1.ui.events.ActionEvent ev) {\n ");
w.write("generated.StateMachineBase.instance.handleComponentAction__((com.codename1.ui.Component)ev.getSource(), ev);\n ");
w.write("generated.StateMachineBase.instance.on");
w.write(normalizedUiName);
w.write("_");
String normalizedActionListenerName = ResourceEditorView.normalizeFormName(actionListenerNames);
w.write(normalizedActionListenerName);
w.write("Action__((com.codename1.ui.Component)ev.getSource(), ev);\n }\n\n");
stateMachineBaseSource.append(" protected void on");
stateMachineBaseSource.append(normalizedUiName);
stateMachineBaseSource.append("_");
stateMachineBaseSource.append(normalizedActionListenerName);
stateMachineBaseSource.append("Action(Component cmp, ActionEvent ev) {\n }\n\n");
stateMachineBaseSource.append(" public void on");
stateMachineBaseSource.append(normalizedUiName);
stateMachineBaseSource.append("_");
stateMachineBaseSource.append(normalizedActionListenerName);
stateMachineBaseSource.append("Action__(Component cmp, ActionEvent ev) {\n on");
stateMachineBaseSource.append(normalizedUiName);
stateMachineBaseSource.append("_");
stateMachineBaseSource.append(normalizedActionListenerName);
stateMachineBaseSource.append("Action(cmp, ev);\n }\n\n");
}
w.write(prePostCode);
w.write("}\n");
w.flush();
w.close();
}
formNameMapBuilder.append("}\n");
invokeFormExitBuilder.append("}\n");
stateMachineBaseSource.append(formNameMapBuilder);
stateMachineBaseSource.append(invokeFormExitBuilder);
ArrayList<String> uniqueNames = new ArrayList<String>();
for (String cmpName : UserInterfaceEditor.componentNames.keySet()) {
String nomName = ResourceEditorView.normalizeFormName(cmpName);
if (uniqueNames.contains(nomName)) {
continue;
}
uniqueNames.add(nomName);
stateMachineBaseSource.append(" public ");
stateMachineBaseSource.append(UserInterfaceEditor.componentNames.get(cmpName).getName());
stateMachineBaseSource.append(" find");
stateMachineBaseSource.append(nomName);
stateMachineBaseSource.append("(Component root) {\n return (");
stateMachineBaseSource.append(UserInterfaceEditor.componentNames.get(cmpName).getName());
stateMachineBaseSource.append(")findByName(\"");
stateMachineBaseSource.append(cmpName);
stateMachineBaseSource.append("\", getRootComponent__(root));\n }\n\n");
stateMachineBaseSource.append(" public ");
stateMachineBaseSource.append(UserInterfaceEditor.componentNames.get(cmpName).getName());
stateMachineBaseSource.append(" find");
stateMachineBaseSource.append(nomName);
stateMachineBaseSource.append("() {\n return (");
stateMachineBaseSource.append(UserInterfaceEditor.componentNames.get(cmpName).getName());
stateMachineBaseSource.append(")findByName(\"");
stateMachineBaseSource.append(cmpName);
stateMachineBaseSource.append("\", Display.getInstance().getCurrent());\n }\n\n");
}
ArrayList<Integer> commandIdsAdded = new ArrayList<Integer>();
ArrayList<String> commandNamesAdded = new ArrayList<String>();
for (ActionCommand cmd : UserInterfaceEditor.commandList) {
String formName = (String) cmd.getClientProperty("FORMNAME");
if (formName == null) {
continue;
}
String normalizedCommandName = ResourceEditorView.normalizeFormName(formName) + ResourceEditorView.normalizeFormName(cmd.getCommandName());
if (commandNamesAdded.contains(normalizedCommandName)) {
continue;
}
if (commandIdsAdded.contains(cmd.getId())) {
continue;
}
commandIdsAdded.add(cmd.getId());
commandNamesAdded.add(normalizedCommandName);
stateMachineBaseSource.append(" public static final int COMMAND_");
stateMachineBaseSource.append(normalizedCommandName);
stateMachineBaseSource.append(" = ");
stateMachineBaseSource.append(cmd.getId());
stateMachineBaseSource.append(";\n\n protected boolean on");
stateMachineBaseSource.append(normalizedCommandName);
stateMachineBaseSource.append("() {\n return false;\n }\n\n");
}
stateMachineBaseSource.append(" protected void processCommand(ActionEvent ev, Command cmd) {\n");
stateMachineBaseSource.append(" switch(cmd.getId()) {\n");
commandIdsAdded.clear();
commandNamesAdded.clear();
for (ActionCommand cmd : UserInterfaceEditor.commandList) {
String formName = (String) cmd.getClientProperty("FORMNAME");
if (formName == null) {
continue;
}
String normalizedCommandName = ResourceEditorView.normalizeFormName(formName) + ResourceEditorView.normalizeFormName(cmd.getCommandName());
if (commandNamesAdded.contains(normalizedCommandName)) {
continue;
}
if (commandIdsAdded.contains(cmd.getId())) {
continue;
}
commandIdsAdded.add(cmd.getId());
commandNamesAdded.add(normalizedCommandName);
stateMachineBaseSource.append("\n case COMMAND_");
stateMachineBaseSource.append(normalizedCommandName);
stateMachineBaseSource.append(":\n");
if (cmd.getAction() != null && cmd.getAction().length() > 0) {
if (!cmd.getAction().startsWith("$")) {
stateMachineBaseSource.append(" showForm(\"");
stateMachineBaseSource.append(cmd.getAction());
stateMachineBaseSource.append("\", null);\n");
}
}
stateMachineBaseSource.append(" if(on");
stateMachineBaseSource.append(normalizedCommandName);
stateMachineBaseSource.append("()) {\n");
stateMachineBaseSource.append(" ev.consume();\n");
stateMachineBaseSource.append(" return;\n");
stateMachineBaseSource.append(" }\n");
stateMachineBaseSource.append(" break;\n\n");
}
stateMachineBaseSource.append(" }\n");
stateMachineBaseSource.append(" if(ev.getComponent() != null) {\n");
stateMachineBaseSource.append(" handleComponentAction(ev.getComponent(), ev);\n");
stateMachineBaseSource.append(" }\n");
stateMachineBaseSource.append(" }\n\n");
stateMachineBaseSource.append("\n}\n");
FileOutputStream sbout = new FileOutputStream(stateMachineBase);
sbout.write(stateMachineBaseSource.toString().getBytes("UTF-8"));
sbout.close();
props.remove("mainForm");
props.remove("package");
props.remove("guiResource");
props.remove("baseClass");
props.remove("userClass");
FileOutputStream pOut = new FileOutputStream(propertiesFile);
props.store(pOut, "Updated by GUI builder migration wizard");
pOut.close();
System.out.println("Conversion completed successfully!");
System.exit(0);
}
use of com.codename1.ui.util.UIBuilderOverride in project CodenameOne by codenameone.
the class UserInterfaceEditor method getListOfAllCommands.
private static List<com.codename1.ui.Command> getListOfAllCommands(EditableResources res) {
final List<com.codename1.ui.Command> response = new ArrayList<com.codename1.ui.Command>();
UIBuilderOverride tempBuilder = new UIBuilderOverride() {
public com.codename1.ui.Command createCommandImpl(String commandName, com.codename1.ui.Image icon, int commandId, String action, boolean isBack, String arg) {
com.codename1.ui.Command c = super.createCommandImpl(commandName, icon, commandId, action, isBack, arg);
if (!response.contains(c)) {
response.add(c);
}
return c;
}
};
for (String uiResourceName : res.getUIResourceNames()) {
tempBuilder.createContainer(res, uiResourceName);
}
Collections.sort(response, new Comparator<com.codename1.ui.Command>() {
public int compare(com.codename1.ui.Command o1, com.codename1.ui.Command o2) {
if (o1 == null) {
if (o2 == null) {
return 0;
}
return -1;
}
if (o2 == null) {
return 1;
}
return String.CASE_INSENSITIVE_ORDER.compare(o1.getCommandName(), o2.getCommandName());
}
});
return response;
}
use of com.codename1.ui.util.UIBuilderOverride 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")) {
theme.put(key, Integer.valueOf(v.getValue()));
continue;
}
if (key.endsWith(Style.BACKGROUND_TYPE) || key.endsWith(Style.BACKGROUND_ALIGNMENT)) {
theme.put(key, Byte.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.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 ("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());
rb = rb.bottomOnlyMode(b.isBottomOnlyMode());
rb = rb.topOnlyMode(b.isTopOnlyMode());
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