Search in sources :

Example 11 with DBNResource

use of org.jkiss.dbeaver.model.navigator.DBNResource in project dbeaver by serge-rider.

the class ScriptsImportWizardPage method createControl.

@Override
public void createControl(Composite parent) {
    String externalDir = DBeaverCore.getGlobalPreferenceStore().getString(ScriptsExportWizardPage.PREF_SCRIPTS_EXPORT_OUT_DIR);
    if (CommonUtils.isEmpty(externalDir)) {
        externalDir = RuntimeUtils.getUserHomeDir().getAbsolutePath();
    }
    Composite placeholder = UIUtils.createPlaceholder(parent, 1);
    placeholder.setLayout(new GridLayout(1, false));
    // Input settings
    Composite generalSettings = UIUtils.createPlaceholder(placeholder, 3);
    generalSettings.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    {
        UIUtils.createControlLabel(generalSettings, CoreMessages.dialog_scripts_import_wizard_label_input_directory);
        directoryText = new Text(generalSettings, SWT.BORDER);
        directoryText.setText(externalDir);
        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_scripts_import_wizard_dialog_choose_dir_message);
                dialog.setText(CoreMessages.dialog_scripts_import_wizard_dialog_choose_dir_text);
                String directory = directoryText.getText();
                if (!CommonUtils.isEmpty(directory)) {
                    dialog.setFilterPath(directory);
                }
                directory = dialog.open();
                if (directory != null) {
                    directoryText.setText(directory);
                }
            }
        });
        //$NON-NLS-2$
        extensionsText = UIUtils.createLabelText(generalSettings, CoreMessages.dialog_scripts_import_wizard_label_file_mask, "*.sql,*.txt");
        GridData gd = new GridData(GridData.FILL_HORIZONTAL);
        gd.horizontalSpan = 2;
        extensionsText.setLayoutData(gd);
        UIUtils.createControlLabel(generalSettings, CoreMessages.dialog_scripts_import_wizard_label_default_connection);
        scriptsDataSources = new CSmartCombo<>(generalSettings, SWT.BORDER | SWT.DROP_DOWN | SWT.READ_ONLY, new ConnectionLabelProvider());
        for (DataSourceDescriptor dataSourceDescriptor : DataSourceRegistry.getAllDataSources()) {
            scriptsDataSources.addItem(dataSourceDescriptor);
        }
        if (scriptsDataSources.getItemCount() > 0) {
            scriptsDataSources.select(0);
        }
        gd = new GridData(GridData.FILL_HORIZONTAL);
        gd.horizontalSpan = 2;
        gd.verticalIndent = 2;
        scriptsDataSources.setLayoutData(gd);
    }
    UIUtils.createControlLabel(placeholder, CoreMessages.dialog_scripts_import_wizard_label_root_folder);
    importRoot = DBeaverCore.getInstance().getNavigatorModel().getRoot();
    final DatabaseNavigatorTree scriptsNavigator = new DatabaseNavigatorTree(placeholder, importRoot, SWT.BORDER | SWT.SINGLE, false);
    scriptsNavigator.setLayoutData(new GridData(GridData.FILL_BOTH));
    scriptsNavigator.getViewer().addSelectionChangedListener(new ISelectionChangedListener() {

        @Override
        public void selectionChanged(SelectionChangedEvent event) {
            IStructuredSelection sel = (IStructuredSelection) event.getSelection();
            if (sel == null || sel.isEmpty()) {
                importRoot = null;
            } else {
                importRoot = (DBNNode) sel.getFirstElement();
            }
            updateState();
        }
    });
    scriptsNavigator.getViewer().addFilter(new ViewerFilter() {

        @Override
        public boolean select(Viewer viewer, Object parentElement, Object element) {
            return element instanceof DBNResource && ((DBNResource) element).getResource() instanceof IContainer;
        }
    });
    scriptsNavigator.getViewer().expandToLevel(2);
    overwriteCheck = UIUtils.createCheckbox(placeholder, CoreMessages.dialog_project_export_wizard_page_checkbox_overwrite_files, false);
    GridData gd = new GridData(GridData.BEGINNING);
    gd.horizontalSpan = 3;
    overwriteCheck.setLayoutData(gd);
    setControl(placeholder);
    updateState();
}
Also used : ModifyListener(org.eclipse.swt.events.ModifyListener) DBNNode(org.jkiss.dbeaver.model.navigator.DBNNode) GridLayout(org.eclipse.swt.layout.GridLayout) ModifyEvent(org.eclipse.swt.events.ModifyEvent) CSmartCombo(org.jkiss.dbeaver.ui.controls.CSmartCombo) Button(org.eclipse.swt.widgets.Button) SelectionEvent(org.eclipse.swt.events.SelectionEvent) IContainer(org.eclipse.core.resources.IContainer) DirectoryDialog(org.eclipse.swt.widgets.DirectoryDialog) DataSourceDescriptor(org.jkiss.dbeaver.registry.DataSourceDescriptor) Composite(org.eclipse.swt.widgets.Composite) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) Text(org.eclipse.swt.widgets.Text) DatabaseNavigatorTree(org.jkiss.dbeaver.ui.navigator.database.DatabaseNavigatorTree) DBNResource(org.jkiss.dbeaver.model.navigator.DBNResource) GridData(org.eclipse.swt.layout.GridData)

Example 12 with DBNResource

use of org.jkiss.dbeaver.model.navigator.DBNResource in project dbeaver by serge-rider.

the class NavigatorAdapterFactory method getAdapter.

