Search in sources :

Example 1 with SimpleDatabaseEditorContext

use of org.jkiss.dbeaver.ui.editors.SimpleDatabaseEditorContext in project dbeaver by serge-rider.

the class NavigatorHandlerAssociateScript method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    final Shell activeShell = HandlerUtil.getActiveShell(event);
    List<IFile> scripts = new ArrayList<>();
    final ISelection selection = HandlerUtil.getCurrentSelection(event);
    if (!selection.isEmpty() && selection instanceof IStructuredSelection) {
        for (Iterator iter = ((IStructuredSelection) selection).iterator(); iter.hasNext(); ) {
            final DBNNode node = RuntimeUtils.getObjectAdapter(iter.next(), DBNNode.class);
            if (node instanceof DBNResource) {
                IResource resource = ((DBNResource) node).getResource();
                if (resource instanceof IFile) {
                    scripts.add((IFile) resource);
                }
            }
        }
    }
    if (!scripts.isEmpty()) {
        SelectDataSourceDialog dialog = new SelectDataSourceDialog(activeShell, DBWorkbench.getPlatform().getWorkspace().getProject(scripts.get(0).getProject()), null);
        if (dialog.open() == IDialogConstants.CANCEL_ID) {
            return null;
        }
        DBPDataSourceContainer dataSource = dialog.getDataSource();
        for (IFile script : scripts) {
            EditorUtils.setFileDataSource(script, new SimpleDatabaseEditorContext(dataSource));
            DBNUtils.refreshNavigatorResource(script, dataSource);
        }
    }
    return null;
}
Also used : IFile(org.eclipse.core.resources.IFile) SimpleDatabaseEditorContext(org.jkiss.dbeaver.ui.editors.SimpleDatabaseEditorContext) DBNNode(org.jkiss.dbeaver.model.navigator.DBNNode) ArrayList(java.util.ArrayList) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) DBNResource(org.jkiss.dbeaver.model.navigator.DBNResource) Shell(org.eclipse.swt.widgets.Shell) ISelection(org.eclipse.jface.viewers.ISelection) Iterator(java.util.Iterator) SelectDataSourceDialog(org.jkiss.dbeaver.ui.navigator.dialogs.SelectDataSourceDialog) DBPDataSourceContainer(org.jkiss.dbeaver.model.DBPDataSourceContainer) IResource(org.eclipse.core.resources.IResource)

Example 2 with SimpleDatabaseEditorContext

use of org.jkiss.dbeaver.ui.editors.SimpleDatabaseEditorContext in project dbeaver by dbeaver.

the class ScriptsImportWizard method importScripts.

private int importScripts(DBRProgressMonitor monitor, ScriptsImportData importData) throws IOException, CoreException {
    List<Pattern> masks = new ArrayList<>();
    // $NON-NLS-1$
    StringTokenizer st = new StringTokenizer(importData.getFileMasks(), ",; ");
    while (st.hasMoreTokens()) {
        String mask = st.nextToken().trim();
        if (!CommonUtils.isEmpty(mask)) {
            // $NON-NLS-1$ //$NON-NLS-2$
            mask = mask.replace("*", ".*");
            masks.add(Pattern.compile(mask));
        }
    }
    List<File> filesToImport = new ArrayList<>();
    collectFiles(importData.getInputDir(), masks, filesToImport);
    if (filesToImport.isEmpty()) {
        return 0;
    }
    // Use null monitor for resource actions to not break our main monitor
    final IProgressMonitor nullMonitor = new NullProgressMonitor();
    // Import scripts
    int imported = filesToImport.size();
    monitor.beginTask(CoreMessages.dialog_scripts_import_wizard_monitor_import_scripts, imported);
    for (File file : filesToImport) {
        // Create dirs
        monitor.subTask(file.getName());
        List<File> path = new ArrayList<>();
        for (File parent = file.getParentFile(); !parent.equals(importData.getInputDir()); parent = parent.getParentFile()) {
            path.add(0, parent);
        }
        // Get target dir
        final IResource srcResource = importData.getImportDir().getResource();
        if (!(srcResource instanceof IFolder)) {
            // $NON-NLS-1$ //$NON-NLS-2$
            log.warn("Resource '" + srcResource + "' is not a folder");
            continue;
        }
        IFolder targetDir = (IFolder) srcResource;
        for (File folder : path) {
            targetDir = targetDir.getFolder(folder.getName());
            if (!targetDir.exists()) {
                targetDir.create(true, true, nullMonitor);
            }
        }
        String targetName = file.getName();
        if (!targetName.toLowerCase().endsWith("." + SQLEditorUtils.SCRIPT_FILE_EXTENSION)) {
            // $NON-NLS-1$
            // $NON-NLS-1$
            targetName += "." + SQLEditorUtils.SCRIPT_FILE_EXTENSION;
        }
        final IFile targetFile = targetDir.getFile(targetName);
        if (targetFile.exists()) {
            if (importData.isOverwriteFiles()) {
                // $NON-NLS-1$ //$NON-NLS-2$
                log.warn("Overwriting file '" + targetFile.getFullPath() + "'");
                targetFile.delete(true, true, RuntimeUtils.getNestedMonitor(monitor));
            } else {
                // $NON-NLS-1$ //$NON-NLS-2$
                log.warn("File '" + targetFile.getFullPath() + "' already exists - skipped");
                imported--;
                continue;
            }
        }
        // Copy file
        try (FileInputStream in = new FileInputStream(file)) {
            targetFile.create(in, true, nullMonitor);
        }
        // Set datasource
        if (importData.getDataSourceContainer() != null) {
            EditorUtils.setFileDataSource(targetFile, new SimpleDatabaseEditorContext(importData.getDataSourceContainer()));
        }
        // Done
        monitor.worked(1);
    }
    monitor.done();
    return imported;
}
Also used : Pattern(java.util.regex.Pattern) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IFile(org.eclipse.core.resources.IFile) SimpleDatabaseEditorContext(org.jkiss.dbeaver.ui.editors.SimpleDatabaseEditorContext) ArrayList(java.util.ArrayList) FileInputStream(java.io.FileInputStream) StringTokenizer(java.util.StringTokenizer) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IFile(org.eclipse.core.resources.IFile) File(java.io.File) IResource(org.eclipse.core.resources.IResource) IFolder(org.eclipse.core.resources.IFolder)

