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;
}
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;
}
Aggregations