Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refact: theme associations and content type bindings preferences UI #748

Merged
merged 1 commit into from
May 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ protected Control createContents(final @NonNullByDefault({}) Composite parent) {
: selectedDefinitions.get(0), manager);
});
langCfgsTable.setInput(manager);
langCfgsTable.selectFirstRow();

return control;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ ThemePreferencePage_preview=Previe&w with grammar:
# Widgets
ContentTypesBindingWidget_description=Content type bindings:
ThemeAssociationsWidget_description=Theme associations:
ThemeAssociationsWidget_remove_dialog_title=Remove theme associations
ThemeAssociationsWidget_remove_dialog_message=Are you sure to remove selected theme associations?
ThemeAssociationLabelProvider_light=Use ''{0}'' theme.
ThemeAssociationLabelProvider_dark=Use ''{0}'' theme with Eclipse 'Dark' theme.
ThemeAssociationsWidget_remove_dialog_title=Remove theme association
ThemeAssociationsWidget_remove_dialog_message=Are you sure to remove selected theme association?
ThemeAssociationLabelProvider_light=When Eclipse 'Light' theme: use {0}''{1}'' theme.
ThemeAssociationLabelProvider_dark=When Eclipse 'Dark' theme: use {0}''{1}'' theme.
GrammarInfoWidget_name_text=Name:
GrammarInfoWidget_scopeName_text=Scope:
GrammarInfoWidget_fileTypes_text=File types:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,13 @@

import static org.eclipse.tm4e.core.internal.utils.NullSafetyHelper.lazyNonNull;

import java.util.Arrays;
import java.util.stream.Collectors;

