Search in sources :

Example 16 with DBDContent

use of org.jkiss.dbeaver.model.data.DBDContent in project dbeaver by serge-rider.

the class DialogUtils method saveToFile.

public static void saveToFile(IValueController controller) {
    if (!(controller.getValue() instanceof DBDContent)) {
        log.error(CoreMessages.model_jdbc_bad_content_value_ + controller.getValue());
        return;
    }
    Shell shell = UIUtils.getShell(controller.getValueSite());
    final File saveFile = selectFileForSave(shell, controller.getValueName());
    if (saveFile == null) {
        return;
    }
    final DBDContent value = (DBDContent) controller.getValue();
    try {
        DBeaverUI.runInProgressService(new DBRRunnableWithProgress() {

            @Override
            public void run(DBRProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                try {
                    DBDContentStorage storage = value.getContents(monitor);
                    if (ContentUtils.isTextContent(value)) {
                        try (Reader cr = storage.getContentReader()) {
                            ContentUtils.saveContentToFile(cr, saveFile, GeneralUtils.UTF8_ENCODING, monitor);
                        }
                    } else {
                        try (InputStream cs = storage.getContentStream()) {
                            ContentUtils.saveContentToFile(cs, saveFile, monitor);
                        }
                    }
                } catch (Exception e) {
                    throw new InvocationTargetException(e);
                }
            }
        });
    } catch (InvocationTargetException e) {
        UIUtils.showErrorDialog(shell, CoreMessages.model_jdbc_could_not_save_content, //$NON-NLS-2$
        CoreMessages.model_jdbc_could_not_save_content_to_file_ + saveFile.getAbsolutePath() + "'", e.getTargetException());
    } catch (InterruptedException e) {
    // do nothing
    }
}
Also used : DBDContent(org.jkiss.dbeaver.model.data.DBDContent) InputStream(java.io.InputStream) DBRRunnableWithProgress(org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress) Reader(java.io.Reader) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) File(java.io.File) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBDContentStorage(org.jkiss.dbeaver.model.data.DBDContentStorage) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 17 with DBDContent

use of org.jkiss.dbeaver.model.data.DBDContent in project dbeaver by serge-rider.

the class TextViewDialog method extractEditorValue.

@Override
public Object extractEditorValue() {
    Object prevValue = getValueController().getValue();
    Object rawValue;
    if (prevValue instanceof DBDContent) {
        if (ContentUtils.isTextContent((DBDContent) prevValue)) {
            rawValue = isTextEditorActive() ? textEdit.getText() : getBinaryString();
        } else {
            rawValue = isTextEditorActive() ? GeneralUtils.convertToBytes(textEdit.getText()) : getBinaryContent();
        }
    } else {
        if (isTextEditorActive()) {
            rawValue = textEdit.getText();
        } else {
            rawValue = getBinaryString();
        }
    }
    try (DBCSession session = getValueController().getExecutionContext().openSession(VoidProgressMonitor.INSTANCE, DBCExecutionPurpose.UTIL, "Make text value from editor")) {
        return getValueController().getValueHandler().getValueFromObject(session, getValueController().getValueType(), rawValue, false);
    } catch (Exception e) {
        UIUtils.showErrorDialog(getShell(), "Extract editor value", "Can't extract editor value", e);
        return null;
    }
}
Also used : DBDContent(org.jkiss.dbeaver.model.data.DBDContent) DBSTypedObject(org.jkiss.dbeaver.model.struct.DBSTypedObject) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) DBCSession(org.jkiss.dbeaver.model.exec.DBCSession)

Aggregations

DBDContent (org.jkiss.dbeaver.model.data.DBDContent)17 DBDContentStorage (org.jkiss.dbeaver.model.data.DBDContentStorage)7 DBException (org.jkiss.dbeaver.DBException)5 DBDAttributeBinding (org.jkiss.dbeaver.model.data.DBDAttributeBinding)4 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)4 File (java.io.File)3 Reader (java.io.Reader)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 DBRRunnableWithProgress (org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress)3 DBDContentStorageLocal (org.jkiss.dbeaver.model.data.DBDContentStorageLocal)2 DBCException (org.jkiss.dbeaver.model.exec.DBCException)2 StringContentStorage (org.jkiss.dbeaver.model.impl.StringContentStorage)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Date (java.util.Date)1 Action (org.eclipse.jface.action.Action)1 Separator (org.eclipse.jface.action.Separator)1 Nullable (org.jkiss.code.Nullable)1 DBPNamedObject (org.jkiss.dbeaver.model.DBPNamedObject)1