use of org.jkiss.dbeaver.model.DBPDataSourceFolder in project dbeaver by serge-rider.
the class SelectDataSourceDialog method createDialogArea.
@Override
protected Control createDialogArea(Composite parent) {
showConnected = getDialogBoundsSettings().getBoolean(PARAM_SHOW_CONNECTED);
showAllProjects = getDialogBoundsSettings().getBoolean(PARAM_SHOW_ALL_PROJECTS);
Composite group = (Composite) super.createDialogArea(parent);
GridData gd = new GridData(GridData.FILL_BOTH);
group.setLayoutData(gd);
rootNode = DBWorkbench.getPlatform().getNavigatorModel().getRoot();
projectNode = null;
if (project != null) {
DBNProject projectBaseNode = DBWorkbench.getPlatform().getNavigatorModel().getRoot().getProjectNode(project);
if (projectBaseNode != null) {
projectNode = projectBaseNode.getDatabases();
}
}
INavigatorFilter dsFilter = new INavigatorFilter() {
@Override
public boolean filterFolders() {
return true;
}
@Override
public boolean isLeafObject(Object object) {
return object instanceof DBNDataSource;
}
@Override
public boolean select(Object element) {
return element instanceof DBNProject || element instanceof DBNProjectDatabases || element instanceof DBNLocalFolder;
}
};
DatabaseNavigatorTree dataSourceTree = new DatabaseNavigatorTree(group, getTreeRootNode(), SWT.SINGLE | SWT.BORDER, false, dsFilter) {
@Override
protected void onTreeRefresh() {
DBNNode treeRootNode = getTreeRootNode();
if (dataSource != null && projectNode != null) {
DBPDataSourceFolder dsFolder;
for (dsFolder = dataSource.getFolder(); dsFolder != null; dsFolder = dsFolder.getParent()) {
if (dsFolder.getParent() == null) {
break;
}
}
if (dsFolder != null) {
// Expand only current DS folder
DBNLocalFolder folderNode = projectNode.getFolderNode(dsFolder);
if (folderNode != null) {
expandFolders(this, folderNode);
}
} else {
// Do not expand anything
}
return;
}
expandFolders(this, treeRootNode);
}
};
gd = new GridData(GridData.FILL_BOTH);
gd.heightHint = 500;
gd.minimumHeight = 100;
gd.minimumWidth = 100;
dataSourceTree.setLayoutData(gd);
final TreeViewer treeViewer = dataSourceTree.getViewer();
final Text descriptionText = new Text(group, SWT.READ_ONLY);
descriptionText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
final Button showConnectedCheck = new Button(group, SWT.CHECK);
showConnectedCheck.setText(UINavigatorMessages.label_show_connected);
showConnectedCheck.setSelection(showConnected);
showConnectedCheck.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
showConnected = showConnectedCheck.getSelection();
treeViewer.getControl().setRedraw(false);
try {
treeViewer.refresh();
if (showConnected) {
treeViewer.expandAll();
}
} finally {
treeViewer.getControl().setRedraw(true);
}
getDialogBoundsSettings().put(PARAM_SHOW_CONNECTED, showConnected);
}
});
final Button showAllProjectsCheck = new Button(group, SWT.CHECK);
showAllProjectsCheck.setText(UINavigatorMessages.label_show_all_projects);
showAllProjectsCheck.setSelection(showAllProjects);
showAllProjectsCheck.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
showAllProjects = showAllProjectsCheck.getSelection();
treeViewer.getControl().setRedraw(false);
try {
dataSourceTree.reloadTree(getTreeRootNode());
if (showAllProjects) {
treeViewer.expandToLevel(3);
}
} finally {
treeViewer.getControl().setRedraw(true);
}
getDialogBoundsSettings().put(PARAM_SHOW_ALL_PROJECTS, showAllProjects);
}
});
if (this.dataSource != null) {
DBNDatabaseNode dsNode = DBWorkbench.getPlatform().getNavigatorModel().getNodeByObject(this.dataSource);
if (dsNode != null) {
treeViewer.setSelection(new StructuredSelection(dsNode), true);
}
}
group.setTabList(new Control[] { dataSourceTree, showConnectedCheck, showAllProjectsCheck });
treeViewer.addFilter(new ViewerFilter() {
@Override
public boolean select(Viewer viewer, Object parentElement, Object element) {
if (showConnected) {
if (element instanceof DBNDataSource) {
return ((DBNDataSource) element).getDataSource() != null;
}
if (element instanceof DBNLocalFolder) {
return ((DBNLocalFolder) element).hasConnected();
}
}
return element instanceof DBNProject || element instanceof DBNProjectDatabases || element instanceof DBNLocalFolder || element instanceof DBNDataSource;
}
});
treeViewer.addSelectionChangedListener(event -> {
IStructuredSelection structSel = (IStructuredSelection) event.getSelection();
Object selNode = structSel.isEmpty() ? null : structSel.getFirstElement();
if (selNode instanceof DBNDataSource) {
dataSource = ((DBNDataSource) selNode).getObject();
getButton(IDialogConstants.OK_ID).setEnabled(true);
String description = dataSource.getDescription();
if (description == null) {
description = dataSource.getName();
}
descriptionText.setText(description);
} else {
dataSource = null;
getButton(IDialogConstants.OK_ID).setEnabled(false);
}
});
treeViewer.addDoubleClickListener(event -> {
if (getButton(IDialogConstants.OK_ID).isEnabled()) {
okPressed();
}
});
UIUtils.asyncExec(() -> {
Point treeSize = dataSourceTree.getViewer().getTree().computeSize(SWT.DEFAULT, SWT.DEFAULT);
Point shellSize = getShell().getSize();
if (treeSize.x >= shellSize.x) {
Point shellCompSize = getShell().computeSize(SWT.DEFAULT, SWT.DEFAULT);
getShell().setSize(shellCompSize.x, shellSize.y);
getShell().layout(true);
}
dataSourceTree.getFilterControl().setFocus();
if (showConnected) {
treeViewer.expandAll();
}
});
closeOnFocusLost(treeViewer.getControl(), dataSourceTree.getFilterControl(), descriptionText, showConnectedCheck, showAllProjectsCheck);
return group;
}
Aggregations