diff options
author | Vincent Lorenzo | 2019-11-15 16:19:02 +0000 |
---|---|---|
committer | Patrick Tessier | 2019-11-26 18:21:43 +0000 |
commit | 1ec69d3d2e2ef3f857eef6a440376adaab901140 (patch) | |
tree | 0f66c0a1cf99b78eaab91a1cc31b91003d5836d1 | |
parent | 96d9e273af029deb22d5e2c5424216a6d05bac37 (diff) | |
download | org.eclipse.papyrus-1ec69d3d2e2ef3f857eef6a440376adaab901140.tar.gz org.eclipse.papyrus-1ec69d3d2e2ef3f857eef6a440376adaab901140.tar.xz org.eclipse.papyrus-1ec69d3d2e2ef3f857eef6a440376adaab901140.zip |
Bug 553107: [Xtext] The Xtext editors must allows to display line number
Change-Id: Ib838da56780b6c6fbc66be4ce296cbadc429c442
Signed-off-by: Vincent Lorenzo <vincent.lorenzo@cea.fr>
6 files changed, 156 insertions, 11 deletions
diff --git a/plugins/infra/ui/org.eclipse.papyrus.infra.widgets/META-INF/MANIFEST.MF b/plugins/infra/ui/org.eclipse.papyrus.infra.widgets/META-INF/MANIFEST.MF index 74b2689da51..1bd8feb5a1f 100644 --- a/plugins/infra/ui/org.eclipse.papyrus.infra.widgets/META-INF/MANIFEST.MF +++ b/plugins/infra/ui/org.eclipse.papyrus.infra.widgets/META-INF/MANIFEST.MF @@ -31,7 +31,7 @@ Export-Package: org.eclipse.papyrus.infra.widgets, org.eclipse.papyrus.infra.widgets.wizard.pages Bundle-Vendor: %providerName Bundle-ActivationPolicy: lazy -Bundle-Version: 3.5.0.qualifier +Bundle-Version: 3.6.0.qualifier Bundle-Name: %pluginName Bundle-Localization: plugin Bundle-ManifestVersion: 2 diff --git a/plugins/infra/ui/org.eclipse.papyrus.infra.widgets/pom.xml b/plugins/infra/ui/org.eclipse.papyrus.infra.widgets/pom.xml index fa6404e31ab..87c7e96db3e 100644 --- a/plugins/infra/ui/org.eclipse.papyrus.infra.widgets/pom.xml +++ b/plugins/infra/ui/org.eclipse.papyrus.infra.widgets/pom.xml @@ -1,5 +1,6 @@ <?xml version="1.0" encoding="UTF-8"?> -<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> @@ -8,6 +9,6 @@ <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>org.eclipse.papyrus.infra.widgets</artifactId> - <version>3.5.0-SNAPSHOT</version> + <version>3.6.0-SNAPSHOT</version> <packaging>eclipse-plugin</packaging> </project> diff --git a/plugins/infra/ui/org.eclipse.papyrus.infra.widgets/src/org/eclipse/papyrus/infra/widgets/util/StyledTextUtils.java b/plugins/infra/ui/org.eclipse.papyrus.infra.widgets/src/org/eclipse/papyrus/infra/widgets/util/StyledTextUtils.java new file mode 100755 index 00000000000..0e6ce61f657 --- /dev/null +++ b/plugins/infra/ui/org.eclipse.papyrus.infra.widgets/src/org/eclipse/papyrus/infra/widgets/util/StyledTextUtils.java @@ -0,0 +1,140 @@ +/***************************************************************************** + * Copyright (c) 2019 CEA LIST and others. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - Initial API and implementation + * + *****************************************************************************/ + +package org.eclipse.papyrus.infra.widgets.util; + +import org.eclipse.swt.SWT; +import org.eclipse.swt.custom.Bullet; +import org.eclipse.swt.custom.LineStyleEvent; +import org.eclipse.swt.custom.LineStyleListener; +import org.eclipse.swt.custom.ST; +import org.eclipse.swt.custom.StyleRange; +import org.eclipse.swt.custom.StyledText; +import org.eclipse.swt.events.ModifyEvent; +import org.eclipse.swt.events.ModifyListener; +import org.eclipse.swt.graphics.Color; +import org.eclipse.swt.graphics.GlyphMetrics; +import org.eclipse.swt.graphics.RGB; +import org.eclipse.swt.widgets.Display; + +/** + * This class provides useful methods for StyledText + * + * @since 3.6 + */ +public class StyledTextUtils { + + // TODO : use GUI helper + /** + * The color used as background (a light blue) + */ + private static final Color STYLED_TEXT_BACKGROUND_COLOR = new Color(Display.getDefault(), new RGB(232, 242, 254)); + + /** + * the color used as foreground + */ + private static final Color STYLED_TEXT_FOREGROUND = Display.getCurrent().getSystemColor(SWT.COLOR_DARK_GRAY); + + /** + * This method add line number support for StyledText used in Xtext context + * (usage of {@link StyleRange} instead of {@link LineStyleListener} + * + * @param styledText + * a styled text editor + */ + public static final void addLineNumberSupportForXtext(final StyledText styledText) { + configureLineNumberDisplay(styledText); + styledText.addModifyListener(new ModifyListener() { + + @Override + public void modifyText(ModifyEvent e) { + configureLineNumberDisplay(styledText); + } + }); + } + + /** + * This method configure the editor to display the line numbers + * + * @param styledText + * a styled text editor + */ + private static void configureLineNumberDisplay(final StyledText styledText) { + int maxLine1 = styledText.getLineCount(); + + StyleRange style = new StyleRange(); + style.foreground = StyledTextUtils.STYLED_TEXT_FOREGROUND; + style.background = StyledTextUtils.STYLED_TEXT_BACKGROUND_COLOR; + + // 3. get the width required to display the last line number + int bulletLength = Integer.toString(maxLine1).length(); + + // Width of numbers character is half the height in monospaced font, add 1 character width for right padding. + int bulletWidth = (bulletLength + 1) * styledText.getLineHeight() / 2; + style.metrics = new GlyphMetrics(0, 0, bulletWidth); + Bullet bullet1 = new Bullet(ST.BULLET_NUMBER, style); + styledText.setLineBullet(0, styledText.getLineCount(), null); + styledText.setLineBullet(0, styledText.getLineCount(), bullet1); + } + + + /** + * This method allows to display the line number in the text editor. + * This method breaks Xtext editor (lost of red underline and others in case of errors), because XText uses {@link StyleRange} + * and not {@link LineStyleListener} + * + * @see StyledText documentation + * + * @since 1.4 + */ + public static final void addLineNumberSupport(final StyledText styledText) { + styledText.addModifyListener(new ModifyListener() { + + @Override + public void modifyText(ModifyEvent e) { + // required to update properly line number display + styledText.redraw(); + } + + }); + + styledText.addLineStyleListener(new LineStyleListener() { + @Override + public void lineGetStyle(LineStyleEvent event) { + // 1. create a new style range + StyleRange styleRange = new StyleRange(); + + // 2. define colors + styleRange.foreground = STYLED_TEXT_FOREGROUND; + styleRange.background = STYLED_TEXT_BACKGROUND_COLOR; + + + // 3. get the width required to display the last line number + int maxLine = styledText.getLineCount(); + int bulletLength = Integer.toString(maxLine).length(); + + // Width of numbers character is half the height in monospaced font, add 1 character width for right padding. + int bulletWidth = (bulletLength + 1) * styledText.getLineHeight() / 2; + styleRange.metrics = new GlyphMetrics(0, 0, bulletWidth); + event.bullet = new Bullet(ST.BULLET_TEXT, styleRange); + + int bulletLine = styledText.getLineAtOffset(event.lineOffset) + 1; + event.bullet.text = String.format("%" + bulletLength + "s", bulletLine); //$NON-NLS-1$ //$NON-NLS-2$ + } + }); + } + + +} diff --git a/plugins/uml/properties/org.eclipse.papyrus.uml.properties.xtext/META-INF/MANIFEST.MF b/plugins/uml/properties/org.eclipse.papyrus.uml.properties.xtext/META-INF/MANIFEST.MF index a45c280433b..90d94ba9271 100644 --- a/plugins/uml/properties/org.eclipse.papyrus.uml.properties.xtext/META-INF/MANIFEST.MF +++ b/plugins/uml/properties/org.eclipse.papyrus.uml.properties.xtext/META-INF/MANIFEST.MF @@ -4,7 +4,7 @@ Require-Bundle: org.eclipse.papyrus.uml.properties;bundle-version="[3.2.0,4.0.0) org.eclipse.papyrus.uml.textedit.valuespecification.xtext.ui;bundle-version="[2.1.0,3.0.0)" Bundle-Vendor: %providerName Bundle-ActivationPolicy: lazy -Bundle-Version: 1.3.0.qualifier +Bundle-Version: 1.3.100.qualifier Bundle-Localization: plugin Bundle-Name: %pluginName Bundle-ManifestVersion: 2 diff --git a/plugins/uml/properties/org.eclipse.papyrus.uml.properties.xtext/pom.xml b/plugins/uml/properties/org.eclipse.papyrus.uml.properties.xtext/pom.xml index cded9f34f25..4b8860cc6d2 100644 --- a/plugins/uml/properties/org.eclipse.papyrus.uml.properties.xtext/pom.xml +++ b/plugins/uml/properties/org.eclipse.papyrus.uml.properties.xtext/pom.xml @@ -1,13 +1,14 @@ <?xml version="1.0" encoding="UTF-8"?> -<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> - <groupId>org.eclipse.papyrus</groupId> - <artifactId>org.eclipse.papyrus.uml-properties</artifactId> - <version>0.0.1-SNAPSHOT</version> + <groupId>org.eclipse.papyrus</groupId> + <artifactId>org.eclipse.papyrus.uml-properties</artifactId> + <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>org.eclipse.papyrus.uml.properties.xtext</artifactId> - <version>1.3.0-SNAPSHOT</version> + <version>1.3.100-SNAPSHOT</version> <packaging>eclipse-plugin</packaging> </project> diff --git a/plugins/uml/properties/org.eclipse.papyrus.uml.properties.xtext/src/org/eclipse/papyrus/uml/properties/xtext/XtextLanguageEditor.java b/plugins/uml/properties/org.eclipse.papyrus.uml.properties.xtext/src/org/eclipse/papyrus/uml/properties/xtext/XtextLanguageEditor.java index 6aa8b6a48a9..19e94311052 100644 --- a/plugins/uml/properties/org.eclipse.papyrus.uml.properties.xtext/src/org/eclipse/papyrus/uml/properties/xtext/XtextLanguageEditor.java +++ b/plugins/uml/properties/org.eclipse.papyrus.uml.properties.xtext/src/org/eclipse/papyrus/uml/properties/xtext/XtextLanguageEditor.java @@ -9,7 +9,7 @@ * SPDX-License-Identifier: EPL-2.0 * * Contributors: - * Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea. fr - Bug 536594 + * Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea. fr - Bug 536594, 553107 *****************************************************************************/ package org.eclipse.papyrus.uml.properties.xtext; @@ -22,13 +22,14 @@ import org.eclipse.gmf.runtime.common.core.command.ICommand; import org.eclipse.gmf.runtime.common.ui.services.parser.IParser; import org.eclipse.gmf.runtime.emf.core.util.EObjectAdapter; import org.eclipse.jface.preference.IPreferenceStore; +import org.eclipse.papyrus.infra.emf.gmf.command.GMFtoEMFCommandWrapper; import org.eclipse.papyrus.infra.gmfdiag.extensionpoints.editors.Activator; import org.eclipse.papyrus.infra.gmfdiag.extensionpoints.editors.configuration.IDirectEditorConfiguration; import org.eclipse.papyrus.infra.gmfdiag.extensionpoints.editors.utils.DirectEditorsUtil; import org.eclipse.papyrus.infra.gmfdiag.extensionpoints.editors.utils.IDirectEditorsIds; -import org.eclipse.papyrus.infra.emf.gmf.command.GMFtoEMFCommandWrapper; import org.eclipse.papyrus.infra.properties.ui.modelelement.ModelElement; import org.eclipse.papyrus.infra.ui.emf.dialog.NestedEditingDialogContext; +import org.eclipse.papyrus.infra.widgets.util.StyledTextUtils; import org.eclipse.papyrus.uml.properties.modelelement.UMLModelElement; import org.eclipse.papyrus.uml.properties.widgets.LanguageBodyEditor; import org.eclipse.papyrus.uml.xtext.integration.DefaultXtextDirectEditorConfiguration; @@ -156,6 +157,8 @@ public class XtextLanguageEditor implements LanguageBodyEditor, IContextElementP } } }); + + StyledTextUtils.addLineNumberSupportForXtext(this.textControl); } /** |