use of org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress in project dbeaver by serge-rider.
the class EditForeignKeyPage method handleRefTableSelect.
private void handleRefTableSelect(ISelection selection) {
DBNDatabaseNode refTableNode = null;
if (!selection.isEmpty() && selection instanceof IStructuredSelection && ((IStructuredSelection) selection).size() == 1) {
final Object element = ((IStructuredSelection) selection).getFirstElement();
if (element instanceof DBNDatabaseNode && ((DBNDatabaseNode) element).getObject() instanceof DBSTable && ((DBNDatabaseNode) element).getObject().isPersisted()) {
refTableNode = (DBNDatabaseNode) element;
}
}
if (refTableNode != null) {
if (refTableNode.getObject() == curRefTable) {
// The same selection
return;
} else {
curRefTable = (DBSTable) refTableNode.getObject();
}
}
uniqueKeyCombo.removeAll();
try {
curConstraints = new ArrayList<>();
curConstraint = null;
if (refTableNode != null) {
final DBSTable refTable = (DBSTable) refTableNode.getObject();
DBeaverUI.runInProgressService(new DBRRunnableWithProgress() {
@Override
public void run(DBRProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
try {
// Cache own table columns
ownTable.getAttributes(monitor);
// Cache ref table columns
refTable.getAttributes(monitor);
// Get constraints
final Collection<? extends DBSTableConstraint> constraints = refTable.getConstraints(monitor);
if (!CommonUtils.isEmpty(constraints)) {
for (DBSTableConstraint constraint : constraints) {
if (constraint.getConstraintType().isUnique()) {
curConstraints.add(constraint);
}
}
}
} catch (DBException e) {
throw new InvocationTargetException(e);
}
}
});
}
for (DBSTableConstraint constraint : curConstraints) {
uniqueKeyCombo.add(constraint.getName());
}
uniqueKeyCombo.select(0);
uniqueKeyCombo.setEnabled(curConstraints.size() > 1);
if (curConstraints.size() == 1) {
curConstraint = curConstraints.get(0);
}
} catch (InvocationTargetException e) {
UIUtils.showErrorDialog(getShell(), CoreMessages.dialog_struct_edit_fk_error_load_constraints_title, CoreMessages.dialog_struct_edit_fk_error_load_constraints_message, e.getTargetException());
} catch (InterruptedException e) {
// do nothing
}
handleUniqueKeySelect();
updatePageState();
}
use of org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress in project dbeaver by serge-rider.
the class DialogUtils method saveToFile.
public static void saveToFile(IValueController controller) {
if (!(controller.getValue() instanceof DBDContent)) {
log.error(CoreMessages.model_jdbc_bad_content_value_ + controller.getValue());
return;
}
Shell shell = UIUtils.getShell(controller.getValueSite());
final File saveFile = selectFileForSave(shell, controller.getValueName());
if (saveFile == null) {
return;
}
final DBDContent value = (DBDContent) controller.getValue();
try {
DBeaverUI.runInProgressService(new DBRRunnableWithProgress() {
@Override
public void run(DBRProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
try {
DBDContentStorage storage = value.getContents(monitor);
if (ContentUtils.isTextContent(value)) {
try (Reader cr = storage.getContentReader()) {
ContentUtils.saveContentToFile(cr, saveFile, GeneralUtils.UTF8_ENCODING, monitor);
}
} else {
try (InputStream cs = storage.getContentStream()) {
ContentUtils.saveContentToFile(cs, saveFile, monitor);
}
}
} catch (Exception e) {
throw new InvocationTargetException(e);
}
}
});
} catch (InvocationTargetException e) {
UIUtils.showErrorDialog(shell, CoreMessages.model_jdbc_could_not_save_content, //$NON-NLS-2$
CoreMessages.model_jdbc_could_not_save_content_to_file_ + saveFile.getAbsolutePath() + "'", e.getTargetException());
} catch (InterruptedException e) {
// do nothing
}
}
use of org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress in project dbeaver by serge-rider.
the class ERDResourceHandler method createDiagram.
public static IFile createDiagram(final EntityDiagram copyFrom, final String title, IFolder folder, DBRProgressMonitor monitor) throws DBException {
if (folder == null) {
try {
folder = getDiagramsFolder(DBeaverCore.getInstance().getProjectRegistry().getActiveProject(), true);
} catch (CoreException e) {
throw new DBException("Can't obtain folder for diagram", e);
}
}
if (folder == null) {
throw new DBException("Can't detect folder for diagram");
}
final IFile file = ContentUtils.getUniqueFile(folder, CommonUtils.escapeFileName(title), ERD_EXT);
try {
DBRRunnableWithProgress runnable = new DBRRunnableWithProgress() {
@Override
public void run(DBRProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
try {
EntityDiagram newDiagram = copyFrom == null ? new EntityDiagram(null, "<Diagram>") : copyFrom.copy();
newDiagram.setName(title);
newDiagram.setLayoutManualAllowed(true);
newDiagram.setLayoutManualDesired(true);
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
DiagramLoader.save(monitor, null, newDiagram, false, buffer);
InputStream data = new ByteArrayInputStream(buffer.toByteArray());
file.create(data, true, RuntimeUtils.getNestedMonitor(monitor));
} catch (Exception e) {
throw new InvocationTargetException(e);
}
}
};
if (monitor == null) {
DBeaverUI.runInProgressService(runnable);
} else {
runnable.run(monitor);
}
} catch (InvocationTargetException e) {
throw new DBException("Error creating diagram", e.getTargetException());
} catch (InterruptedException e) {
// interrupted
}
return file;
}
use of org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress in project dbeaver by dbeaver.
the class MockDataSettings method init.
// populate attribute generators properties map
public void init(MockDataExecuteWizard wizard) throws DBException {
List<DBSDataManipulator> databaseObjects = wizard.getDatabaseObjects();
// TODO only the first
DBSDataManipulator dataManipulator = databaseObjects.iterator().next();
entity = (DBSEntity) dataManipulator;
attributes = new ArrayList<>();
try {
DBeaverUI.run(wizard.getContainer(), true, true, new DBRRunnableWithProgress() {
@Override
public void run(DBRProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
try {
attributes.addAll(DBUtils.getRealAttributes(entity.getAttributes(monitor)));
MockGeneratorRegistry generatorRegistry = MockGeneratorRegistry.getInstance();
for (DBSAttributeBase attribute : attributes) {
AttributeGeneratorProperties generatorProperties = new AttributeGeneratorProperties(attribute);
attributeGenerators.put(attribute.getName(), generatorProperties);
// ((JDBCColumnKeyType) attribute).isInUniqueKey()
List<DBSEntityReferrer> attributeReferrers = DBUtils.getAttributeReferrers(monitor, (DBSEntityAttribute) attribute);
if (!CommonUtils.isEmpty(attributeReferrers)) {
MockGeneratorDescriptor generator = generatorRegistry.getGenerator(FK_GENERATOR_ID);
putGenerator(generatorProperties, generator);
} else {
List<MockGeneratorDescriptor> generators = generatorRegistry.findAllGenerators(dataManipulator.getDataSource(), attribute);
for (MockGeneratorDescriptor generator : generators) {
putGenerator(generatorProperties, generator);
}
}
}
} catch (DBException e) {
throw new InvocationTargetException(e);
}
}
});
} catch (InvocationTargetException e) {
DBUserInterface.getInstance().showError("Transfer init failed", "Can't start data transfer", e.getTargetException());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
use of org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress in project dbeaver by dbeaver.
the class CompileHandler method execute.
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
final IWorkbenchPart activePart = HandlerUtil.getActiveEditor(event);
final List<OracleSourceObject> objects = getSelectedObjects(event);
if (!objects.isEmpty()) {
if (activePart instanceof EntityEditor) {
// Save editor before compile
// USe null monitor as entity editor has its own detached job for save
EntityEditor entityEditor = (EntityEditor) activePart;
if (entityEditor.isDirty()) {
NullProgressMonitor monitor = new NullProgressMonitor();
entityEditor.doSave(monitor);
if (monitor.isCanceled()) {
// Save failed - doesn't make sense to compile
return null;
}
}
}
final Shell activeShell = HandlerUtil.getActiveShell(event);
if (objects.size() == 1) {
final OracleSourceObject unit = objects.get(0);
DBCSourceHost sourceHost = null;
if (activePart != null) {
sourceHost = RuntimeUtils.getObjectAdapter(activePart, DBCSourceHost.class);
if (sourceHost == null) {
sourceHost = activePart.getAdapter(DBCSourceHost.class);
}
}
if (sourceHost != null && sourceHost.getSourceObject() != unit) {
sourceHost = null;
}
final DBCCompileLog compileLog = sourceHost == null ? new DBCCompileLogBase() : sourceHost.getCompileLog();
compileLog.clearLog();
Throwable error = null;
try {
DBeaverUI.runInProgressService(new DBRRunnableWithProgress() {
@Override
public void run(DBRProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
try {
compileUnit(monitor, compileLog, unit);
} catch (DBCException e) {
throw new InvocationTargetException(e);
}
}
});
if (compileLog.getError() != null) {
error = compileLog.getError();
}
} catch (InvocationTargetException e) {
error = e.getTargetException();
} catch (InterruptedException e) {
return null;
}
if (error != null) {
DBUserInterface.getInstance().showError("Unexpected compilation error", null, error);
} else if (!CommonUtils.isEmpty(compileLog.getErrorStack())) {
// Show compile errors
int line = -1, position = -1;
StringBuilder fullMessage = new StringBuilder();
for (DBCCompileError oce : compileLog.getErrorStack()) {
fullMessage.append(oce.toString()).append(GeneralUtils.getDefaultLineSeparator());
if (line < 0) {
line = oce.getLine();
position = oce.getPosition();
}
}
// If compiled object is currently open in editor - try to position on error line
if (sourceHost != null && sourceHost.getSourceObject() == unit && line > 0 && position > 0) {
sourceHost.positionSource(line, position);
activePart.getSite().getPage().activate(activePart);
}
String errorTitle = unit.getName() + " compilation failed";
if (sourceHost != null) {
sourceHost.setCompileInfo(errorTitle, true);
sourceHost.showCompileLog();
}
DBUserInterface.getInstance().showError(errorTitle, fullMessage.toString());
} else {
String message = unit.getName() + " compiled successfully";
if (sourceHost != null) {
sourceHost.setCompileInfo(message, true);
}
UIUtils.showMessageBox(activeShell, "Done", message, SWT.ICON_INFORMATION);
}
} else {
OracleCompilerDialog dialog = new OracleCompilerDialog(activeShell, objects);
dialog.open();
}
}
return null;
}
Aggregations