Search in sources :

Example 1 with File

use of com.codename1.io.File in project CodenameOne by codenameone.

the class Resources method loadBitmapFont.

Font loadBitmapFont(DataInputStream input, String id, com.codename1.ui.Font font) throws IOException {
    Image bitmap = createImage(input);
    int charCount = input.readShort();
    int[] cutOffsets = new int[charCount];
    int[] charWidth = new int[charCount];
    for (int iter = 0; iter < charCount; iter++) {
        cutOffsets[iter] = input.readShort();
    }
    for (int iter = 0; iter < charCount; iter++) {
        charWidth[iter] = input.readByte();
    }
    String charset = input.readUTF();
    readRenderingHint(input);
    if (font == null) {
        if (Font.isBitmapFontEnabled()) {
            Font old = Font.getBitmapFont(id);
            if (old != null) {
                // old resource file use Font.clearBitmapCache()
                return old;
            }
            return Font.createBitmapFont(id, bitmap, cutOffsets, charWidth, charset);
        }
    }
    return font;
}
Also used : Image(com.codename1.ui.Image) EncodedImage(com.codename1.ui.EncodedImage) Font(com.codename1.ui.Font)

Example 2 with File

use of com.codename1.io.File in project CodenameOne by codenameone.

the class ResourceEditorApp method main.

/**
 * Main method launching the application.
 */
