Search in sources :

Example 1 with ResourceInfo

use of org.jkiss.dbeaver.ui.editors.sql.SQLEditorUtils.ResourceInfo in project dbeaver by serge-rider.

the class ScriptSelectorPanel method createDialogArea.

@Override
protected Control createDialogArea(Composite parent) {
    Composite composite = (Composite) super.createDialogArea(parent);
    /*Rectangle bounds = new Rectangle(100, 100, 500, 200);
        final String boundsStr = getBoundsSettings().get(CONFIG_BOUNDS_PARAM);
        if (boundsStr != null && !boundsStr.isEmpty()) {
            final String[] bc = boundsStr.split(",");
            try {
                bounds = new Rectangle(
                    Integer.parseInt(bc[0]),
                    Integer.parseInt(bc[1]),
                    Integer.parseInt(bc[2]),
                    Integer.parseInt(bc[3]));
            } catch (NumberFormatException e) {
                log.warn(e);
            }
        }*/
    patternText = new Text(composite, SWT.BORDER);
    patternText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    UIUtils.addEmptyTextHint(patternText, text -> "Enter a part of script name here");
    // patternText.setForeground(fg);
    // patternText.setBackground(bg);
    patternText.addModifyListener(e -> {
        if (filterJob != null) {
            return;
        }
        filterJob = new FilterJob();
        filterJob.schedule(250);
    });
    // parent.getDisplay().getSystemColor(SWT.COLOR_INFO_FOREGROUND);
    final Color fg = patternText.getForeground();
    // parent.getDisplay().getSystemColor(SWT.COLOR_INFO_BACKGROUND);
    final Color bg = patternText.getBackground();
    composite.setForeground(fg);
    composite.setBackground(bg);
    Button newButton = new Button(composite, SWT.PUSH | SWT.FLAT);
    newButton.setText("&New Script");
    newButton.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            IFile scriptFile;
            try {
                scriptFile = SQLEditorUtils.createNewScript(DBWorkbench.getPlatform().getWorkspace().getProject(rootFolder.getProject()), rootFolder, navigatorContext);
                SQLEditorHandlerOpenEditor.openResource(scriptFile, navigatorContext);
            } catch (CoreException ex) {
                log.error(ex);
            }
            cancelPressed();
        }
    });
    ((GridData) UIUtils.createHorizontalLine(composite).getLayoutData()).horizontalSpan = 2;
    Tree scriptTree = new Tree(composite, SWT.SINGLE | SWT.FULL_SELECTION);
    final GridData gd = new GridData(GridData.FILL_BOTH);
    gd.horizontalSpan = 2;
    gd.widthHint = 500;
    gd.heightHint = 200;
    scriptTree.setLayoutData(gd);
    scriptTree.setForeground(fg);
    scriptTree.setBackground(bg);
    scriptTree.setLinesVisible(true);
    // scriptViewer.setHeaderVisible(true);
    this.scriptViewer = new TreeViewer(scriptTree);
    ColumnViewerToolTipSupport.enableFor(this.scriptViewer);
    // scriptTree.setS
    this.scriptViewer.setContentProvider(new TreeContentProvider() {

        @Override
        public Object[] getChildren(Object parentElement) {
            if (parentElement instanceof ResourceInfo) {
                final List<ResourceInfo> children = ((ResourceInfo) parentElement).getChildren();
                return CommonUtils.isEmpty(children) ? null : children.toArray();
            }
            return null;
        }

        @Override
        public boolean hasChildren(Object element) {
            if (element instanceof ResourceInfo) {
                final List<ResourceInfo> children = ((ResourceInfo) element).getChildren();
                return !CommonUtils.isEmpty(children);
            }
            return false;
        }
    });
    ViewerColumnController columnController = new ViewerColumnController("scriptSelectorViewer", scriptViewer);
    columnController.addColumn("Script", "Resource name", SWT.LEFT, true, true, new ColumnLabelProvider() {

        @Override
        public Image getImage(Object element) {
            final ResourceInfo ri = (ResourceInfo) element;
            if (!ri.isDirectory()) {
                if (ri.getDataSource() == null) {
                    return DBeaverIcons.getImage(UIIcon.SQL_SCRIPT);
                } else {
                    return DBeaverIcons.getImage(ri.getDataSource().getDriver().getIcon());
                }
            } else {
                return DBeaverIcons.getImage(DBIcon.TREE_FOLDER);
            }
        }

        @Override
        public String getText(Object element) {
            return ((ResourceInfo) element).getName();
        }

        @Override
        public String getToolTipText(Object element) {
            final DBPDataSourceContainer dataSource = ((ResourceInfo) element).getDataSource();
            return dataSource == null ? null : dataSource.getName();
        }

        @Override
        public Image getToolTipImage(Object element) {
            final DBPDataSourceContainer dataSource = ((ResourceInfo) element).getDataSource();
            return dataSource == null ? null : DBeaverIcons.getImage(dataSource.getDriver().getIcon());
        }
    });
    columnController.addColumn("Time", "Modification time", SWT.LEFT, true, true, new ColumnLabelProvider() {

        private SimpleDateFormat sdf = new SimpleDateFormat(DBConstants.DEFAULT_TIMESTAMP_FORMAT);

        @Override
        public String getText(Object element) {
            final File localFile = ((ResourceInfo) element).getLocalFile();
            if (localFile.isDirectory()) {
                return null;
            } else {
                return sdf.format(new Date(localFile.lastModified()));
            }
        }
    });
    columnController.addColumn("Info", "Script preview", SWT.LEFT, true, true, new ColumnLabelProvider() {

        @Override
        public String getText(Object element) {
            // ((ResourceInfo)element).getDescription();
            return "";
        }

        @Override
        public Color getForeground(Object element) {
            return getShell().getDisplay().getSystemColor(SWT.COLOR_WIDGET_DARK_SHADOW);
        }

        @Override
        public String getToolTipText(Object element) {
            final ResourceInfo ri = (ResourceInfo) element;
            String description = ri.getDescription();
            return description == null ? null : description.trim();
        }
    });
    columnController.createColumns();
    columnController.sortByColumn(1, SWT.UP);
    scriptTree.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetDefaultSelected(SelectionEvent e) {
            List<ResourceInfo> files = new ArrayList<>();
            for (Object item : ((IStructuredSelection) scriptViewer.getSelection()).toArray()) {
                if (!((ResourceInfo) item).isDirectory()) {
                    files.add((ResourceInfo) item);
                }
            }
            if (files.isEmpty()) {
                return;
            }
            cancelPressed();
            for (ResourceInfo ri : files) {
                SQLEditorHandlerOpenEditor.openResourceEditor(ScriptSelectorPanel.this.workbenchWindow, ri, navigatorContext);
            }
        }
    });
    scriptTree.addListener(SWT.PaintItem, event -> {
        final TreeItem item = (TreeItem) event.item;
        final ResourceInfo ri = (ResourceInfo) item.getData();
        if (ri != null && !ri.isDirectory() && CommonUtils.isEmpty(item.getText(2))) {
            UIUtils.asyncExec(() -> {
                if (!item.isDisposed()) {
                    item.setText(2, CommonUtils.getSingleLineString(CommonUtils.notEmpty(ri.getDescription())));
                }
            });
        }
    });
    this.patternText.addKeyListener(new KeyAdapter() {

        @Override
        public void keyPressed(KeyEvent e) {
            final Tree tree = scriptViewer.getTree();
            if (e.keyCode == SWT.ARROW_DOWN) {
                // scriptViewer.get
                scriptViewer.setSelection(new StructuredSelection(tree.getItem(0).getData()));
                tree.setFocus();
            } else if (e.keyCode == SWT.ARROW_UP) {
                scriptViewer.setSelection(new StructuredSelection(tree.getItem(tree.getItemCount() - 1).getData()));
                tree.setFocus();
            }
        }
    });
    closeOnFocusLost(patternText, scriptViewer.getTree(), newButton);
    scriptViewer.setInput(scriptFiles);
    UIUtils.expandAll(scriptViewer);
    final Tree tree = scriptViewer.getTree();
    final TreeColumn[] columns = tree.getColumns();
    columns[0].pack();
    columns[0].setWidth(columns[0].getWidth() + 10);
    columns[1].pack();
    columns[2].setWidth(200 * 8);
    UIUtils.asyncExec(scriptTree::setFocus);
    return composite;
}
Also used : IFile(org.eclipse.core.resources.IFile) KeyAdapter(org.eclipse.swt.events.KeyAdapter) Image(org.eclipse.swt.graphics.Image) KeyEvent(org.eclipse.swt.events.KeyEvent) SelectionEvent(org.eclipse.swt.events.SelectionEvent) ArrayList(java.util.ArrayList) List(java.util.List) DBPDataSourceContainer(org.jkiss.dbeaver.model.DBPDataSourceContainer) ResourceInfo(org.jkiss.dbeaver.ui.editors.sql.SQLEditorUtils.ResourceInfo) Color(org.eclipse.swt.graphics.Color) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) Date(java.util.Date) CoreException(org.eclipse.core.runtime.CoreException) GridData(org.eclipse.swt.layout.GridData) SimpleDateFormat(java.text.SimpleDateFormat) IFile(org.eclipse.core.resources.IFile) File(java.io.File)