Example 3 with SimpleDatabaseEditorContext

use of org.jkiss.dbeaver.ui.editors.SimpleDatabaseEditorContext in project dbeaver by serge-rider.

the class ScriptsImportWizard method importScripts.

private int importScripts(DBRProgressMonitor monitor, ScriptsImportData importData) throws IOException, CoreException {
    List<Pattern> masks = new ArrayList<>();
    // $NON-NLS-1$
    StringTokenizer st = new StringTokenizer(importData.getFileMasks(), ",; ");
    while (st.hasMoreTokens()) {
        String mask = st.nextToken().trim();
        if (!CommonUtils.isEmpty(mask)) {
            // $NON-NLS-1$ //$NON-NLS-2$
            mask = mask.replace("*", ".*");
            masks.add(Pattern.compile(mask));
        }
    }
    List<File> filesToImport = new ArrayList<>();
    collectFiles(importData.getInputDir(), masks, filesToImport);
    if (filesToImport.isEmpty()) {
        return 0;
    }
    // Use null monitor for resource actions to not break our main monitor
    final IProgressMonitor nullMonitor = new NullProgressMonitor();
    // Import scripts
    int imported = filesToImport.size();
    monitor.beginTask(CoreMessages.dialog_scripts_import_wizard_monitor_import_scripts, imported);
    for (File file : filesToImport) {
        // Create dirs
        monitor.subTask(file.getName());
        List<File> path = new ArrayList<>();
        for (File parent = file.getParentFile(); !parent.equals(importData.getInputDir()); parent = parent.getParentFile()) {
            path.add(0, parent);
        }
        // Get target dir
        final IResource srcResource = importData.getImportDir().getResource();
        if (!(srcResource instanceof IFolder)) {
            // $NON-NLS-1$ //$NON-NLS-2$
            log.warn("Resource '" + srcResource + "' is not a folder");
            continue;
        }
        IFolder targetDir = (IFolder) srcResource;
        for (File folder : path) {
            targetDir = targetDir.getFolder(folder.getName());
            if (!targetDir.exists()) {
                targetDir.create(true, true, nullMonitor);
            }
        }
        String targetName = file.getName();
        if (!targetName.toLowerCase().endsWith("." + SQLEditorUtils.SCRIPT_FILE_EXTENSION)) {
            // $NON-NLS-1$
            // $NON-NLS-1$
            targetName += "." + SQLEditorUtils.SCRIPT_FILE_EXTENSION;
        }
        final IFile targetFile = targetDir.getFile(targetName);
        if (targetFile.exists()) {
            if (importData.isOverwriteFiles()) {
                // $NON-NLS-1$ //$NON-NLS-2$
                log.warn("Overwriting file '" + targetFile.getFullPath() + "'");
                targetFile.delete(true, true, RuntimeUtils.getNestedMonitor(monitor));
            } else {
                // $NON-NLS-1$ //$NON-NLS-2$
                log.warn("File '" + targetFile.getFullPath() + "' already exists - skipped");
                imported--;
                continue;
            }
        }
        // Copy file
        try (FileInputStream in = new FileInputStream(file)) {
            targetFile.create(in, true, nullMonitor);
        }
        // Set datasource
        if (importData.getDataSourceContainer() != null) {
            EditorUtils.setFileDataSource(targetFile, new SimpleDatabaseEditorContext(importData.getDataSourceContainer()));
        }
        // Done
        monitor.worked(1);
    }
    monitor.done();
    return imported;
}
Also used : Pattern(java.util.regex.Pattern) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IFile(org.eclipse.core.resources.IFile) SimpleDatabaseEditorContext(org.jkiss.dbeaver.ui.editors.SimpleDatabaseEditorContext) ArrayList(java.util.ArrayList) FileInputStream(java.io.FileInputStream) StringTokenizer(java.util.StringTokenizer) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IFile(org.eclipse.core.resources.IFile) File(java.io.File) IResource(org.eclipse.core.resources.IResource) IFolder(org.eclipse.core.resources.IFolder)

