Search in sources :

Example 16 with DBPProject

use of org.jkiss.dbeaver.model.app.DBPProject in project dbeaver by serge-rider.

the class SQLEditor method createPartControl.

@Override
public void createPartControl(Composite parent) {
    setRangeIndicator(new DefaultRangeIndicator());
    // divides editor area and results/panels area
    resultsSash = UIUtils.createPartDivider(this, parent, resultSetOrientation.getSashOrientation() | SWT.SMOOTH);
    CSSUtils.setCSSClass(resultsSash, DBStyles.COLORED_BY_CONNECTION_TYPE);
    resultsSash.setSashWidth(5);
    UIUtils.setHelp(resultsSash, IHelpContextIds.CTX_SQL_EDITOR);
    Composite editorContainer;
    sqlEditorPanel = UIUtils.createPlaceholder(resultsSash, 3, 0);
    // Create left vertical toolbar
    createControlsBar(sqlEditorPanel);
    // Create editor presentations sash
    Composite pPlaceholder = null;
    if (extraPresentationDescriptor != null) {
        presentationSash = UIUtils.createPartDivider(this, sqlEditorPanel, ((resultSetOrientation.getSashOrientation() == SWT.VERTICAL) ? SWT.HORIZONTAL : SWT.VERTICAL) | SWT.SMOOTH);
        presentationSash.setSashWidth(5);
        presentationSash.setLayoutData(new GridData(GridData.FILL_BOTH));
        editorContainer = presentationSash;
        pPlaceholder = new Composite(presentationSash, SWT.NONE);
        pPlaceholder.setLayout(new FillLayout());
    } else {
        editorContainer = sqlEditorPanel;
    }
    super.createPartControl(editorContainer);
    getEditorControlWrapper().setLayoutData(new GridData(GridData.FILL_BOTH));
    // Create right vertical toolbar
    createPresentationSwitchBar(sqlEditorPanel);
    if (pPlaceholder != null) {
        switch(extraPresentationDescriptor.getActivationType()) {
            case HIDDEN:
                presentationSash.setMaximizedControl(presentationSash.getChildren()[SQL_EDITOR_CONTROL_INDEX]);
                break;
            case MAXIMIZED:
            case VISIBLE:
                extraPresentation.createPresentation(pPlaceholder, this);
                if (extraPresentationDescriptor.getActivationType() == SQLEditorPresentation.ActivationType.MAXIMIZED) {
                    if (presentationSash.getChildren()[EXTRA_CONTROL_INDEX] != null) {
                        presentationSash.setMaximizedControl(pPlaceholder);
                    }
                }
                break;
        }
    }
    getSite().setSelectionProvider(new DynamicSelectionProvider());
    DBPProject project = getProject();
    if (project != null && project.isRegistryLoaded()) {
        createResultTabs();
    } else {
        UIExecutionQueue.queueExec(this::createResultTabs);
    }
    setAction(ITextEditorActionConstants.SHOW_INFORMATION, null);
    SourceViewer viewer = getViewer();
    if (viewer != null) {
        StyledText textWidget = viewer.getTextWidget();
        if (textWidget != null) {
            textWidget.addModifyListener(this::onTextChange);
            textWidget.addFocusListener(new FocusAdapter() {

                @Override
                public void focusGained(FocusEvent e) {
                    refreshActions();
                }
            });
        }
    }
    SQLEditorFeatures.SQL_EDITOR_OPEN.use();
    // Start output reader
    new ServerOutputReader().schedule();
    updateExecutionContext(null);
    // Update controls
    UIExecutionQueue.queueExec(this::onDataSourceChange);
}
Also used : DefaultRangeIndicator(org.eclipse.ui.texteditor.DefaultRangeIndicator) SourceViewer(org.eclipse.jface.text.source.SourceViewer) StyledText(org.eclipse.swt.custom.StyledText) GridData(org.eclipse.swt.layout.GridData) DBPProject(org.jkiss.dbeaver.model.app.DBPProject) FillLayout(org.eclipse.swt.layout.FillLayout) DefaultServerOutputReader(org.jkiss.dbeaver.model.impl.DefaultServerOutputReader)

Example 17 with DBPProject

