Search in sources :

Example 1 with FileNameSuggestionFileChooser

use of com.servoy.j2db.util.gui.FileNameSuggestionFileChooser in project servoy-client by Servoy.

the class FileChooserUtils method getAWriteFile.

public static File getAWriteFile(Component parent, File file, boolean doFileSuggestion, String title) {
    JFileChooser fc = null;
    if (file != null) {
        if (file.isDirectory()) {
            fc = new JFileChooser(file);
            lastDir = file;
        } else if (doFileSuggestion) {
            fc = new FileNameSuggestionFileChooser(file.getParentFile());
            lastDir = file.getParentFile();
            ((FileNameSuggestionFileChooser) fc).suggestFileName(file.getName());
        }
    } else {
        fc = new JFileChooser(lastDir);
    }
    if (fc != null) {
        if (title == null) {
            // $NON-NLS-1$
            fc.setDialogTitle(Messages.getString("servoy.filechooser.title"));
        } else {
            fc.setDialogTitle(title);
        }
        // $NON-NLS-1$);
        fc.setApproveButtonText(Messages.getString("servoy.filechooser.button.title"));
        int returnVal = fc.showSaveDialog(parent);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            file = fc.getSelectedFile();
            lastDir = fc.getCurrentDirectory();
        } else {
            file = null;
        }
    }
    return file;
}
Also used : JFileChooser(javax.swing.JFileChooser) FileNameSuggestionFileChooser(com.servoy.j2db.util.gui.FileNameSuggestionFileChooser)

Example 2 with FileNameSuggestionFileChooser

use of com.servoy.j2db.util.gui.FileNameSuggestionFileChooser in project servoy-client by Servoy.

the class DataImgMediaField method saveToFile.

void saveToFile() {
    if (value instanceof byte[]) {
        byte[] array = (byte[]) value;
        FileNameSuggestionFileChooser fc = new FileNameSuggestionFileChooser();
        String fileName = null;
        // dataprovider_filename and dataprovider_mimetype fields will be used like in the web TODO maybe refactor to avoid cast below
        if (resolver instanceof DataAdapterList) {
            Object val = ((DataAdapterList) resolver).getValueObject(((DataAdapterList) resolver).getState(), dataProviderID + IMediaFieldConstants.FILENAME);
            fileName = (val instanceof String) ? (String) val : null;
        }
        if (fileName != null) {
            fc.suggestFileName(fileName);
        } else if (// jpeg
        array.length > 3 && array[0] == -1 && array[1] == -40 && array[2] == -1) {
            fc.suggestFileName("image.jpg");
        } else if (// gif
        array.length > 3 && array[0] == 0x47 && array[1] == 0x49 && array[2] == 0x46) {
            fc.suggestFileName("image.gif");
        } else {
            fc.suggestFileName("filename.unknown");
        }
        int returnVal = fc.showSaveDialog(((ISmartClientApplication) application).getMainApplicationFrame());
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            try {
                File f = fc.getSelectedFile();
                OutputStream os = new FileOutputStream(f);
                BufferedOutputStream bos = new BufferedOutputStream(os);
                bos.write(array);
                bos.close();
            } 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);
                }
            }
        }
    }
}
Also used : ISmartClientApplication(com.servoy.j2db.ISmartClientApplication) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) DataAdapterList(com.servoy.j2db.dataprocessing.DataAdapterList) FileNameSuggestionFileChooser(com.servoy.j2db.util.gui.FileNameSuggestionFileChooser) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) Point(java.awt.Point) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) IOException(java.io.IOException)

Aggregations

FileNameSuggestionFileChooser (com.servoy.j2db.util.gui.FileNameSuggestionFileChooser)2 ISmartClientApplication (com.servoy.j2db.ISmartClientApplication)1 DataAdapterList (com.servoy.j2db.dataprocessing.DataAdapterList)1 Point (java.awt.Point)1 UnsupportedFlavorException (java.awt.datatransfer.UnsupportedFlavorException)1 BufferedOutputStream (java.io.BufferedOutputStream)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 JFileChooser (javax.swing.JFileChooser)1