提交 80694645 编写于 作者: S serge-rider

#5250 Content editor: text formatting

上级 6801c556
/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2019 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;
/**
* BaseTextEditorCommands
*/
public class BaseTextEditorCommands {
public static final String CMD_CONTENT_FORMAT = "org.jkiss.dbeaver.ui.editors.text.content.format"; //$NON-NLS-1$
}
\ No newline at end of file
...@@ -3,5 +3,4 @@ ...@@ -3,5 +3,4 @@
<plugin> <plugin>
</plugin> </plugin>
...@@ -625,6 +625,14 @@ ...@@ -625,6 +625,14 @@
</enabledWhen> </enabledWhen>
</handler> </handler>
<handler commandId="org.jkiss.dbeaver.ui.editors.text.content.format" class="org.jkiss.dbeaver.ui.editors.content.ContentFormatHandler">
<enabledWhen>
<with variable="activeEditor">
<instanceof value="org.jkiss.dbeaver.ui.editors.content.ContentEditor"/>
</with>
</enabledWhen>
</handler>
</extension> </extension>
<extension point="org.eclipse.ui.menus"> <extension point="org.eclipse.ui.menus">
......
...@@ -27,15 +27,20 @@ import org.eclipse.swt.widgets.Composite; ...@@ -27,15 +27,20 @@ import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.*; import org.eclipse.ui.*;
import org.eclipse.ui.actions.RetargetAction;
import org.eclipse.ui.part.MultiPageEditorActionBarContributor; import org.eclipse.ui.part.MultiPageEditorActionBarContributor;
import org.eclipse.ui.texteditor.BasicTextEditorActionContributor; import org.eclipse.ui.texteditor.BasicTextEditorActionContributor;
import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds;
import org.eclipse.ui.texteditor.RetargetTextEditorAction;
import org.jkiss.dbeaver.model.DBIcon; import org.jkiss.dbeaver.model.DBIcon;
import org.jkiss.dbeaver.runtime.DBWorkbench; import org.jkiss.dbeaver.runtime.DBWorkbench;
import org.jkiss.dbeaver.ui.ActionUtils;
import org.jkiss.dbeaver.ui.DBeaverIcons; import org.jkiss.dbeaver.ui.DBeaverIcons;
import org.jkiss.dbeaver.ui.UIIcon; import org.jkiss.dbeaver.ui.UIIcon;
import org.jkiss.dbeaver.ui.UIUtils; import org.jkiss.dbeaver.ui.UIUtils;
import org.jkiss.dbeaver.ui.data.IValueController; import org.jkiss.dbeaver.ui.data.IValueController;
import org.jkiss.dbeaver.ui.dialogs.DialogUtils; import org.jkiss.dbeaver.ui.dialogs.DialogUtils;
import org.jkiss.dbeaver.ui.editors.BaseTextEditorCommands;
import java.io.File; import java.io.File;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
...@@ -44,8 +49,7 @@ import java.lang.reflect.InvocationTargetException; ...@@ -44,8 +49,7 @@ import java.lang.reflect.InvocationTargetException;
* Content Editor contributor. * Content Editor contributor.
* Uses text editor contributor to fill status bar and menu for possible integrated text editors. * Uses text editor contributor to fill status bar and menu for possible integrated text editors.
*/ */
public class ContentEditorContributor extends MultiPageEditorActionBarContributor public class ContentEditorContributor extends MultiPageEditorActionBarContributor {
{
private final BasicTextEditorActionContributor textContributor; private final BasicTextEditorActionContributor textContributor;
private ContentEditor activeEditor; private ContentEditor activeEditor;
//private IEditorPart activePage; //private IEditorPart activePage;
...@@ -55,40 +59,34 @@ public class ContentEditorContributor extends MultiPageEditorActionBarContributo ...@@ -55,40 +59,34 @@ public class ContentEditorContributor extends MultiPageEditorActionBarContributo
private final IAction infoAction = new InfoAction(); private final IAction infoAction = new InfoAction();
private Combo encodingCombo; private Combo encodingCombo;
public ContentEditorContributor() public ContentEditorContributor() {
{
textContributor = new BasicTextEditorActionContributor(); textContributor = new BasicTextEditorActionContributor();
} }
ContentEditor getEditor() ContentEditor getEditor() {
{
return activeEditor; return activeEditor;
} }
@Override @Override
public void init(IActionBars bars, IWorkbenchPage page) public void init(IActionBars bars, IWorkbenchPage page) {
{
super.init(bars, page); super.init(bars, page);
textContributor.init(bars, page); textContributor.init(bars, page);
} }
@Override @Override
public void init(IActionBars bars) public void init(IActionBars bars) {
{
super.init(bars); super.init(bars);
textContributor.init(bars); textContributor.init(bars);
} }
@Override @Override
public void dispose() public void dispose() {
{
textContributor.dispose(); textContributor.dispose();
super.dispose(); super.dispose();
} }
@Override @Override
public void setActiveEditor(IEditorPart part) public void setActiveEditor(IEditorPart part) {
{
super.setActiveEditor(part); super.setActiveEditor(part);
this.activeEditor = (ContentEditor) part; this.activeEditor = (ContentEditor) part;
...@@ -108,15 +106,13 @@ public class ContentEditorContributor extends MultiPageEditorActionBarContributo ...@@ -108,15 +106,13 @@ public class ContentEditorContributor extends MultiPageEditorActionBarContributo
} }
@Override @Override
public void setActivePage(IEditorPart activeEditor) public void setActivePage(IEditorPart activeEditor) {
{
//this.activePage = activeEditor; //this.activePage = activeEditor;
this.textContributor.setActiveEditor(activeEditor); this.textContributor.setActiveEditor(activeEditor);
} }
@Override @Override
public void contributeToMenu(IMenuManager manager) public void contributeToMenu(IMenuManager manager) {
{
super.contributeToMenu(manager); super.contributeToMenu(manager);
textContributor.contributeToMenu(manager); textContributor.contributeToMenu(manager);
...@@ -127,18 +123,21 @@ public class ContentEditorContributor extends MultiPageEditorActionBarContributo ...@@ -127,18 +123,21 @@ public class ContentEditorContributor extends MultiPageEditorActionBarContributo
menu.add(new Separator()); menu.add(new Separator());
menu.add(infoAction); menu.add(infoAction);
menu.add(new Separator()); menu.add(new Separator());
IMenuManager editMenu = manager.findMenuUsingPath(IWorkbenchActionConstants.M_EDIT);
if (editMenu != null) {
editMenu.add(ActionUtils.makeCommandContribution(UIUtils.getActiveWorkbenchWindow(), BaseTextEditorCommands.CMD_CONTENT_FORMAT));
}
} }
@Override @Override
public void contributeToStatusLine(IStatusLineManager statusLineManager) public void contributeToStatusLine(IStatusLineManager statusLineManager) {
{
super.contributeToStatusLine(statusLineManager); super.contributeToStatusLine(statusLineManager);
textContributor.contributeToStatusLine(statusLineManager); textContributor.contributeToStatusLine(statusLineManager);
} }
@Override @Override
public void contributeToToolBar(IToolBarManager manager) public void contributeToToolBar(IToolBarManager manager) {
{
super.contributeToToolBar(manager); super.contributeToToolBar(manager);
textContributor.contributeToToolBar(manager); textContributor.contributeToToolBar(manager);
// Execution // Execution
...@@ -147,11 +146,9 @@ public class ContentEditorContributor extends MultiPageEditorActionBarContributo ...@@ -147,11 +146,9 @@ public class ContentEditorContributor extends MultiPageEditorActionBarContributo
manager.add(new Separator()); manager.add(new Separator());
manager.add(infoAction); manager.add(infoAction);
manager.add(new Separator()); manager.add(new Separator());
manager.add(new ControlContribution("Encoding") manager.add(new ControlContribution("Encoding") {
{
@Override @Override
protected Control createControl(Composite parent) protected Control createControl(Composite parent) {
{
String curCharset = null; String curCharset = null;
if (getEditor() != null) { if (getEditor() != null) {
curCharset = getEditor().getEditorInput().getEncoding(); curCharset = getEditor().getEditorInput().getEncoding();
...@@ -188,8 +185,7 @@ public class ContentEditorContributor extends MultiPageEditorActionBarContributo ...@@ -188,8 +185,7 @@ public class ContentEditorContributor extends MultiPageEditorActionBarContributo
public abstract class SimpleAction extends Action { public abstract class SimpleAction extends Action {
SimpleAction(String id, String text, String toolTip, DBIcon icon) SimpleAction(String id, String text, String toolTip, DBIcon icon) {
{
super(text, DBeaverIcons.getImageDescriptor(icon)); super(text, DBeaverIcons.getImageDescriptor(icon));
setId(id); setId(id);
//setActionDefinitionId(id); //setActionDefinitionId(id);
...@@ -201,30 +197,24 @@ public class ContentEditorContributor extends MultiPageEditorActionBarContributo ...@@ -201,30 +197,24 @@ public class ContentEditorContributor extends MultiPageEditorActionBarContributo
} }
private class FileExportAction extends SimpleAction private class FileExportAction extends SimpleAction {
{ FileExportAction() {
FileExportAction()
{
super(IWorkbenchCommandConstants.FILE_EXPORT, "Export", "Save to File", UIIcon.SAVE_AS); super(IWorkbenchCommandConstants.FILE_EXPORT, "Export", "Save to File", UIIcon.SAVE_AS);
} }
@Override @Override
public void run() public void run() {
{
getEditor().doSaveAs(); getEditor().doSaveAs();
} }
} }
private class FileImportAction extends SimpleAction private class FileImportAction extends SimpleAction {
{ FileImportAction() {
FileImportAction()
{
super(IWorkbenchCommandConstants.FILE_IMPORT, "Import", "Load from File", UIIcon.LOAD); super(IWorkbenchCommandConstants.FILE_IMPORT, "Import", "Load from File", UIIcon.LOAD);
} }
@Override @Override
public void run() public void run() {
{
final ContentEditor editor = getEditor(); final ContentEditor editor = getEditor();
if (editor == null) { if (editor == null) {
return; return;
...@@ -238,12 +228,10 @@ public class ContentEditorContributor extends MultiPageEditorActionBarContributo ...@@ -238,12 +228,10 @@ public class ContentEditorContributor extends MultiPageEditorActionBarContributo
editor.getSite().getWorkbenchWindow().run(true, true, new IRunnableWithProgress() { editor.getSite().getWorkbenchWindow().run(true, true, new IRunnableWithProgress() {
@Override @Override
public void run(IProgressMonitor monitor) public void run(IProgressMonitor monitor)
throws InvocationTargetException, InterruptedException throws InvocationTargetException, InterruptedException {
{
try { try {
editor.getEditorInput().loadFromExternalFile(loadFile, monitor); editor.getEditorInput().loadFromExternalFile(loadFile, monitor);
} } catch (CoreException e) {
catch (CoreException e) {
throw new InvocationTargetException(e); throw new InvocationTargetException(e);
} }
} }
...@@ -254,29 +242,24 @@ public class ContentEditorContributor extends MultiPageEditorActionBarContributo ...@@ -254,29 +242,24 @@ public class ContentEditorContributor extends MultiPageEditorActionBarContributo
} }
editor.setDirty(true); editor.setDirty(true);
editor.fireContentChanged(); editor.fireContentChanged();
} } catch (InvocationTargetException e) {
catch (InvocationTargetException e) {
DBWorkbench.getPlatformUI().showError( DBWorkbench.getPlatformUI().showError(
"Can't load content", "Can't load content",
"Can't load content from file '" + loadFile.getAbsolutePath() + "'", "Can't load content from file '" + loadFile.getAbsolutePath() + "'",
e.getTargetException()); e.getTargetException());
} } catch (InterruptedException e) {
catch (InterruptedException e) {
// do nothing // do nothing
} }
} }
} }
private class InfoAction extends SimpleAction private class InfoAction extends SimpleAction {
{ InfoAction() {
InfoAction()
{
super("org.jkiss.dbeaver.lob.actions.info", "Info", "Show column information", DBIcon.TREE_INFO); super("org.jkiss.dbeaver.lob.actions.info", "Info", "Show column information", DBIcon.TREE_INFO);
} }
@Override @Override
public void run() public void run() {
{
getEditor().toggleInfoBar(); getEditor().toggleInfoBar();
} }
} }
......
/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2019 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.content;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.TextViewer;
import org.eclipse.jface.text.source.SourceViewer;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.ui.texteditor.AbstractTextEditor;
public class ContentFormatHandler extends AbstractHandler {
public ContentFormatHandler() {
}
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
IEditorPart activeEditor = HandlerUtil.getActiveEditor(event);
if (activeEditor instanceof ContentEditor) {
IEditorPart editorPart = ((ContentEditor) activeEditor).getActiveEditor();
ITextViewer textViewer = editorPart.getAdapter(ITextViewer.class);
if (textViewer instanceof TextViewer) {
((TextViewer)textViewer).doOperation(SourceViewer.FORMAT);
}
}
return null;
}
}
...@@ -55,6 +55,7 @@ import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor; ...@@ -55,6 +55,7 @@ import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.model.sql.*; import org.jkiss.dbeaver.model.sql.*;
import org.jkiss.dbeaver.runtime.DBWorkbench; import org.jkiss.dbeaver.runtime.DBWorkbench;
import org.jkiss.dbeaver.ui.*; import org.jkiss.dbeaver.ui.*;
import org.jkiss.dbeaver.ui.editors.BaseTextEditorCommands;
import org.jkiss.dbeaver.ui.editors.EditorUtils; import org.jkiss.dbeaver.ui.editors.EditorUtils;
import org.jkiss.dbeaver.ui.editors.sql.internal.SQLEditorMessages; import org.jkiss.dbeaver.ui.editors.sql.internal.SQLEditorMessages;
import org.jkiss.dbeaver.ui.editors.sql.preferences.*; import org.jkiss.dbeaver.ui.editors.sql.preferences.*;
...@@ -514,7 +515,7 @@ public abstract class SQLEditorBase extends BaseTextEditor implements DBPContext ...@@ -514,7 +515,7 @@ public abstract class SQLEditorBase extends BaseTextEditor implements DBPContext
SQLEditorContributor.getActionResourcePrefix(SQLEditorContributor.ACTION_CONTENT_FORMAT_PROPOSAL), SQLEditorContributor.getActionResourcePrefix(SQLEditorContributor.ACTION_CONTENT_FORMAT_PROPOSAL),
this, this,
ISourceViewer.FORMAT); ISourceViewer.FORMAT);
a.setActionDefinitionId(SQLEditorCommands.CMD_CONTENT_FORMAT); a.setActionDefinitionId(BaseTextEditorCommands.CMD_CONTENT_FORMAT);
setAction(SQLEditorContributor.ACTION_CONTENT_FORMAT_PROPOSAL, a); setAction(SQLEditorContributor.ACTION_CONTENT_FORMAT_PROPOSAL, a);
setAction(ITextEditorActionConstants.CONTEXT_PREFERENCES, new Action(SQLEditorMessages.editor_sql_preference) { //$NON-NLS-1$ setAction(ITextEditorActionConstants.CONTEXT_PREFERENCES, new Action(SQLEditorMessages.editor_sql_preference) { //$NON-NLS-1$
......
...@@ -43,7 +43,7 @@ public interface SQLEditorCommands ...@@ -43,7 +43,7 @@ public interface SQLEditorCommands
String CMD_SQL_ASSIST_TEMPLATES = "org.jkiss.dbeaver.ui.editors.sql.assist.templates"; //$NON-NLS-1$ String CMD_SQL_ASSIST_TEMPLATES = "org.jkiss.dbeaver.ui.editors.sql.assist.templates"; //$NON-NLS-1$
String CMD_SYNC_CONNECTION = "org.jkiss.dbeaver.ui.editors.sql.sync.connection"; String CMD_SYNC_CONNECTION = "org.jkiss.dbeaver.ui.editors.sql.sync.connection";
String CMD_SQL_RENAME = "org.jkiss.dbeaver.ui.editors.sql.rename"; String CMD_SQL_RENAME = "org.jkiss.dbeaver.ui.editors.sql.rename";
String CMD_CONTENT_FORMAT = "org.jkiss.dbeaver.ui.editors.text.content.format"; //$NON-NLS-1$
String CMD_OPEN_FILE = "org.jkiss.dbeaver.ui.editors.sql.open.file"; //$NON-NLS-1$ String CMD_OPEN_FILE = "org.jkiss.dbeaver.ui.editors.sql.open.file"; //$NON-NLS-1$
String CMD_SAVE_FILE = "org.jkiss.dbeaver.ui.editors.sql.save.file"; //$NON-NLS-1$ String CMD_SAVE_FILE = "org.jkiss.dbeaver.ui.editors.sql.save.file"; //$NON-NLS-1$
} }
...@@ -29,6 +29,7 @@ import org.eclipse.ui.texteditor.StatusLineContributionItem; ...@@ -29,6 +29,7 @@ import org.eclipse.ui.texteditor.StatusLineContributionItem;
import org.jkiss.dbeaver.Log; import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.ui.ActionUtils; import org.jkiss.dbeaver.ui.ActionUtils;
import org.jkiss.dbeaver.ui.UIUtils; import org.jkiss.dbeaver.ui.UIUtils;
import org.jkiss.dbeaver.ui.editors.BaseTextEditorCommands;
import org.jkiss.dbeaver.ui.editors.sql.internal.SQLEditorMessages; import org.jkiss.dbeaver.ui.editors.sql.internal.SQLEditorMessages;
import java.util.ResourceBundle; import java.util.ResourceBundle;
...@@ -78,7 +79,7 @@ public class SQLEditorContributor extends TextEditorActionContributor ...@@ -78,7 +79,7 @@ public class SQLEditorContributor extends TextEditorActionContributor
contentAssistProposal = new RetargetTextEditorAction(bundle, getActionResourcePrefix(ACTION_CONTENT_ASSIST_PROPOSAL)); contentAssistProposal = new RetargetTextEditorAction(bundle, getActionResourcePrefix(ACTION_CONTENT_ASSIST_PROPOSAL));
contentAssistProposal.setActionDefinitionId(ITextEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS); contentAssistProposal.setActionDefinitionId(ITextEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS);
contentFormatProposal = new RetargetTextEditorAction(bundle, getActionResourcePrefix(ACTION_CONTENT_FORMAT_PROPOSAL)); contentFormatProposal = new RetargetTextEditorAction(bundle, getActionResourcePrefix(ACTION_CONTENT_FORMAT_PROPOSAL));
contentFormatProposal.setActionDefinitionId(SQLEditorCommands.CMD_CONTENT_FORMAT); contentFormatProposal.setActionDefinitionId(BaseTextEditorCommands.CMD_CONTENT_FORMAT);
contentAssistTip = new RetargetTextEditorAction(bundle, getActionResourcePrefix(ACTION_CONTENT_ASSIST_TIP)); contentAssistTip = new RetargetTextEditorAction(bundle, getActionResourcePrefix(ACTION_CONTENT_ASSIST_TIP));
contentAssistTip.setActionDefinitionId(ITextEditorActionDefinitionIds.CONTENT_ASSIST_CONTEXT_INFORMATION); contentAssistTip.setActionDefinitionId(ITextEditorActionDefinitionIds.CONTENT_ASSIST_CONTEXT_INFORMATION);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册