Example 2 with ResourceInfo

use of org.jkiss.dbeaver.ui.editors.sql.SQLEditorUtils.ResourceInfo in project dbeaver by dbeaver.

the class ScriptSelectorPanel method createDialogArea.

@Override
protected Control createDialogArea(Composite parent) {
    Composite composite = (Composite) super.createDialogArea(parent);
    /*Rectangle bounds = new Rectangle(100, 100, 500, 200);
        final String boundsStr = getBoundsSettings().get(CONFIG_BOUNDS_PARAM);
        if (boundsStr != null && !boundsStr.isEmpty()) {
            final String[] bc = boundsStr.split(",");
            try {
                bounds = new Rectangle(
                    Integer.parseInt(bc[0]),
                    Integer.parseInt(bc[1]),
                    Integer.parseInt(bc[2]),
                    Integer.parseInt(bc[3]));
            } catch (NumberFormatException e) {
                log.warn(e);
            }
        }*/
    patternText = new Text(composite, SWT.BORDER);
    patternText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    UIUtils.addEmptyTextHint(patternText, text -> "Enter a part of script name here");
    // patternText.setForeground(fg);
    // patternText.setBackground(bg);
    patternText.addModifyListener(e -> {
        if (filterJob != null) {
            return;
        }
        filterJob = new FilterJob();
        filterJob.schedule(250);
    });
    // parent.getDisplay().getSystemColor(SWT.COLOR_INFO_FOREGROUND);
    final Color fg = patternText.getForeground();
    // parent.getDisplay().getSystemColor(SWT.COLOR_INFO_BACKGROUND);
    final Color bg = patternText.getBackground();
    composite.setForeground(fg);
    composite.setBackground(bg);
    Button newButton = new Button(composite, SWT.PUSH | SWT.FLAT);
    newButton.setText("&New Script");
    newButton.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            IFile scriptFile;
            try {
                scriptFile = SQLEditorUtils.createNewScript(DBWorkbench.getPlatform().getWorkspace().getProject(rootFolder.getProject()), rootFolder, navigatorContext);
                SQLEditorHandlerOpenEditor.openResource(scriptFile, navigatorContext);
            } catch (CoreException ex) {
                log.error(ex);
            }
            cancelPressed();
        }
    });
    ((GridData) UIUtils.createHorizontalLine(composite).getLayoutData()).horizontalSpan = 2;
    Tree scriptTree = new Tree(composite, SWT.SINGLE | SWT.FULL_SELECTION);
    final GridData gd = new GridData(GridData.FILL_BOTH);
    gd.horizontalSpan = 2;
    gd.widthHint = 500;
    gd.heightHint = 200;
    scriptTree.setLayoutData(gd);
    scriptTree.setForeground(fg);
    scriptTree.setBackground(bg);
    scriptTree.setLinesVisible(true);
    // scriptViewer.setHeaderVisible(true);
    this.scriptViewer = new TreeViewer(scriptTree);
    ColumnViewerToolTipSupport.enableFor(this.scriptViewer);
    // scriptTree.setS
    this.scriptViewer.setContentProvider(new TreeContentProvider() {

        @Override
        public Object[] getChildren(Object parentElement) {
            if (parentElement instanceof ResourceInfo) {
                final List<ResourceInfo> children = ((ResourceInfo) parentElement).getChildren();
                return CommonUtils.isEmpty(children) ? null : children.toArray();
            }
            return null;
        }

        @Override
        public boolean hasChildren(Object element) {
            if (element instanceof ResourceInfo) {
                final List<ResourceInfo> children = ((ResourceInfo) element).getChildren();
                return !CommonUtils.isEmpty(children);
            }
            return false;
        }
    });
    ViewerColumnController columnController = new ViewerColumnController("scriptSelectorViewer", scriptViewer);
    columnController.addColumn("Script", "Resource name", SWT.LEFT, true, true, new ColumnLabelProvider() {

        @Override
        public Image getImage(Object element) {
            final ResourceInfo ri = (ResourceInfo) element;
            if (!ri.isDirectory()) {
                if (ri.getDataSource() == null) {
                    return DBeaverIcons.getImage(UIIcon.SQL_SCRIPT);
                } else {
                    return DBeaverIcons.getImage(ri.getDataSource().getDriver().getIcon());
                }
            } else {
                return DBeaverIcons.getImage(DBIcon.TREE_FOLDER);
            }
        }

        @Override
        public String getText(Object element) {
            return ((ResourceInfo) element).getName();
        }

        @Override
        public String getToolTipText(Object element) {
            final DBPDataSourceContainer dataSource = ((ResourceInfo) element).getDataSource();
            return dataSource == null ? null : dataSource.getName();
        }

        @Override
        public Image getToolTipImage(Object element) {
            final DBPDataSourceContainer dataSource = ((ResourceInfo) element).getDataSource();
            return dataSource == null ? null : DBeaverIcons.getImage(dataSource.getDriver().getIcon());
        }
    });
    columnController.addColumn("Time", "Modification time", SWT.LEFT, true, true, new ColumnLabelProvider() {

        private SimpleDateFormat sdf = new SimpleDateFormat(DBConstants.DEFAULT_TIMESTAMP_FORMAT);

        @Override
        public String getText(Object element) {
            final File localFile = ((ResourceInfo) element).getLocalFile();
            if (localFile.isDirectory()) {
                return null;
            } else {
                return sdf.format(new Date(localFile.lastModified()));
            }
        }
    });
    columnController.addColumn("Info", "Script preview", SWT.LEFT, true, true, new ColumnLabelProvider() {

        @Override
        public String getText(Object element) {
            // ((ResourceInfo)element).getDescription();
            return "";
        }

        @Override
        public Color getForeground(Object element) {
            return getShell().getDisplay().getSystemColor(SWT.COLOR_WIDGET_DARK_SHADOW);
        }

        @Override
        public String getToolTipText(Object element) {
            final ResourceInfo ri = (ResourceInfo) element;
            String description = ri.getDescription();
            return description == null ? null : description.trim();
        }
    });
    columnController.createColumns();
    columnController.sortByColumn(1, SWT.UP);
    scriptTree.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetDefaultSelected(SelectionEvent e) {
            List<ResourceInfo> files = new ArrayList<>();
            for (Object item : ((IStructuredSelection) scriptViewer.getSelection()).toArray()) {
                if (!((ResourceInfo) item).isDirectory()) {
                    files.add((ResourceInfo) item);
                }
            }
            if (files.isEmpty()) {
                return;
            }
            cancelPressed();
            for (ResourceInfo ri : files) {
                SQLEditorHandlerOpenEditor.openResourceEditor(ScriptSelectorPanel.this.workbenchWindow, ri, navigatorContext);
            }
        }
    });
    scriptTree.addListener(SWT.PaintItem, event -> {
        final TreeItem item = (TreeItem) event.item;
        final ResourceInfo ri = (ResourceInfo) item.getData();
        if (ri != null && !ri.isDirectory() && CommonUtils.isEmpty(item.getText(2))) {
            UIUtils.asyncExec(() -> {
                if (!item.isDisposed()) {
                    item.setText(2, CommonUtils.getSingleLineString(CommonUtils.notEmpty(ri.getDescription())));
                }
            });
        }
    });
    this.patternText.addKeyListener(new KeyAdapter() {

        @Override
        public void keyPressed(KeyEvent e) {
            final Tree tree = scriptViewer.getTree();
            if (e.keyCode == SWT.ARROW_DOWN) {
                // scriptViewer.get
                scriptViewer.setSelection(new StructuredSelection(tree.getItem(0).getData()));
                tree.setFocus();
            } else if (e.keyCode == SWT.ARROW_UP) {
                scriptViewer.setSelection(new StructuredSelection(tree.getItem(tree.getItemCount() - 1).getData()));
                tree.setFocus();
            }
        }
    });
    closeOnFocusLost(patternText, scriptViewer.getTree(), newButton);
    scriptViewer.setInput(scriptFiles);
    UIUtils.expandAll(scriptViewer);
    final Tree tree = scriptViewer.getTree();
    final TreeColumn[] columns = tree.getColumns();
    columns[0].pack();
    columns[0].setWidth(columns[0].getWidth() + 10);
    columns[1].pack();
    columns[2].setWidth(200 * 8);
    UIUtils.asyncExec(scriptTree::setFocus);
    return composite;
}
Also used : IFile(org.eclipse.core.resources.IFile) KeyAdapter(org.eclipse.swt.events.KeyAdapter) Image(org.eclipse.swt.graphics.Image) KeyEvent(org.eclipse.swt.events.KeyEvent) SelectionEvent(org.eclipse.swt.events.SelectionEvent) ArrayList(java.util.ArrayList) List(java.util.List) DBPDataSourceContainer(org.jkiss.dbeaver.model.DBPDataSourceContainer) ResourceInfo(org.jkiss.dbeaver.ui.editors.sql.SQLEditorUtils.ResourceInfo) Color(org.eclipse.swt.graphics.Color) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) Date(java.util.Date) CoreException(org.eclipse.core.runtime.CoreException) GridData(org.eclipse.swt.layout.GridData) SimpleDateFormat(java.text.SimpleDateFormat) IFile(org.eclipse.core.resources.IFile) File(java.io.File)

Aggregations

File (java.io.File)2 SimpleDateFormat (java.text.SimpleDateFormat)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 List (java.util.List)2 IFile (org.eclipse.core.resources.IFile)2 CoreException (org.eclipse.core.runtime.CoreException)2 KeyAdapter (org.eclipse.swt.events.KeyAdapter)2 KeyEvent (org.eclipse.swt.events.KeyEvent)2 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)2 SelectionEvent (org.eclipse.swt.events.SelectionEvent)2 Color (org.eclipse.swt.graphics.Color)2 Image (org.eclipse.swt.graphics.Image)2 GridData (org.eclipse.swt.layout.GridData)2 DBPDataSourceContainer (org.jkiss.dbeaver.model.DBPDataSourceContainer)2 ResourceInfo (org.jkiss.dbeaver.ui.editors.sql.SQLEditorUtils.ResourceInfo)2