Search in sources :

Example 6 with DataAdapterList

use of com.servoy.j2db.dataprocessing.DataAdapterList in project servoy-client by Servoy.

the class DataImgMediaField method copyToClipboard.

protected void copyToClipboard() {
    try {
        // we copy to clipboard as byte array and filename/mimetype array used when pasting into another media field and as image icon for external pastes or if byte array is not available (and for external types)
        final Icon icon = enclosedComponent.getIcon();
        final byte[] bytes = getByteArrayContents(value);
        final String[] fileNameAndMime = new String[2];
        if (resolver instanceof DataAdapterList) {
            DataAdapterList dal = ((DataAdapterList) resolver);
            Object tmp = dal.getValueObject(dal.getState(), dataProviderID + IMediaFieldConstants.FILENAME);
            fileNameAndMime[0] = (tmp instanceof String) ? (String) tmp : null;
            tmp = dal.getValueObject(dal.getState(), dataProviderID + IMediaFieldConstants.MIMETYPE);
            fileNameAndMime[1] = (tmp instanceof String) ? (String) tmp : null;
        }
        if (icon instanceof ImageIcon || icon instanceof MyImageIcon) {
            Transferable tr = new Transferable() {

                public DataFlavor[] getTransferDataFlavors() {
                    List<DataFlavor> list = new ArrayList<DataFlavor>(3);
                    if (bytes != null)
                        list.add(getByteArrayDataFlavor());
                    list.add(DataFlavor.imageFlavor);
                    if (fileNameAndMime[0] != null || fileNameAndMime[1] != null) {
                        list.add(getStringArrayDataFlavor());
                        list.add(DataFlavor.stringFlavor);
                    }
                    return list.toArray(new DataFlavor[list.size()]);
                }

                public boolean isDataFlavorSupported(DataFlavor flavor) {
                    return DataFlavor.imageFlavor.equals(flavor) || (bytes != null && getByteArrayDataFlavor().equals(flavor)) || ((fileNameAndMime[0] != null || fileNameAndMime[1] != null) && (getStringArrayDataFlavor().equals(flavor) || DataFlavor.stringFlavor.equals(flavor)));
                }

                public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException {
                    if (!isDataFlavorSupported(flavor)) {
                        throw new UnsupportedFlavorException(flavor);
                    }
                    if (DataFlavor.imageFlavor.equals(flavor)) {
                        if (icon instanceof ImageIcon) {
                            return ((ImageIcon) icon).getImage();
                        } else if (icon instanceof MyImageIcon) {
                            return ((MyImageIcon) icon).getOriginal().getImage();
                        }
                    } else if (getByteArrayDataFlavor().equals(flavor)) {
                        return bytes;
                    } else if (getStringArrayDataFlavor().equals(flavor)) {
                        return fileNameAndMime;
                    } else if (DataFlavor.stringFlavor.equals(flavor)) {
                        return (fileNameAndMime[0] != null ? "Filename: '" + fileNameAndMime[0] + "' " : "") + (fileNameAndMime[1] != null ? "Type: '" + fileNameAndMime[1] + "'" : "");
                    }
                    return null;
                }
            };
            Toolkit.getDefaultToolkit().getSystemClipboard().setContents(tr, null);
        }
    } catch (Exception e) {
        Debug.error(e);
    }
}
Also used : MyImageIcon(com.servoy.j2db.util.gui.MyImageIcon) ImageIcon(javax.swing.ImageIcon) Transferable(java.awt.datatransfer.Transferable) ArrayList(java.util.ArrayList) DataAdapterList(com.servoy.j2db.dataprocessing.DataAdapterList) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) IOException(java.io.IOException) DataFlavor(java.awt.datatransfer.DataFlavor) MyImageIcon(com.servoy.j2db.util.gui.MyImageIcon) Icon(javax.swing.Icon) ImageIcon(javax.swing.ImageIcon) MyImageIcon(com.servoy.j2db.util.gui.MyImageIcon)

Example 7 with DataAdapterList

use of com.servoy.j2db.dataprocessing.DataAdapterList in project servoy-client by Servoy.

the class DataImgMediaField method drop.