public static void main(String[] args) throws Exception {
    JavaSEPortWithSVGSupport.blockMonitors();
    JavaSEPortWithSVGSupport.setDesignMode(true);
    JavaSEPortWithSVGSupport.setShowEDTWarnings(false);
    JavaSEPortWithSVGSupport.setShowEDTViolationStacks(false);
    // creates a deadlock between FX, Swing and CN1. Horrible horrible deadlock...
    JavaSEPortWithSVGSupport.blockNativeBrowser = true;
    if (args.length > 0) {
        if (args[0].equalsIgnoreCase("-buildVersion")) {
            Properties p = new Properties();
            try {
                p.load(ResourceEditorApp.class.getResourceAsStream("/version.properties"));
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            System.out.println(p.getProperty("build", "1"));
            System.exit(0);
            return;
        }
        if (args[0].equalsIgnoreCase("-style")) {
            java.awt.Container cnt = new java.awt.Container();
            com.codename1.ui.Display.init(cnt);
            final String uiid = args[2];
            String themeName = args[3];
            boolean isXMLEnabled = Preferences.userNodeForPackage(ResourceEditorView.class).getBoolean("XMLFileMode", true);
            EditableResources.setXMLEnabled(isXMLEnabled);
            EditableResources res = new EditableResources();
            File resourceFile = new File(args[1]);
            res.openFileWithXMLSupport(resourceFile);
            Hashtable themeHash = res.getTheme(themeName);
            final AddThemeEntry entry = new AddThemeEntry(false, res, null, new Hashtable(themeHash), "", themeName);
            entry.setKeyValues(uiid, "");
            entry.setPreferredSize(new Dimension(1000, 600));
            JPanel wrapper = new JPanel(new BorderLayout());
            wrapper.add(entry, BorderLayout.CENTER);
            JPanel bottom = new JPanel();
            ButtonGroup gr = new ButtonGroup();
            JRadioButton unsel = new JRadioButton("Unselected", true);
            gr.add(unsel);
            unsel.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    entry.setPrefix("");
                    entry.setKeyValues(uiid, "");
                    entry.revalidate();
                }
            });
            bottom.add(unsel);
            JRadioButton sel = new JRadioButton("Selected");
            gr.add(sel);
            sel.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    entry.setPrefix("sel#");
                    entry.setKeyValues(uiid, "sel#");
                    entry.revalidate();
                }
            });
            bottom.add(sel);
            JRadioButton press = new JRadioButton("Pressed");
            gr.add(press);
            press.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    entry.setPrefix("press#");
                    entry.setKeyValues(uiid, "press#");
                    entry.revalidate();
                }
            });
            bottom.add(press);
            JRadioButton dis = new JRadioButton("Disabled");
            gr.add(dis);
            dis.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    entry.setPrefix("dis#");
                    entry.setKeyValues(uiid, "dis#");
                    entry.revalidate();
                }
            });
            bottom.add(dis);
            wrapper.add(bottom, BorderLayout.SOUTH);
            if (ModifiableJOptionPane.showConfirmDialog(null, wrapper, "Edit") == JOptionPane.OK_OPTION) {
                Hashtable tmp = new Hashtable(themeHash);
                entry.updateThemeHashtable(tmp);
                res.setTheme(themeName, tmp);
            }
            try (FileOutputStream fos = new FileOutputStream(resourceFile)) {
                res.save(fos);
            }
            res.saveXML(resourceFile);
            System.exit(0);
            return;
        }
        if (args[0].equalsIgnoreCase("-img")) {
            java.awt.Container cnt = new java.awt.Container();
            com.codename1.ui.Display.init(cnt);
            String imageName;
            String fileName;
            if (args.length == 3) {
                imageName = args[2];
                fileName = args[2];
            } else {
                if (args.length == 4) {
                    imageName = args[3];
                    fileName = args[2];
                } else {
                    System.out.println("The img command works as: -img path_to_resourceFile.res pathToImageFile [image name]");
                    System.exit(1);
                    return;
                }
            }
            File imageFile = new File(fileName);
            if (!imageFile.exists()) {
                System.out.println("File not found: " + imageFile.getAbsolutePath());
                System.exit(1);
                return;
            }
            com.codename1.ui.Image img = ImageRGBEditor.createImageStatic(imageFile);
            boolean isXMLEnabled = Preferences.userNodeForPackage(ResourceEditorView.class).getBoolean("XMLFileMode", true);
            EditableResources.setXMLEnabled(isXMLEnabled);
            EditableResources res = new EditableResources();
            File resourceFile = new File(args[1]);
            res.openFileWithXMLSupport(resourceFile);
            res.setImage(imageName, img);
            try (FileOutputStream fos = new FileOutputStream(resourceFile)) {
                res.save(fos);
            }
            res.saveXML(resourceFile);
            System.exit(0);
            return;
        }
        if (args[0].equalsIgnoreCase("-mimg")) {
            java.awt.Container cnt = new java.awt.Container();
            com.codename1.ui.Display.init(cnt);
            String fileName;
            if (args.length == 4) {
                fileName = args[3];
            } else {
                System.out.println("The mimg command works as: -img path_to_resourceFile.res dpi pathToImageFile");
                System.out.println("dpi can be one of:  high, veryhigh, hd, 560, 2hd, 4k");
                System.exit(1);
                return;
            }
            String dpi = args[2];
            int dpiInt = -1;
            switch(dpi.toLowerCase()) {
                case "high":
                    dpiInt = 3;
                    break;
                case "veryhigh":
                    dpiInt = 4;
                    break;
                case "hd":
                    dpiInt = 5;
                    break;
                case "560":
                    dpiInt = 6;
                    break;
                case "2hd":
                    dpiInt = 7;
                    break;
                case "4k":
                    dpiInt = 8;
                    break;
                default:
                    System.out.println("dpi can be one of:  high, veryhigh, hd, 560, 2hd, 4k");
                    System.exit(1);
                    return;
            }
            File imageFile = new File(fileName);
            if (!imageFile.exists()) {
                System.out.println("File not found: " + imageFile.getAbsolutePath());
                System.exit(1);
                return;
            }
            boolean isXMLEnabled = Preferences.userNodeForPackage(ResourceEditorView.class).getBoolean("XMLFileMode", true);
            EditableResources.setXMLEnabled(isXMLEnabled);
            EditableResources res = new EditableResources();
            File resourceFile = new File(args[1]);
            res.openFileWithXMLSupport(resourceFile);
            AddAndScaleMultiImage.generateImpl(new File[] { imageFile }, res, dpiInt);
            try (FileOutputStream fos = new FileOutputStream(resourceFile)) {
                res.save(fos);
            }
            res.saveXML(resourceFile);
            System.exit(0);
            return;
        }
        if (args[0].equalsIgnoreCase("gen")) {
            java.awt.Container cnt = new java.awt.Container();
            com.codename1.ui.Display.init(cnt);
            File output = new File(args[1]);
            generateResourceFile(output, args[2], args[3]);
            System.exit(0);
            return;
        }
        if (args[0].equalsIgnoreCase("mig")) {
            java.awt.Container cnt = new java.awt.Container();
            com.codename1.ui.Display.init(cnt);
            File projectDir = new File(args[1]);
            EditableResources.setXMLEnabled(true);
            EditableResources res = new EditableResources();
            res.openFileWithXMLSupport(new File(args[2]));
            migrateGuiBuilder(projectDir, res, args[3]);
            System.exit(0);
            return;
        }
        if (args[0].equalsIgnoreCase("-regen")) {
            java.awt.Container cnt = new java.awt.Container();
            com.codename1.ui.Display.init(cnt);
            File output = new File(args[1]);
            EditableResources.setXMLEnabled(true);
            EditableResources res = new EditableResources();
            res.openFileWithXMLSupport(output);
            FileOutputStream fos = new FileOutputStream(output);
            res.save(fos);
            fos.close();
            generate(res, output);
            System.exit(0);
            return;
        }
    }
    JavaSEPortWithSVGSupport.setDefaultInitTarget(new JPanel());
    Display.init(null);
    launch(ResourceEditorApp.class, args);
}
Also used : JPanel(javax.swing.JPanel) JRadioButton(javax.swing.JRadioButton) ActionEvent(java.awt.event.ActionEvent) Properties(java.util.Properties) Container(com.codename1.ui.Container) BorderLayout(java.awt.BorderLayout) EditableResources(com.codename1.ui.util.EditableResources) UIBuilderOverride(com.codename1.ui.util.UIBuilderOverride) Hashtable(java.util.Hashtable) IOException(java.io.IOException) Dimension(java.awt.Dimension) ActionListener(java.awt.event.ActionListener) ButtonGroup(javax.swing.ButtonGroup) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 3 with File

