Search in sources :

Example 1 with IAttributeController

use of org.jkiss.dbeaver.ui.data.IAttributeController in project dbeaver by serge-rider.

the class ReferenceValueEditor method createEditorSelector.

public boolean createEditorSelector(final Composite parent) {
    if (!(valueController instanceof IAttributeController) || valueController.isReadOnly()) {
        return false;
    }
    refConstraint = getEnumerableConstraint();
    if (refConstraint == null) {
        return false;
    }
    if (refConstraint instanceof DBSEntityAssociation) {
        final DBSEntityAssociation association = (DBSEntityAssociation) refConstraint;
        if (association.getReferencedConstraint() != null) {
            final DBSEntity refTable = association.getReferencedConstraint().getParentObject();
            Composite labelGroup = UIUtils.createPlaceholder(parent, 2);
            labelGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
            Link dictLabel = UIUtils.createLink(labelGroup, NLS.bind(CoreMessages.dialog_value_view_label_dictionary, refTable.getName()), new SelectionAdapter() {

                @Override
                public void widgetSelected(SelectionEvent e) {
                    // Open
                    final IWorkbenchWindow window = valueController.getValueSite().getWorkbenchWindow();
                    DBeaverUI.runInUI(window, new DBRRunnableWithProgress() {

                        @Override
                        public void run(DBRProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                            DBNDatabaseNode tableNode = DBeaverCore.getInstance().getNavigatorModel().getNodeByObject(monitor, refTable, true);
                            if (tableNode != null) {
                                NavigatorHandlerObjectOpen.openEntityEditor(tableNode, DatabaseDataEditor.class.getName(), window);
                            }
                        }
                    });
                }
            });
            dictLabel.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING));
            Link hintLabel = UIUtils.createLink(labelGroup, "(<a>Define Description</a>)", new SelectionAdapter() {

                @Override
                public void widgetSelected(SelectionEvent e) {
                    EditDictionaryPage editDictionaryPage = new EditDictionaryPage("Dictionary structure", refTable);
                    if (editDictionaryPage.edit(parent.getShell())) {
                        loaderJob.schedule();
                    }
                }
            });
            hintLabel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.HORIZONTAL_ALIGN_END));
        }
    }
    editorSelector = new Table(parent, SWT.BORDER | SWT.SINGLE | SWT.FULL_SELECTION | SWT.H_SCROLL | SWT.V_SCROLL);
    editorSelector.setLinesVisible(true);
    editorSelector.setHeaderVisible(true);
    GridData gd = new GridData(GridData.FILL_BOTH);
    gd.heightHint = 150;
    //gd.widthHint = 300;
    //gd.grabExcessVerticalSpace = true;
    //gd.grabExcessHorizontalSpace = true;
    editorSelector.setLayoutData(gd);
    UIUtils.createTableColumn(editorSelector, SWT.LEFT, CoreMessages.dialog_value_view_column_value);
    UIUtils.createTableColumn(editorSelector, SWT.LEFT, CoreMessages.dialog_value_view_column_description);
    UIUtils.packColumns(editorSelector);
    editorSelector.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            TableItem[] selection = editorSelector.getSelection();
            if (selection != null && selection.length > 0) {
                Object value = selection[0].getData();
                //editorControl.setText(selection[0].getText());
                try {
                    valueEditor.primeEditorValue(value);
                } catch (DBException e1) {
                    log.error(e1);
                }
            }
        }
    });
    Control control = valueEditor.getControl();
    ModifyListener modifyListener = new ModifyListener() {

        @Override
        public void modifyText(ModifyEvent e) {
            Object curEditorValue;
            try {
                curEditorValue = valueEditor.extractEditorValue();
            } catch (DBException e1) {
                log.error(e1);
                return;
            }
            // Try to select current value in the table
            final String curTextValue = valueController.getValueHandler().getValueDisplayString(((IAttributeController) valueController).getBinding(), curEditorValue, DBDDisplayFormat.UI);
            boolean valueFound = false;
            for (TableItem item : editorSelector.getItems()) {
                if (item.getText(0).equals(curTextValue)) {
                    editorSelector.select(editorSelector.indexOf(item));
                    editorSelector.showItem(item);
                    valueFound = true;
                    break;
                }
            }
            if (!valueFound) {
                // Read dictionary
                if (loaderJob.getState() == Job.RUNNING) {
                    // Cancel it and create new one
                    loaderJob.cancel();
                    loaderJob = new SelectorLoaderJob();
                }
                loaderJob.setPattern(curEditorValue);
                if (loaderJob.getState() != Job.WAITING) {
                    loaderJob.schedule(100);
                }
            }
        }
    };
    if (control instanceof Text) {
        ((Text) control).addModifyListener(modifyListener);
    } else if (control instanceof StyledText) {
        ((StyledText) control).addModifyListener(modifyListener);
    }
    loaderJob = new SelectorLoaderJob();
    final Object curValue = valueController.getValue();
    if (curValue instanceof Number) {
        loaderJob.setPattern(curValue);
    }
    loaderJob.schedule(500);
    return true;
}
Also used : DBException(org.jkiss.dbeaver.DBException) ModifyListener(org.eclipse.swt.events.ModifyListener) EditDictionaryPage(org.jkiss.dbeaver.ui.editors.object.struct.EditDictionaryPage) IAttributeController(org.jkiss.dbeaver.ui.data.IAttributeController) ModifyEvent(org.eclipse.swt.events.ModifyEvent) SelectionEvent(org.eclipse.swt.events.SelectionEvent) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode) IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) DatabaseDataEditor(org.jkiss.dbeaver.ui.editors.data.DatabaseDataEditor) StyledText(org.eclipse.swt.custom.StyledText) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) StyledText(org.eclipse.swt.custom.StyledText) GridData(org.eclipse.swt.layout.GridData) DBRRunnableWithProgress(org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)

