Search in sources :

Example 16 with Dimension

use of com.codename1.ui.geom.Dimension 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 17 with Dimension

use of com.codename1.ui.geom.Dimension in project CodenameOne by codenameone.

the class CodenameOneImplementation method setClipRect.

/**
 * Installs a new clipping rectangle
 *
 * @param graphics the graphics context
 * @param rect rectangle representing the new clipping area
 */
public void setClipRect(Object graphics, Rectangle rect) {
    Dimension d = rect.getSize();
    setClip(graphics, rect.getX(), rect.getY(), d.getWidth(), d.getHeight());
}
Also used : Dimension(com.codename1.ui.geom.Dimension)

Example 18 with Dimension

use of com.codename1.ui.geom.Dimension in project CodenameOne by codenameone.

the class ImageDownloadService method createImageToFileSystem.

/**
 * Constructs an image request that will automatically populate the given list
 * when the response arrives, it will cache the file locally as a file
 * in the file storage.
 * This assumes the GenericListCellRenderer style of
 * list which relies on a map based model approach.
 *
 * @param url the image URL
 * @param targetList the list that should be updated when the data arrives
 * @param targetOffset the offset within the list to insert the image
 * @param targetKey the key for the map in the target offset
 * @param destFile local file to store the data into the given path
 */
private static void createImageToFileSystem(final String url, final Component targetList, final ListModel targetModel, final int targetOffset, final String targetKey, final String destFile, final Dimension toScale, final byte priority, final Image placeholderImage, final boolean maintainAspectRatio) {
    if (Display.getInstance().isEdt()) {
        Display.getInstance().scheduleBackgroundTask(new Runnable() {

            public void run() {
                createImageToFileSystem(url, targetList, targetModel, targetOffset, targetKey, destFile, toScale, priority, placeholderImage, maintainAspectRatio);
            }
        });
        return;
    }
    // image not found on cache go and download from the url
    ImageDownloadService i = new ImageDownloadService(url, targetList, targetOffset, targetKey);
    i.targetModel = targetModel;
    i.maintainAspectRatio = maintainAspectRatio;
    Image im = cacheImage(null, false, destFile, toScale, placeholderImage, maintainAspectRatio);
    if (im != null) {
        i.setEntryInListModel(targetOffset, im);
        targetList.repaint();
        return;
    }
    i.cacheImages = true;
    i.destinationFile = destFile;
    i.toScale = toScale;
    i.placeholder = placeholderImage;
    i.setPriority(priority);
    i.setFailSilently(true);
    NetworkManager.getInstance().addToQueue(i);
}
Also used : EncodedImage(com.codename1.ui.EncodedImage) Image(com.codename1.ui.Image) FileEncodedImage(com.codename1.components.FileEncodedImage) StorageImage(com.codename1.components.StorageImage)

Example 19 with Dimension

use of com.codename1.ui.geom.Dimension in project CodenameOne by codenameone.

the class ImageDownloadService method postResponse.

/**
 * {@inheritDoc}
 */
protected void postResponse() {
    // trigger an exception in case of an invalid image
    result.getWidth();
    Image image = result;
    if (toScale != null && toScale.getWidth() != image.getWidth() && toScale.getHeight() != image.getHeight()) {
        image = scaleImage(image, toScale, maintainAspectRatio);
    }
    final Image i = image;
    if (parentLabel != null) {
        final Dimension pref = parentLabel.getPreferredSize();
        if (parentLabel.getComponentForm() != null) {
            Display.getInstance().callSerially(new Runnable() {

                public void run() {
                    if (isDownloadToStyles()) {
                        parentLabel.getUnselectedStyle().setBgImage(i);
                        parentLabel.getSelectedStyle().setBgImage(i);
                        parentLabel.getPressedStyle().setBgImage(i);
                    } else {
                        parentLabel.setIcon(i);
                    }
                    Dimension newPref = parentLabel.getPreferredSize();
                    // sized image in place or has a hardcoded preferred size.
                    if (pref.getWidth() != newPref.getWidth() || pref.getHeight() != newPref.getHeight()) {
                        parentLabel.getComponentForm().revalidate();
                    }
                }
            });
        } else {
            Display.getInstance().callSerially(new Runnable() {

                public void run() {
                    if (isDownloadToStyles()) {
                        parentLabel.getUnselectedStyle().setBgImage(i);
                        parentLabel.getSelectedStyle().setBgImage(i);
                        parentLabel.getPressedStyle().setBgImage(i);
                    } else {
                        parentLabel.setIcon(i);
                    }
                }
            });
        }
        parentLabel.repaint();
        return;
    } else {
        if (targetList != null) {
            setEntryInListModel(targetOffset, image);
            // revalidate only once to avoid multiple revalidate refreshes during scroll
            if (targetList.getParent() != null) {
                if (alwaysRevalidate) {
                    targetList.getParent().revalidate();
                } else {
                    if (targetList.getClientProperty("$imgDSReval") == null) {
                        targetList.putClientProperty("$imgDSReval", Boolean.TRUE);
                        targetList.getParent().revalidate();
                    } else {
                        targetList.repaint();
                    }
                }
            }
        }
    }
    // if this is a list cell renderer component
    fireResponseListener(new NetworkEvent(this, result));
}
Also used : NetworkEvent(com.codename1.io.NetworkEvent) Dimension(com.codename1.ui.geom.Dimension) EncodedImage(com.codename1.ui.EncodedImage) Image(com.codename1.ui.Image) FileEncodedImage(com.codename1.components.FileEncodedImage) StorageImage(com.codename1.components.StorageImage)

Example 20 with Dimension

use of com.codename1.ui.geom.Dimension in project CodenameOne by codenameone.

the class FaceBookAccess method getPictureAndWait.

/**
 * Gets the picture of the given facebook object id
 *
 * @param id the object id to query
 * @param toScale picture dimension or null
 * @return the picture
 */
public EncodedImage getPictureAndWait(String id, Dimension toScale) {
    ImageDownloadService im = new ImageDownloadService(getImageURL(id, toScale), (ActionListener) null);
    NetworkManager.getInstance().addToQueueAndWait(im);
    return im.getResult();
}
Also used : ImageDownloadService(com.codename1.io.services.ImageDownloadService)

Aggregations

Dimension (com.codename1.ui.geom.Dimension)74 Style (com.codename1.ui.plaf.Style)27 Component (com.codename1.ui.Component)16 Image (com.codename1.ui.Image)16 Rectangle (com.codename1.ui.geom.Rectangle)9 EncodedImage (com.codename1.ui.EncodedImage)8 Label (com.codename1.ui.Label)7 Container (com.codename1.ui.Container)6 FileEncodedImage (com.codename1.components.FileEncodedImage)5 StorageImage (com.codename1.components.StorageImage)5 ActionEvent (com.codename1.ui.events.ActionEvent)5 Form (com.codename1.ui.Form)4 Dimension (java.awt.Dimension)4 Font (com.codename1.ui.Font)3 FontImage (com.codename1.ui.FontImage)3 TextArea (com.codename1.ui.TextArea)3 ActionListener (com.codename1.ui.events.ActionListener)3 Point (com.codename1.ui.geom.Point)3 BorderLayout (com.codename1.ui.layouts.BorderLayout)3 Border (com.codename1.ui.plaf.Border)3