@Override
public <T> T getAdapter(Object adaptableObject, Class<T> adapterType) {
    if (adapterType == DBPDataSourceContainer.class) {
        if (adaptableObject instanceof DBNDataSource) {
            return adapterType.cast(((DBNDataSource) adaptableObject).getDataSourceContainer());
        }
        DBSObject object = DBUtils.getFromObject(adaptableObject);
        if (object == null) {
            return null;
        }
        if (object instanceof DBPDataSourceContainer) {
            return adapterType.cast(object);
        }
        DBPDataSource dataSource = object.getDataSource();
        return dataSource == null ? null : adapterType.cast(dataSource.getContainer());
    } else if (DBPObject.class.isAssignableFrom(adapterType)) {
        DBPObject object = null;
        if (adaptableObject instanceof DBSWrapper) {
            object = ((DBSWrapper) adaptableObject).getObject();
        } else if (adaptableObject instanceof DBPObject) {
            object = (DBPObject) adaptableObject;
        }
        if (object != null && adapterType.isAssignableFrom(object.getClass())) {
            return adapterType.cast(object);
        }
    } else if (IResource.class.isAssignableFrom(adapterType)) {
        if (adaptableObject instanceof DBNResource) {
            return ((DBNResource) adaptableObject).getAdapter(adapterType);
        }
    } else if (adapterType == IPropertySource.class) {
        DBPObject dbObject = null;
        if (adaptableObject instanceof DBSWrapper) {
            dbObject = ((DBSWrapper) adaptableObject).getObject();
        } else if (adaptableObject instanceof DBPObject) {
            dbObject = (DBPObject) adaptableObject;
        }
        if (dbObject instanceof IPropertySource) {
            return adapterType.cast(dbObject);
        }
        if (dbObject instanceof DBPPropertySource) {
            return adapterType.cast(new PropertySourceDelegate((DBPPropertySource) dbObject));
        }
        if (dbObject instanceof IAdaptable) {
            Object adapter = ((IAdaptable) dbObject).getAdapter(IPropertySource.class);
            if (adapter != null) {
                return adapterType.cast(adapter);
            }
            adapter = ((IAdaptable) dbObject).getAdapter(DBPPropertySource.class);
            if (adapter != null) {
                return adapterType.cast(new PropertySourceDelegate((DBPPropertySource) adapter));
            }
        }
        if (dbObject != null) {
            PropertyCollector props = new PropertyCollector(adaptableObject, dbObject, true);
            props.collectProperties();
            if (props.isEmpty() && adaptableObject instanceof DBSObject) {
                // Add default properties
                DBSObject meta = (DBSObject) adaptableObject;
                //$NON-NLS-1$
                props.addProperty(null, DBConstants.PROP_ID_NAME, CoreMessages.model_navigator_Name, meta.getName());
                //$NON-NLS-1$
                props.addProperty(null, "desc", CoreMessages.model_navigator_Description, meta.getDescription());
            }
            return adapterType.cast(new PropertySourceDelegate(props));
        }
    } else if (adapterType == IWorkbenchAdapter.class) {
        // Workbench adapter
        if (adaptableObject instanceof DBNNode) {
            final DBNNode node = (DBNNode) adaptableObject;
            WorkbenchAdapter workbenchAdapter = new WorkbenchAdapter() {

                @Override
                public ImageDescriptor getImageDescriptor(Object object) {
                    return DBeaverIcons.getImageDescriptor(node.getNodeIconDefault());
                }

                @Override
                public String getLabel(Object o) {
                    return node.getNodeName();
                }

                @Override
                public Object getParent(Object o) {
                    return node.getParentNode();
                }
            };
            return adapterType.cast(workbenchAdapter);
        } else {
            return null;
        }
    }
    return null;
}
Also used : IAdaptable(org.eclipse.core.runtime.IAdaptable) PropertySourceDelegate(org.jkiss.dbeaver.ui.properties.PropertySourceDelegate) DBSWrapper(org.jkiss.dbeaver.model.struct.DBSWrapper) DBNNode(org.jkiss.dbeaver.model.navigator.DBNNode) DBNDataSource(org.jkiss.dbeaver.model.navigator.DBNDataSource) DBPPropertySource(org.jkiss.dbeaver.model.preferences.DBPPropertySource) PropertyCollector(org.jkiss.dbeaver.runtime.properties.PropertyCollector) IPropertySource(org.eclipse.ui.views.properties.IPropertySource) WorkbenchAdapter(org.eclipse.ui.model.WorkbenchAdapter) IWorkbenchAdapter(org.eclipse.ui.model.IWorkbenchAdapter) DBNResource(org.jkiss.dbeaver.model.navigator.DBNResource) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject)

Aggregations

DBNResource (org.jkiss.dbeaver.model.navigator.DBNResource)12 DBNNode (org.jkiss.dbeaver.model.navigator.DBNNode)5 ISelection (org.eclipse.jface.viewers.ISelection)4 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)4 IFile (org.eclipse.core.resources.IFile)3 IFolder (org.eclipse.core.resources.IFolder)3 IResource (org.eclipse.core.resources.IResource)3 DirectoryDialog (org.eclipse.swt.widgets.DirectoryDialog)3 ArrayList (java.util.ArrayList)2 IContainer (org.eclipse.core.resources.IContainer)2 ModifyEvent (org.eclipse.swt.events.ModifyEvent)2 ModifyListener (org.eclipse.swt.events.ModifyListener)2 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)2 SelectionEvent (org.eclipse.swt.events.SelectionEvent)2 GridData (org.eclipse.swt.layout.GridData)2 GridLayout (org.eclipse.swt.layout.GridLayout)2 Button (org.eclipse.swt.widgets.Button)2 Composite (org.eclipse.swt.widgets.Composite)2 Shell (org.eclipse.swt.widgets.Shell)2 Text (org.eclipse.swt.widgets.Text)2