提交 a95e7744 编写于 作者: S Serge Rider

#11533 Variable add/remove + assign action

上级 c69f21f6
......@@ -102,6 +102,7 @@ import org.jkiss.dbeaver.ui.editors.sql.plan.ExplainPlanViewer;
import org.jkiss.dbeaver.ui.editors.sql.registry.SQLPresentationDescriptor;
import org.jkiss.dbeaver.ui.editors.sql.registry.SQLPresentationPanelDescriptor;
import org.jkiss.dbeaver.ui.editors.sql.registry.SQLPresentationRegistry;
import org.jkiss.dbeaver.ui.editors.sql.variables.AssignVariableAction;
import org.jkiss.dbeaver.ui.editors.sql.variables.SQLVariablesPanel;
import org.jkiss.dbeaver.ui.editors.text.ScriptPositionColumn;
import org.jkiss.dbeaver.ui.navigator.INavigatorModelView;
......@@ -1160,6 +1161,16 @@ public class SQLEditor extends SQLEditorBase implements
}
}
});
if (activeTab.getData() instanceof QueryResultsContainer) {
QueryResultsContainer rc = ((QueryResultsContainer) activeTab.getData());
if (rc.hasData()) {
manager.add(new AssignVariableAction(
SQLEditor.this,
rc.getQuery().getText(),
true,
false));
}
}
}
if (pinnedTabsCount > 1 || resultTabsCount > 1 || (activeTab != null && activeTab.getShowClose())) {
manager.add(new Separator());
......
......@@ -103,6 +103,8 @@ public class SQLEditorMessages extends NLS {
public static String action_result_tabs_unpin_tab;
public static String action_result_tabs_set_name;
public static String action_result_tabs_set_name_title;
public static String action_result_tabs_assign_variable;
public static String action_result_tabs_assign_variable_sql;
public static String action_popup_sqleditor_layout_horizontal;
public static String action_popup_sqleditor_layout_vertical;
......
......@@ -260,6 +260,8 @@ action_result_tabs_pin_tab = Pin tab
action_result_tabs_unpin_tab = Unpin tab
action_result_tabs_set_name = Rename tab
action_result_tabs_set_name_title = New tab name
action_result_tabs_assign_variable = Assign variable
action_result_tabs_assign_variable_sql = Assign variable with query text value
action_popup_sqleditor_layout_detached = Detached
action_popup_sqleditor_layout_horizontal = Horizontal
......
/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2021 DBeaver Corp and others
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jkiss.dbeaver.ui.editors.sql.variables;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.PartInitException;
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.model.exec.DBCExecutionContext;
import org.jkiss.dbeaver.ui.UIUtils;
import org.jkiss.dbeaver.ui.dialogs.EnterNameDialog;
import org.jkiss.dbeaver.ui.editors.StringEditorInput;
import org.jkiss.dbeaver.ui.editors.SubEditorSite;
import org.jkiss.dbeaver.ui.editors.sql.SQLEditor;
import org.jkiss.dbeaver.ui.editors.sql.SQLEditorBase;
import org.jkiss.dbeaver.ui.editors.sql.internal.SQLEditorMessages;
import org.jkiss.dbeaver.utils.GeneralUtils;
public class AssignVariableAction extends Action {
static protected final Log log = Log.getLog(AssignVariableAction.class);
private final SQLEditor editor;
private final String queryText;
private final boolean isQuery;
private final boolean isEditable;
private SQLEditorBase valueEditor;
public AssignVariableAction(SQLEditor editor, String varValue, boolean isQuery, boolean isEditable) {
super(SQLEditorMessages.action_result_tabs_assign_variable);
this.editor = editor;
this.queryText = varValue;
this.isQuery = isQuery;
this.isEditable = isEditable;
}
@Override
public void run() {
EnterNameDialog dialog = new EnterNameDialog(
editor.getEditorControlWrapper().getShell(),
isQuery ? SQLEditorMessages.action_result_tabs_assign_variable_sql : SQLEditorMessages.action_result_tabs_assign_variable,
"") {
@Override
protected IDialogSettings getDialogBoundsSettings() {
return UIUtils.getDialogSettings("DBeaver.SQLEditor.AssignVariableDialog"); //$NON-NLS-1$
}
@Override
protected Composite createDialogArea(Composite parent) {
final Composite area = super.createDialogArea(parent);
valueEditor = new SQLEditorBase() {
@Nullable
@Override
public DBCExecutionContext getExecutionContext() {
return editor.getExecutionContext();
}
};
try {
valueEditor.init(new SubEditorSite(editor.getSite()),
new StringEditorInput("Variable value", queryText, !isEditable, GeneralUtils.getDefaultFileEncoding()));
} catch (PartInitException e) {
log.error(e);
}
Composite editorPH = UIUtils.createComposite(area, 1);
editorPH.setLayoutData(new GridData(GridData.FILL_BOTH));
UIUtils.createControlLabel(editorPH, isQuery ? "Query" : "Value");
valueEditor.createPartControl(editorPH);
valueEditor.getEditorControlWrapper().setLayoutData(new GridData(GridData.FILL_BOTH));
valueEditor.setWordWrap(true);
valueEditor.reloadSyntaxRules();
UIUtils.asyncExec(() -> propNameText.setFocus());
return area;
}
};
if (dialog.open() == IDialogConstants.OK_ID) {
editor.getGlobalScriptContext().setVariable(dialog.getResult(), queryText);
}
}
}
/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2021 DBeaver Corp and others
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jkiss.dbeaver.ui.editors.sql.variables;
import org.eclipse.jface.action.Action;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.ui.UIUtils;
import org.jkiss.dbeaver.ui.editors.sql.SQLEditor;
import org.jkiss.dbeaver.ui.editors.sql.internal.SQLEditorMessages;
public class RemoveVariableAction extends Action {
static protected final Log log = Log.getLog(RemoveVariableAction.class);
private final SQLEditor editor;
private final String varName;
public RemoveVariableAction(SQLEditor editor, String varName) {
super(SQLEditorMessages.action_result_tabs_assign_variable);
this.editor = editor;
this.varName = varName;
}
@Override
public void run() {
if (UIUtils.confirmAction(
editor.getSite().getShell(),
"Delete variable '" + varName + "'?",
"Delete variable '" + varName + "'?"))
{
editor.getGlobalScriptContext().removeVariable(varName);
}
}
}
......@@ -123,6 +123,7 @@ public class SQLVariablesPanel extends Composite implements DBCScriptContextList
log.error(e);
}
valueEditor.createPartControl(editorPH);
valueEditor.setWordWrap(true);
valueEditor.reloadSyntaxRules();
//valueEditor.getEditorControl().setEnabled(false);
......@@ -303,14 +304,21 @@ public class SQLVariablesPanel extends Composite implements DBCScriptContextList
addAction = new Action("Add variable", DBeaverIcons.getImageDescriptor(UIIcon.ADD)) {
@Override
public void run() {
super.run();
AssignVariableAction action = new AssignVariableAction(mainEditor, "", false, true);
action.run();
}
};
contributionManager.add(addAction);
deleteAction = new Action("Delete variable", DBeaverIcons.getImageDescriptor(UIIcon.DELETE)) {
@Override
public void run() {
super.run();
if (!varsTable.getSelection().isEmpty()) {
Object varElement = ((IStructuredSelection) varsTable.getSelection()).getFirstElement();
if (varElement instanceof DBCScriptContext.VariableInfo) {
RemoveVariableAction action = new RemoveVariableAction(mainEditor, ((DBCScriptContext.VariableInfo) varElement).name);
action.run();
}
}
}
};
deleteAction.setEnabled(false);
......
......@@ -31,7 +31,7 @@ public class EnterNameDialog extends Dialog {
private String propertyName;
private String propertyValue;
private Text propNameText;
protected Text propNameText;
private String result;
public EnterNameDialog(Shell parentShell, String propertyName, String propertyValue)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册