Example usage for javax.swing JComponent getToolkit

List of usage examples for javax.swing JComponent getToolkit

Introduction

In this page you can find the example usage for javax.swing JComponent getToolkit.

Prototype

public Toolkit getToolkit() 

Source Link

Document

Gets the toolkit of this component.

Usage

From source file:FileTransferHandler.java

/**
 * If the wrapped handler can import strings and the specified Transferable
 * can provide its data as a List of File objects, then we read the files, and
 * pass their contents as a string to the wrapped handler. Otherwise, we offer
 * the Transferable to the wrapped handler to handle on its own.
 *//*from w  ww .j  a  v  a2 s  .  c  o  m*/
public boolean importData(JComponent c, Transferable t) {
    // See if we're offered a java.util.List of java.io.File objects.
    // We handle this case first because the Transferable is likely to
    // also offer the filenames as strings, and we want to import the
    // file contents, not their names!
    if (t.isDataFlavorSupported(DataFlavor.javaFileListFlavor)
            && wrappedHandler.canImport(c, stringFlavorArray)) {
        try {
            List filelist = (List) t.getTransferData(DataFlavor.javaFileListFlavor);

            // Loop through the files to determine total size
            int numfiles = filelist.size();
            int numbytes = 0;
            for (int i = 0; i < numfiles; i++) {
                File f = (File) filelist.get(i);
                numbytes += (int) f.length();
            }

            // There will never be more characters than bytes in the files
            char[] text = new char[numbytes]; // to hold file contents
            int p = 0; // current position in the text[] array

            // Loop through the files again, reading their content as text
            for (int i = 0; i < numfiles; i++) {
                File f = (File) filelist.get(i);
                Reader r = new BufferedReader(new FileReader(f));
                p += r.read(text, p, (int) f.length());
            }

            // Convert the character array to a string and wrap it
            // in a pre-defined Transferable class for transferring strings
            StringSelection selection = new StringSelection(new String(text, 0, p));

            // Ask the wrapped handler to import the string
            return wrappedHandler.importData(c, selection);
        }
        // If anything goes wrong, just beep to tell the user
        catch (UnsupportedFlavorException e) {
            c.getToolkit().beep(); // audible error
            return false; // return failure code
        } catch (IOException e) {
            c.getToolkit().beep(); // audible error
            return false; // return failure code
        }
    }

    // Otherwise let the wrapped class handle this transferable itself
    return wrappedHandler.importData(c, t);
}

From source file:org.eclipse.birt.chart.device.swing.SwingEventHandler.java

private void setCursor(JComponent composite, org.eclipse.birt.chart.model.attribute.Cursor cursor,
        Cursor defaultCursor) {//from   ww w.  j a  va  2s  . com
    if (cursor == null || cursor.getType() == CursorType.AUTO) {
        composite.setCursor(defaultCursor);
        return;
    } else if (cursor.getType() == CursorType.CUSTOM) {
        // Find the first valid image as custom cursor.
        EList<org.eclipse.birt.chart.model.attribute.Image> uris = cursor.getImage();
        for (org.eclipse.birt.chart.model.attribute.Image uri : uris) {
            try {
                Image image = null;
                if (uri instanceof EmbeddedImage) {
                    try {
                        byte[] data = Base64.decodeBase64(((EmbeddedImage) uri).getData().getBytes());

                        image = new ImageIcon(data).getImage();
                    } catch (Exception ilex) {
                        logger.log(ilex);
                    }
                } else {
                    URI u = new URI(uri.getURL());
                    image = composite.getToolkit().createImage(SecurityUtil.toURL(u));
                }

                if (image != null) {
                    composite.setCursor(composite.getToolkit().createCustomCursor(image, new Point(0, 0), ""));//$NON-NLS-1$
                    return;
                }
            } catch (URISyntaxException e) {
                // Do not process exception here.
            } catch (MalformedURLException e) {
                // Do not process exception here.
            }
        }

        // No valid image is found, set default cursor.
        composite.setCursor(defaultCursor);
        return;
    }

    composite.setCursor(Cursor.getPredefinedCursor(SwingHelper.CURSOR_MAP.get(cursor.getType()).intValue()));
}