Skip to content

Commit

Permalink
[ui] Add preference accessor for SARL source viewer.
Browse files Browse the repository at this point in the history
The first preference is related to the auto-formatting of the pasted
text.

see #755

Signed-off-by: Stéphane Galland <galland@arakhne.org>
  • Loading branch information
gallandarakhneorg committed Sep 25, 2017
1 parent ca64d49 commit 4c8fc58
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ public class SARLSourceViewer extends RichStringAwareSourceViewer {
@Inject
private Provider<IDocumentAutoFormatter> autoFormatterProvider;

@Inject
private SARLSourceViewerPreferenceAccess preferences;

/** Constructor.
*
* @param parent the container.
Expand All @@ -67,6 +70,15 @@ public SARLSourceViewer(Composite parent, IVerticalRuler ruler, IOverviewRuler o
super(parent, ruler, overviewRuler, showsAnnotationOverview, styles);
}

/** Replies if the auto-formatting feature is enable on paste actions.
*
* @return {@code true} if the auto-formatter is turn on.
* @since 0.7
*/
public boolean isAutoFormattingEnable() {
return this.preferences.isAutoFormattingEnabled();
}

/** Replies the document auto-formatter.
*
* @return the service.
Expand All @@ -85,7 +97,7 @@ public IDocumentAutoFormatter getDocumentAutoFormatter() {

@Override
public void doOperation(int operation) {
if (operation == ITextOperationTarget.PASTE) {
if (operation == ITextOperationTarget.PASTE && isAutoFormattingEnable()) {
final IRewriteTarget target = getRewriteTarget();
target.beginCompoundChange();
final IDocumentAutoFormatter formatter = getDocumentAutoFormatter();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* $Id$
*
* SARL is an general-purpose agent programming language.
* More details on http://www.sarl.io
*
* Copyright (C) 2014-2017 the original authors or authors.
*
* 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 io.sarl.lang.ui.editor;

import com.google.inject.Inject;
import org.eclipse.jdt.internal.ui.preferences.OptionsConfigurationBlock;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.xtext.ui.editor.preferences.IPreferenceStoreAccess;
import org.eclipse.xtext.ui.editor.preferences.IPreferenceStoreInitializer;
import org.eclipse.xtext.ui.editor.preferences.PreferenceStoreAccessImpl;

/** Preferences for the SARL Source viewer.
*
* @author $Author: sgalland$
* @version $FullVersion$
* @mavengroupid $GroupId$
* @mavenartifactid $ArtifactId$
* @since 0.7
*/
public class SARLSourceViewerPreferenceAccess {

/** Prefix for the preference keys.
*/
public static final String PREFIX = SARLSourceViewerPreferenceAccess.class.getPackage().getName() + "."; //$NON-NLS-1$

/** Key for saving the enabling state of the auto-formatting feature.
*/
public static final String AUTOFORMATTING_PROPERTY = PREFIX + "sarlAutoFormatting"; //$NON-NLS-1$

/** Default value for saving the enabling state of the auto-formatting feature.
*/
public static final boolean AUTOFORMATTING_DEFAULT_VALUE = true;

private PreferenceStoreAccessImpl preferenceStoreAccess;

/** Change the preference accessor.
*
* <p>The parameter is a preference store implementation in order to have access to the correct preference set.
* It is an implementation choice from {@link OptionsConfigurationBlock}.
*
* @param preferenceStoreAccess the accessor.
*/
@Inject
public void setPreferenceStoreAccess(PreferenceStoreAccessImpl preferenceStoreAccess) {
this.preferenceStoreAccess = preferenceStoreAccess;
}

/** Replies the preference accessor.
*
* @return the accessor.
*/
@Inject
public IPreferenceStoreAccess getPreferenceStoreAccess() {
return this.preferenceStoreAccess;
}

/** Replies the writable preference store to be used for the SARL editor.
*
* @return the modifiable preference store.
* @see #getPreferenceStore()
*/
public IPreferenceStore getWritablePreferenceStore() {
return getPreferenceStoreAccess().getWritablePreferenceStore();
}

/** Replies the readable preference store to be used for the SARL editor.
*
* @return the unmodifiable preference store.
* @see #getWritablePreferenceStore()
*/
public IPreferenceStore getPreferenceStore() {
return getPreferenceStoreAccess().getPreferenceStore();
}

/** Replies if the auto-formatting feature is enable into the SARL editor.
*
* @return {@code true} if it is enabled.
*/
public boolean isAutoFormattingEnabled() {
final IPreferenceStore store = getPreferenceStore();
return store.getBoolean(AUTOFORMATTING_PROPERTY);
}

/** Initializer of the preferences for the SARL Source viewer.
*
* @author $Author: sgalland$
* @version $FullVersion$
* @mavengroupid $GroupId$
* @mavenartifactid $ArtifactId$
* @since 0.7
*/
public static class Initializer implements IPreferenceStoreInitializer {
@Override
public void initialize(IPreferenceStoreAccess access) {
access.getWritablePreferenceStore().setDefault(AUTOFORMATTING_PROPERTY, AUTOFORMATTING_DEFAULT_VALUE);
}
}

/** Set the values of the preferences to the default values.
*/
public void setToDefault() {
final IPreferenceStore store = getWritablePreferenceStore();
store.setToDefault(AUTOFORMATTING_PROPERTY);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.eclipse.xtext.validation.ConfigurableIssueCodesProvider;

import io.sarl.lang.ui.compiler.extra.ExtensionPointExtraLanguagePreferenceInitializer;
import io.sarl.lang.ui.editor.SARLSourceViewerPreferenceAccess;

/** Initialize the preference store with SARL specific information.
*
Expand All @@ -52,6 +53,9 @@ public class SARLPreferenceStoreInitializer implements IPreferenceStoreInitializ
@Inject
private ExtensionPointExtraLanguagePreferenceInitializer extraLanguagePreferenceInitializer;

@Inject
private SARLSourceViewerPreferenceAccess.Initializer sourceViewerInitializer;

@Override
public void initialize(IPreferenceStoreAccess preferenceStoreAccess) {
this.preferenceStoreAccess = preferenceStoreAccess;
Expand All @@ -67,6 +71,9 @@ public void initialize(IPreferenceStoreAccess preferenceStoreAccess) {
PreferenceConstants.EDITOR_SUB_WORD_NAVIGATION,
preferenceStore.getBoolean(org.eclipse.jdt.ui.PreferenceConstants.EDITOR_SUB_WORD_NAVIGATION));

// Initialize the editor preferences
setupSourceViewerDefaults(preferenceStoreAccess);

// Initialize the generators for the extra languages.
setupExtraLanguageGeneratorDefaults(preferenceStoreAccess);
}
Expand All @@ -82,6 +89,10 @@ private void setupExtraLanguageGeneratorDefaults(IPreferenceStoreAccess preferen
this.extraLanguagePreferenceInitializer.initialize(preferenceStoreAccess);
}

private void setupSourceViewerDefaults(IPreferenceStoreAccess preferenceStoreAccess) {
this.sourceViewerInitializer.initialize(preferenceStoreAccess);
}

@Override
public void propertyChange(PropertyChangeEvent event) {
if (this.preferenceStoreAccess == null) {
Expand Down

0 comments on commit 4c8fc58

Please sign in to comment.