use of org.jkiss.dbeaver.model.struct.DBSObject in project dbeaver by serge-rider.
the class NavigatorUtils method isDefaultElement.
public static boolean isDefaultElement(Object element) {
if (element instanceof DBSWrapper) {
DBSObject object = ((DBSWrapper) element).getObject();
DBSObjectSelector activeContainer = DBUtils.getParentAdapter(DBSObjectSelector.class, object);
if (activeContainer != null) {
return activeContainer.getDefaultObject() == object;
}
} else if (element instanceof DBNProject) {
if (((DBNProject) element).getProject() == DBeaverCore.getInstance().getProjectRegistry().getActiveProject()) {
return true;
}
}
return false;
}
use of org.jkiss.dbeaver.model.struct.DBSObject in project dbeaver by serge-rider.
the class NavigatorUtils method createContextMenu.
public static MenuManager createContextMenu(final IWorkbenchSite workbenchSite, final Viewer viewer, final IMenuListener menuListener) {
final Control control = viewer.getControl();
final MenuManager menuMgr = new MenuManager();
Menu menu = menuMgr.createContextMenu(control);
menu.addMenuListener(new MenuListener() {
@Override
public void menuHidden(MenuEvent e) {
}
@Override
public void menuShown(MenuEvent e) {
Menu m = (Menu) e.widget;
DBNNode node = getSelectedNode(viewer.getSelection());
if (node != null && !node.isLocked() && node.allowsOpen()) {
String commandID = NavigatorUtils.getNodeActionCommand(DBXTreeNodeHandler.Action.open, node, CoreCommands.CMD_OBJECT_OPEN);
// Get contribution item from menu item and check it's ID
for (MenuItem item : m.getItems()) {
Object itemData = item.getData();
if (itemData instanceof IContributionItem) {
String contribId = ((IContributionItem) itemData).getId();
if (contribId != null && contribId.equals(commandID)) {
m.setDefaultItem(item);
}
}
}
}
}
});
menuMgr.addMenuListener(new IMenuListener() {
@Override
public void menuAboutToShow(final IMenuManager manager) {
ViewerColumnController columnController = ViewerColumnController.getFromControl(control);
if (columnController != null && columnController.isClickOnHeader()) {
columnController.fillConfigMenu(manager);
manager.add(new Separator());
return;
}
manager.add(new GroupMarker(MB_NAVIGATOR_ADDITIONS));
final IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
final DBNNode selectedNode = getSelectedNode(viewer);
if (selectedNode != null && !selectedNode.isLocked() && workbenchSite != null) {
// Add "Set active object" menu
if (selectedNode.isPersisted() && selectedNode instanceof DBNDatabaseNode && !(selectedNode instanceof DBNDatabaseFolder) && ((DBNDatabaseNode) selectedNode).getObject() != null) {
final DBSObjectSelector activeContainer = DBUtils.getParentAdapter(DBSObjectSelector.class, ((DBNDatabaseNode) selectedNode).getObject());
if (activeContainer != null && activeContainer.supportsDefaultChange()) {
DBSObject activeChild;
activeChild = activeContainer.getDefaultObject();
if (activeChild != ((DBNDatabaseNode) selectedNode).getObject()) {
DBNDatabaseNode databaseNode = (DBNDatabaseNode) selectedNode;
if (databaseNode.getObject() != null && (activeChild == null || activeChild.getClass() == databaseNode.getObject().getClass())) {
// + databaseNode.getNodeType();
String text = "Set Active ";
// Fill context menu
IAction action = ActionUtils.makeAction(new NavigatorActionSetActiveObject(), workbenchSite, selection, text, null, null);
manager.add(action);
}
}
}
}
manager.add(new Separator());
manager.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
manager.add(new GroupMarker(IActionConstants.MB_ADDITIONS_END));
// Add properties button
if (PreferencesUtil.hasPropertiesContributors(selection.getFirstElement())) {
manager.add(ActionUtils.makeCommandContribution(workbenchSite, IWorkbenchCommandConstants.FILE_PROPERTIES));
}
if (selectedNode.isPersisted()) {
// Add refresh button
manager.add(ActionUtils.makeCommandContribution(workbenchSite, IWorkbenchCommandConstants.FILE_REFRESH));
}
}
manager.add(new GroupMarker(CoreCommands.GROUP_TOOLS));
if (menuListener != null) {
menuListener.menuAboutToShow(manager);
}
}
});
menuMgr.setRemoveAllWhenShown(true);
control.setMenu(menu);
control.addDisposeListener(new DisposeListener() {
@Override
public void widgetDisposed(DisposeEvent e) {
menuMgr.dispose();
}
});
return menuMgr;
}
use of org.jkiss.dbeaver.model.struct.DBSObject in project dbeaver by serge-rider.
the class SQLCompletionProcessor method computeCompletionProposals.
@Override
public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentOffset) {
final SQLCompletionAnalyzer.CompletionRequest request = new SQLCompletionAnalyzer.CompletionRequest(editor, documentOffset, simpleMode);
SQLWordPartDetector wordDetector = request.wordDetector = new SQLWordPartDetector(viewer.getDocument(), editor.getSyntaxManager(), documentOffset);
request.wordPart = wordDetector.getWordPart();
if (lookupTemplates) {
return makeTemplateProposals(viewer, request);
}
request.queryType = null;
String searchPrefix = request.wordPart;
{
final String prevKeyWord = wordDetector.getPrevKeyWord();
if (!CommonUtils.isEmpty(prevKeyWord)) {
if (editor.getSyntaxManager().getDialect().isEntityQueryWord(prevKeyWord)) {
// TODO: its an ugly hack. Need a better way
if (SQLConstants.KEYWORD_INTO.equals(prevKeyWord) && !CommonUtils.isEmpty(wordDetector.getPrevWords()) && ("(".equals(wordDetector.getPrevDelimiter()) || ",".equals(wordDetector.getPrevDelimiter()))) {
request.queryType = QueryType.COLUMN;
} else {
request.queryType = QueryType.TABLE;
}
} else if (editor.getSyntaxManager().getDialect().isAttributeQueryWord(prevKeyWord)) {
request.queryType = QueryType.COLUMN;
if (!request.simpleMode && CommonUtils.isEmpty(request.wordPart) && wordDetector.getPrevDelimiter().equals(ALL_COLUMNS_PATTERN)) {
wordDetector.moveToDelimiter();
searchPrefix = ALL_COLUMNS_PATTERN;
}
}
}
}
request.wordPart = searchPrefix;
if (request.wordPart != null) {
if (editor.getDataSource() != null) {
ProposalSearchJob searchJob = new ProposalSearchJob(request);
searchJob.schedule();
// Wait until job finished
UIUtils.waitJobCompletion(searchJob);
}
}
if (!CommonUtils.isEmpty(request.wordPart)) {
// Keyword assist
List<String> matchedKeywords = editor.getSyntaxManager().getDialect().getMatchedKeywords(request.wordPart);
if (!request.simpleMode) {
// Sort using fuzzy match
Collections.sort(matchedKeywords, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return TextUtils.fuzzyScore(o1, request.wordPart) - TextUtils.fuzzyScore(o2, request.wordPart);
}
});
}
for (String keyWord : matchedKeywords) {
DBPKeywordType keywordType = editor.getSyntaxManager().getDialect().getKeywordType(keyWord);
if (keywordType != null) {
request.proposals.add(SQLCompletionAnalyzer.createCompletionProposal(request, keyWord, keyWord, keywordType, null, false, null));
}
}
}
// Remove duplications
for (int i = 0; i < request.proposals.size(); i++) {
SQLCompletionProposal proposal = request.proposals.get(i);
for (int j = i + 1; j < request.proposals.size(); ) {
SQLCompletionProposal proposal2 = request.proposals.get(j);
if (proposal.getDisplayString().equals(proposal2.getDisplayString())) {
request.proposals.remove(j);
} else {
j++;
}
}
}
DBSObject selectedObject = DBUtils.getActiveInstanceObject(editor.getDataSource());
boolean hideDups = editor.getActivePreferenceStore().getBoolean(SQLPreferenceConstants.HIDE_DUPLICATE_PROPOSALS) && selectedObject != null;
if (hideDups) {
for (int i = 0; i < request.proposals.size(); i++) {
SQLCompletionProposal proposal = request.proposals.get(i);
for (int j = 0; j < request.proposals.size(); ) {
SQLCompletionProposal proposal2 = request.proposals.get(j);
if (i != j && proposal.hasStructObject() && proposal2.hasStructObject() && CommonUtils.equalObjects(proposal.getObject().getName(), proposal2.getObject().getName()) && proposal.getObjectContainer() == selectedObject) {
request.proposals.remove(j);
} else {
j++;
}
}
}
}
if (hideDups) {
if (selectedObject instanceof DBSObjectContainer) {
//List<ICompletionProposal>
}
}
return ArrayUtils.toArray(ICompletionProposal.class, request.proposals);
}
use of org.jkiss.dbeaver.model.struct.DBSObject in project dbeaver by serge-rider.
the class SQLInformationProvider method getInformation2.
@Override
public Object getInformation2(ITextViewer textViewer, IRegion subject) {
if (implementation != null) {
Object s = implementation.getHoverInfo2(textViewer, subject);
if (s != null) {
return s;
}
}
//SQLCompletionProposal proposal = new SQLCompletionProposal();
SQLContextInformer informer = new SQLContextInformer(editor, editor.getSyntaxManager());
informer.searchInformation(subject);
DBSObject object = null;
if (informer.hasObjects()) {
// Make object description
DBRProgressMonitor monitor = VoidProgressMonitor.INSTANCE;
final DBSObjectReference objectRef = informer.getObjectReferences().get(0);
try {
object = objectRef.resolveObject(monitor);
} catch (DBException e) {
// Can't resolve
return e.getMessage();
}
} else if (ArrayUtils.isEmpty(informer.getKeywords())) {
return null;
}
return SQLContextInformer.readAdditionalProposalInfo(null, editor.getDataSource(), object, informer.getKeywords(), informer.getKeywordType());
}
use of org.jkiss.dbeaver.model.struct.DBSObject in project dbeaver by serge-rider.
the class OpenHandler method getDataSourceContainers.
private static List<DBPDataSourceContainer> getDataSourceContainers(ExecutionEvent event) {
List<DBPDataSourceContainer> containers = new ArrayList<>();
ISelection selection = HandlerUtil.getCurrentSelection(event);
if (selection instanceof IStructuredSelection) {
for (Object obj : ((IStructuredSelection) selection).toArray()) {
if (obj instanceof DBNLocalFolder) {
for (DBNDataSource ds : ((DBNLocalFolder) obj).getDataSources()) {
containers.add(ds.getDataSourceContainer());
}
} else {
DBSObject selectedObject = DBUtils.getFromObject(obj);
if (selectedObject != null) {
if (selectedObject instanceof DBPDataSourceContainer) {
containers.add((DBPDataSourceContainer) selectedObject);
} else {
containers.add(selectedObject.getDataSource().getContainer());
}
}
}
}
}
if (containers.isEmpty()) {
IWorkbenchPart activePart = HandlerUtil.getActivePart(event);
DBPDataSourceContainer partContainer = getDataSourceContainers(activePart);
if (partContainer != null) {
containers.add(partContainer);
}
}
return containers;
}
Aggregations