use of org.jkiss.dbeaver.model.navigator.DBNNode in project dbeaver by serge-rider.
the class DatabaseConsumerPageMapping method createControl.
@Override
public void createControl(Composite parent) {
initializeDialogUnits(parent);
final DatabaseConsumerSettings settings = getWizard().getPageSettings(this, DatabaseConsumerSettings.class);
Composite composite = new Composite(parent, SWT.NULL);
GridLayout gl = new GridLayout();
gl.marginHeight = 0;
gl.marginWidth = 0;
composite.setLayout(gl);
composite.setLayoutData(new GridData(GridData.FILL_BOTH));
{
// Target container
Composite containerPanel = new Composite(composite, SWT.NONE);
containerPanel.setLayout(new GridLayout(4, false));
containerPanel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
UIUtils.createControlLabel(containerPanel, "Target container");
containerIcon = new Label(containerPanel, SWT.NONE);
containerIcon.setImage(DBeaverIcons.getImage(DBIcon.TYPE_UNKNOWN));
containerName = new Text(containerPanel, SWT.BORDER | SWT.READ_ONLY);
containerName.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
containerName.setText("");
Button browseButton = new Button(containerPanel, SWT.PUSH);
browseButton.setImage(DBeaverIcons.getImage(DBIcon.TREE_FOLDER));
browseButton.setText("...");
browseButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
IProject activeProject = DBeaverCore.getInstance().getProjectRegistry().getActiveProject();
if (activeProject != null) {
final DBNModel navigatorModel = DBeaverCore.getInstance().getNavigatorModel();
final DBNProject rootNode = navigatorModel.getRoot().getProject(activeProject);
DBNNode selectedNode = settings.getContainerNode();
if (selectedNode == null && !settings.getDataMappings().isEmpty()) {
// Use first source object as cur selection (it's better than nothing)
DBSDataContainer firstSource = settings.getDataMappings().keySet().iterator().next();
selectedNode = navigatorModel.getNodeByObject(firstSource);
while (selectedNode != null) {
if (selectedNode instanceof DBSWrapper && ((DBSWrapper) selectedNode).getObject() instanceof DBSObjectContainer) {
break;
} else {
selectedNode = selectedNode.getParentNode();
}
}
}
DBNNode node = BrowseObjectDialog.selectObject(getShell(), "Choose container", rootNode.getDatabases(), selectedNode, new Class[] { DBSObjectContainer.class }, null);
if (node instanceof DBNDatabaseNode) {
settings.setContainerNode((DBNDatabaseNode) node);
containerIcon.setImage(DBeaverIcons.getImage(node.getNodeIconDefault()));
containerName.setText(settings.getContainerFullName());
// Reset mappings
for (DatabaseMappingContainer mappingContainer : settings.getDataMappings().values()) {
if (mappingContainer.getMappingType() == DatabaseMappingType.create) {
try {
mappingContainer.refreshMappingType(getContainer(), DatabaseMappingType.create);
} catch (DBException e1) {
log.error(e1);
}
}
}
mappingViewer.refresh();
updatePageCompletion();
}
}
}
});
}
createMappingsTree(composite);
{
// Control buttons
Composite buttonsPanel = new Composite(composite, SWT.NONE);
buttonsPanel.setLayout(new GridLayout(4, false));
buttonsPanel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
final Button mapTableButton = new Button(buttonsPanel, SWT.PUSH);
mapTableButton.setImage(DBeaverIcons.getImage(DBIcon.TREE_TABLE));
mapTableButton.setText("Existing table ...");
mapTableButton.setEnabled(false);
mapTableButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
mapExistingTable((DatabaseMappingContainer) getSelectedMapping());
}
});
final Button createNewButton = new Button(buttonsPanel, SWT.PUSH);
createNewButton.setImage(DBeaverIcons.getImage(DBIcon.TREE_VIEW));
createNewButton.setText("New table...");
createNewButton.setEnabled(false);
createNewButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
mapNewTable((DatabaseMappingContainer) getSelectedMapping());
}
});
final Button columnsButton = new Button(buttonsPanel, SWT.PUSH);
columnsButton.setImage(DBeaverIcons.getImage(DBIcon.TREE_COLUMNS));
columnsButton.setText("Columns' mappings ...");
columnsButton.setEnabled(false);
columnsButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
DatabaseMappingObject selectedMapping = getSelectedMapping();
mapColumns(selectedMapping instanceof DatabaseMappingContainer ? (DatabaseMappingContainer) selectedMapping : ((DatabaseMappingAttribute) selectedMapping).getParent());
}
});
final Button ddlButton = new Button(buttonsPanel, SWT.PUSH);
ddlButton.setImage(DBeaverIcons.getImage(UIIcon.SQL_TEXT));
ddlButton.setText("DDL ...");
ddlButton.setEnabled(false);
ddlButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
DatabaseMappingObject selectedMapping = getSelectedMapping();
showDDL(selectedMapping instanceof DatabaseMappingContainer ? (DatabaseMappingContainer) selectedMapping : ((DatabaseMappingAttribute) selectedMapping).getParent());
}
});
mappingViewer.getTree().addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e) {
try {
boolean updated = false;
Object element = null;
if (e.character == SWT.CR) {
//Object element = mappingViewer.getStructuredSelection().getFirstElement();
//mappingViewer.editElement(element, 1);
} else if (e.character == SWT.DEL) {
for (TreeItem item : mappingViewer.getTree().getSelection()) {
element = item.getData();
if (element instanceof DatabaseMappingAttribute) {
DatabaseMappingAttribute attribute = (DatabaseMappingAttribute) element;
attribute.setMappingType(DatabaseMappingType.skip);
} else if (element instanceof DatabaseMappingContainer) {
DatabaseMappingContainer container = (DatabaseMappingContainer) element;
container.refreshMappingType(getContainer(), DatabaseMappingType.skip);
}
}
updated = true;
} else if (e.character == SWT.SPACE) {
for (TreeItem item : mappingViewer.getTree().getSelection()) {
element = item.getData();
if (element instanceof DatabaseMappingAttribute) {
DatabaseMappingAttribute attribute = (DatabaseMappingAttribute) item.getData();
attribute.setMappingType(DatabaseMappingType.existing);
attribute.updateMappingType(VoidProgressMonitor.INSTANCE);
} else if (element instanceof DatabaseMappingContainer) {
DatabaseMappingContainer container = (DatabaseMappingContainer) element;
setMappingTarget(container, container.getSource().getName());
}
}
updated = true;
}
if (updated) {
mappingViewer.refresh();
updatePageCompletion();
if (element instanceof DatabaseMappingContainer) {
// Select next container
@SuppressWarnings("unchecked") List<DatabaseMappingContainer> model = (List<DatabaseMappingContainer>) mappingViewer.getInput();
int curIndex = model.indexOf(element);
if (curIndex < model.size() - 1) {
mappingViewer.setSelection(new StructuredSelection(model.get(curIndex + 1)), true);
}
}
}
} catch (DBException e1) {
UIUtils.showDatabaseError(getShell(), "Mapping error", "Error mapping table", e1);
}
}
});
mappingViewer.addSelectionChangedListener(new ISelectionChangedListener() {
@Override
public void selectionChanged(SelectionChangedEvent event) {
DatabaseMappingObject mapping = getSelectedMapping();
mapTableButton.setEnabled(mapping instanceof DatabaseMappingContainer);
createNewButton.setEnabled(mapping instanceof DatabaseMappingContainer && settings.getContainerNode() != null);
final boolean hasMappings = (mapping instanceof DatabaseMappingContainer && mapping.getMappingType() != DatabaseMappingType.unspecified) || (mapping instanceof DatabaseMappingAttribute && ((DatabaseMappingAttribute) mapping).getParent().getMappingType() != DatabaseMappingType.unspecified);
columnsButton.setEnabled(hasMappings);
ddlButton.setEnabled(hasMappings);
}
});
mappingViewer.addDoubleClickListener(new IDoubleClickListener() {
@Override
public void doubleClick(DoubleClickEvent event) {
DatabaseMappingObject selectedMapping = getSelectedMapping();
if (selectedMapping != null) {
if (selectedMapping instanceof DatabaseMappingContainer) {
/*
if (selectedMapping.getMappingType() == DatabaseMappingType.unspecified) {
mapExistingTable((DatabaseMappingContainer) selectedMapping);
} else {
mapColumns((DatabaseMappingContainer) selectedMapping);
}
*/
}
}
}
});
}
{
Composite hintPanel = new Composite(composite, SWT.NONE);
hintPanel.setLayout(new GridLayout(3, false));
hintPanel.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING));
new Label(hintPanel, SWT.NONE).setText("* DEL - skip column(s) SPACE - map column(s)");
}
setControl(composite);
}
use of org.jkiss.dbeaver.model.navigator.DBNNode in project dbeaver by serge-rider.
the class DatabaseConsumerSettings method loadNode.
public void loadNode(IRunnableContext runnableContext) {
if (containerNode == null && !CommonUtils.isEmpty(containerNodePath)) {
if (!CommonUtils.isEmpty(containerNodePath)) {
try {
runnableContext.run(true, true, new IRunnableWithProgress() {
@Override
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
try {
DBNNode node = DBeaverCore.getInstance().getNavigatorModel().getNodeByPath(new DefaultProgressMonitor(monitor), containerNodePath);
if (node instanceof DBNDatabaseNode) {
containerNode = (DBNDatabaseNode) node;
}
} catch (DBException e) {
throw new InvocationTargetException(e);
}
}
});
checkContainerConnection(runnableContext);
} catch (InvocationTargetException e) {
log.error("Error getting container node", e.getTargetException());
} catch (InterruptedException e) {
// ignore
}
}
}
}
use of org.jkiss.dbeaver.model.navigator.DBNNode 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();
}
use of org.jkiss.dbeaver.model.navigator.DBNNode in project dbeaver by serge-rider.
the class DatabaseConsumerPageMapping method mapExistingTable.
private void mapExistingTable(DatabaseMappingContainer mapping) {
final DatabaseConsumerSettings settings = getWizard().getPageSettings(this, DatabaseConsumerSettings.class);
IProject activeProject = DBeaverCore.getInstance().getProjectRegistry().getActiveProject();
if (activeProject != null) {
DBNNode rootNode = settings.getContainerNode();
if (rootNode == null) {
rootNode = DBeaverCore.getInstance().getNavigatorModel().getRoot().getProject(activeProject).getDatabases();
}
DBNNode selectedNode = rootNode;
if (mapping.getTarget() != null) {
selectedNode = DBeaverCore.getInstance().getNavigatorModel().getNodeByObject(mapping.getTarget());
}
DBNNode node = BrowseObjectDialog.selectObject(getShell(), "Choose target table", rootNode, selectedNode, new Class[] { DBSObjectContainer.class, DBSDataManipulator.class }, new Class[] { DBSDataManipulator.class });
if (node != null && node instanceof DBSWrapper) {
DBSObject object = ((DBSWrapper) node).getObject();
try {
if (object instanceof DBSDataManipulator) {
mapping.setTarget((DBSDataManipulator) object);
mapping.refreshMappingType(getWizard().getContainer(), DatabaseMappingType.existing);
mapColumns(mapping);
} else {
mapping.setTarget(null);
mapping.refreshMappingType(getWizard().getContainer(), DatabaseMappingType.unspecified);
}
} catch (DBException e) {
UIUtils.showDatabaseError(getShell(), "Mapping error", "Error mapping table", e);
}
mappingViewer.refresh();
updatePageCompletion();
}
}
}
use of org.jkiss.dbeaver.model.navigator.DBNNode 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;
}
Aggregations