use of com.codename1.io.File in project CodenameOne by codenameone.
the class UIManager method initFirstTheme.
/**
* This is a shorthand notation for boilerplate code for initializing the first theme in the given resource file
* and catching/doing nothing with the IOException since this would be invoked too early in the program
* where we would be out of options if something like that happens. Effectively this is the same as writing:
* <pre>
* try {
* theme = Resources.openLayered(resourceFile);
* UIManager.getInstance().setThemeProps(theme.getTheme(theme.getThemeResourceNames()[0]));
* } catch(IOException e){
* e.printStackTrace();
* }
* </pre>
* @param resourceFile the name of the resource file starting with / and without the res extension
* @return the resource file or null in case of a failure
*/
public static Resources initFirstTheme(String resourceFile) {
try {
Resources theme = Resources.openLayered(resourceFile);
UIManager.getInstance().setThemeProps(theme.getTheme(theme.getThemeResourceNames()[0]));
Resources.setGlobalResources(theme);
return theme;
} catch (IOException e) {
Log.e(e);
}
return null;
}
use of com.codename1.io.File in project CodenameOne by codenameone.
the class ListFilesTest method start.
public void start() {
if (current != null) {
current.show();
return;
}
Form hi = new Form("Hi World", new BorderLayout());
Button add = new Button("Add Directory");
add.addActionListener(e -> {
callSerially(() -> {
Command ok = new Command("OK");
Command cancel = new Command("Cancel");
TextField fileName = new TextField("", "File Path", 20, TextField.NON_PREDICTIVE);
if (ok == Dialog.show("File Path", fileName, new Command[] { ok, cancel })) {
File f = new File(fileName.getText());
if (!f.mkdir()) {
ToastBar.showErrorMessage("Failed to create directory");
}
}
});
});
Button rename = new Button("Rename File");
rename.addActionListener(e -> {
callSerially(() -> {
Command ok = new Command("OK");
Command cancel = new Command("Cancel");
TextField fileName = new TextField("", "File Path", 20, TextField.NON_PREDICTIVE);
TextField newName = new TextField("", "New Name", 20, TextField.NON_PREDICTIVE);
if (ok == Dialog.show("File Path", BoxLayout.encloseY(fileName, newName), new Command[] { ok, cancel })) {
File f = new File(fileName.getText());
FileSystemStorage fs = FileSystemStorage.getInstance();
fs.rename(f.getPath(), newName.getText());
}
});
});
TextArea listing = new TextArea();
Button refresh = new Button("Refresh");
refresh.addActionListener(e -> {
File f = new File(FileSystemStorage.getInstance().getAppHomePath());
StringBuilder sb = new StringBuilder();
appendChildren(sb, f, 0);
listing.setText(sb.toString());
});
hi.add(BorderLayout.NORTH, FlowLayout.encloseCenter(add, rename)).add(BorderLayout.CENTER, listing).add(BorderLayout.SOUTH, refresh);
hi.show();
}
use of com.codename1.io.File in project CodenameOne by codenameone.
the class MediaRecorderSample method start.
public void start() {
if (current != null) {
current.show();
return;
}
Form hi = new Form("Capture", BoxLayout.y());
hi.setToolbar(new Toolbar());
Style s = UIManager.getInstance().getComponentStyle("Title");
FontImage icon = FontImage.createMaterial(FontImage.MATERIAL_MIC, s);
FileSystemStorage fs = FileSystemStorage.getInstance();
String recordingsDir = fs.getAppHomePath() + "recordings/";
fs.mkdir(recordingsDir);
try {
for (String file : fs.listFiles(recordingsDir)) {
MultiButton mb = new MultiButton(file.substring(file.lastIndexOf("/") + 1));
mb.addActionListener((e) -> {
try {
Media m = MediaManager.createMedia(recordingsDir + file, false);
m.play();
} catch (Throwable err) {
Log.e(err);
}
});
hi.add(mb);
}
hi.getToolbar().addCommandToRightBar("", icon, (ev) -> {
try {
String file = Capture.captureAudio();
if (file != null) {
SimpleDateFormat sd = new SimpleDateFormat("yyyy-MMM-dd-kk-mm");
String fileName = sd.format(new Date());
String filePath = recordingsDir + fileName;
Util.copy(fs.openInputStream(file), fs.openOutputStream(filePath));
MultiButton mb = new MultiButton(fileName);
mb.addActionListener((e) -> {
try {
Media m = MediaManager.createMedia(filePath, false);
m.play();
} catch (IOException err) {
Log.e(err);
}
});
hi.add(mb);
hi.revalidate();
}
} catch (IOException err) {
Log.e(err);
}
});
} catch (IOException err) {
Log.e(err);
}
hi.show();
}
use of com.codename1.io.File in project CodenameOne by codenameone.
the class TestRunner method init.
private void init(String[] argv) {
try {
if (argv[0].startsWith("-") || argv[0].startsWith("/")) {
printUsage();
return;
}
try {
mainClass = argv[0];
int pos = 1;
while (pos < argv.length) {
String s = argv[pos];
if (s.equalsIgnoreCase("-stopOnFail")) {
pos++;
stopOnFail = true;
continue;
}
if (s.equalsIgnoreCase("-testCases")) {
pos++;
testCases = argv[pos].split(",");
pos++;
continue;
}
if (s.equalsIgnoreCase("-skins")) {
pos++;
skins = argv[pos].split(",");
pos++;
continue;
}
if (s.equalsIgnoreCase("-landscape")) {
forceLandscape = true;
pos++;
continue;
}
if (s.equalsIgnoreCase("-portrait")) {
forcePortrait = true;
pos++;
continue;
}
if (s.equalsIgnoreCase("-quietMode")) {
quietMode = true;
pos++;
continue;
}
if (s.equalsIgnoreCase("-junitXML")) {
TestReporting.setInstance(new JUnitXMLReporting());
}
if (s.equalsIgnoreCase("-cleanMode")) {
cleanMode = true;
pos++;
continue;
}
System.out.println("Unrecognized argument: " + s);
printUsage();
System.exit(1);
return;
}
} catch (Exception err) {
err.printStackTrace();
printUsage();
return;
}
String[] tests;
if (testCases == null || testCases.length == 0) {
InputStream is = getClass().getResourceAsStream("/tests.dat");
if (is == null) {
System.err.println("Test data not found in the file, make sure the ant task was executed in full");
System.exit(2);
return;
}
DataInputStream di = new DataInputStream(is);
int version = di.readInt();
if (version > VERSION) {
System.err.println("Tests were built with a new version of Codename One and can't be executed with this runner");
System.exit(4);
return;
}
tests = new String[di.readInt()];
for (int iter = 0; iter < tests.length; iter++) {
tests[iter] = di.readUTF();
}
di.close();
} else {
tests = testCases;
}
if (forceLandscape) {
System.out.println("Forcing landscape");
Preferences pref = Preferences.userNodeForPackage(JavaSEPort.class);
pref.putBoolean("Portrait", false);
pref.sync();
} else {
if (forcePortrait) {
System.out.println("Forcing portrait");
Preferences pref = Preferences.userNodeForPackage(JavaSEPort.class);
pref.putBoolean("Portrait", true);
pref.sync();
}
}
System.out.println("Preparing to execute " + tests.length + " tests");
String classPathStr = System.getProperty("java.class.path");
if (System.getProperty("cn1.class.path") != null) {
classPathStr += File.pathSeparator + System.getProperty("cn1.class.path");
}
StringTokenizer t = new StringTokenizer(classPathStr, File.pathSeparator);
File[] files = new File[t.countTokens()];
for (int iter = 0; iter < files.length; iter++) {
files[iter] = new File(t.nextToken());
}
int passedTests = 0;
int failedTests = 0;
if (cleanMode) {
for (String currentTestClass : tests) {
ClassLoader ldr = new ClassPathLoader(files);
Class c = Class.forName("com.codename1.impl.javase.TestExecuter", true, ldr);
Method m = c.getDeclaredMethod("runTest", String.class, String.class, Boolean.TYPE);
Boolean passed = (Boolean) m.invoke(null, mainClass, currentTestClass, quietMode);
if (passed.booleanValue()) {
passedTests++;
} else {
failedTests++;
if (stopOnFail) {
System.exit(100);
return;
}
}
}
} else {
ClassLoader ldr = new ClassPathLoader(files);
Class c = Class.forName("com.codename1.impl.javase.TestExecuter", true, ldr);
for (String currentTestClass : tests) {
Method m = c.getDeclaredMethod("runTest", String.class, String.class, Boolean.TYPE);
Boolean passed = (Boolean) m.invoke(null, mainClass, currentTestClass, quietMode);
if (passed.booleanValue()) {
System.out.println(currentTestClass + " passed!");
passedTests++;
} else {
System.out.println(currentTestClass + " failed!");
failedTests++;
if (stopOnFail) {
System.exit(100);
return;
}
}
}
}
TestReporting.getInstance().testExecutionFinished();
int exitCode = 0;
if (failedTests > 0) {
System.out.println("Test execution finished, some failed tests occured. Passed: " + passedTests + " tests. Failed: " + failedTests + " tests.");
exitCode = 100;
} else {
System.out.println("All tests passed. Total " + passedTests + " tests passed");
}
System.exit(exitCode);
} catch (Exception ex) {
ex.printStackTrace();
System.exit(3);
}
}
use of com.codename1.io.File in project CodenameOne by codenameone.
the class JavaFXLoader method getJavaFXJars.
public URL[] getJavaFXJars() throws IOException {
if (!javafxDir.exists()) {
downloadJavaFX();
}
if (!javafxDir.exists()) {
throw new RuntimeException("Failed to download JavaFX");
}
File javafxLibDir = new File(javafxDir, "lib");
if (!javafxLibDir.exists()) {
throw new RuntimeException("JavaFX is missing. This application requires a JDK with JavaFX.");
}
File jarsDir = javafxLibDir;
if ("8".equals(getJavaFXVersionStr())) {
// JavaFX 8 jar files are in the lib/ext directory
jarsDir = new File(javafxLibDir, "ext");
}
java.util.List<java.net.URL> javafxUrls = new ArrayList<java.net.URL>();
for (File f : jarsDir.listFiles()) {
if (!f.getName().endsWith(".jar")) {
continue;
}
try {
java.net.URL u = f.toURI().toURL();
javafxUrls.add(u);
} catch (Exception ex2) {
ex2.printStackTrace();
}
}
if (javafxUrls.isEmpty()) {
throw new RuntimeException("JavaFX is missing. This application requires a JDK with JavaFX.");
}
try {
// Necessary for loading native libs
javafxUrls.add(javafxLibDir.toURI().toURL());
} catch (MalformedURLException mex) {
throw new RuntimeException(mex);
}
return javafxUrls.toArray(new java.net.URL[javafxUrls.size()]);
}
Aggregations