use of com.codename1.io.File in project CodenameOne by codenameone.

the class CloudStorage method deleteCloudFile.

/**
 * Deletes a file from the cloud storage
 *
 * @param fileId the file id to delete
 * @return true if the operation was successful
 * @deprecated this API is currently deprecated due to Googles cloud storage deprection
 */
public boolean deleteCloudFile(String fileId) {
    if (CloudPersona.getCurrentPersona().getToken() == null) {
        CloudPersona.createAnonymous();
    }
    ConnectionRequest req = new ConnectionRequest();
    req.setPost(false);
    req.setFailSilently(true);
    req.setUrl(SERVER_URL + "/fileStoreDelete");
    req.addArgument("i", fileId);
    req.addArgument("t", CloudPersona.getCurrentPersona().getToken());
    NetworkManager.getInstance().addToQueueAndWait(req);
    if (req.getResponseCode() == 200) {
        return new String(req.getResponseData()).equals("OK");
    }
    return false;
}
Also used : ConnectionRequest(com.codename1.io.ConnectionRequest)

Example 4 with File

use of com.codename1.io.File in project CodenameOne by codenameone.

the class Executor method main.

public static void main(final String[] argv) throws Exception {
    setProxySettings();
    final Properties p = new Properties();
    String currentDir = System.getProperty("user.dir");
    File props = new File(currentDir, "codenameone_settings.properties");
    if (props.exists()) {
        FileInputStream f = null;
        try {
            f = new FileInputStream(props);
            p.load(f);
            f.close();
        } catch (Exception ex) {
        } finally {
            try {
                f.close();
            } catch (IOException ex) {
            }
        }
    }
    SwingUtilities.invokeLater(new Runnable() {

        @Override
        public void run() {
            try {
                String packageName = p.getProperty("codename1.packageName");
                String mainName = p.getProperty("codename1.mainName");
                if (argv.length > 1) {
                    if (argv[1].equalsIgnoreCase("-force") || packageName == null) {
                        c = Class.forName(argv[0]);
                    } else {
                        c = Class.forName(packageName + "." + mainName);
                    }
                } else {
                    if (packageName == null || System.getenv("FORCE_CLASS") != null) {
                        c = Class.forName(argv[0]);
                    } else {
                        c = Class.forName(packageName + "." + mainName);
                    }
                }
                try {
                    Method m = c.getDeclaredMethod("main", String[].class);
                    m.invoke(null, new Object[] { null });
                } catch (NoSuchMethodException noMain) {
                    try {
                        Method m = c.getDeclaredMethod("startApp");
                        m.invoke(c.newInstance());
                    } catch (NoSuchMethodException noStartApp) {
                        if (Display.isInitialized()) {
                            Display.deinitialize();
                        }
                        final Method m = c.getDeclaredMethod("init", Object.class);
                        if (m.getExceptionTypes() != null && m.getExceptionTypes().length > 0) {
                            System.err.println("ERROR: the init method can't declare a throws clause");
                            System.exit(1);
                        }
                        app = c.newInstance();
                        if (app instanceof PushCallback) {
                            CodenameOneImplementation.setPushCallback((PushCallback) app);
                        }
                        if (app instanceof PurchaseCallback) {
                            CodenameOneImplementation.setPurchaseCallback((PurchaseCallback) app);
                        }
                        Display.init(null);
                        Display.getInstance().callSerially(new Runnable() {

                            @Override
                            public void run() {
                                try {
                                    m.invoke(app, new Object[] { null });
                                    Method start = c.getDeclaredMethod("start", new Class[0]);
                                    if (start.getExceptionTypes() != null && start.getExceptionTypes().length > 0) {
                                        System.err.println("ERROR: the start method can't declare a throws clause");
                                        System.exit(1);
                                    }
                                    start.invoke(app, new Object[0]);
                                } catch (NoSuchMethodException err) {
                                    System.out.println("Couldn't find a main or a startup in " + argv[0]);
                                } catch (InvocationTargetException err) {
                                    err.getTargetException().printStackTrace();
                                    System.exit(1);
                                } catch (Exception err) {
                                    err.printStackTrace();
                                    System.exit(1);
                                }
                            }
                        });
                    }
                }
            } catch (Exception err) {
                err.printStackTrace();
                System.exit(1);
            }
        }
    });
}
Also used : IOException(java.io.IOException) Method(java.lang.reflect.Method) Properties(java.util.Properties) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) PushCallback(com.codename1.push.PushCallback) PurchaseCallback(com.codename1.payment.PurchaseCallback) File(java.io.File)

