use of org.jkiss.dbeaver.ui.editors.SimpleDatabaseEditorContext in project dbeaver by serge-rider.
the class NavigatorHandlerAssociateScript method execute.
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
final Shell activeShell = HandlerUtil.getActiveShell(event);
List<IFile> scripts = new ArrayList<>();
final ISelection selection = HandlerUtil.getCurrentSelection(event);
if (!selection.isEmpty() && selection instanceof IStructuredSelection) {
for (Iterator iter = ((IStructuredSelection) selection).iterator(); iter.hasNext(); ) {
final DBNNode node = RuntimeUtils.getObjectAdapter(iter.next(), DBNNode.class);
if (node instanceof DBNResource) {
IResource resource = ((DBNResource) node).getResource();
if (resource instanceof IFile) {
scripts.add((IFile) resource);
}
}
}
}
if (!scripts.isEmpty()) {
SelectDataSourceDialog dialog = new SelectDataSourceDialog(activeShell, DBWorkbench.getPlatform().getWorkspace().getProject(scripts.get(0).getProject()), null);
if (dialog.open() == IDialogConstants.CANCEL_ID) {
return null;
}
DBPDataSourceContainer dataSource = dialog.getDataSource();
for (IFile script : scripts) {
EditorUtils.setFileDataSource(script, new SimpleDatabaseEditorContext(dataSource));
DBNUtils.refreshNavigatorResource(script, dataSource);
}
}
return null;
}
use of org.jkiss.dbeaver.ui.editors.SimpleDatabaseEditorContext in project dbeaver by dbeaver.
the class ScriptsImportWizard method importScripts.
private int importScripts(DBRProgressMonitor monitor, ScriptsImportData importData) throws IOException, CoreException {
List<Pattern> masks = new ArrayList<>();
// $NON-NLS-1$
StringTokenizer st = new StringTokenizer(importData.getFileMasks(), ",; ");
while (st.hasMoreTokens()) {
String mask = st.nextToken().trim();
if (!CommonUtils.isEmpty(mask)) {
// $NON-NLS-1$ //$NON-NLS-2$
mask = mask.replace("*", ".*");
masks.add(Pattern.compile(mask));
}
}
List<File> filesToImport = new ArrayList<>();
collectFiles(importData.getInputDir(), masks, filesToImport);
if (filesToImport.isEmpty()) {
return 0;
}
// Use null monitor for resource actions to not break our main monitor
final IProgressMonitor nullMonitor = new NullProgressMonitor();
// Import scripts
int imported = filesToImport.size();
monitor.beginTask(CoreMessages.dialog_scripts_import_wizard_monitor_import_scripts, imported);
for (File file : filesToImport) {
// Create dirs
monitor.subTask(file.getName());
List<File> path = new ArrayList<>();
for (File parent = file.getParentFile(); !parent.equals(importData.getInputDir()); parent = parent.getParentFile()) {
path.add(0, parent);
}
// Get target dir
final IResource srcResource = importData.getImportDir().getResource();
if (!(srcResource instanceof IFolder)) {
// $NON-NLS-1$ //$NON-NLS-2$
log.warn("Resource '" + srcResource + "' is not a folder");
continue;
}
IFolder targetDir = (IFolder) srcResource;
for (File folder : path) {
targetDir = targetDir.getFolder(folder.getName());
if (!targetDir.exists()) {
targetDir.create(true, true, nullMonitor);
}
}
String targetName = file.getName();
if (!targetName.toLowerCase().endsWith("." + SQLEditorUtils.SCRIPT_FILE_EXTENSION)) {
// $NON-NLS-1$
// $NON-NLS-1$
targetName += "." + SQLEditorUtils.SCRIPT_FILE_EXTENSION;
}
final IFile targetFile = targetDir.getFile(targetName);
if (targetFile.exists()) {
if (importData.isOverwriteFiles()) {
// $NON-NLS-1$ //$NON-NLS-2$
log.warn("Overwriting file '" + targetFile.getFullPath() + "'");
targetFile.delete(true, true, RuntimeUtils.getNestedMonitor(monitor));
} else {
// $NON-NLS-1$ //$NON-NLS-2$
log.warn("File '" + targetFile.getFullPath() + "' already exists - skipped");
imported--;
continue;
}
}
// Copy file
try (FileInputStream in = new FileInputStream(file)) {
targetFile.create(in, true, nullMonitor);
}
// Set datasource
if (importData.getDataSourceContainer() != null) {
EditorUtils.setFileDataSource(targetFile, new SimpleDatabaseEditorContext(importData.getDataSourceContainer()));
}
// Done
monitor.worked(1);
}
monitor.done();
return imported;
}
use of org.jkiss.dbeaver.ui.editors.SimpleDatabaseEditorContext in project dbeaver by serge-rider.
the class ScriptsImportWizard method importScripts.
private int importScripts(DBRProgressMonitor monitor, ScriptsImportData importData) throws IOException, CoreException {
List<Pattern> masks = new ArrayList<>();
// $NON-NLS-1$
StringTokenizer st = new StringTokenizer(importData.getFileMasks(), ",; ");
while (st.hasMoreTokens()) {
String mask = st.nextToken().trim();
if (!CommonUtils.isEmpty(mask)) {
// $NON-NLS-1$ //$NON-NLS-2$
mask = mask.replace("*", ".*");
masks.add(Pattern.compile(mask));
}
}
List<File> filesToImport = new ArrayList<>();
collectFiles(importData.getInputDir(), masks, filesToImport);
if (filesToImport.isEmpty()) {
return 0;
}
// Use null monitor for resource actions to not break our main monitor
final IProgressMonitor nullMonitor = new NullProgressMonitor();
// Import scripts
int imported = filesToImport.size();
monitor.beginTask(CoreMessages.dialog_scripts_import_wizard_monitor_import_scripts, imported);
for (File file : filesToImport) {
// Create dirs
monitor.subTask(file.getName());
List<File> path = new ArrayList<>();
for (File parent = file.getParentFile(); !parent.equals(importData.getInputDir()); parent = parent.getParentFile()) {
path.add(0, parent);
}
// Get target dir
final IResource srcResource = importData.getImportDir().getResource();
if (!(srcResource instanceof IFolder)) {
// $NON-NLS-1$ //$NON-NLS-2$
log.warn("Resource '" + srcResource + "' is not a folder");
continue;
}
IFolder targetDir = (IFolder) srcResource;
for (File folder : path) {
targetDir = targetDir.getFolder(folder.getName());
if (!targetDir.exists()) {
targetDir.create(true, true, nullMonitor);
}
}
String targetName = file.getName();
if (!targetName.toLowerCase().endsWith("." + SQLEditorUtils.SCRIPT_FILE_EXTENSION)) {
// $NON-NLS-1$
// $NON-NLS-1$
targetName += "." + SQLEditorUtils.SCRIPT_FILE_EXTENSION;
}
final IFile targetFile = targetDir.getFile(targetName);
if (targetFile.exists()) {
if (importData.isOverwriteFiles()) {
// $NON-NLS-1$ //$NON-NLS-2$
log.warn("Overwriting file '" + targetFile.getFullPath() + "'");
targetFile.delete(true, true, RuntimeUtils.getNestedMonitor(monitor));
} else {
// $NON-NLS-1$ //$NON-NLS-2$
log.warn("File '" + targetFile.getFullPath() + "' already exists - skipped");
imported--;
continue;
}
}
// Copy file
try (FileInputStream in = new FileInputStream(file)) {
targetFile.create(in, true, nullMonitor);
}
// Set datasource
if (importData.getDataSourceContainer() != null) {
EditorUtils.setFileDataSource(targetFile, new SimpleDatabaseEditorContext(importData.getDataSourceContainer()));
}
// Done
monitor.worked(1);
}
monitor.done();
return imported;
}
use of org.jkiss.dbeaver.ui.editors.SimpleDatabaseEditorContext in project dbeaver by dbeaver.
the class NavigatorHandlerAssociateScript method execute.
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
final Shell activeShell = HandlerUtil.getActiveShell(event);
List<IFile> scripts = new ArrayList<>();
final ISelection selection = HandlerUtil.getCurrentSelection(event);
if (!selection.isEmpty() && selection instanceof IStructuredSelection) {
for (Iterator iter = ((IStructuredSelection) selection).iterator(); iter.hasNext(); ) {
final DBNNode node = RuntimeUtils.getObjectAdapter(iter.next(), DBNNode.class);
if (node instanceof DBNResource) {
IResource resource = ((DBNResource) node).getResource();
if (resource instanceof IFile) {
scripts.add((IFile) resource);
}
}
}
}
if (!scripts.isEmpty()) {
SelectDataSourceDialog dialog = new SelectDataSourceDialog(activeShell, DBWorkbench.getPlatform().getWorkspace().getProject(scripts.get(0).getProject()), null);
if (dialog.open() == IDialogConstants.CANCEL_ID) {
return null;
}
DBPDataSourceContainer dataSource = dialog.getDataSource();
for (IFile script : scripts) {
EditorUtils.setFileDataSource(script, new SimpleDatabaseEditorContext(dataSource));
DBNUtils.refreshNavigatorResource(script, dataSource);
}
}
return null;
}
Aggregations