use of org.jkiss.dbeaver.model.navigator.DBNDataSource in project dbeaver by dbeaver.
the class NavigatorHandlerLocalFolderCreate method execute.
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
final IWorkbenchPart activePart = HandlerUtil.getActivePart(event);
final ISelection selection = HandlerUtil.getCurrentSelection(event);
if (selection instanceof IStructuredSelection) {
IStructuredSelection structSelection = (IStructuredSelection) selection;
List<DBNDataSource> dataSources = new ArrayList<>();
DBNProjectDatabases databasesNode = null;
DBNLocalFolder parentFolder = null;
for (Iterator iter = structSelection.iterator(); iter.hasNext(); ) {
Object element = iter.next();
if (element instanceof DBNDataSource) {
dataSources.add((DBNDataSource) element);
element = ((DBNDataSource) element).getParentNode();
}
if (element instanceof DBNLocalFolder) {
parentFolder = (DBNLocalFolder) element;
databasesNode = parentFolder.getParentNode();
} else if (element instanceof DBNProjectDatabases) {
databasesNode = (DBNProjectDatabases) element;
}
}
if (databasesNode != null) {
createFolder(HandlerUtil.getActiveWorkbenchWindow(event), activePart, databasesNode, parentFolder, dataSources, null);
}
}
return null;
}
use of org.jkiss.dbeaver.model.navigator.DBNDataSource in project dbeaver by dbeaver.
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)) {
if (adaptableObject instanceof DBNDatabaseFolder) {
adaptableObject = ((DBNDatabaseFolder) adaptableObject).getParentObject();
}
DBPObject object = null;
if (adaptableObject instanceof DBSWrapper) {
object = ((DBSWrapper) adaptableObject).getObject();
} else if (adaptableObject instanceof DBPObject) {
object = (DBPObject) adaptableObject;
}
if (object instanceof DBSObject) {
object = DBUtils.getPublicObject((DBSObject) object);
}
if (object != null && adapterType.isAssignableFrom(object.getClass())) {
return adapterType.cast(object);
}
// } else if (IProject.class == adapterType) {
// DBPProject project = null;
// if (adaptableObject instanceof DBNNode) {
// project = ((DBNNode) adaptableObject).getOwnerProject();
// }
// return project == null ? null : adapterType.cast(project.getEclipseProject());
} 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, ModelMessages.model_navigator_Name, meta.getName());
// $NON-NLS-1$
props.addProperty(null, "desc", ModelMessages.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;
}
use of org.jkiss.dbeaver.model.navigator.DBNDataSource in project dbeaver by dbeaver.
the class StatisticsNavigatorNodeRenderer method paintNodeDetails.
public void paintNodeDetails(DBNNode node, Tree tree, GC gc, Event event) {
super.paintNodeDetails(node, tree, gc, event);
ScrollBar hSB = tree.getHorizontalBar();
boolean scrollEnabled = (hSB != null && hSB.isVisible());
Object element = event.item.getData();
if (element instanceof DBNDatabaseNode) {
if (element instanceof DBNDataSource) {
int widthOccupied = 0;
if (!scrollEnabled && DBWorkbench.getPlatform().getPreferenceStore().getBoolean(NavigatorPreferences.NAVIGATOR_SHOW_NODE_ACTIONS)) {
widthOccupied += renderDataSourceNodeActions((DBNDatabaseNode) element, tree, gc, event);
}
if (DBWorkbench.getPlatform().getPreferenceStore().getBoolean(NavigatorPreferences.NAVIGATOR_SHOW_CONNECTION_HOST_NAME)) {
renderDataSourceHostName((DBNDataSource) element, tree, gc, event, widthOccupied);
}
}
if (!scrollEnabled && DBWorkbench.getPlatform().getPreferenceStore().getBoolean(NavigatorPreferences.NAVIGATOR_SHOW_STATISTICS_INFO)) {
renderObjectStatistics((DBNDatabaseNode) element, tree, gc, event);
}
}
}
use of org.jkiss.dbeaver.model.navigator.DBNDataSource in project dbeaver by dbeaver.
the class AbstractSearchPage method loadTreeState.
protected List<DBNNode> loadTreeState(DBRProgressMonitor monitor, DBPProject project, String sources) {
final List<DBNNode> result = new ArrayList<>();
if (!CommonUtils.isEmpty(sources)) {
// Keep broken datasources to make connect attempt only once
Set<DBNDataSource> brokenDataSources = new HashSet<>();
// Find all nodes
// $NON-NLS-1$
StringTokenizer st = new StringTokenizer(sources, "|");
while (st.hasMoreTokens()) {
String nodePath = st.nextToken();
try {
DBNDataSource dsNode = DBWorkbench.getPlatform().getNavigatorModel().getDataSourceByPath(project, nodePath);
if (dsNode == null || brokenDataSources.contains(dsNode)) {
continue;
}
DBNNode node = DBWorkbench.getPlatform().getNavigatorModel().getNodeByPath(monitor, dsNode.getOwnerProject(), nodePath);
if (node != null) {
result.add(node);
} else {
brokenDataSources.add(dsNode);
}
} catch (DBException e) {
log.error(e);
}
}
}
return result;
}
use of org.jkiss.dbeaver.model.navigator.DBNDataSource in project dbeaver by dbeaver.
the class DatabaseLazyEditorInput method initializeRealInput.
public IDatabaseEditorInput initializeRealInput(final DBRProgressMonitor monitor) throws DBException {
// Get the node path.
if (project != null) {
dataSourceContainer = project.getDataSourceRegistry().getDataSource(dataSourceId);
}
if (dataSourceContainer == null) {
// $NON-NLS-2$
log.error("Can't find data source '" + dataSourceId + "'");
return null;
}
if (project == null) {
project = dataSourceContainer.getRegistry().getProject();
}
final DBNModel navigatorModel = DBWorkbench.getPlatform().getNavigatorModel();
navigatorModel.ensureProjectLoaded(project);
// dataSourceContainer, project, nodePath, nodeName, activePageId, activeFolderId
DBPDataSource dataSource;
while (!dataSourceContainer.isConnected()) {
try {
dataSourceContainer.connect(monitor, true, true);
} catch (final DBException e) {
// Connection error
final Integer result = new UITask<Integer>() {
@Override
protected Integer runTask() {
ConnectionLostDialog clDialog = new ConnectionLostDialog(UIUtils.getActiveWorkbenchShell(), dataSourceContainer, e, "Close");
return clDialog.open();
}
}.execute();
if (result == IDialogConstants.STOP_ID) {
// Close editor
return null;
} else if (result == IDialogConstants.RETRY_ID) {
continue;
} else {
return new ErrorEditorInput(GeneralUtils.makeExceptionStatus(e), navigatorModel.getNodeByObject(dataSourceContainer));
}
}
break;
}
try {
dataSource = dataSourceContainer.getDataSource();
if (dataSource == null) {
throw new DBException("Connection to '" + dataSourceContainer.getName() + "' canceled");
}
final DBNNode[] editorNodeResult = new DBNNode[1];
DBExecUtils.tryExecuteRecover(monitor, dataSource, param -> {
try {
DBNDataSource dsNode = (DBNDataSource) navigatorModel.getNodeByObject(monitor, this.dataSourceContainer, true);
if (dsNode == null) {
throw new DBException("Datasource '" + this.dataSourceContainer.getName() + "' navigator node not found");
}
dsNode.initializeNode(monitor, null);
editorNodeResult[0] = navigatorModel.getNodeByPath(monitor, project, nodePath);
} catch (Exception e) {
throw new InvocationTargetException(e);
}
});
DBNNode node = editorNodeResult[0];
if (node == null) {
throw new DBException("Navigator node '" + nodePath + "' not found");
}
if (node instanceof DBNDatabaseNode) {
DatabaseNodeEditorInput realInput = new DatabaseNodeEditorInput((DBNDatabaseNode) node);
realInput.setDefaultFolderId(activeFolderId);
realInput.setDefaultPageId(activePageId);
return realInput;
} else {
throw new DBException("Database node has bad type: " + node.getClass().getName());
}
} catch (DBException e) {
return new ErrorEditorInput(GeneralUtils.makeExceptionStatus(e), navigatorModel.getNodeByObject(dataSourceContainer));
}
}
Aggregations