import org.eclipse.core.runtime.content.IContentType;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.window.Window;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.swt.SWT;
Expand All @@ -43,7 +39,6 @@
import org.eclipse.tm4e.ui.TMUIPlugin;
import org.eclipse.tm4e.ui.internal.TMUIMessages;
import org.eclipse.tm4e.ui.internal.themes.ThemeManager;
import org.eclipse.tm4e.ui.internal.widgets.ContentTypesBindingWidget;
import org.eclipse.tm4e.ui.internal.widgets.GrammarInfoWidget;
import org.eclipse.tm4e.ui.internal.widgets.TMViewer;
import org.eclipse.tm4e.ui.internal.widgets.TableWidget;
Expand Down Expand Up @@ -75,7 +70,7 @@ public final class GrammarPreferencePage extends AbstractPreferencePage {

// Grammar info tabs
private GrammarInfoWidget grammarInfoWidget = lazyNonNull();
private ContentTypesBindingWidget contentTypesWidget = lazyNonNull();
private TableWithControlsWidget<IContentType> contentTypesWidget = lazyNonNull();
private ThemeAssociationsWidget themeAssociationsWidget = lazyNonNull();

private TMViewer grammarPreview = lazyNonNull();
Expand Down Expand Up @@ -103,7 +98,6 @@ protected void configureLowerArea(final Composite parent) {
Dialog.applyDialogFont(control);

grammarsTable.setInput(grammarManager);
grammarsTable.selectFirstRow();

return control;
}
Expand Down Expand Up @@ -235,7 +229,30 @@ private void createContentTypeTab(final TabFolder folder) {
parent.setLayout(new GridLayout());
parent.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

contentTypesWidget = new ContentTypesBindingWidget(parent, SWT.NONE);
contentTypesWidget = new TableWithControlsWidget<>(parent, TMUIMessages.ContentTypesBindingWidget_description, false) {

@Override
protected TableWidget<IContentType> createTable(final Composite parent) {
return new TableWidget<>(parent, false) {
{
getTable().setHeaderVisible(false);
}

@Override
protected void createColumns() {
createColumn("", 100, 0);
}

@Override
protected @Nullable String getColumnText(final IContentType contentType, final int columnIndex) {
return switch (columnIndex) {
case 0 -> contentType.getName() + " (" + contentType.getId() + ")";
default -> null;
};
}
};
}
};
contentTypesWidget.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

tab.setControl(parent);
Expand All @@ -252,35 +269,17 @@ private void createThemeTab(final TabFolder folder) {
parent.setLayout(new GridLayout());
parent.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

themeAssociationsWidget = new ThemeAssociationsWidget(themeManager, parent, SWT.NONE);
themeAssociationsWidget = new ThemeAssociationsWidget(themeManager, parent);
final var data = new GridData(GridData.FILL_HORIZONTAL);
data.horizontalSpan = 2;
themeAssociationsWidget.setLayoutData(data);
themeAssociationsWidget.addSelectionChangedListener(new ISelectionChangedListener() {

@Override
public void selectionChanged(final @Nullable SelectionChangedEvent e) {
if (e == null)
return;
final var association = (IThemeAssociation) ((IStructuredSelection) e.getSelection()).getFirstElement();
selectTheme(association);
}

private void selectTheme(final @Nullable IThemeAssociation association) {
themeAssociationsWidget.getNewButton().setEnabled(association != null /* && association.getPluginId() == null */);
themeAssociationsWidget.getRemoveButton().setEnabled(association != null /* && association.getPluginId() == null */);
if (association != null) {
setPreviewTheme(association.getThemeId());
}
themeAssociationsWidget.getTable().onSelectionChanged(associations -> {
if (!associations.isEmpty()) {
setPreviewTheme(associations.get(0).getThemeId());
}
});

tab.setControl(parent);

grammarsTable.onSelectionChanged(selectedGrammarDefinitions -> {
themeAssociationsWidget.getNewButton().setEnabled(false);
themeAssociationsWidget.getRemoveButton().setEnabled(false);
});
}

private void setPreviewTheme(final String themeId) {
Expand Down Expand Up @@ -309,8 +308,8 @@ private void createInjectionTab(final TabFolder folder) {
private void selectGrammar(final IGrammarDefinition definition) {
fillGeneralTab(definition);
fillContentTypeTab(definition);
final IThemeAssociation selectedAssociation = fillThemeTab(definition);
preview(definition, selectedAssociation);
fillThemeTab(definition);
preview(definition, themeAssociationsWidget.getTable().getFirstSelectedElement());
}

private void fillGeneralTab(final IGrammarDefinition definition) {
Expand All @@ -320,28 +319,22 @@ private void fillGeneralTab(final IGrammarDefinition definition) {

private void fillContentTypeTab(final IGrammarDefinition definition) {
// Load the content type binding for the given grammar
contentTypesWidget.setInput(grammarManager.getContentTypesForScope(definition.getScope()));
contentTypesWidget.getTable().setInput(grammarManager.getContentTypesForScope(definition.getScope()));
}

@Nullable
private IThemeAssociation fillThemeTab(final IGrammarDefinition definition) {
IThemeAssociation selectedAssociation = null;
final IStructuredSelection oldSelection = themeAssociationsWidget.getSelection();
// Load the theme associations for the given grammar
final IThemeAssociation[] themeAssociations = themeAssociationsWidget.setGrammarDefinition(definition);
// Try to keep selection
if (!oldSelection.isEmpty() && Arrays.asList(themeAssociations).contains(oldSelection.getFirstElement())) {
selectedAssociation = (IThemeAssociation) oldSelection.getFirstElement();
themeAssociationsWidget.setSelection(oldSelection);
} else {
selectedAssociation = themeAssociations.length > 0
? themeAssociations[0]
: null;
if (selectedAssociation != null) {
themeAssociationsWidget.setSelection(new StructuredSelection(selectedAssociation));
private void fillThemeTab(final IGrammarDefinition definition) {
final var selectedAssociation = themeAssociationsWidget.getTable().getFirstSelectedElement();
themeAssociationsWidget.setGrammarDefinition(definition);

// restore theme selection
if (selectedAssociation != null) {
for (IThemeAssociation association : themeAssociationsWidget.getTable().getElements()) {
if (association.getThemeId().equals(selectedAssociation.getThemeId())) {
themeAssociationsWidget.getTable().setSelection(association);
break;
}
}
}
return selectedAssociation;
}

private void createThemePreview(final Composite parent) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ protected void configureLowerArea(Composite parent) {
Dialog.applyDialogFont(control);

themesTable.setInput(themeManager);
themesTable.selectFirstRow();

return control;
}
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,15 @@ protected void createAutoResizeColumn(final String label, final int... secondary
autoResizeColumns.add(col);
}

protected void createColumn(final String label, final int columnWeight, final int minColWidth,
protected void createColumn(final @Nullable String label, final int columnWeight, final int minColWidth,
final int... secondarySortColumns) {
final var col = new TableColumn(getTable(), SWT.NONE);
col.setText(label);
col.setText(label == null ? "" : label);
this.secondarySortColumns.add(secondarySortColumns);
col.addSelectionListener(new ColumnSelectionAdapter(this, viewerComparator, secondarySortColumns));

tableColumnLayout.setColumnData(col,
new ColumnWeightData(columnWeight, Math.max(UI.getTextWidth(label) + 15, minColWidth), true));
new ColumnWeightData(columnWeight, Math.max(UI.getTextWidth(col.getText()) + 15, minColWidth), true));
}

protected abstract void createColumns();
Expand All @@ -118,6 +118,11 @@ protected void createColumn(final String label, final int columnWeight, final in
*/
protected abstract @Nullable Object getColumnText(T element, int columnIndex);

@SuppressWarnings({ "unchecked" })
public List<T> getElements() {
return (List<T>) List.of(getElements(getInput()));
}

protected Object[] getElements(final @Nullable Object input) {
if (input == null)
return new Object[0];
Expand Down Expand Up @@ -151,6 +156,10 @@ protected void inputChanged(final @Nullable Object input, final @Nullable Object

// auto refresh when input has changed
refresh();

if (getFirstSelectedElement() == null) {
selectFirstRow();
}
}

public TableWidget<T> onSelectionChanged(final Consumer<List<T>> consumer) {
Expand Down
Loading
Loading