use of org.jkiss.dbeaver.model.edit.DBECommandContext in project dbeaver by serge-rider.
the class RedoChangesHandler method updateElement.
@Override
public void updateElement(UIElement element, Map parameters) {
IWorkbenchWindow workbenchWindow = element.getServiceLocator().getService(IWorkbenchWindow.class);
if (workbenchWindow == null || workbenchWindow.getActivePage() == null) {
return;
}
final IEditorPart activeEditor = workbenchWindow.getActivePage().getActiveEditor();
if (activeEditor instanceof EntityEditor) {
final DBECommandContext commandContext = ((EntityEditor) activeEditor).getCommandContext();
String text = "Redo";
if (commandContext != null && commandContext.getRedoCommand() != null) {
text += " " + commandContext.getRedoCommand().getTitle();
}
element.setText(text);
}
}
use of org.jkiss.dbeaver.model.edit.DBECommandContext in project dbeaver by serge-rider.
the class EntityEditor method saveCommandContext.
private boolean saveCommandContext(final DBRProgressMonitor monitor) {
int previewResult = IDialogConstants.PROCEED_ID;
if (DBeaverCore.getGlobalPreferenceStore().getBoolean(DBeaverPreferences.NAVIGATOR_SHOW_SQL_PREVIEW)) {
monitor.beginTask(CoreMessages.editors_entity_monitor_preview_changes, 1);
previewResult = showChanges(true);
monitor.done();
}
if (previewResult != IDialogConstants.PROCEED_ID) {
return true;
}
monitor.beginTask("Save entity", 1);
Throwable error = null;
final DBECommandContext commandContext = getCommandContext();
if (commandContext == null) {
log.warn("Null command context");
return true;
}
try {
commandContext.saveChanges(monitor);
} catch (DBException e) {
error = e;
}
if (getDatabaseObject() instanceof DBPStatefulObject) {
try {
((DBPStatefulObject) getDatabaseObject()).refreshObjectState(monitor);
} catch (DBCException e) {
// Just report an error
log.error(e);
}
}
if (error == null) {
// Refresh underlying node
// It'll refresh database object and all it's descendants
// So we'll get actual data from database
final DBNDatabaseNode treeNode = getEditorInput().getNavigatorNode();
try {
DBeaverUI.runInProgressService(new DBRRunnableWithProgress() {
@Override
public void run(DBRProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
try {
treeNode.refreshNode(monitor, DBNEvent.FORCE_REFRESH);
} catch (DBException e) {
throw new InvocationTargetException(e);
}
}
});
} catch (InvocationTargetException e) {
error = e.getTargetException();
} catch (InterruptedException e) {
// ok
}
}
monitor.done();
if (error == null) {
return true;
} else {
// Try to handle error in nested editors
final Throwable vError = error;
DBeaverUI.syncExec(new Runnable() {
@Override
public void run() {
final IErrorVisualizer errorVisualizer = getAdapter(IErrorVisualizer.class);
if (errorVisualizer != null) {
errorVisualizer.visualizeError(monitor, vError);
}
}
});
// Show error dialog
DBeaverUI.asyncExec(new Runnable() {
@Override
public void run() {
UIUtils.showErrorDialog(getSite().getShell(), "Can't save '" + getDatabaseObject().getName() + "'", null, vError);
}
});
return false;
}
}
use of org.jkiss.dbeaver.model.edit.DBECommandContext in project dbeaver by serge-rider.
the class EntityEditor method createPages.
@Override
protected void createPages() {
final IDatabaseEditorInput editorInput = getEditorInput();
if (editorInput instanceof DatabaseLazyEditorInput) {
try {
addPage(new ProgressEditorPart(this), editorInput);
setPageText(0, "Initializing ...");
setActivePage(0);
} catch (PartInitException e) {
log.error(e);
}
return;
} else if (editorInput instanceof ErrorEditorInput) {
ErrorEditorInput errorInput = (ErrorEditorInput) editorInput;
try {
addPage(new ErrorEditorPartEx(errorInput.getError()), errorInput);
setPageImage(0, UIUtils.getShardImage(ISharedImages.IMG_OBJS_ERROR_TSK));
setPageText(0, "Error");
setActivePage(0);
} catch (PartInitException e) {
log.error(e);
}
return;
}
// Command listener
commandListener = new DBECommandAdapter() {
@Override
public void onCommandChange(DBECommand command) {
DBeaverUI.syncExec(new Runnable() {
@Override
public void run() {
firePropertyChange(IEditorPart.PROP_DIRTY);
}
});
}
};
DBECommandContext commandContext = getCommandContext();
if (commandContext != null) {
commandContext.addCommandListener(commandListener);
}
// Property listener
addPropertyListener(new IPropertyListener() {
@Override
public void propertyChanged(Object source, int propId) {
if (propId == IEditorPart.PROP_DIRTY) {
EntityEditorPropertyTester.firePropertyChange(EntityEditorPropertyTester.PROP_DIRTY);
EntityEditorPropertyTester.firePropertyChange(EntityEditorPropertyTester.PROP_CAN_UNDO);
EntityEditorPropertyTester.firePropertyChange(EntityEditorPropertyTester.PROP_CAN_REDO);
}
}
});
super.createPages();
DBSObject databaseObject = editorInput.getDatabaseObject();
EditorDefaults editorDefaults = null;
if (databaseObject == null) {
// Weird
log.debug("Null database object in EntityEditor");
} else {
synchronized (defaultPageMap) {
editorDefaults = defaultPageMap.get(databaseObject.getClass().getName());
}
EntityEditorsRegistry editorsRegistry = EntityEditorsRegistry.getInstance();
// Add object editor page
EntityEditorDescriptor defaultEditor = editorsRegistry.getMainEntityEditor(databaseObject);
hasPropertiesEditor = false;
if (defaultEditor != null) {
hasPropertiesEditor = addEditorTab(defaultEditor);
}
if (hasPropertiesEditor) {
DBNNode node = editorInput.getNavigatorNode();
int propEditorIndex = getPageCount() - 1;
setPageText(propEditorIndex, CoreMessages.editors_entity_properties_text);
setPageToolTip(propEditorIndex, node.getNodeType() + CoreMessages.editors_entity_properties_tooltip_suffix);
setPageImage(propEditorIndex, DBeaverIcons.getImage(node.getNodeIconDefault()));
}
}
// Add contributed pages
addContributions(EntityEditorDescriptor.POSITION_PROPS);
addContributions(EntityEditorDescriptor.POSITION_START);
addContributions(EntityEditorDescriptor.POSITION_MIDDLE);
// Add contributed pages
addContributions(EntityEditorDescriptor.POSITION_END);
String defPageId = editorInput.getDefaultPageId();
String defFolderId = editorInput.getDefaultFolderId();
if (defPageId == null && editorDefaults != null) {
defPageId = editorDefaults.pageId;
}
if (defPageId != null) {
IEditorPart defEditorPage = editorMap.get(defPageId);
if (defEditorPage != null) {
setActiveEditor(defEditorPage);
}
} else {
setActiveEditor(getEditor(0));
}
this.activeEditor = getActiveEditor();
if (activeEditor instanceof ITabbedFolderContainer) {
if (defFolderId == null && editorDefaults != null) {
defFolderId = editorDefaults.folderId;
}
if (defFolderId != null) {
((ITabbedFolderContainer) activeEditor).switchFolder(defFolderId);
}
}
UIUtils.setHelp(getContainer(), IHelpContextIds.CTX_ENTITY_EDITOR);
}
use of org.jkiss.dbeaver.model.edit.DBECommandContext in project dbeaver by serge-rider.
the class EntityEditor method showChanges.
public int showChanges(boolean allowSave) {
DBECommandContext commandContext = getCommandContext();
if (commandContext == null) {
return IDialogConstants.CANCEL_ID;
}
Collection<? extends DBECommand> commands = commandContext.getFinalCommands();
StringBuilder script = new StringBuilder();
for (DBECommand command : commands) {
try {
command.validateCommand();
} catch (final DBException e) {
log.debug(e);
DBeaverUI.syncExec(new Runnable() {
@Override
public void run() {
UIUtils.showErrorDialog(getSite().getShell(), "Validation", e.getMessage());
}
});
return IDialogConstants.CANCEL_ID;
}
script.append(SQLUtils.generateScript(commandContext.getExecutionContext().getDataSource(), command.getPersistActions(), false));
}
ChangesPreviewer changesPreviewer = new ChangesPreviewer(script, allowSave);
DBeaverUI.syncExec(changesPreviewer);
return changesPreviewer.getResult();
}
use of org.jkiss.dbeaver.model.edit.DBECommandContext in project dbeaver by serge-rider.
the class EntityEditor method revertChanges.
public void revertChanges() {
if (isDirty()) {
if (ConfirmationDialog.showConfirmDialog(null, DBeaverPreferences.CONFIRM_ENTITY_REVERT, ConfirmationDialog.QUESTION, getDatabaseObject().getName()) != IDialogConstants.YES_ID) {
return;
}
DBECommandContext commandContext = getCommandContext();
if (commandContext != null) {
commandContext.resetChanges();
}
firePropertyChange(IEditorPart.PROP_DIRTY);
}
}
Aggregations