use of org.jkiss.dbeaver.model.app.DBPProject in project dbeaver by serge-rider.

the class SQLEditor method getDataSourceFromContent.

private DBPDataSourceContainer getDataSourceFromContent() {
    DBPProject project = getProject();
    IDocument document = getDocument();
    if (document == null || document.getNumberOfLines() == 0) {
        return null;
    }
    try {
        IRegion region = document.getLineInformation(0);
        String line = document.get(region.getOffset(), region.getLength());
        Matcher matcher = EMBEDDED_BINDING_PREFIX_PATTERN.matcher(line);
        if (matcher.matches()) {
            String connSpec = matcher.group(1).trim();
            if (!CommonUtils.isEmpty(connSpec)) {
                final DBPDataSourceContainer dataSource = DataSourceUtils.getDataSourceBySpec(project, connSpec, null, true, false);
                if (dataSource != null) {
                    return dataSource;
                }
            }
        }
    } catch (Throwable e) {
        log.debug("Error extracting datasource info from script's content", e);
    }
    return null;
}
Also used : Matcher(java.util.regex.Matcher) DBPProject(org.jkiss.dbeaver.model.app.DBPProject)

Example 18 with DBPProject

use of org.jkiss.dbeaver.model.app.DBPProject in project dbeaver by serge-rider.

the class ProjectExportWizardPage method createControl.

@Override
public void createControl(Composite parent) {
    String outDir = DBWorkbench.getPlatform().getPreferenceStore().getString(PREF_PROJECTS_EXPORT_OUT_DIR);
    if (CommonUtils.isEmpty(outDir)) {
        outDir = RuntimeUtils.getUserHomeDir().getAbsolutePath();
    }
    Set<DBPProject> projectList = new LinkedHashSet<>();
    final ISelection selection = UIUtils.getActiveWorkbenchWindow().getActivePage().getSelection();
    if (selection != null && !selection.isEmpty() && selection instanceof IStructuredSelection) {
        for (Iterator<?> iter = ((IStructuredSelection) selection).iterator(); iter.hasNext(); ) {
            Object element = iter.next();
            IResource resource = RuntimeUtils.getObjectAdapter(element, IResource.class);
            if (resource != null) {
                projectList.add(DBWorkbench.getPlatform().getWorkspace().getProject(resource.getProject()));
            }
        }
    }
    if (projectList.isEmpty()) {
        DBPProject activeProject = DBWorkbench.getPlatform().getWorkspace().getActiveProject();
        if (activeProject != null) {
            projectList.add(activeProject);
        }
    }
    Composite placeholder = UIUtils.createPlaceholder(parent, 1);
    placeholder.setLayout(new GridLayout(1, false));
    // Project list
    projectsTable = new Table(placeholder, SWT.MULTI | SWT.CHECK | SWT.BORDER);
    GridData gd = new GridData(GridData.FILL_BOTH);
    projectsTable.setLayoutData(gd);
    projectsTable.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            updateState();
        }
    });
    for (DBPProject project : DBWorkbench.getPlatform().getWorkspace().getProjects()) {
        final TableItem item = new TableItem(projectsTable, SWT.NONE);
        item.setImage(DBeaverIcons.getImage(DBIcon.PROJECT));
        item.setText(project.getName());
        item.setData(project);
        if (projectList.contains(project)) {
            item.setChecked(true);
        }
    }
    final Composite fileNameGroup = UIUtils.createPlaceholder(placeholder, 2);
    fileNameGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    // $NON-NLS-2$
    fileNameText = UIUtils.createLabelText(fileNameGroup, CoreMessages.dialog_project_export_wizard_start_label_output_file, null);
    fileNameText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    fileNameText.addModifyListener(new ModifyListener() {

        @Override
        public void modifyText(ModifyEvent e) {
            if (!CommonUtils.equalObjects(fileNameText.getText(), getArchiveFileName(getProjectsToExport()))) {
                fileNameEdited = true;
            }
        }
    });
    // Output folder
    Composite generalSettings = UIUtils.createPlaceholder(placeholder, 3);
    generalSettings.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    {
        UIUtils.createControlLabel(generalSettings, CoreMessages.dialog_project_export_wizard_start_label_directory);
        directoryText = new Text(generalSettings, SWT.BORDER);
        directoryText.setText(outDir);
        directoryText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        directoryText.addModifyListener(new ModifyListener() {

            @Override
            public void modifyText(ModifyEvent e) {
                updateState();
            }
        });
        Button openFolder = new Button(generalSettings, SWT.PUSH);
        openFolder.setImage(DBeaverIcons.getImage(DBIcon.TREE_FOLDER));
        openFolder.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                DirectoryDialog dialog = new DirectoryDialog(getShell(), SWT.NONE);
                dialog.setMessage(CoreMessages.dialog_project_export_wizard_start_dialog_directory_message);
                dialog.setText(CoreMessages.dialog_project_export_wizard_start_dialog_directory_text);
                String directory = directoryText.getText();
                if (!CommonUtils.isEmpty(directory)) {
                    dialog.setFilterPath(directory);
                }
                directory = dialog.open();
                if (directory != null) {
                    directoryText.setText(directory);
                }
            }
        });
    }
    exportDriverCheck = UIUtils.createCheckbox(placeholder, CoreMessages.dialog_project_export_wizard_start_checkbox_libraries, false);
    gd = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
    gd.horizontalSpan = 3;
    exportDriverCheck.setLayoutData(gd);
    setControl(placeholder);
    updateState();
}
Also used : ModifyListener(org.eclipse.swt.events.ModifyListener) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) DBPProject(org.jkiss.dbeaver.model.app.DBPProject) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) GridLayout(org.eclipse.swt.layout.GridLayout) ModifyEvent(org.eclipse.swt.events.ModifyEvent) ISelection(org.eclipse.jface.viewers.ISelection) GridData(org.eclipse.swt.layout.GridData) SelectionEvent(org.eclipse.swt.events.SelectionEvent) IResource(org.eclipse.core.resources.IResource)