Example 4 with SimpleDatabaseEditorContext

use of org.jkiss.dbeaver.ui.editors.SimpleDatabaseEditorContext in project dbeaver by dbeaver.

the class NavigatorHandlerAssociateScript method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    final Shell activeShell = HandlerUtil.getActiveShell(event);
    List<IFile> scripts = new ArrayList<>();
    final ISelection selection = HandlerUtil.getCurrentSelection(event);
    if (!selection.isEmpty() && selection instanceof IStructuredSelection) {
        for (Iterator iter = ((IStructuredSelection) selection).iterator(); iter.hasNext(); ) {
            final DBNNode node = RuntimeUtils.getObjectAdapter(iter.next(), DBNNode.class);
            if (node instanceof DBNResource) {
                IResource resource = ((DBNResource) node).getResource();
                if (resource instanceof IFile) {
                    scripts.add((IFile) resource);
                }
            }
        }
    }
    if (!scripts.isEmpty()) {
        SelectDataSourceDialog dialog = new SelectDataSourceDialog(activeShell, DBWorkbench.getPlatform().getWorkspace().getProject(scripts.get(0).getProject()), null);
        if (dialog.open() == IDialogConstants.CANCEL_ID) {
            return null;
        }
        DBPDataSourceContainer dataSource = dialog.getDataSource();
        for (IFile script : scripts) {
            EditorUtils.setFileDataSource(script, new SimpleDatabaseEditorContext(dataSource));
            DBNUtils.refreshNavigatorResource(script, dataSource);
        }
    }
    return null;
}
Also used : IFile(org.eclipse.core.resources.IFile) SimpleDatabaseEditorContext(org.jkiss.dbeaver.ui.editors.SimpleDatabaseEditorContext) DBNNode(org.jkiss.dbeaver.model.navigator.DBNNode) ArrayList(java.util.ArrayList) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) DBNResource(org.jkiss.dbeaver.model.navigator.DBNResource) Shell(org.eclipse.swt.widgets.Shell) ISelection(org.eclipse.jface.viewers.ISelection) Iterator(java.util.Iterator) SelectDataSourceDialog(org.jkiss.dbeaver.ui.navigator.dialogs.SelectDataSourceDialog) DBPDataSourceContainer(org.jkiss.dbeaver.model.DBPDataSourceContainer) IResource(org.eclipse.core.resources.IResource)

Aggregations

ArrayList (java.util.ArrayList)4 IFile (org.eclipse.core.resources.IFile)4 IResource (org.eclipse.core.resources.IResource)4 SimpleDatabaseEditorContext (org.jkiss.dbeaver.ui.editors.SimpleDatabaseEditorContext)4 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 Iterator (java.util.Iterator)2 StringTokenizer (java.util.StringTokenizer)2 Pattern (java.util.regex.Pattern)2 IFolder (org.eclipse.core.resources.IFolder)2 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)2 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)2 ISelection (org.eclipse.jface.viewers.ISelection)2 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)2 Shell (org.eclipse.swt.widgets.Shell)2 DBPDataSourceContainer (org.jkiss.dbeaver.model.DBPDataSourceContainer)2 DBNNode (org.jkiss.dbeaver.model.navigator.DBNNode)2 DBNResource (org.jkiss.dbeaver.model.navigator.DBNResource)2 SelectDataSourceDialog (org.jkiss.dbeaver.ui.navigator.dialogs.SelectDataSourceDialog)2