public synchronized void drop(DropTargetDropEvent dropTargetDropEvent) {
    try {
        Transferable tr = dropTargetDropEvent.getTransferable();
        if (popup != null && tr.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
            dropTargetDropEvent.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
            List fileList = (List) tr.getTransferData(DataFlavor.javaFileListFlavor);
            Iterator iterator = fileList.iterator();
            if (iterator.hasNext()) {
                try {
                    File file = (File) iterator.next();
                    if (editProvider != null)
                        editProvider.startEdit();
                    byte[] content = FileChooserUtils.paintingReadFile(application.getScheduledExecutor(), application, file);
                    setValueObject(content);
                    if (editProvider != null)
                        editProvider.commitData();
                    // dataprovider_filename and dataprovider_mimetype fields will be set like in the web TODO maybe refactor to avoid cast below and existence of method setValueObject in DataAdapterList
                    if (resolver instanceof DataAdapterList) {
                        ((DataAdapterList) resolver).setValueObject(dataProviderID + IMediaFieldConstants.FILENAME, file.getName());
                        ((DataAdapterList) resolver).setValueObject(dataProviderID + IMediaFieldConstants.MIMETYPE, MimeTypes.getContentType(content, file.getName()));
                    }
                } catch (Exception e) {
                    if (application instanceof ISmartClientApplication) {
                        ((ISmartClientApplication) application).reportError(this, application.getI18NMessage("servoy.imageMedia.error.loading"), e);
                    } else {
                        application.reportError(application.getI18NMessage("servoy.imageMedia.error.loading"), e);
                    }
                }
            }
            dropTargetDropEvent.getDropTargetContext().dropComplete(true);
        } else {
            Debug.trace("Rejected");
            dropTargetDropEvent.rejectDrop();
        }
    } catch (Exception io) {
        Debug.error(io);
        dropTargetDropEvent.rejectDrop();
    }
}
Also used : ISmartClientApplication(com.servoy.j2db.ISmartClientApplication) Transferable(java.awt.datatransfer.Transferable) Iterator(java.util.Iterator) DataAdapterList(com.servoy.j2db.dataprocessing.DataAdapterList) List(java.util.List) DataAdapterList(com.servoy.j2db.dataprocessing.DataAdapterList) ArrayList(java.util.ArrayList) File(java.io.File) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) IOException(java.io.IOException)

Example 8 with DataAdapterList

use of com.servoy.j2db.dataprocessing.DataAdapterList in project servoy-client by Servoy.

the class WebDataRenderer method createDataAdapter.

void createDataAdapter(IApplication app, IDataProviderLookup dataProviderLookup, IScriptExecuter el, ControllerUndoManager undoManager) throws Exception {
    dataAdapterList = new DataAdapterList(app, dataProviderLookup, fieldComponents, el.getFormController(), null, undoManager);
    // make it really fields only
    HashMap<IPersist, IDisplay> f = new HashMap<IPersist, IDisplay>();
    Iterator<Map.Entry<IPersist, IDisplay>> it = fieldComponents.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry<IPersist, IDisplay> element = it.next();
        if (element.getValue() instanceof IDisplayData) {
            // String id = ((IDisplayData)element.getValue()).getDataProviderID();
            // if (dataProviderLookup.getDataProvider(id) instanceof ScriptVariable)
            // {
            // globalFields.add(element.getValue());
            // }
            f.put(element.getKey(), element.getValue());
        }
    }
    fieldComponents = f;
}
Also used : IPersist(com.servoy.j2db.persistence.IPersist) HashMap(java.util.HashMap) DataAdapterList(com.servoy.j2db.dataprocessing.DataAdapterList) IDisplay(com.servoy.j2db.dataprocessing.IDisplay) IDisplayData(com.servoy.j2db.dataprocessing.IDisplayData) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

DataAdapterList (com.servoy.j2db.dataprocessing.DataAdapterList)8 UnsupportedFlavorException (java.awt.datatransfer.UnsupportedFlavorException)5 IOException (java.io.IOException)5 ISmartClientApplication (com.servoy.j2db.ISmartClientApplication)4 IDisplay (com.servoy.j2db.dataprocessing.IDisplay)3 Transferable (java.awt.datatransfer.Transferable)3 File (java.io.File)3 IDisplayData (com.servoy.j2db.dataprocessing.IDisplayData)2 IPersist (com.servoy.j2db.persistence.IPersist)2 Point (java.awt.Point)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 FormController (com.servoy.j2db.FormController)1 ScriptVariable (com.servoy.j2db.persistence.ScriptVariable)1 IDataRenderer (com.servoy.j2db.ui.IDataRenderer)1 FileNameSuggestionFileChooser (com.servoy.j2db.util.gui.FileNameSuggestionFileChooser)1 MyImageIcon (com.servoy.j2db.util.gui.MyImageIcon)1 Image (java.awt.Image)1 DataFlavor (java.awt.datatransfer.DataFlavor)1