Example 19 with DBPProject

use of org.jkiss.dbeaver.model.app.DBPProject in project dbeaver by serge-rider.

the class DataSourceDescriptorManager method createNewObject.

@Override
public DataSourceDescriptor createNewObject(DBRProgressMonitor monitor, DBECommandContext commandContext, Object container, Object copyFrom, Map<String, Object> options) {
    if (copyFrom != null) {
        DataSourceDescriptor dsTpl = (DataSourceDescriptor) copyFrom;
        DBPDataSourceRegistry registry;
        DBPDataSourceFolder folder = null;
        if (container instanceof DataSourceRegistry) {
            registry = (DBPDataSourceRegistry) container;
        } else if (container instanceof DBPProject) {
            registry = ((DBPProject) container).getDataSourceRegistry();
        } else if (container instanceof DBPDataSourceFolder) {
            folder = (DBPDataSourceFolder) container;
            registry = folder.getDataSourceRegistry();
        } else {
            registry = dsTpl.getRegistry();
        }
        DataSourceDescriptor dataSource = new DataSourceDescriptor(registry, DataSourceDescriptor.generateNewId(dsTpl.getDriver()), dsTpl.getDriver(), new DBPConnectionConfiguration(dsTpl.getConnectionConfiguration()));
        dataSource.copyFrom(dsTpl);
        if (folder != null) {
            dataSource.setFolder(folder);
        } else if (dsTpl.getRegistry() == registry) {
            // Copy folder only if we copy in the same project
            dataSource.setFolder(dsTpl.getFolder());
        }
        // Generate new name
        String origName = dsTpl.getName();
        String newName = origName;
        for (int i = 0; ; i++) {
            if (registry.findDataSourceByName(newName) == null) {
                break;
            }
            newName = origName + " " + (i + 1);
        }
        dataSource.setName(newName);
        registry.addDataSource(dataSource);
    } else {
        UIUtils.asyncExec(() -> NewConnectionDialog.openNewConnectionDialog(UIUtils.getActiveWorkbenchWindow()));
    }
    return null;
}
Also used : DBPConnectionConfiguration(org.jkiss.dbeaver.model.connection.DBPConnectionConfiguration) DBPDataSourceRegistry(org.jkiss.dbeaver.model.app.DBPDataSourceRegistry) DBPDataSourceFolder(org.jkiss.dbeaver.model.DBPDataSourceFolder) DBPProject(org.jkiss.dbeaver.model.app.DBPProject) DBPDataSourceRegistry(org.jkiss.dbeaver.model.app.DBPDataSourceRegistry)

