Skip to content

Commit

Permalink
[eclipse] Add enabling/disabling state for the codemining components.
Browse files Browse the repository at this point in the history
Signed-off-by: Stéphane Galland <galland@arakhne.org>
  • Loading branch information
gallandarakhneorg committed May 13, 2021
1 parent 706cfa2 commit c488954
Show file tree
Hide file tree
Showing 5 changed files with 252 additions and 5 deletions.
Expand Up @@ -46,6 +46,11 @@ public class Messages extends NLS {
public static String SREsPreferencePage_9;
public static String SarlEditorPreferencePage_0;
public static String SarlEditorPreferencePage_1;
public static String SarlEditorPreferencePage_2;
public static String SarlEditorPreferencePage_3;
public static String SarlEditorPreferencePage_4;
public static String SarlEditorPreferencePage_5;
public static String SarlEditorPreferencePage_6;
static {
// initialize resource bundle
NLS.initializeMessages(BUNDLE_NAME, Messages.class);
Expand Down
Expand Up @@ -27,11 +27,14 @@
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
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.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;

Expand Down Expand Up @@ -63,6 +66,14 @@ public class SarlEditorPreferencePage extends PreferencePage implements IWorkben

private Button codeminingButton;

private Button codeminingFieldTypeButton;

private Button codeminingVariableTypeButton;

private Button codeminingActionReturnTypeButton;

private Button codeminingArgumentNameButton;

/**
* Constructor.
*/
Expand Down Expand Up @@ -125,30 +136,88 @@ protected Control createContents(Composite parent) {
null,
getSourceViewerPreferenceAccessor().isAutoFormattingEnabled(),
2);

SWTFactory.createVerticalSpacer(pageComponent, 1);

this.codeminingButton = SWTFactory.createCheckButton(pageComponent,
Messages.SarlEditorPreferencePage_1,
null,
getCodeminingPreferenceAccessor().isCodeminingEnabled(),
2);
this.codeminingButton.addSelectionListener(new SelectionListener() {
@Override
public void widgetSelected(SelectionEvent e) {
enableButtons();
}

@Override
public void widgetDefaultSelected(SelectionEvent e) {
enableButtons();
}
});
final Group codeminingGroup = SWTFactory.createGroup(parent, Messages.SarlEditorPreferencePage_2, 1, 2, GridData.FILL_HORIZONTAL);
this.codeminingFieldTypeButton = SWTFactory.createCheckButton(codeminingGroup,
Messages.SarlEditorPreferencePage_3,
null,
getCodeminingPreferenceAccessor().isCodeminingFieldTypeEnabled(),
1);
this.codeminingVariableTypeButton = SWTFactory.createCheckButton(codeminingGroup,
Messages.SarlEditorPreferencePage_4,
null,
getCodeminingPreferenceAccessor().isCodeminingVariableTypeEnabled(),
1);
this.codeminingActionReturnTypeButton = SWTFactory.createCheckButton(codeminingGroup,
Messages.SarlEditorPreferencePage_5,
null,
getCodeminingPreferenceAccessor().isCodeminingActionReturnTypeEnabled(),
1);
this.codeminingArgumentNameButton = SWTFactory.createCheckButton(codeminingGroup,
Messages.SarlEditorPreferencePage_6,
null,
getCodeminingPreferenceAccessor().isCodeminingFeatureCallArgumentNameEnabled(),
1);

SWTFactory.createVerticalSpacer(pageComponent, 1);

applyDialogFont(parent);

enableButtons();

return parent;
}

/**
* Enables the buttons.
*/
private void enableButtons() {
final boolean isCodeminingEnabled = this.codeminingButton.getSelection();
this.codeminingFieldTypeButton.setEnabled(isCodeminingEnabled);
this.codeminingVariableTypeButton.setEnabled(isCodeminingEnabled);
this.codeminingActionReturnTypeButton.setEnabled(isCodeminingEnabled);
this.codeminingArgumentNameButton.setEnabled(isCodeminingEnabled);
}

@Override
protected void performDefaults() {
this.autoformattingButton.setSelection(SARLSourceViewerPreferenceAccess.AUTOFORMATTING_DEFAULT_VALUE);
this.codeminingFieldTypeButton.setSelection(SARLCodeminingPreferenceAccess.CODEMINING_FIELD_TYPE_VALUE);
this.codeminingVariableTypeButton.setSelection(SARLCodeminingPreferenceAccess.CODEMINING_VARIABLE_TYPE_VALUE);
this.codeminingActionReturnTypeButton.setSelection(SARLCodeminingPreferenceAccess.CODEMINING_ACTION_RETURN_TYPE_VALUE);
this.codeminingArgumentNameButton.setSelection(SARLCodeminingPreferenceAccess.CODEMINING_FEATURECALL_ARGUMENT_NAME_VALUE);
this.codeminingButton.setSelection(SARLCodeminingPreferenceAccess.CODEMINING_DEFAULT_VALUE);
super.performDefaults();
enableButtons();
}

@Override
public boolean performOk() {
getSourceViewerPreferenceAccessor().setAutoFormattingEnabled(this.autoformattingButton.getSelection());
getCodeminingPreferenceAccessor().setCodeminingEnabled(this.codeminingButton.getSelection());
final SARLCodeminingPreferenceAccess codeminingAccessor = getCodeminingPreferenceAccessor();
codeminingAccessor.setCodeminingFieldTypeEnabled(this.codeminingFieldTypeButton.getSelection());
codeminingAccessor.setCodeminingVariableTypeEnabled(this.codeminingVariableTypeButton.getSelection());
codeminingAccessor.setCodeminingActionReturnTypeEnabled(this.codeminingActionReturnTypeButton.getSelection());
codeminingAccessor.setCodeminingFeatureCallArgumentNameEnabled(this.codeminingArgumentNameButton.getSelection());
codeminingAccessor.setCodeminingEnabled(this.codeminingButton.getSelection());
return super.performOk();
}

Expand Down
Expand Up @@ -11,3 +11,8 @@ SREsPreferencePage_8=You must specify a default SRE.
SREsPreferencePage_9=Internal Error: {0}
SarlEditorPreferencePage_0=Auto formatting of the inserted code on paste actions
SarlEditorPreferencePage_1=Show implicit SARL code in the editors (codemining)
SarlEditorPreferencePage_2=Code mining components
SarlEditorPreferencePage_3=Show the inferred type of type fields
SarlEditorPreferencePage_4=Show the inferred type of local variables
SarlEditorPreferencePage_5=Show the inferred type of the action return types
SarlEditorPreferencePage_6=Show the name of the formal arguments
Expand Up @@ -153,9 +153,18 @@ private void disableCodeMining(ITextViewer viewer, Throwable error) {
@Override
protected void createCodeMinings(IDocument document, XtextResource resource, CancelIndicator indicator,
IAcceptor<? super ICodeMining> acceptor) throws BadLocationException {
createImplicitActionReturnType(resource, acceptor);
createImplicitFieldType(resource, acceptor);
createImplicitVariableType(resource, acceptor);
if (this.codeminingPreferences.isCodeminingActionReturnTypeEnabled()) {
createImplicitActionReturnType(resource, acceptor);
}
if (this.codeminingPreferences.isCodeminingFieldTypeEnabled()) {
createImplicitFieldType(resource, acceptor);
}
if (this.codeminingPreferences.isCodeminingVariableTypeEnabled()) {
createImplicitVariableType(resource, acceptor);
}
if (this.codeminingPreferences.isCodeminingFeatureCallArgumentNameEnabled()) {
createFeatureCallArgumentNames(resource, acceptor);
}
}

/** Add an annotation when the var/val declaration's type is implicit and inferred by the SARL compiler.
Expand Down Expand Up @@ -345,6 +354,16 @@ private void createImplicitActionReturnType(XtextResource resource, IAcceptor<?
}
}

/** Add an annotation with the names of the arguments into a feature call.
*
* @param resource the resource to parse.
* @param acceptor the code mining acceptor.
* @since 0.12
*/
private void createFeatureCallArgumentNames(XtextResource resource, IAcceptor<? super ICodeMining> acceptor) {
//
}

/** This indicator checks if monitor is canceled or if
* CancelableUnitOfWork-cancelIndicator is canceled.
* <strong>Only for fixing bug 1041.</strong>
Expand Down
Expand Up @@ -53,6 +53,46 @@ public class SARLCodeminingPreferenceAccess extends AbstractPreferenceAccess {
*/
public static final boolean CODEMINING_DEFAULT_VALUE = true;

/** Key for saving the enabling state of the code mining feature of the action return type.
* @since 0.12
*/
public static final String CODEMINING_ACTION_RETURN_TYPE_PROPERTY = PREFIX + "codemining.actionReturnType"; //$NON-NLS-1$

/** Default value for saving the enabling state of the code mining feature of the action return type.
* @since 0.12
*/
public static final boolean CODEMINING_ACTION_RETURN_TYPE_VALUE = true;

/** Key for saving the enabling state of the code mining feature of the field type.
* @since 0.12
*/
public static final String CODEMINING_FIELD_TYPE_PROPERTY = PREFIX + "codemining.fieldType"; //$NON-NLS-1$

/** Default value for saving the enabling state of the code mining feature of the field type.
* @since 0.12
*/
public static final boolean CODEMINING_FIELD_TYPE_VALUE = true;

/** Key for saving the enabling state of the code mining feature of the variable type.
* @since 0.12
*/
public static final String CODEMINING_VARIABLE_TYPE_PROPERTY = PREFIX + "codemining.variableType"; //$NON-NLS-1$

/** Default value for saving the enabling state of the code mining feature of the variable type.
* @since 0.12
*/
public static final boolean CODEMINING_VARIABLE_TYPE_VALUE = true;

/** Key for saving the enabling state of the code mining feature of the function's argument names.
* @since 0.12
*/
public static final String CODEMINING_FEATURECALL_ARGUMENT_NAME_PROPERTY = PREFIX + "codemining.featureCallArgumentName"; //$NON-NLS-1$

/** Default value for saving the enabling state of the code mining feature of the function's argument names.
* @since 0.12
*/
public static final boolean CODEMINING_FEATURECALL_ARGUMENT_NAME_VALUE = true;

/** Replies if the code mining feature is enable into the SARL editor.
*
* @return {@code true} if it is enabled.
Expand All @@ -79,10 +119,114 @@ public void setCodeminingEnabled(Boolean enable) {
}
}

/** Replies if the code mining feature is enable into the SARL editor for the action return types.
*
* @return {@code true} if it is enabled.
* @since 0.12
*/
public boolean isCodeminingActionReturnTypeEnabled() {
final IPreferenceStore store = getWritablePreferenceStore(null);
return store.getBoolean(CODEMINING_ACTION_RETURN_TYPE_PROPERTY);
}

/** Enable or disable the code mining feature into the SARL editor for the action return types.
*
* @param enable is {@code true} if it is enabled; {@code false} if it is disable; {@code null}
* to restore the default value.
* @since 0.12
*/
public void setCodeminingActionReturnTypeEnabled(Boolean enable) {
final IPreferenceStore store = getWritablePreferenceStore(null);
if (enable == null) {
store.setToDefault(CODEMINING_ACTION_RETURN_TYPE_PROPERTY);
} else {
store.setValue(CODEMINING_ACTION_RETURN_TYPE_PROPERTY, enable.booleanValue());
}
}

/** Replies if the code mining feature is enable into the SARL editor for the field types.
*
* @return {@code true} if it is enabled.
* @since 0.12
*/
public boolean isCodeminingFieldTypeEnabled() {
final IPreferenceStore store = getWritablePreferenceStore(null);
return store.getBoolean(CODEMINING_FIELD_TYPE_PROPERTY);
}

/** Enable or disable the code mining feature into the SARL editor for the field types.
*
* @param enable is {@code true} if it is enabled; {@code false} if it is disable; {@code null}
* to restore the default value.
* @since 0.12
*/
public void setCodeminingFieldTypeEnabled(Boolean enable) {
final IPreferenceStore store = getWritablePreferenceStore(null);
if (enable == null) {
store.setToDefault(CODEMINING_FIELD_TYPE_PROPERTY);
} else {
store.setValue(CODEMINING_FIELD_TYPE_PROPERTY, enable.booleanValue());
}
}

/** Replies if the code mining feature is enable into the SARL editor for the variable types.
*
* @return {@code true} if it is enabled.
* @since 0.12
*/
public boolean isCodeminingVariableTypeEnabled() {
final IPreferenceStore store = getWritablePreferenceStore(null);
return store.getBoolean(CODEMINING_VARIABLE_TYPE_PROPERTY);
}

/** Enable or disable the code mining feature into the SARL editor for the variable types.
*
* @param enable is {@code true} if it is enabled; {@code false} if it is disable; {@code null}
* to restore the default value.
* @since 0.12
*/
public void setCodeminingVariableTypeEnabled(Boolean enable) {
final IPreferenceStore store = getWritablePreferenceStore(null);
if (enable == null) {
store.setToDefault(CODEMINING_VARIABLE_TYPE_PROPERTY);
} else {
store.setValue(CODEMINING_VARIABLE_TYPE_PROPERTY, enable.booleanValue());
}
}

/** Replies if the code mining feature is enable into the SARL editor for the function's argument names.
*
* @return {@code true} if it is enabled.
* @since 0.12
*/
public boolean isCodeminingFeatureCallArgumentNameEnabled() {
final IPreferenceStore store = getWritablePreferenceStore(null);
return store.getBoolean(CODEMINING_FEATURECALL_ARGUMENT_NAME_PROPERTY);
}

/** Enable or disable the code mining feature into the SARL editor for the function's argument names.
*
* @param enable is {@code true} if it is enabled; {@code false} if it is disable; {@code null}
* to restore the default value.
* @since 0.12
*/
public void setCodeminingFeatureCallArgumentNameEnabled(Boolean enable) {
final IPreferenceStore store = getWritablePreferenceStore(null);
if (enable == null) {
store.setToDefault(CODEMINING_FEATURECALL_ARGUMENT_NAME_PROPERTY);
} else {
store.setValue(CODEMINING_FEATURECALL_ARGUMENT_NAME_PROPERTY, enable.booleanValue());
}
}

@Override
public void setToDefault(Object context) {
final IPreferenceStore store = getWritablePreferenceStore(context);
store.setToDefault(CODEMINING_PROPERTY);
store.setToDefault(CODEMINING_ACTION_RETURN_TYPE_PROPERTY);
store.setToDefault(CODEMINING_FIELD_TYPE_PROPERTY);
store.setToDefault(CODEMINING_VARIABLE_TYPE_PROPERTY);
store.setToDefault(CODEMINING_FEATURECALL_ARGUMENT_NAME_PROPERTY);
}

/** Initializer of the preferences for the SARL code mining feature.
Expand All @@ -96,7 +240,12 @@ public void setToDefault(Object context) {
public static class Initializer implements IPreferenceStoreInitializer {
@Override
public void initialize(IPreferenceStoreAccess access) {
access.getWritablePreferenceStore().setDefault(CODEMINING_PROPERTY, CODEMINING_DEFAULT_VALUE);
final IPreferenceStore store = access.getWritablePreferenceStore();
store.setDefault(CODEMINING_PROPERTY, CODEMINING_DEFAULT_VALUE);
store.setDefault(CODEMINING_ACTION_RETURN_TYPE_PROPERTY, CODEMINING_ACTION_RETURN_TYPE_VALUE);
store.setDefault(CODEMINING_FIELD_TYPE_PROPERTY, CODEMINING_FIELD_TYPE_VALUE);
store.setDefault(CODEMINING_VARIABLE_TYPE_PROPERTY, CODEMINING_VARIABLE_TYPE_VALUE);
store.setDefault(CODEMINING_FEATURECALL_ARGUMENT_NAME_PROPERTY, CODEMINING_FEATURECALL_ARGUMENT_NAME_VALUE);
}
}

Expand Down

0 comments on commit c488954

Please sign in to comment.