Example 2 with IAttributeController

use of org.jkiss.dbeaver.ui.data.IAttributeController in project dbeaver by serge-rider.

the class ContentEditorInput method prepareContent.

private void prepareContent(DBRProgressMonitor monitor) throws DBException {
    DBDContent content = getContent();
    DBDContentStorage storage = content.getContents(monitor);
    if (contentDetached) {
        release(monitor);
        contentDetached = false;
    }
    if (storage instanceof DBDContentStorageLocal) {
        // User content's storage directly
        contentFile = ((DBDContentStorageLocal) storage).getDataFile();
        contentDetached = true;
    } else {
        // Copy content to local file
        try {
            // Create file
            if (contentFile == null) {
                String valueId;
                if (valueController instanceof IAttributeController) {
                    valueId = ((IAttributeController) valueController).getColumnId();
                } else {
                    valueId = valueController.getValueName();
                }
                contentFile = ContentUtils.createTempContentFile(monitor, DBeaverCore.getInstance(), valueId);
            }
            // Write value to file
            copyContentToFile(content, monitor);
        } catch (IOException e) {
            // Delete temp file
            if (contentFile != null && contentFile.exists()) {
                if (!contentFile.delete()) {
                    log.warn("Can't delete temporary content file '" + contentFile.getAbsolutePath() + "'");
                }
            }
            throw new DBException("Can't delete content file", e);
        }
    }
    // Mark file as readonly
    if (valueController.isReadOnly()) {
        markReadOnly(true);
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBDContent(org.jkiss.dbeaver.model.data.DBDContent) DBDContentStorageLocal(org.jkiss.dbeaver.model.data.DBDContentStorageLocal) IAttributeController(org.jkiss.dbeaver.ui.data.IAttributeController) DBDContentStorage(org.jkiss.dbeaver.model.data.DBDContentStorage)

Aggregations

DBException (org.jkiss.dbeaver.DBException)2 IAttributeController (org.jkiss.dbeaver.ui.data.IAttributeController)2 StyledText (org.eclipse.swt.custom.StyledText)1 ModifyEvent (org.eclipse.swt.events.ModifyEvent)1 ModifyListener (org.eclipse.swt.events.ModifyListener)1 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)1 SelectionEvent (org.eclipse.swt.events.SelectionEvent)1 GridData (org.eclipse.swt.layout.GridData)1 IWorkbenchWindow (org.eclipse.ui.IWorkbenchWindow)1 DBDContent (org.jkiss.dbeaver.model.data.DBDContent)1 DBDContentStorage (org.jkiss.dbeaver.model.data.DBDContentStorage)1 DBDContentStorageLocal (org.jkiss.dbeaver.model.data.DBDContentStorageLocal)1 DBNDatabaseNode (org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)1 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)1 DBRRunnableWithProgress (org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress)1 DatabaseDataEditor (org.jkiss.dbeaver.ui.editors.data.DatabaseDataEditor)1 EditDictionaryPage (org.jkiss.dbeaver.ui.editors.object.struct.EditDictionaryPage)1