Example 20 with DBPProject

use of org.jkiss.dbeaver.model.app.DBPProject in project dbeaver by serge-rider.

the class ProjectExportWizard method exportProjects.

private void exportProjects(DBRProgressMonitor monitor, final ProjectExportData exportData) throws IOException, CoreException, InterruptedException {
    if (!exportData.getOutputFolder().exists()) {
        if (!exportData.getOutputFolder().mkdirs()) {
            // $NON-NLS-2$
            throw new IOException("Cannot create directory '" + exportData.getOutputFolder().getAbsolutePath() + "'");
        }
    }
    String archiveName = exportData.getArchiveFileName() + ExportConstants.ARCHIVE_FILE_EXT;
    File archiveFile = new File(exportData.getOutputFolder(), archiveName);
    FileOutputStream exportStream = new FileOutputStream(archiveFile);
    try {
        ByteArrayOutputStream metaBuffer = new ByteArrayOutputStream(10000);
        ZipOutputStream archiveStream = new ZipOutputStream(exportStream);
        // Start meta
        XMLBuilder meta = new XMLBuilder(metaBuffer, GeneralUtils.UTF8_ENCODING);
        meta.startElement(ExportConstants.TAG_ARCHIVE);
        meta.addAttribute(ExportConstants.ATTR_VERSION, ExportConstants.ARCHIVE_VERSION_CURRENT);
        exportData.initExport(DBWorkbench.getPlatform().getWorkspace(), meta, archiveStream);
        {
            // Export source info
            meta.startElement(ExportConstants.TAG_SOURCE);
            meta.addAttribute(ExportConstants.ATTR_TIME, System.currentTimeMillis());
            meta.addAttribute(ExportConstants.ATTR_ADDRESS, InetAddress.getLocalHost().getHostAddress());
            meta.addAttribute(ExportConstants.ATTR_HOST, InetAddress.getLocalHost().getHostName());
            meta.endElement();
        }
        Map<DBPProject, Integer> resCountMap = new HashMap<>();
        monitor.beginTask(CoreMessages.dialog_project_export_wizard_monitor_collect_info, exportData.getProjectsToExport().size());
        for (DBPProject project : exportData.getProjectsToExport()) {
            // Add used drivers to export data
            final DBPDataSourceRegistry dataSourceRegistry = project.getDataSourceRegistry();
            if (dataSourceRegistry != null) {
                for (DBPDataSourceContainer dataSourceDescriptor : dataSourceRegistry.getDataSources()) {
                    exportData.usedDrivers.add(dataSourceDescriptor.getDriver());
                }
            }
            resCountMap.put(project, getChildCount(exportData, project.getEclipseProject()));
            monitor.worked(1);
        }
        monitor.done();
        {
            // Export projects
            exportData.meta.startElement(ExportConstants.TAG_PROJECTS);
            for (DBPProject project : exportData.getProjectsToExport()) {
                monitor.beginTask(NLS.bind(CoreMessages.dialog_project_export_wizard_monitor_export_project, project.getName()), resCountMap.get(project));
                try {
                    exportProject(monitor, exportData, project.getEclipseProject());
                } finally {
                    monitor.done();
                }
            }
            exportData.meta.endElement();
        }
        if (exportData.isExportDrivers()) {
            // Export driver libraries
            Set<File> libFiles = new HashSet<>();
            Map<String, File> libPathMap = new HashMap<>();
            for (DBPDriver driver : exportData.usedDrivers) {
                for (DBPDriverLibrary fileDescriptor : driver.getDriverLibraries()) {
                    final File libraryFile = fileDescriptor.getLocalFile();
                    if (libraryFile != null && !fileDescriptor.isDisabled() && libraryFile.exists()) {
                        libFiles.add(libraryFile);
                        libPathMap.put(fileDescriptor.getPath(), libraryFile);
                    }
                }
            }
            if (!libFiles.isEmpty()) {
                monitor.beginTask(CoreMessages.dialog_project_export_wizard_monitor_export_libraries, libFiles.size());
                // $NON-NLS-1$
                final ZipEntry driversFolder = new ZipEntry(ExportConstants.DIR_DRIVERS + "/");
                // $NON-NLS-1$
                driversFolder.setComment("Database driver libraries");
                exportData.archiveStream.putNextEntry(driversFolder);
                exportData.archiveStream.closeEntry();
                exportData.meta.startElement(ExportConstants.TAG_LIBRARIES);
                Set<String> libFileNames = new HashSet<>();
                for (String libPath : libPathMap.keySet()) {
                    final File libFile = libPathMap.get(libPath);
                    // Check for file name duplications
                    final String libFileName = libFile.getName();
                    if (libFileNames.contains(libFileName)) {
                        // $NON-NLS-1$
                        log.warn("Duplicate driver library file name: " + libFileName);
                        continue;
                    }
                    libFileNames.add(libFileName);
                    monitor.subTask(libFileName);
                    exportData.meta.startElement(RegistryConstants.TAG_FILE);
                    exportData.meta.addAttribute(ExportConstants.ATTR_PATH, libPath);
                    // $NON-NLS-1$
                    exportData.meta.addAttribute(ExportConstants.ATTR_FILE, "drivers/" + libFileName);
                    exportData.meta.endElement();
                    // $NON-NLS-1$
                    final ZipEntry driverFile = new ZipEntry(ExportConstants.DIR_DRIVERS + "/" + libFileName);
                    // $NON-NLS-1$
                    driverFile.setComment("Driver library");
                    exportData.archiveStream.putNextEntry(driverFile);
                    try (InputStream is = new FileInputStream(libFile)) {
                        IOUtils.copyStream(is, exportData.archiveStream, COPY_BUFFER_SIZE);
                    }
                    exportData.archiveStream.closeEntry();
                    monitor.worked(1);
                }
                exportData.meta.endElement();
                monitor.done();
            }
        }
        // Add meta to archive
        {
            exportData.meta.endElement();
            exportData.meta.flush();
            archiveStream.putNextEntry(new ZipEntry(ExportConstants.META_FILENAME));
            archiveStream.write(metaBuffer.toByteArray());
            archiveStream.closeEntry();
        }
        // Finish archive creation
        archiveStream.finish();
    } finally {
        ContentUtils.close(exportStream);
    }
}
Also used : HashMap(java.util.HashMap) ZipEntry(java.util.zip.ZipEntry) DBPProject(org.jkiss.dbeaver.model.app.DBPProject) XMLBuilder(org.jkiss.utils.xml.XMLBuilder) DBPDriver(org.jkiss.dbeaver.model.connection.DBPDriver) ZipOutputStream(java.util.zip.ZipOutputStream) DBPDriverLibrary(org.jkiss.dbeaver.model.connection.DBPDriverLibrary) DBPDataSourceRegistry(org.jkiss.dbeaver.model.app.DBPDataSourceRegistry) IFile(org.eclipse.core.resources.IFile) DBPDataSourceContainer(org.jkiss.dbeaver.model.DBPDataSourceContainer) HashSet(java.util.HashSet)

Aggregations

DBPProject (org.jkiss.dbeaver.model.app.DBPProject)90 IFile (org.eclipse.core.resources.IFile)20 DBPDataSourceContainer (org.jkiss.dbeaver.model.DBPDataSourceContainer)18 DBException (org.jkiss.dbeaver.DBException)14 DBPDataSourceRegistry (org.jkiss.dbeaver.model.app.DBPDataSourceRegistry)14 IFolder (org.eclipse.core.resources.IFolder)8 DBPWorkspace (org.jkiss.dbeaver.model.app.DBPWorkspace)8 IResource (org.eclipse.core.resources.IResource)6 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)6 SelectionEvent (org.eclipse.swt.events.SelectionEvent)6 IEditorPart (org.eclipse.ui.IEditorPart)6 DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)6 GridData (org.eclipse.swt.layout.GridData)5 DBNNode (org.jkiss.dbeaver.model.navigator.DBNNode)5 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 ArrayList (java.util.ArrayList)4 CoreException (org.eclipse.core.runtime.CoreException)4 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)4 DBPImage (org.jkiss.dbeaver.model.DBPImage)4 Document (org.w3c.dom.Document)4