Example 5 with File

use of com.codename1.io.File in project CodenameOne by codenameone.

the class ResourceEditorView method setNativeTheme.

// GEN-LAST:event_imageBorderWizardMenuItemActionPerformed
private void setNativeTheme(String file, boolean local) {
    try {
        InputStream i;
        if (local) {
            i = getClass().getResourceAsStream(file);
        } else {
            i = new FileInputStream(file);
        }
        EditableResources er = new EditableResources();
        er.openFile(i);
        JavaSEPortWithSVGSupport.setNativeTheme(er);
        JavaSEPortWithSVGSupport.setShowEDTWarnings(false);
        JavaSEPortWithSVGSupport.setShowEDTViolationStacks(false);
        i.close();
        Preferences p = Preferences.userNodeForPackage(getClass());
        p.put("nativeCN1Theme", file);
        p.putBoolean("nativeCN1Local", local);
        if (selectedResource != null) {
            setSelectedResource(selectedResource);
        }
    } catch (IOException ex) {
        ex.printStackTrace();
        JOptionPane.showMessageDialog(mainPanel, "Error " + ex, "Error", JOptionPane.ERROR_MESSAGE);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) DataInputStream(java.io.DataInputStream) ZipInputStream(java.util.zip.ZipInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) Preferences(java.util.prefs.Preferences) EditableResources(com.codename1.ui.util.EditableResources) FileInputStream(java.io.FileInputStream)

Aggregations

IOException (java.io.IOException)76 File (java.io.File)61 FileInputStream (java.io.FileInputStream)43 InputStream (java.io.InputStream)33 EncodedImage (com.codename1.ui.EncodedImage)23 FileOutputStream (java.io.FileOutputStream)23 OutputStream (java.io.OutputStream)22 SortedProperties (com.codename1.ant.SortedProperties)18 FileSystemStorage (com.codename1.io.FileSystemStorage)17 BufferedInputStream (com.codename1.io.BufferedInputStream)15 Image (com.codename1.ui.Image)15 ByteArrayInputStream (java.io.ByteArrayInputStream)15 ActionEvent (com.codename1.ui.events.ActionEvent)14 RandomAccessFile (java.io.RandomAccessFile)14 ArrayList (java.util.ArrayList)14 EditableResources (com.codename1.ui.util.EditableResources)13 InvocationTargetException (java.lang.reflect.InvocationTargetException)13 Properties (java.util.Properties)12 File (com.codename1.io.File)11 BufferedImage (java.awt.image.BufferedImage)11