Skip to content

Commit

Permalink
refact: add/use AbstractTableWidget
Browse files Browse the repository at this point in the history
  • Loading branch information
sebthom committed Jan 26, 2024
1 parent a5896c6 commit 3f9e2b6
Show file tree
Hide file tree
Showing 7 changed files with 219 additions and 305 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,22 @@ public final class LanguageConfigurationMessages extends NLS {
public static String AutoClosingPairConditionalTableWidget_notIn;
public static String CharacterPairsTableWidget_end;
public static String CharacterPairsTableWidget_start;
public static String LanguageConfigurationInfoWidget_autoClosingPairs;
public static String LanguageConfigurationInfoWidget_autoClosingPairs_title;
public static String LanguageConfigurationInfoWidget_autoCloseBefore_message;
public static String LanguageConfigurationInfoWidget_autoCloseBefore_title;
public static String LanguageConfigurationInfoWidget_blockCommentsEnd;
public static String LanguageConfigurationInfoWidget_blockCommentsStart;
public static String LanguageConfigurationInfoWidget_brackets;
public static String LanguageConfigurationInfoWidget_comments;
public static String LanguageConfigurationInfoWidget_brackets_title;
public static String LanguageConfigurationInfoWidget_comments_title;
public static String LanguageConfigurationInfoWidget_end;
public static String LanguageConfigurationInfoWidget_folding_title;
public static String LanguageConfigurationInfoWidget_lineComments;
public static String LanguageConfigurationInfoWidget_markers;
public static String LanguageConfigurationInfoWidget_offSide;
public static String LanguageConfigurationInfoWidget_offSide_tooltip;
public static String LanguageConfigurationInfoWidget_onEnterRules;
public static String LanguageConfigurationInfoWidget_onEnterRules_title;
public static String LanguageConfigurationInfoWidget_start;
public static String LanguageConfigurationInfoWidget_surroundingPairs;
public static String LanguageConfigurationInfoWidget_surroundingPairs_title;
public static String LanguageConfigurationInfoWidget_wordPattern_message;
public static String LanguageConfigurationInfoWidget_wordPattern_title;
public static String LanguageConfigurationPreferencePage_contentTypeName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@
AutoClosingPairConditionalTableWidget_notIn=Not In
CharacterPairsTableWidget_start=Start
CharacterPairsTableWidget_end=End
LanguageConfigurationInfoWidget_autoClosingPairs=Auto Closing Pairs
LanguageConfigurationInfoWidget_autoClosingPairs_title=Auto Closing Pairs
LanguageConfigurationInfoWidget_autoCloseBefore_message=Auto Close Before:
LanguageConfigurationInfoWidget_autoCloseBefore_title=Auto Close Before
LanguageConfigurationInfoWidget_blockCommentsEnd=Block Comment End:
LanguageConfigurationInfoWidget_blockCommentsStart=Block Comment Start:
LanguageConfigurationInfoWidget_brackets=Brackets
LanguageConfigurationInfoWidget_comments=Comments
LanguageConfigurationInfoWidget_brackets_title=Brackets
LanguageConfigurationInfoWidget_comments_title=Comments
LanguageConfigurationInfoWidget_end=End:
LanguageConfigurationInfoWidget_folding_title=Folding
LanguageConfigurationInfoWidget_lineComments=Line Comment:
LanguageConfigurationInfoWidget_markers=markers
LanguageConfigurationInfoWidget_offSide=Off Side:
LanguageConfigurationInfoWidget_offSide_tooltip=Empty lines trailing a code section belong to the next folding section
LanguageConfigurationInfoWidget_onEnterRules=On Enter Rules
LanguageConfigurationInfoWidget_onEnterRules_title=On Enter Rules
LanguageConfigurationInfoWidget_start=Start:
LanguageConfigurationInfoWidget_surroundingPairs=Surrounding Pairs
LanguageConfigurationInfoWidget_surroundingPairs_title=Surrounding Pairs
LanguageConfigurationInfoWidget_wordPattern_message=Word Pattern:
LanguageConfigurationInfoWidget_wordPattern_title=Word Pattern
LanguageConfigurationPreferencePage_contentTypeName=Content Type
Expand All @@ -44,7 +44,7 @@ LanguageConfigurationPreferencesWidget_enableOnEnterActions=Enable on enter acti
LanguageConfigurationPreferencesWidget_enableMatchingBrackets=Enable matching brackets
OnEnterRuleTableWidget_beforeText=Before Text
OnEnterRuleTableWidget_afterText=After Text
OnEnterRuleTableWidget_previousLineText=Prev. Line Text
OnEnterRuleTableWidget_previousLineText=Previous Line Text
OnEnterRuleTableWidget_indentAction=Indent Action
OnEnterRuleTableWidget_appendText=Append Text
OnEnterRuleTableWidget_removeText=Remove Text
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/**
* Copyright (c) 2024 Vegard IT GmbH and others.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* - Sebastian Thomschke - initial implementation
*/
package org.eclipse.tm4e.languageconfiguration.internal.widgets;

import java.util.Collections;
import java.util.List;

import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jface.layout.TableColumnLayout;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.TableColumn;

abstract class AbstractTableWidget<T> extends TableViewer {

private final class CellLabelProvider extends LabelProvider implements ITableLabelProvider {
@Override
public @Nullable Image getColumnImage(final @Nullable Object element, final int columnIndex) {
return null;
}

@Override
public String getText(final @Nullable Object element) {
return getColumnText(element, 0);
}

@Override
@SuppressWarnings("unchecked")
public String getColumnText(@Nullable final Object element, final int columnIndex) {
return element == null ? "" : AbstractTableWidget.this.getColumnText((T) element, columnIndex);
}
}

private static final class RowContentProvider<T> implements IStructuredContentProvider {
private List<T> items = Collections.emptyList();

@Override
public void dispose() {
items = Collections.emptyList();
}

@Override
public Object[] getElements(final @Nullable Object input) {
return items.toArray(Object[]::new);
}

@Override
@SuppressWarnings("unchecked")
public void inputChanged(final @Nullable Viewer viewer, final @Nullable Object oldInput, final @Nullable Object newInput) {
items = newInput == null ? Collections.emptyList() : (List<T>) newInput;
}
}

private final TableColumnLayout tableColumnLayout = new TableColumnLayout();

protected AbstractTableWidget(Composite parent) {
this(parent, SWT.BORDER | SWT.FULL_SELECTION | SWT.H_SCROLL | SWT.V_SCROLL);
}

protected AbstractTableWidget(Composite parent, int style) {
super(new Composite(parent, SWT.NONE), style);

setContentProvider(new RowContentProvider<>());
setLabelProvider(new CellLabelProvider());

var table = getTable();
var container = table.getParent();
container.setLayout(new TableColumnLayout());
if (parent.getLayout() instanceof GridLayout) {
container.setLayoutData(new GridData(GridData.FILL_BOTH));
}
table.setHeaderVisible(true);
table.setLinesVisible(true);

createColumns();
}

protected abstract void createColumns();

protected void createColumn(String label, int columnWeight, int minColWidth, boolean resizable) {
final var col = new TableColumn(getTable(), SWT.NONE);
col.setText(label);
final GC gc = new GC(getTable().getShell());
try {
gc.setFont(JFaceResources.getDialogFont());
final int labelWidth = gc.stringExtent(label).x + 15;
tableColumnLayout.setColumnData(col, new ColumnWeightData(columnWeight, Math.max(labelWidth, minColWidth), resizable));
} finally {
gc.dispose();
}
}

protected abstract String getColumnText(final T element, final int columnIndex);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,52 +8,33 @@
*
* Contributors:
* Lucas Bullen (Red Hat Inc.) - initial API and implementation
* Sebastian Thomschke - refactored to extend AbstractTableWidget
*/
package org.eclipse.tm4e.languageconfiguration.internal.widgets;

import static org.eclipse.tm4e.languageconfiguration.internal.LanguageConfigurationMessages.*;
import static org.eclipse.tm4e.languageconfiguration.internal.LanguageConfigurationMessages.AutoClosingPairConditionalTableWidget_notIn;

import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jface.layout.TableColumnLayout;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.tm4e.languageconfiguration.internal.model.AutoClosingPairConditional;
import org.eclipse.tm4e.languageconfiguration.internal.model.CharacterPair;

final class AutoClosingPairConditionalTableWidget extends CharacterPairsTableWidget {

AutoClosingPairConditionalTableWidget(final Table table) {
super(table);
setLabelProvider(new AutoClosingPairConditionalLabelProvider());

final GC gc = new GC(table.getShell());
gc.setFont(JFaceResources.getDialogFont());
final var columnLayout = new TableColumnLayout();

final var column2 = new TableColumn(table, SWT.NONE);
column2.setText(AutoClosingPairConditionalTableWidget_notIn);
final int minWidth = computeMinimumColumnWidth(gc, AutoClosingPairConditionalTableWidget_notIn);
columnLayout.setColumnData(column2, new ColumnWeightData(2, minWidth, true));

gc.dispose();
AutoClosingPairConditionalTableWidget(final Composite parent) {
super(parent);
}

private static final class AutoClosingPairConditionalLabelProvider extends CharacterPairLabelProvider {
@Override
protected void createColumns() {
super.createColumns();
createColumn(AutoClosingPairConditionalTableWidget_notIn, 2, 0, true);
}

@Nullable
@Override
public String getColumnText(@Nullable final Object element, final int columnIndex) {
if (columnIndex == 2) {
if (element instanceof final AutoClosingPairConditional conditionalPair) {
return String.join(", ", conditionalPair.notIn); //$NON-NLS-1$
}
return ""; //$NON-NLS-1$
}
return super.getColumnText(element, columnIndex);
@Override
protected String getColumnText(CharacterPair charPair, int columnIndex) {
if (columnIndex == 2 && charPair instanceof final AutoClosingPairConditional conditionalPair) {
return String.join(", ", conditionalPair.notIn);
}
return super.getColumnText(charPair, columnIndex);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,110 +8,33 @@
*
* Contributors:
* Lucas Bullen (Red Hat Inc.) - initial API and implementation
* Sebastian Thomschke - refactored to extend AbstractTableWidget
*/
package org.eclipse.tm4e.languageconfiguration.internal.widgets;

import static org.eclipse.tm4e.languageconfiguration.internal.LanguageConfigurationMessages.*;

import java.util.Collections;
import java.util.List;

import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jface.layout.TableColumnLayout;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.tm4e.languageconfiguration.internal.model.CharacterPair;

class CharacterPairsTableWidget extends TableViewer {

CharacterPairsTableWidget(final Table table) {
super(table);
setContentProvider(new CharacterPairContentProvider());
setLabelProvider(new CharacterPairLabelProvider());

final GC gc = new GC(table.getShell());
gc.setFont(JFaceResources.getDialogFont());
final var columnLayout = new TableColumnLayout();

final var column1 = new TableColumn(table, SWT.NONE);
column1.setText(CharacterPairsTableWidget_start);
int minWidth = computeMinimumColumnWidth(gc, CharacterPairsTableWidget_start);
columnLayout.setColumnData(column1, new ColumnWeightData(2, minWidth, true));
class CharacterPairsTableWidget extends AbstractTableWidget<CharacterPair> {

final var column2 = new TableColumn(table, SWT.NONE);
column2.setText(CharacterPairsTableWidget_end);
minWidth = computeMinimumColumnWidth(gc, CharacterPairsTableWidget_end);
columnLayout.setColumnData(column2, new ColumnWeightData(2, minWidth, true));

gc.dispose();
}

protected int computeMinimumColumnWidth(final GC gc, final String string) {
return gc.stringExtent(string).x + 10;
CharacterPairsTableWidget(final Composite parent) {
super(parent);
}

private static final class CharacterPairContentProvider implements IStructuredContentProvider {

private List<CharacterPair> characterPairList = Collections.emptyList();

@Override
public Object[] getElements(@Nullable final Object input) {
return characterPairList.toArray(CharacterPair[]::new);
}

@SuppressWarnings("unchecked")
@Override
public void inputChanged(@Nullable final Viewer viewer, @Nullable final Object oldInput,
@Nullable final Object newInput) {
if (newInput == null) {
characterPairList = Collections.emptyList();
} else {
characterPairList = (List<CharacterPair>) newInput;
}
}

@Override
public void dispose() {
characterPairList = Collections.emptyList();
}
@Override
protected String getColumnText(CharacterPair charPair, int columnIndex) {
return switch (columnIndex) {
case 0 -> charPair.open;
case 1 -> charPair.close;
default -> "";
};
}

protected static class CharacterPairLabelProvider extends LabelProvider implements ITableLabelProvider {

@Nullable
@Override
public Image getColumnImage(@Nullable final Object element, final int columnIndex) {
return null;
}

@Nullable
@Override
public String getText(@Nullable final Object element) {
return getColumnText(element, 0);
}

@Nullable
@Override
public String getColumnText(@Nullable final Object element, final int columnIndex) {
if (element == null)
return null;

return switch (columnIndex) {
case 0 -> ((CharacterPair) element).open;
case 1 -> ((CharacterPair) element).close;
default -> ""; //$NON-NLS-1$
};
}
@Override
protected void createColumns() {
createColumn(CharacterPairsTableWidget_start, 1, 0, true);
createColumn(CharacterPairsTableWidget_end, 1, 0, true);
}

}
Loading

0 comments on commit 3f9e2b6

Please sign in to comment.