Skip to content

Commit

Permalink
feat: auto select first entry in grammar/themes/langcfg table views
Browse files Browse the repository at this point in the history
  • Loading branch information
sebthom committed Nov 16, 2023
1 parent efb6c76 commit cfe0d54
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
package org.eclipse.tm4e.languageconfiguration.internal.preferences;

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

import java.util.Collection;
Expand Down Expand Up @@ -51,6 +52,7 @@
import org.eclipse.tm4e.languageconfiguration.internal.widgets.ColumnViewerComparator;
import org.eclipse.tm4e.languageconfiguration.internal.widgets.LanguageConfigurationPreferencesWidget;
import org.eclipse.tm4e.languageconfiguration.internal.wizards.LanguageConfigurationImportWizard;
import org.eclipse.tm4e.ui.internal.utils.UI;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;
import org.osgi.service.prefs.BackingStoreException;
Expand All @@ -67,11 +69,8 @@ public final class LanguageConfigurationPreferencePage extends PreferencePage im
private final ILanguageConfigurationRegistryManager manager = new WorkingCopyLanguageConfigurationRegistryManager(
LanguageConfigurationRegistryManager.getInstance());

@Nullable
private TableViewer definitionViewer;

@Nullable
private LanguageConfigurationPreferencesWidget infoWidget;
private TableViewer definitionViewer = lazyNonNull();
private LanguageConfigurationPreferencesWidget infoWidget = lazyNonNull();

public LanguageConfigurationPreferencePage() {
setDescription(LanguageConfigurationPreferencePage_description);
Expand All @@ -98,21 +97,18 @@ protected Control createContents(@Nullable final Composite ancestor) {

createDefinitionsListContent(parent);

assert definitionViewer != null;
definitionViewer.setInput(manager);

final var infoWidget = new LanguageConfigurationPreferencesWidget(parent, SWT.NONE);
this.infoWidget = infoWidget;

infoWidget = new LanguageConfigurationPreferencesWidget(parent, SWT.NONE);
final var data = new GridData(GridData.FILL_HORIZONTAL);
data.horizontalSpan = 2;
infoWidget.setLayoutData(data);

Dialog.applyDialogFont(parent);
innerParent.layout();

return parent;
definitionViewer.setInput(manager);
UI.selectFirstElement(definitionViewer);

return parent;
}

/**
Expand Down Expand Up @@ -141,8 +137,7 @@ private void createDefinitionsListContent(final Composite parent) {

final var viewerComparator = new ColumnViewerComparator();

final var definitionViewer = new TableViewer(table);
this.definitionViewer = definitionViewer;
this.definitionViewer = new TableViewer(table);

for (int i = 0; i < 4; i++) {
final var column = new TableColumn(table, SWT.NONE);
Expand Down Expand Up @@ -227,7 +222,6 @@ private void remove() {
@Override
public void selectionChanged(@Nullable final SelectionChangedEvent e) {
final var selection = definitionViewer.getStructuredSelection();
assert infoWidget != null;
infoWidget.refresh(null, manager);
if (selection.isEmpty()) {
return;
Expand All @@ -240,7 +234,6 @@ public void selectionChanged(@Nullable final SelectionChangedEvent e) {
}

private void selectDefinition(final ILanguageConfigurationDefinition definition) {
assert infoWidget != null;
infoWidget.refresh(definition, manager);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import org.eclipse.tm4e.ui.TMUIPlugin;
import org.eclipse.tm4e.ui.internal.TMUIMessages;
import org.eclipse.tm4e.ui.internal.themes.WorkingCopyThemeManager;
import org.eclipse.tm4e.ui.internal.utils.UI;
import org.eclipse.tm4e.ui.internal.widgets.ColumnSelectionAdapter;
import org.eclipse.tm4e.ui.internal.widgets.ColumnViewerComparator;
import org.eclipse.tm4e.ui.internal.widgets.ContentTypesBindingWidget;
Expand Down Expand Up @@ -171,6 +172,7 @@ protected Control createContents(@Nullable final Composite ancestor) {

previewViewer = doCreateViewer(innerParent);
grammarViewer.setInput(grammarRegistryManager);
UI.selectFirstElement(grammarViewer);

updateButtons();
Dialog.applyDialogFont(parent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import org.eclipse.tm4e.registry.TMEclipseRegistryPlugin;
import org.eclipse.tm4e.ui.TMUIPlugin;
import org.eclipse.tm4e.ui.internal.TMUIMessages;
import org.eclipse.tm4e.ui.internal.utils.UI;
import org.eclipse.tm4e.ui.internal.widgets.ColumnSelectionAdapter;
import org.eclipse.tm4e.ui.internal.widgets.ColumnViewerComparator;
import org.eclipse.tm4e.ui.internal.widgets.GrammarDefinitionContentProvider;
Expand Down Expand Up @@ -116,6 +117,7 @@ protected Control createContents(@Nullable final Composite ancestor) {
parent.setWeights(2, 1);

themesTable.setInput(themeManager);
UI.selectFirstElement(themesTable);

Dialog.applyDialogFont(parent);
innerParent.layout();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbenchPage;
Expand Down Expand Up @@ -91,6 +93,14 @@ public static Display getDisplay() {
return Display.getDefault();
}

public static boolean selectFirstElement(final TableViewer viewer) {
final var firstElement = viewer.getElementAt(0);
if (firstElement == null)
return false;
viewer.setSelection(new StructuredSelection(firstElement), true);
return true;
}

private UI() {
}
}

0 comments on commit cfe0d54

Please sign in to comment.