use of com.codename1.rad.ui in project CodenameOne by codenameone.
the class Component method drawPainters.
private void drawPainters(com.codename1.ui.Graphics g, Component par, Component c, int x, int y, int w, int h) {
if (flatten && getWidth() > 0 && getHeight() > 0) {
Image i = (Image) getClientProperty("$FLAT");
int absX = getAbsoluteX() + getScrollX();
int absY = getAbsoluteY() + getScrollY();
if (i == null || i.getWidth() != getWidth() || i.getHeight() != getHeight()) {
i = Image.createImage(getWidth(), getHeight());
Graphics tg = i.getGraphics();
// tg.translate(g.getTranslateX(), g.getTranslateY());
drawPaintersImpl(tg, par, c, x, y, w, h);
paintBackgroundImpl(tg);
putClientProperty("$FLAT", i);
}
int tx = g.getTranslateX();
int ty = g.getTranslateY();
g.translate(-tx + absX, -ty + absY);
g.drawImage(i, 0, 0);
g.translate(tx - absX, ty - absY);
return;
}
drawPaintersImpl(g, par, c, x, y, w, h);
}
use of com.codename1.rad.ui in project CodenameOne by codenameone.
the class TestComponent method testSimpleDateFormat.
public void testSimpleDateFormat() {
try {
SimpleDateFormat format = new SimpleDateFormat("EEE, dd-MMM-yyyy HH:mm:ss z");
format.parse("mon, 20-nov-2017 19:49:58 gmt");
format = new SimpleDateFormat("EEE dd-MMM-yyyy HH:mm:ss z");
format.parse("mon 20-nov-2017 19:49:58 gmt");
format = new SimpleDateFormat("EEE dd-MMM-yyyy HH:mm:ss z");
java.util.Date dt = format.parse("tue 21-nov-2017 17:04:27 gmt");
TestUtils.assertBool(dt.getTime() / 1000L == 1511283867L, "Incorrect date for simple date format parse");
format = new SimpleDateFormat("EEE, dd-MMM-yyyy HH:mm:ss z");
com.codename1.l10n.ParseException pex = null;
try {
format.parse("tue 21-nov-2017 17:04:27 gmt");
} catch (com.codename1.l10n.ParseException pex2) {
pex = pex2;
}
TestUtils.assertTrue(pex != null, "Parsing date with wrong format should give parse exception");
format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z");
dt = format.parse("sun, 22 nov 2037 13:20:46 -0000");
// 2142508846
// Log.p("Difference = "+(dt.getTime() - 2142508846000L));
TestUtils.assertEqual(dt.getTime() / 1000L, 2142508846L, "Failed to parse RFC822 datetime. " + dt);
String dateStr = "sun 22 nov 2037 13:20:46 -0000";
format = new SimpleDateFormat("EEE dd MMM yyyy HH:mm:ss Z");
dt = format.parse("sun 22 nov 2037 13:20:46 -0000");
TestUtils.assertEqual(dt.getTime() / 1000L, 2142508846L, "Failed to parse RFC822 datetime no comma. " + dt);
} catch (Throwable t) {
Log.e(t);
throw new RuntimeException("Failed to parse date mon 20-nov-2017 19:49:58 gmt: " + t.getMessage());
}
}
use of com.codename1.rad.ui in project CodenameOne by codenameone.
the class ResourceEditorView method launchOptiPngActionPerformed.
private void launchOptiPngActionPerformed(java.awt.event.ActionEvent evt) {
// GEN-FIRST:event_launchOptiPngActionPerformed
if (loadedResources != null && configureOptiPNG()) {
final ProgressMonitor pm = new ProgressMonitor(mainPanel, "Processing Images", "", 0, loadedResources.getImageResourceNames().length);
new Thread() {
public void run() {
String node = Preferences.userNodeForPackage(ResourceEditorView.class).get("optiPng", null);
int prog = 0;
for (String imageName : loadedResources.getImageResourceNames()) {
if (pm.isCanceled()) {
pm.close();
return;
}
pm.setProgress(prog);
prog++;
pm.setNote(imageName);
Object image = loadedResources.getImage(imageName);
if (image instanceof com.codename1.ui.EncodedImage) {
if (loadedResources.getResourceObject(imageName) != image) {
// multi-image...
EditableResources.MultiImage multi = (EditableResources.MultiImage) loadedResources.getResourceObject(imageName);
EditableResources.MultiImage n = new EditableResources.MultiImage();
EncodedImage[] arr = new EncodedImage[multi.getInternalImages().length];
for (int iter = 0; iter < multi.getInternalImages().length; iter++) {
EncodedImage current = optimize(multi.getInternalImages()[iter], node);
if (current != null) {
arr[iter] = current;
} else {
arr[iter] = multi.getInternalImages()[iter];
}
}
n.setInternalImages(arr);
n.setDpi(multi.getDpi());
loadedResources.setMultiImage(imageName, n);
} else {
EncodedImage current = optimize((EncodedImage) image, node);
if (current != null) {
loadedResources.setImage(imageName, current);
}
}
}
}
pm.close();
}
}.start();
}
}
use of com.codename1.rad.ui in project CodenameOne by codenameone.
the class ResourceEditorView method initImagesComboBox.
/**
* Creates a sorted image combo box that includes image previews. The combo box
* can be searched by typing a letter even when images are used for the values...
*/
public static void initImagesComboBox(JComboBox cb, final EditableResources res, boolean asString, final boolean includeNull, boolean blockTimelines) {
String[] imgs = res.getImageResourceNames();
if (blockTimelines) {
List<String> nonT = new ArrayList<String>();
for (String c : imgs) {
if (!(res.getImage(c) instanceof Timeline)) {
nonT.add(c);
}
}
imgs = new String[nonT.size()];
nonT.toArray(imgs);
}
final String[] images = imgs;
Arrays.sort(images, String.CASE_INSENSITIVE_ORDER);
if (asString) {
if (includeNull) {
String[] n = new String[images.length + 1];
System.arraycopy(images, 0, n, 1, images.length);
cb.setModel(new DefaultComboBoxModel(n));
} else {
cb.setModel(new DefaultComboBoxModel(images));
}
cb.setRenderer(new DefaultListCellRenderer() {
@Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
boolean n = false;
if (value == null) {
value = "[null]";
n = true;
}
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
if (!n) {
setIcon(new CodenameOneImageIcon(res.getImage((String) value), 24, 24));
} else {
setIcon(null);
}
return this;
}
});
} else {
int offset = 0;
com.codename1.ui.Image[] arr;
if (includeNull) {
arr = new com.codename1.ui.Image[images.length + 1];
offset++;
} else {
arr = new com.codename1.ui.Image[images.length];
}
for (String c : images) {
arr[offset] = res.getImage(c);
offset++;
}
cb.setModel(new DefaultComboBoxModel(arr));
cb.setKeySelectionManager(new JComboBox.KeySelectionManager() {
private String current;
private long lastPress;
public int selectionForKey(char aKey, ComboBoxModel aModel) {
long t = System.currentTimeMillis();
aKey = Character.toLowerCase(aKey);
if (t - lastPress < 800) {
current += aKey;
} else {
current = "" + aKey;
}
lastPress = t;
for (int iter = 0; iter < images.length; iter++) {
if (images[iter].toLowerCase().startsWith(current)) {
if (includeNull) {
return iter + 1;
}
return iter;
}
}
return -1;
}
});
cb.setRenderer(new DefaultListCellRenderer() {
@Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
com.codename1.ui.Image i = (com.codename1.ui.Image) value;
if (value == null) {
value = "[null]";
} else {
value = res.findId(value);
}
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
if (i != null) {
setIcon(new CodenameOneImageIcon(i, 24, 24));
} else {
setIcon(null);
}
return this;
}
});
}
}
use of com.codename1.rad.ui in project CodenameOne by codenameone.
the class ResourceEditorView method generateStateMachineCodeImpl.
private static String generateStateMachineCodeImpl(String uiResourceName, File destFile, boolean promptUserForPackageName, EditableResources loadResources, java.awt.Component errorParent) {
String packageString = "";
File currentFile = destFile;
while (currentFile.getParent() != null) {
String shortName = currentFile.getParentFile().getName();
if (shortName.equalsIgnoreCase("src")) {
break;
}
if (shortName.indexOf(':') > -1 || shortName.length() == 0) {
break;
}
if (shortName.equalsIgnoreCase("org") || shortName.equalsIgnoreCase("com") || shortName.equalsIgnoreCase("net") || shortName.equalsIgnoreCase("gov")) {
if (packageString.length() > 0) {
packageString = shortName + "." + packageString;
} else {
packageString = shortName;
}
break;
}
if (packageString.length() > 0) {
packageString = shortName + "." + packageString;
} else {
packageString = shortName;
}
currentFile = currentFile.getParentFile();
}
final Map<String, String> nameToClassLookup = new HashMap<String, String>();
final Map<String, Integer> commandMap = new HashMap<String, Integer>();
final List<Integer> unhandledCommands = new ArrayList<Integer>();
final List<String[]> actionComponents = new ArrayList<String[]>();
final Map<String, String> allComponents = new HashMap<String, String>();
initCommandMapAndNameToClassLookup(nameToClassLookup, commandMap, unhandledCommands, actionComponents, allComponents);
// list all the .ovr files and add them to the nameToClassLookup
if (loadedFile != null && loadedFile.getParentFile() != null) {
File overrideDir = new File(loadedFile.getParentFile().getParentFile(), "override");
if (overrideDir.exists()) {
File[] ovrFiles = overrideDir.listFiles(new FilenameFilter() {
@Override
public boolean accept(File file, String string) {
return string.endsWith(".ovr");
}
});
for (File ovr : ovrFiles) {
try {
EditableResources er = EditableResources.open(new FileInputStream(ovr));
for (String currentResourceName : er.getUIResourceNames()) {
UIBuilder b = new UIBuilder() {
protected com.codename1.ui.Component createComponentInstance(String componentType, Class cls) {
if (cls.getName().startsWith("com.codename1.ui.")) {
// subpackage of CodenameOne should be registered
if (cls.getName().lastIndexOf(".") > 15) {
nameToClassLookup.put(componentType, cls.getName());
}
} else {
nameToClassLookup.put(componentType, cls.getName());
}
return null;
}
};
b.createContainer(er, currentResourceName);
}
} catch (IOException ioErr) {
ioErr.printStackTrace();
}
}
}
}
if (promptUserForPackageName) {
JTextField packageName = new JTextField(packageString);
JOptionPane.showMessageDialog(errorParent, packageName, "Please Pick The Package Name", JOptionPane.PLAIN_MESSAGE);
packageString = packageName.getText();
}
List<String> createdMethodNames = new ArrayList<String>();
try {
Writer w = new FileWriter(destFile);
w.write("/**\n");
w.write(" * This class contains generated code from the Codename One Designer, DO NOT MODIFY!\n");
w.write(" * This class is designed for subclassing that way the code generator can overwrite it\n");
w.write(" * anytime without erasing your changes which should exist in a subclass!\n");
w.write(" * For details about this file and how it works please read this blog post:\n");
w.write(" * http://codenameone.blogspot.com/2010/10/ui-builder-class-how-to-actually-use.html\n");
w.write("*/\n");
if (packageString.length() > 0) {
w.write("package " + packageString + ";\n\n");
}
String className = destFile.getName().substring(0, destFile.getName().indexOf('.'));
boolean hasIo = false;
for (String currentName : nameToClassLookup.keySet()) {
if (nameToClassLookup.get(currentName).indexOf("com.codename1.ui.io") > -1) {
hasIo = true;
break;
}
}
w.write("import com.codename1.ui.*;\n");
w.write("import com.codename1.ui.util.*;\n");
w.write("import com.codename1.ui.plaf.*;\n");
w.write("import java.util.Hashtable;\n");
if (hasIo) {
w.write("import com.codename1.ui.io.*;\n");
w.write("import com.codename1.components*;\n");
}
w.write("import com.codename1.ui.events.*;\n\n");
w.write("public abstract class " + className + " extends UIBuilder {\n");
w.write(" private Container aboutToShowThisContainer;\n");
w.write(" /**\n");
w.write(" * this method should be used to initialize variables instead of\n");
w.write(" * the constructor/class scope to avoid race conditions\n");
w.write(" */\n");
w.write(" /**\n * @deprecated use the version that accepts a resource as an argument instead\n \n**/\n");
w.write(" protected void initVars() {}\n\n");
w.write(" protected void initVars(Resources res) {}\n\n");
w.write(" public " + className + "(Resources res, String resPath, boolean loadTheme) {\n");
w.write(" startApp(res, resPath, loadTheme);\n");
w.write(" }\n\n");
w.write(" public Container startApp(Resources res, String resPath, boolean loadTheme) {\n");
w.write(" initVars();\n");
if (hasIo) {
w.write(" NetworkManager.getInstance().start();\n");
}
for (String currentName : nameToClassLookup.keySet()) {
w.write(" UIBuilder.registerCustomComponent(\"" + currentName + "\", " + nameToClassLookup.get(currentName) + ".class);\n");
}
w.write(" if(loadTheme) {\n");
w.write(" if(res == null) {\n");
w.write(" try {\n");
w.write(" if(resPath.endsWith(\".res\")) {\n");
w.write(" res = Resources.open(resPath);\n");
w.write(" System.out.println(\"Warning: you should construct the state machine without the .res extension to allow theme overlays\");\n");
w.write(" } else {\n");
w.write(" res = Resources.openLayered(resPath);\n");
w.write(" }\n");
w.write(" } catch(java.io.IOException err) { err.printStackTrace(); }\n");
w.write(" }\n");
w.write(" initTheme(res);\n");
w.write(" }\n");
w.write(" if(res != null) {\n");
w.write(" setResourceFilePath(resPath);\n");
w.write(" setResourceFile(res);\n");
w.write(" initVars(res);\n");
w.write(" return showForm(getFirstFormName(), null);\n");
w.write(" } else {\n");
w.write(" Form f = (Form)createContainer(resPath, getFirstFormName());\n");
w.write(" initVars(fetchResourceFile());\n");
w.write(" beforeShow(f);\n");
w.write(" f.show();\n");
w.write(" postShow(f);\n");
w.write(" return f;\n");
w.write(" }\n");
w.write(" }\n\n");
w.write(" protected String getFirstFormName() {\n");
w.write(" return \"" + uiResourceName + "\";\n");
w.write(" }\n\n");
w.write(" public Container createWidget(Resources res, String resPath, boolean loadTheme) {\n");
w.write(" initVars();\n");
if (hasIo) {
w.write(" NetworkManager.getInstance().start();\n");
}
for (String currentName : nameToClassLookup.keySet()) {
w.write(" UIBuilder.registerCustomComponent(\"" + currentName + "\", " + nameToClassLookup.get(currentName) + ".class);\n");
}
w.write(" if(loadTheme) {\n");
w.write(" if(res == null) {\n");
w.write(" try {\n");
w.write(" res = Resources.openLayered(resPath);\n");
w.write(" } catch(java.io.IOException err) { err.printStackTrace(); }\n");
w.write(" }\n");
w.write(" initTheme(res);\n");
w.write(" }\n");
w.write(" return createContainer(resPath, \"" + uiResourceName + "\");\n");
w.write(" }\n\n");
w.write(" protected void initTheme(Resources res) {\n");
w.write(" String[] themes = res.getThemeResourceNames();\n");
w.write(" if(themes != null && themes.length > 0) {\n");
w.write(" UIManager.getInstance().setThemeProps(res.getTheme(themes[0]));\n");
w.write(" }\n");
w.write(" }\n\n");
w.write(" public " + className + "() {\n");
w.write(" }\n\n");
w.write(" public " + className + "(String resPath) {\n");
w.write(" this(null, resPath, true);\n");
w.write(" }\n\n");
w.write(" public " + className + "(Resources res) {\n");
w.write(" this(res, null, true);\n");
w.write(" }\n\n");
w.write(" public " + className + "(String resPath, boolean loadTheme) {\n");
w.write(" this(null, resPath, loadTheme);\n");
w.write(" }\n\n");
w.write(" public " + className + "(Resources res, boolean loadTheme) {\n");
w.write(" this(res, null, loadTheme);\n");
w.write(" }\n\n");
for (String componentName : allComponents.keySet()) {
String componentType = allComponents.get(componentName);
String methodName = " find" + normalizeFormName(componentName);
// exists without a space might trigger this situation and thus code that won't compile
if (!createdMethodNames.contains(methodName)) {
if (componentType.equals("com.codename1.ui.Form") || componentType.equals("com.codename1.ui.Dialog")) {
continue;
}
createdMethodNames.add(methodName);
w.write(" public " + componentType + methodName + "(Component root) {\n");
w.write(" return (" + componentType + ")" + "findByName(\"" + componentName + "\", root);\n");
w.write(" }\n\n");
w.write(" public " + componentType + methodName + "() {\n");
w.write(" " + componentType + " cmp = (" + componentType + ")" + "findByName(\"" + componentName + "\", Display.getInstance().getCurrent());\n");
w.write(" if(cmp == null && aboutToShowThisContainer != null) {\n");
w.write(" cmp = (" + componentType + ")" + "findByName(\"" + componentName + "\", aboutToShowThisContainer);\n");
w.write(" }\n");
w.write(" return cmp;\n");
w.write(" }\n\n");
}
}
if (commandMap.size() > 0) {
for (String key : commandMap.keySet()) {
w.write(" public static final int COMMAND_" + key + " = " + commandMap.get(key) + ";\n");
}
w.write("\n");
StringBuilder methodSwitch = new StringBuilder(" protected void processCommand(ActionEvent ev, Command cmd) {\n switch(cmd.getId()) {\n");
for (String key : commandMap.keySet()) {
String camelCase = "on" + key;
boolean isAbstract = unhandledCommands.contains(commandMap.get(key));
if (isAbstract) {
w.write(" protected abstract void ");
w.write(camelCase);
w.write("();\n\n");
} else {
w.write(" protected boolean ");
w.write(camelCase);
w.write("() {\n return false;\n }\n\n");
}
methodSwitch.append(" case COMMAND_");
methodSwitch.append(key);
methodSwitch.append(":\n");
methodSwitch.append(" ");
if (isAbstract) {
methodSwitch.append(camelCase);
methodSwitch.append("();\n break;\n\n");
} else {
methodSwitch.append("if(");
methodSwitch.append(camelCase);
methodSwitch.append("()) {\n ev.consume();\n return;\n }\n break;\n\n");
}
}
methodSwitch.append(" }\n if(ev.getComponent() != null) {\n handleComponentAction(ev.getComponent(), ev);\n }\n }\n\n");
w.write(methodSwitch.toString());
}
writeFormCallbackCode(w, " protected void exitForm(Form f) {\n", "f.getName()", "exit", "f", "Form f");
writeFormCallbackCode(w, " protected void beforeShow(Form f) {\n aboutToShowThisContainer = f;\n", "f.getName()", "before", "f", "Form f");
writeFormCallbackCode(w, " protected void beforeShowContainer(Container c) {\n aboutToShowThisContainer = c;\n", "c.getName()", "beforeContainer", "c", "Container c");
writeFormCallbackCode(w, " protected void postShow(Form f) {\n", "f.getName()", "post", "f", "Form f");
writeFormCallbackCode(w, " protected void postShowContainer(Container c) {\n", "c.getName()", "postContainer", "c", "Container c");
writeFormCallbackCode(w, " protected void onCreateRoot(String rootName) {\n", "rootName", "onCreate", "", "");
writeFormCallbackCode(w, " protected Hashtable getFormState(Form f) {\n Hashtable h = super.getFormState(f);\n", "f.getName()", "getState", "f, h", "Form f, Hashtable h", "return h;");
writeFormCallbackCode(w, " protected void setFormState(Form f, Hashtable state) {\n super.setFormState(f, state);\n", "f.getName()", "setState", "f, state", "Form f, Hashtable state");
List<String> listComponents = new ArrayList<String>();
for (String currentName : allComponents.keySet()) {
String value = allComponents.get(currentName);
if (value.equals("com.codename1.ui.List") || value.equals("com.codename1.ui.ComboBox") || value.equals("com.codename1.ui.list.MultiList") || value.equals("com.codename1.ui.Calendar")) {
listComponents.add(currentName);
}
}
List<String> containerListComponents = new ArrayList<String>();
for (String currentName : allComponents.keySet()) {
String value = allComponents.get(currentName);
if (value.equals("com.codename1.ui.list.ContainerList")) {
containerListComponents.add(currentName);
}
}
if (listComponents.size() > 0) {
w.write(" protected boolean setListModel(List cmp) {\n");
w.write(" String listName = cmp.getName();\n");
for (String listName : listComponents) {
w.write(" if(\"");
w.write(listName);
w.write("\".equals(listName)) {\n");
w.write(" return initListModel");
w.write(normalizeFormName(listName));
w.write("(cmp);\n }\n");
}
w.write(" return super.setListModel(cmp);\n }\n\n");
for (String listName : listComponents) {
w.write(" protected boolean initListModel");
w.write(normalizeFormName(listName));
w.write("(List cmp) {\n");
w.write(" return false;\n }\n\n");
}
}
if (containerListComponents.size() > 0) {
w.write(" protected boolean setListModel(com.codename1.ui.list.ContainerList cmp) {\n");
w.write(" String listName = cmp.getName();\n");
for (String listName : containerListComponents) {
w.write(" if(\"");
w.write(listName);
w.write("\".equals(listName)) {\n");
w.write(" return initListModel");
w.write(normalizeFormName(listName));
w.write("(cmp);\n }\n");
}
w.write(" return super.setListModel(cmp);\n }\n\n");
for (String listName : containerListComponents) {
w.write(" protected boolean initListModel");
w.write(normalizeFormName(listName));
w.write("(com.codename1.ui.list.ContainerList cmp) {\n");
w.write(" return false;\n }\n\n");
}
}
if (actionComponents.size() > 0) {
Object lastFormName = null;
StringBuilder methods = new StringBuilder();
w.write(" protected void handleComponentAction(Component c, ActionEvent event) {\n");
w.write(" Container rootContainerAncestor = getRootAncestor(c);\n");
w.write(" if(rootContainerAncestor == null) return;\n");
w.write(" String rootContainerName = rootContainerAncestor.getName();\n");
w.write(" Container leadParentContainer = c.getParent().getLeadParent();\n");
w.write(" if(leadParentContainer != null && leadParentContainer.getClass() != Container.class) {\n");
w.write(" c = c.getParent().getLeadParent();\n");
w.write(" }\n");
w.write(" if(rootContainerName == null) return;\n");
for (String[] currentCmp : actionComponents) {
if (lastFormName != currentCmp[1]) {
if (lastFormName != null) {
w.write(" }\n");
}
w.write(" if(rootContainerName.equals(\"");
w.write(currentCmp[1]);
w.write("\")) {\n");
lastFormName = currentCmp[1];
}
w.write(" if(\"");
w.write(currentCmp[0]);
w.write("\".equals(c.getName())) {\n");
String methodName = "on" + normalizeFormName(currentCmp[1]) + "_" + normalizeFormName(currentCmp[0]) + "Action";
w.write(" ");
w.write(methodName);
w.write("(c, event);\n");
w.write(" return;\n");
w.write(" }\n");
methods.append(" protected void ");
methods.append(methodName);
methods.append("(Component c, ActionEvent event) {\n }\n\n");
}
w.write(" }\n }\n\n");
w.write(methods.toString());
}
w.write("}\n");
w.close();
} catch (IOException ioErr) {
ioErr.printStackTrace();
JOptionPane.showMessageDialog(errorParent, "IO Error: " + ioErr, "IO Error", JOptionPane.ERROR_MESSAGE);
}
return packageString;
}
Aggregations