未验证 提交 b6caea12 编写于 作者: S Serge Rider 提交者: GitHub

Merge pull request #14332 from dbeaver/test-editor-change-encoding#13593

#13593 Change encoding support for text editor
......@@ -349,6 +349,12 @@ public class ResultSetMessages extends NLS {
public static String validate_unique_key_usage_dialog_use_all_columns_no_valid_columns_found;
public static String validate_unique_key_usage_dialog_use_all_columns_cannot_reload_unique_columns;
public static String panel_editor_text_loading_placeholder_label;
public static String panel_editor_text_word_wrap_name;
public static String panel_editor_text_auto_format_name;
public static String panel_editor_text_encoding_name;
public static String panel_editor_text_encoding_title;
static {
// initialize resource bundle
NLS.initializeMessages(BUNDLE_NAME, ResultSetMessages.class);
......
......@@ -327,3 +327,9 @@ validate_unique_key_usage_dialog_use_all_columns = Use All Columns
validate_unique_key_usage_dialog_custom_unique_key = Custom Unique Key
validate_unique_key_usage_dialog_use_all_columns_no_valid_columns_found = No valid columns found for unique key
validate_unique_key_usage_dialog_use_all_columns_cannot_reload_unique_columns = Can't reload unique columns
panel_editor_text_loading_placeholder_label = Loading content ... ({0})
panel_editor_text_word_wrap_name = Word Wrap
panel_editor_text_auto_format_name = Auto Format
panel_editor_text_encoding_name = Encoding...
panel_editor_text_encoding_title = Change encoding
\ No newline at end of file
......@@ -274,3 +274,9 @@ virtual_edit_columns_page_dialog_button_edit = \u0418\u0437\u043C\u0435\u043D\u0
virtual_edit_columns_page_dialog_button_remove = \u0423\u0434\u0430\u043B\u0438\u0442\u044C
virtual_edit_columns_page_confirm_action_delete = \u0423\u0434\u0430\u043B\u0438\u0442\u044C \u0432\u0438\u0440\u0442\u0443\u0430\u043B\u044C\u043D\u044B\u0439 \u0441\u0442\u043E\u043B\u0431\u0435\u0446
virtual_edit_columns_page_confirm_action_question_delete_column = \u0412\u044B \u0443\u0432\u0435\u0440\u0435\u043D\u044B, \u0447\u0442\u043E \u0445\u043E\u0442\u0438\u0442\u0435 \u0443\u0434\u0430\u043B\u0438\u0442\u044C \u0432\u0438\u0440\u0442\u0443\u0430\u043B\u044C\u043D\u044B\u0439 \u0441\u0442\u043E\u043B\u0431\u0435\u0446 "{0}"?
panel_editor_text_loading_placeholder_label = \u0417\u0430\u0433\u0440\u0443\u0437\u043A\u0430 \u0434\u0430\u043D\u043D\u044B\u0445 ... ({0})
panel_editor_text_word_wrap_name = \u041F\u0435\u0440\u0435\u043D\u043E\u0441 \u0441\u043B\u043E\u0432
panel_editor_text_auto_format_name = \u0410\u0432\u0442\u043E\u0444\u043E\u0440\u043C\u0430\u0442\u0438\u0440\u043E\u0432\u0430\u043D\u0438\u0435
panel_editor_text_encoding_name = \u041A\u043E\u0434\u0438\u0440\u043E\u0432\u043A\u0430...
panel_editor_text_encoding_title = \u0421\u043C\u0435\u043D\u0438\u0442\u044C \u043A\u043E\u0434\u0438\u0440\u043E\u0432\u043A\u0443
......@@ -21,15 +21,21 @@ import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IContributionManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.jface.text.IUndoManager;
import org.eclipse.jface.text.TextViewer;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ST;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.IWorkbenchCommandConstants;
......@@ -37,16 +43,20 @@ import org.eclipse.ui.PartInitException;
import org.eclipse.ui.texteditor.ITextEditorActionConstants;
import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds;
import org.jkiss.code.NotNull;
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.model.DBPMessageType;
import org.jkiss.dbeaver.model.data.DBDContent;
import org.jkiss.dbeaver.model.data.storage.StringContentStorage;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.model.runtime.VoidProgressMonitor;
import org.jkiss.dbeaver.ui.UIUtils;
import org.jkiss.dbeaver.ui.controls.StyledTextUtils;
import org.jkiss.dbeaver.ui.controls.resultset.internal.ResultSetMessages;
import org.jkiss.dbeaver.ui.data.IStreamValueEditor;
import org.jkiss.dbeaver.ui.data.IValueController;
import org.jkiss.dbeaver.ui.dialogs.BaseDialog;
import org.jkiss.dbeaver.ui.editors.StringEditorInput;
import org.jkiss.dbeaver.ui.editors.SubEditorSite;
import org.jkiss.dbeaver.ui.editors.TextEditorUtils;
......@@ -55,6 +65,7 @@ import org.jkiss.dbeaver.ui.editors.data.internal.DataEditorsActivator;
import org.jkiss.dbeaver.ui.editors.text.BaseTextEditor;
import org.jkiss.dbeaver.utils.RuntimeUtils;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
/**
......@@ -64,6 +75,7 @@ public abstract class AbstractTextPanelEditor<EDITOR extends BaseTextEditor> imp
private static final String PREF_TEXT_EDITOR_WORD_WRAP = "content.text.editor.word-wrap";
private static final String PREF_TEXT_EDITOR_AUTO_FORMAT = "content.text.editor.auto-format";
private static final String PREF_TEXT_EDITOR_ENCODING = "content.text.editor.encoding";
private static final Log log = Log.getLog(AbstractTextPanelEditor.class);
public static final int LONG_CONTENT_LENGTH = 10000;
......@@ -128,7 +140,7 @@ public abstract class AbstractTextPanelEditor<EDITOR extends BaseTextEditor> imp
public void contributeSettings(@NotNull IContributionManager manager, @NotNull final StyledText editorControl) {
manager.add(new Separator());
{
Action wwAction = new Action("Word Wrap", Action.AS_CHECK_BOX) {
Action wwAction = new Action(ResultSetMessages.panel_editor_text_word_wrap_name, Action.AS_CHECK_BOX) {
@Override
public void run() {
boolean newWW = !editorControl.getWordWrap();
......@@ -147,6 +159,33 @@ public abstract class AbstractTextPanelEditor<EDITOR extends BaseTextEditor> imp
afAction.setChecked(getPanelSettings().getBoolean(PREF_TEXT_EDITOR_AUTO_FORMAT));
manager.add(afAction);
}
if (textEditor != null) {
manager.add(new Action(ResultSetMessages.panel_editor_text_encoding_name) {
@Override
public void run() {
final ChangeEncodingDialog dialog = new ChangeEncodingDialog(getPanelSettings().get(PREF_TEXT_EDITOR_ENCODING));
if (dialog.open() != IDialogConstants.OK_ID) {
return;
}
getPanelSettings().put(PREF_TEXT_EDITOR_ENCODING, dialog.getEncoding());
final EDITOR editor = getTextEditor();
if (editor != null) {
final TextViewer viewer = editor.getTextViewer();
if (viewer != null) {
final StyledText control = viewer.getTextWidget();
if (control != null && !control.isDisposed()) {
try {
primeEditorValue(new VoidProgressMonitor(), control, null);
} catch (DBException e) {
log.error("Can't refresh editor", e);
}
}
}
}
}
});
}
}
protected EDITOR getTextEditor() {
......@@ -210,12 +249,13 @@ public abstract class AbstractTextPanelEditor<EDITOR extends BaseTextEditor> imp
}
@Override
public void primeEditorValue(@NotNull DBRProgressMonitor monitor, @NotNull StyledText control, @NotNull DBDContent value) throws DBException
public void primeEditorValue(@NotNull DBRProgressMonitor monitor, @NotNull StyledText control, @Nullable DBDContent value) throws DBException
{
try {
// Load contents in two steps (empty + real in async mode). Workaround for some strange bug in StyledText in E4.13 (#6701)
TextViewer textViewer = editor.getTextViewer();
final ContentEditorInput textInput = new ContentEditorInput(valueController, null, null, monitor);
final TextViewer textViewer = editor.getTextViewer();
final String encoding = getPanelSettings().get(PREF_TEXT_EDITOR_ENCODING);
final ContentEditorInput textInput = new ContentEditorInput(valueController, null, null, encoding, monitor);
boolean longContent = textInput.getContentLength() > LONG_CONTENT_LENGTH;
if (longContent) {
UIUtils.asyncExec(() -> {
......@@ -229,7 +269,7 @@ public abstract class AbstractTextPanelEditor<EDITOR extends BaseTextEditor> imp
if (textWidget != null && longContent) {
GC gc = new GC(textWidget);
try {
UIUtils.drawMessageOverControl(textWidget, gc, "Loading content ... (" + textInput.getContentLength() + ")", 0);
UIUtils.drawMessageOverControl(textWidget, gc, NLS.bind(ResultSetMessages.panel_editor_text_loading_placeholder_label, textInput.getContentLength()), 0);
editor.setInput(textInput);
} finally {
gc.dispose();
......@@ -283,7 +323,7 @@ public abstract class AbstractTextPanelEditor<EDITOR extends BaseTextEditor> imp
return viewerSettings;
}
private class WordWrapAction extends StyledTextUtils.StyledTextActionEx {
private static class WordWrapAction extends StyledTextUtils.StyledTextActionEx {
private final StyledText text;
WordWrapAction(StyledText text) {
......@@ -304,7 +344,7 @@ public abstract class AbstractTextPanelEditor<EDITOR extends BaseTextEditor> imp
private class AutoFormatAction extends Action {
AutoFormatAction() {
super("Auto Format", Action.AS_CHECK_BOX);
super(ResultSetMessages.panel_editor_text_auto_format_name, Action.AS_CHECK_BOX);
}
@Override
......@@ -320,4 +360,54 @@ public abstract class AbstractTextPanelEditor<EDITOR extends BaseTextEditor> imp
applyEditorStyle();
}
}
private static class ChangeEncodingDialog extends BaseDialog {
private String encoding;
public ChangeEncodingDialog(@NotNull String defaultEncoding) {
super(UIUtils.getActiveShell(), ResultSetMessages.panel_editor_text_encoding_title, null);
this.encoding = defaultEncoding;
this.setShellStyle(SWT.DIALOG_TRIM);
}
@Override
protected Composite createDialogArea(Composite parent) {
final Composite composite = super.createDialogArea(parent);
{
final Composite innerComposite = UIUtils.createComposite(composite, 1);
innerComposite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
final Combo encodingCombo = UIUtils.createEncodingCombo(innerComposite, encoding);
encodingCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
encodingCombo.addModifyListener(event -> {
encoding = encodingCombo.getText();
updateCompletion();
});
}
return composite;
}
@Override
protected void createButtonsForButtonBar(Composite parent) {
super.createButtonsForButtonBar(parent);
updateCompletion();
}
private void updateCompletion() {
final Button button = getButton(IDialogConstants.OK_ID);
try {
Charset.forName(encoding);
button.setEnabled(true);
} catch (Exception ignored) {
button.setEnabled(false);
}
}
@NotNull
public String getEncoding() {
return encoding;
}
}
}
......@@ -24,6 +24,7 @@ import org.eclipse.core.runtime.Path;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.*;
import org.eclipse.ui.editors.text.IEncodingSupport;
import org.jkiss.code.NotNull;
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.Log;
......@@ -75,10 +76,10 @@ public class ContentEditorInput implements IPathEditorInput, IStatefulEditorInpu
private StringEditorInput.StringStorage stringStorage;
public ContentEditorInput(
IValueController valueController,
IEditorPart[] editorParts,
IEditorPart defaultPart,
DBRProgressMonitor monitor)
@NotNull IValueController valueController,
@Nullable IEditorPart[] editorParts,
@Nullable IEditorPart defaultPart,
@NotNull DBRProgressMonitor monitor)
throws DBException
{
this.valueController = valueController;
......@@ -87,6 +88,20 @@ public class ContentEditorInput implements IPathEditorInput, IStatefulEditorInpu
this.fileCharset = getDefaultEncoding();
this.prepareContent(monitor);
}
public ContentEditorInput(
@NotNull IValueController valueController,
@Nullable IEditorPart[] editorParts,
@Nullable IEditorPart defaultPart,
@Nullable String charset,
@NotNull DBRProgressMonitor monitor)
throws DBException
{
this.valueController = valueController;
this.editorParts = editorParts;
this.defaultPart = defaultPart;
this.fileCharset = CommonUtils.isEmpty(charset) ? getDefaultEncoding() : charset;
this.prepareContent(monitor);
}
public IValueController getValueController()
{
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册