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

#1492 SQL converter settings dialog

上级 a96df967
......@@ -17,16 +17,24 @@
package org.jkiss.dbeaver.ui.editors.sql.convert;
import org.eclipse.jface.text.IDocument;
import org.jkiss.code.NotNull;
import org.jkiss.dbeaver.model.sql.SQLDialect;
import org.jkiss.dbeaver.model.sql.SQLSyntaxManager;
import java.util.Map;
/**
* SQL code converter
*/
public interface ISQLCodeConverter {
public interface ISQLTextConverter {
@NotNull
String convertSQL(@NotNull String source, @NotNull Map<String, Object> options);
String convertText(@NotNull SQLDialect dialect,
@NotNull SQLSyntaxManager syntaxManager,
@NotNull IDocument document,
int startPos,
int length,
@NotNull Map<String, Object> options);
}
......@@ -60,8 +60,8 @@ public class SQLTargetConverterDescriptor extends AbstractContextDescriptor {
return icon;
}
public ISQLCodeConverter createInstance() throws DBException {
return implClass.createInstance(ISQLCodeConverter.class);
public ISQLTextConverter createInstance() throws DBException {
return implClass.createInstance(ISQLTextConverter.class);
}
@Override
......
/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2017 Serge Rider (serge@jkiss.org)
*
* 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.convert;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.rules.IToken;
import org.jkiss.code.NotNull;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.model.sql.SQLDialect;
import org.jkiss.dbeaver.model.sql.SQLSyntaxManager;
import org.jkiss.dbeaver.ui.TextUtils;
import org.jkiss.dbeaver.ui.editors.sql.syntax.SQLRuleManager;
import org.jkiss.dbeaver.ui.editors.sql.syntax.tokens.SQLCommentToken;
import org.jkiss.utils.Pair;
import java.util.Map;
/**
* UnformattedSQLConverter
*/
public class UnformattedSQLConverter implements ISQLTextConverter {
private static final Log log = Log.getLog(UnformattedSQLConverter.class);
@NotNull
@Override
public String convertText(
@NotNull SQLDialect dialect,
@NotNull SQLSyntaxManager syntaxManager,
@NotNull IDocument document,
int startPos,
int length,
@NotNull Map<String, Object> options)
{
StringBuilder result = new StringBuilder();
SQLRuleManager ruleManager = new SQLRuleManager(syntaxManager);
ruleManager.setRange(document, startPos, length);
String[] singleLineComments = dialect.getSingleLineComments();
Pair<String, String> multiLineComments = dialect.getMultiLineComments();
boolean lastWhitespace = false;
try {
for (;;) {
IToken token = ruleManager.nextToken();
if (token.isEOF()) {
break;
}
int tokenOffset = ruleManager.getTokenOffset();
final int tokenLength = ruleManager.getTokenLength();
if (token.isWhitespace()) {
if (!lastWhitespace) {
result.append(' ');
}
lastWhitespace = true;
} else if (token instanceof SQLCommentToken) {
String comment = document.get(tokenOffset, tokenLength);
for (String slc : singleLineComments) {
if (comment.startsWith(slc)) {
if (multiLineComments != null) {
comment = multiLineComments.getFirst() + comment.substring(slc.length()) + multiLineComments.getSecond();
}
break;
}
}
comment = TextUtils.compactWhiteSpaces(comment);
result.append(comment);
} else {
lastWhitespace = false;
result.append(document.get(tokenOffset, tokenLength));
}
}
} catch (BadLocationException e) {
log.error("Error unformatting SQL", e);
}
return result.toString().trim();
}
}
......@@ -19,16 +19,31 @@ package org.jkiss.dbeaver.ui.editors.sql.handlers;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.jface.text.TextSelection;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.ui.handlers.HandlerUtil;
import org.jkiss.dbeaver.model.exec.DBCExecutionContext;
import org.jkiss.dbeaver.registry.sql.SQLCommandsRegistry;
import org.jkiss.dbeaver.ui.UIUtils;
import org.jkiss.dbeaver.ui.dialogs.sql.BaseSQLDialog;
import org.jkiss.dbeaver.ui.editors.sql.SQLEditor;
import org.jkiss.dbeaver.ui.editors.sql.convert.SQLConverterRegistry;
import org.jkiss.dbeaver.ui.editors.sql.convert.SQLTargetConverterDescriptor;
import org.jkiss.dbeaver.ui.properties.PropertyTreeViewer;
import org.jkiss.dbeaver.utils.RuntimeUtils;
public class CopySourceCodeHandler extends AbstractHandler {
@Override
......@@ -43,13 +58,85 @@ public class CopySourceCodeHandler extends AbstractHandler {
if (selection.isEmpty() || !(selection instanceof TextSelection)) {
return null;
}
String text = ((TextSelection) selection).getText();
StringBuilder result = new StringBuilder(text);
TargetFormatDialog dialog = new TargetFormatDialog(editor, (TextSelection)selection);
if (dialog.open() != IDialogConstants.OK_ID) {
return null;
}
UIUtils.setClipboardContents(HandlerUtil.getActiveShell(event).getDisplay(), TextTransfer.getInstance(), result.toString());
UIUtils.setClipboardContents(Display.getCurrent(), TextTransfer.getInstance(), dialog.getConvertedText());
return null;
}
private static class TargetFormatDialog extends BaseSQLDialog {
private static final String DIALOG_ID = "DBeaver.SQLTargetFormatDialog";//$NON-NLS-1$
private final SQLEditor editor;
private final TextSelection selection;
TargetFormatDialog(SQLEditor editor, TextSelection selection) {
super(editor.getSite(), "Choose format", null);
this.editor = editor;
this.selection = selection;
}
@Override
protected IDialogSettings getDialogBoundsSettings()
{
return UIUtils.getDialogSettings(DIALOG_ID);
}
@Override
protected Composite createDialogArea(Composite parent) {
Composite composite = super.createDialogArea(parent);
((GridLayout)composite.getLayout()).numColumns = 2;
{
Composite formatPanel = UIUtils.createPlaceholder(composite, 1);
formatPanel.setLayoutData(new GridData(GridData.FILL_BOTH));
Group formatsGroup = UIUtils.createControlGroup(formatPanel, "Format", 1, GridData.FILL_HORIZONTAL, 0);
for (SQLTargetConverterDescriptor converter : SQLConverterRegistry.getInstance().getTargetConverters()) {
Button formatButton = new Button(formatsGroup, SWT.RADIO);
formatButton.setText(converter.getLabel());
formatButton.setToolTipText(converter.getDescription());
}
Group settingsGroup = UIUtils.createControlGroup(formatPanel, "Settings", 1, GridData.FILL_HORIZONTAL, 0);
settingsGroup.setLayoutData(new GridData(GridData.FILL_BOTH));
PropertyTreeViewer propsViewer = new PropertyTreeViewer(settingsGroup, SWT.BORDER);
}
{
Composite previewPanel = new Composite(composite, SWT.NONE);
previewPanel.setLayoutData(new GridData(GridData.FILL_BOTH));
previewPanel.setLayout(new GridLayout(1, false));
SashForm sash = new SashForm(previewPanel, SWT.VERTICAL);
sash.setLayoutData(new GridData(GridData.FILL_BOTH));
createSQLPanel(sash);
Composite targetGroup = UIUtils.createPlaceholder(sash, 1);
targetGroup.setLayoutData(new GridData(GridData.FILL_BOTH));
UIUtils.createControlLabel(targetGroup, "Result");
StyledText targetText = new StyledText(targetGroup, SWT.BORDER | SWT.READ_ONLY);
targetText.setLayoutData(new GridData(GridData.FILL_BOTH));
}
return composite;
}
String getConvertedText() {
return selection.getText();
}
@Override
protected DBCExecutionContext getExecutionContext() {
return editor.getExecutionContext();
}
@Override
protected String getSQLText() {
return this.selection.getText();
}
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册