Search in sources :

Example 1 with RichTextCellPainter

use of org.eclipse.nebula.widgets.nattable.extension.nebula.richtext.RichTextCellPainter in project nebula.widgets.nattable by eclipse.

the class _6036_SingleFieldFilterExample method createExampleControl.

@Override
public Control createExampleControl(Composite parent) {
    Composite panel = new Composite(parent, SWT.NONE);
    panel.setLayout(new GridLayout());
    GridDataFactory.fillDefaults().grab(true, true).applyTo(panel);
    Text input = new Text(panel, SWT.SINGLE | SWT.SEARCH | SWT.ICON_CANCEL);
    input.setMessage("type filter text");
    GridDataFactory.fillDefaults().grab(true, false).applyTo(input);
    // property names of the Person class
    String[] propertyNames = { "firstName", "lastName", "gender", "married", "birthday", "address.street", "address.housenumber", "address.postalCode", "address.city" };
    // mapping from property to label, needed for column header labels
    Map<String, String> propertyToLabelMap = new HashMap<>();
    propertyToLabelMap.put("firstName", "Firstname");
    propertyToLabelMap.put("lastName", "Lastname");
    propertyToLabelMap.put("gender", "Gender");
    propertyToLabelMap.put("married", "Married");
    propertyToLabelMap.put("birthday", "Birthday");
    propertyToLabelMap.put("address.street", "Street");
    propertyToLabelMap.put("address.housenumber", "Housenumber");
    propertyToLabelMap.put("address.postalCode", "Postal Code");
    propertyToLabelMap.put("address.city", "City");
    IColumnPropertyAccessor<PersonWithAddress> columnPropertyAccessor = new ExtendedReflectiveColumnPropertyAccessor<>(propertyNames);
    BodyLayerStack<PersonWithAddress> bodyLayerStack = new BodyLayerStack<>(PersonService.getPersonsWithAddress(10000), columnPropertyAccessor);
    // build the column header layer
    IDataProvider columnHeaderDataProvider = new DefaultColumnHeaderDataProvider(propertyNames, propertyToLabelMap);
    DataLayer columnHeaderDataLayer = new DefaultColumnHeaderDataLayer(columnHeaderDataProvider);
    ILayer columnHeaderLayer = new ColumnHeaderLayer(columnHeaderDataLayer, bodyLayerStack, (SelectionLayer) null);
    CompositeLayer composite = new CompositeLayer(1, 2);
    composite.setChildLayer(GridRegion.COLUMN_HEADER, columnHeaderLayer, 0, 0);
    composite.setChildLayer(GridRegion.BODY, bodyLayerStack, 0, 1);
    RegexMarkupValue regexMarkup = new RegexMarkupValue("", "<span style=\"background-color:rgb(255, 255, 0)\">", "</span>");
    NatTable natTable = new NatTable(panel, composite, false);
    natTable.addConfiguration(new DefaultNatTableStyleConfiguration() {

        {
            this.cellPainter = new BackgroundPainter(new PaddingDecorator(new RichTextCellPainter(), 2));
        }

        @Override
        public void configureRegistry(IConfigRegistry configRegistry) {
            super.configureRegistry(configRegistry);
            // markup for highlighting
            MarkupDisplayConverter markupConverter = new MarkupDisplayConverter();
            markupConverter.registerMarkup("highlight", regexMarkup);
            // register markup display converter for normal displaymode in
            // the body
            configRegistry.registerConfigAttribute(CellConfigAttributes.DISPLAY_CONVERTER, markupConverter, DisplayMode.NORMAL, GridRegion.BODY);
        }
    });
    natTable.configure();
    natTable.addOverlayPainter(new NatTableBorderOverlayPainter());
    GridDataFactory.fillDefaults().grab(true, true).applyTo(natTable);
    // define a TextMatcherEditor and set it to the FilterList
    TextMatcherEditor<PersonWithAddress> matcherEditor = new TextMatcherEditor<>(new TextFilterator<PersonWithAddress>() {

        @Override
        public void getFilterStrings(List<String> baseList, PersonWithAddress element) {
            // add all values that should be included in filtering
            // Note:
            // if special converters are involved in rendering,
            // consider using them for adding the String values
            baseList.add(element.getFirstName());
            baseList.add(element.getLastName());
            baseList.add("" + element.getGender());
            baseList.add("" + element.isMarried());
            baseList.add("" + element.getBirthday());
            baseList.add(element.getAddress().getStreet());
            baseList.add("" + element.getAddress().getHousenumber());
            baseList.add("" + element.getAddress().getPostalCode());
            baseList.add(element.getAddress().getCity());
        }
    });
    matcherEditor.setMode(TextMatcherEditor.CONTAINS);
    bodyLayerStack.getFilterList().setMatcherEditor(matcherEditor);
    // connect the input field with the matcher
    input.addKeyListener(new KeyAdapter() {

        @Override
        public void keyPressed(KeyEvent e) {
            if (e.keyCode == SWT.CR || e.keyCode == SWT.KEYPAD_CR) {
                String text = input.getText();
                matcherEditor.setFilterText(new String[] { text });
                regexMarkup.setRegexValue(text.isEmpty() ? "" : "(" + text + ")");
                natTable.refresh(false);
            }
        }
    });
    return natTable;
}
Also used : HashMap(java.util.HashMap) ColumnHeaderLayer(org.eclipse.nebula.widgets.nattable.grid.layer.ColumnHeaderLayer) KeyAdapter(org.eclipse.swt.events.KeyAdapter) IDataProvider(org.eclipse.nebula.widgets.nattable.data.IDataProvider) TextMatcherEditor(ca.odell.glazedlists.matchers.TextMatcherEditor) KeyEvent(org.eclipse.swt.events.KeyEvent) GridLayout(org.eclipse.swt.layout.GridLayout) DataLayer(org.eclipse.nebula.widgets.nattable.layer.DataLayer) DefaultColumnHeaderDataLayer(org.eclipse.nebula.widgets.nattable.grid.layer.DefaultColumnHeaderDataLayer) ExtendedReflectiveColumnPropertyAccessor(org.eclipse.nebula.widgets.nattable.data.ExtendedReflectiveColumnPropertyAccessor) DefaultNatTableStyleConfiguration(org.eclipse.nebula.widgets.nattable.config.DefaultNatTableStyleConfiguration) NatTable(org.eclipse.nebula.widgets.nattable.NatTable) NatTableBorderOverlayPainter(org.eclipse.nebula.widgets.nattable.painter.NatTableBorderOverlayPainter) PersonWithAddress(org.eclipse.nebula.widgets.nattable.dataset.person.PersonWithAddress) BackgroundPainter(org.eclipse.nebula.widgets.nattable.painter.cell.BackgroundPainter) Composite(org.eclipse.swt.widgets.Composite) ILayer(org.eclipse.nebula.widgets.nattable.layer.ILayer) PaddingDecorator(org.eclipse.nebula.widgets.nattable.painter.cell.decorator.PaddingDecorator) MarkupDisplayConverter(org.eclipse.nebula.widgets.nattable.extension.nebula.richtext.MarkupDisplayConverter) Text(org.eclipse.swt.widgets.Text) DefaultColumnHeaderDataLayer(org.eclipse.nebula.widgets.nattable.grid.layer.DefaultColumnHeaderDataLayer) RichTextCellPainter(org.eclipse.nebula.widgets.nattable.extension.nebula.richtext.RichTextCellPainter) CompositeLayer(org.eclipse.nebula.widgets.nattable.layer.CompositeLayer) RegexMarkupValue(org.eclipse.nebula.widgets.nattable.extension.nebula.richtext.RegexMarkupValue) IConfigRegistry(org.eclipse.nebula.widgets.nattable.config.IConfigRegistry) DefaultColumnHeaderDataProvider(org.eclipse.nebula.widgets.nattable.grid.data.DefaultColumnHeaderDataProvider)

Example 2 with RichTextCellPainter

use of org.eclipse.nebula.widgets.nattable.extension.nebula.richtext.RichTextCellPainter in project nebula.widgets.nattable by eclipse.

the class _424_NebulaRichTextIntegrationExample method createExampleControl.

@Override
public Control createExampleControl(Composite parent) {
    // set the directory to which the richtext resources should be unpacked
    System.setProperty(RichTextEditor.JAR_UNPACK_LOCATION_PROPERTY, System.getProperty("user.dir") + File.separator + RichTextEditor.class.getPackage().getName());
    String[] propertyNames = new String[] { "firstName", "lastName", "gender", "married", "description" };
    Map<String, String> propertyToLabelMap = new HashMap<>();
    propertyToLabelMap.put("firstName", "Firstname");
    propertyToLabelMap.put("lastName", "Lastname");
    propertyToLabelMap.put("gender", "Gender");
    propertyToLabelMap.put("married", "Married");
    propertyToLabelMap.put("description", "Description");
    IColumnAccessor<Person> columnAccessor = new ReflectiveColumnPropertyAccessor<>(propertyNames);
    List<Person> persons = PersonService.getPersons(10);
    IDataProvider bodyDataProvider = new ListDataProvider<>(persons, columnAccessor);
    DefaultColumnHeaderDataProvider columnHeaderDataProvider = new DefaultColumnHeaderDataProvider(propertyNames, propertyToLabelMap);
    DefaultGridLayer gridLayer = new DefaultGridLayer(bodyDataProvider, columnHeaderDataProvider);
    ((AbstractLayer) gridLayer.getBodyDataLayer()).setConfigLabelAccumulator(new ColumnLabelAccumulator());
    NatTable natTable = new NatTable(parent, gridLayer, false);
    natTable.addConfiguration(new DefaultNatTableStyleConfiguration());
    // add custom painter and editor configuration
    natTable.addConfiguration(new AbstractRegistryConfiguration() {

        @Override
        public void configureRegistry(IConfigRegistry configRegistry) {
            // configure converter
            MarkupDisplayConverter markupConverter = new MarkupDisplayConverter();
            markupConverter.registerMarkup("Simpson", "<em>", "</em>");
            markupConverter.registerMarkup("Smithers", "<span style=\"background-color:rgb(255, 0, 0)\"><strong><s><u>", "</u></s></strong></span>");
            // register markup display converter for normal displaymode
            configRegistry.registerConfigAttribute(CellConfigAttributes.DISPLAY_CONVERTER, markupConverter, DisplayMode.NORMAL, ColumnLabelAccumulator.COLUMN_LABEL_PREFIX + 1);
            // register default display converter for editing, so there is
            // no markup in the editor
            configRegistry.registerConfigAttribute(CellConfigAttributes.DISPLAY_CONVERTER, new DefaultDisplayConverter(), DisplayMode.EDIT, ColumnLabelAccumulator.COLUMN_LABEL_PREFIX + 1);
            configRegistry.registerConfigAttribute(CellConfigAttributes.DISPLAY_CONVERTER, new DefaultBooleanDisplayConverter(), DisplayMode.NORMAL, ColumnLabelAccumulator.COLUMN_LABEL_PREFIX + 3);
            // configure cell painter
            configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_PAINTER, new BackgroundPainter(new PaddingDecorator(new RichTextCellPainter(), 2, 5, 2, 5)), DisplayMode.NORMAL, ColumnLabelAccumulator.COLUMN_LABEL_PREFIX + 1);
            configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_PAINTER, new CheckBoxPainter(), DisplayMode.NORMAL, ColumnLabelAccumulator.COLUMN_LABEL_PREFIX + 3);
            configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_PAINTER, new BackgroundPainter(new PaddingDecorator(new RichTextCellPainter(), 2, 5, 2, 5)), DisplayMode.NORMAL, ColumnLabelAccumulator.COLUMN_LABEL_PREFIX + 4);
            // configure editing
            configRegistry.registerConfigAttribute(EditConfigAttributes.CELL_EDITABLE_RULE, EditableRule.ALWAYS_EDITABLE);
            configRegistry.registerConfigAttribute(EditConfigAttributes.CELL_EDITOR, new CheckBoxCellEditor(), DisplayMode.NORMAL, ColumnLabelAccumulator.COLUMN_LABEL_PREFIX + 3);
            configRegistry.registerConfigAttribute(EditConfigAttributes.CELL_EDITOR, new RichTextCellEditor(), DisplayMode.NORMAL, ColumnLabelAccumulator.COLUMN_LABEL_PREFIX + 4);
        }
    });
    natTable.configure();
    natTable.setTheme(new ModernNatTableThemeConfiguration());
    return natTable;
}
Also used : ListDataProvider(org.eclipse.nebula.widgets.nattable.data.ListDataProvider) RichTextCellEditor(org.eclipse.nebula.widgets.nattable.extension.nebula.richtext.RichTextCellEditor) AbstractLayer(org.eclipse.nebula.widgets.nattable.layer.AbstractLayer) HashMap(java.util.HashMap) AbstractRegistryConfiguration(org.eclipse.nebula.widgets.nattable.config.AbstractRegistryConfiguration) CheckBoxCellEditor(org.eclipse.nebula.widgets.nattable.edit.editor.CheckBoxCellEditor) IDataProvider(org.eclipse.nebula.widgets.nattable.data.IDataProvider) DefaultDisplayConverter(org.eclipse.nebula.widgets.nattable.data.convert.DefaultDisplayConverter) DefaultNatTableStyleConfiguration(org.eclipse.nebula.widgets.nattable.config.DefaultNatTableStyleConfiguration) CheckBoxPainter(org.eclipse.nebula.widgets.nattable.painter.cell.CheckBoxPainter) NatTable(org.eclipse.nebula.widgets.nattable.NatTable) RichTextEditor(org.eclipse.nebula.widgets.richtext.RichTextEditor) ModernNatTableThemeConfiguration(org.eclipse.nebula.widgets.nattable.style.theme.ModernNatTableThemeConfiguration) BackgroundPainter(org.eclipse.nebula.widgets.nattable.painter.cell.BackgroundPainter) DefaultBooleanDisplayConverter(org.eclipse.nebula.widgets.nattable.data.convert.DefaultBooleanDisplayConverter) MarkupDisplayConverter(org.eclipse.nebula.widgets.nattable.extension.nebula.richtext.MarkupDisplayConverter) PaddingDecorator(org.eclipse.nebula.widgets.nattable.painter.cell.decorator.PaddingDecorator) RichTextCellPainter(org.eclipse.nebula.widgets.nattable.extension.nebula.richtext.RichTextCellPainter) ReflectiveColumnPropertyAccessor(org.eclipse.nebula.widgets.nattable.data.ReflectiveColumnPropertyAccessor) IConfigRegistry(org.eclipse.nebula.widgets.nattable.config.IConfigRegistry) DefaultColumnHeaderDataProvider(org.eclipse.nebula.widgets.nattable.grid.data.DefaultColumnHeaderDataProvider) ColumnLabelAccumulator(org.eclipse.nebula.widgets.nattable.layer.cell.ColumnLabelAccumulator) Person(org.eclipse.nebula.widgets.nattable.dataset.person.Person) DefaultGridLayer(org.eclipse.nebula.widgets.nattable.grid.layer.DefaultGridLayer)

Aggregations

HashMap (java.util.HashMap)2 NatTable (org.eclipse.nebula.widgets.nattable.NatTable)2 DefaultNatTableStyleConfiguration (org.eclipse.nebula.widgets.nattable.config.DefaultNatTableStyleConfiguration)2 IConfigRegistry (org.eclipse.nebula.widgets.nattable.config.IConfigRegistry)2 IDataProvider (org.eclipse.nebula.widgets.nattable.data.IDataProvider)2 MarkupDisplayConverter (org.eclipse.nebula.widgets.nattable.extension.nebula.richtext.MarkupDisplayConverter)2 RichTextCellPainter (org.eclipse.nebula.widgets.nattable.extension.nebula.richtext.RichTextCellPainter)2 DefaultColumnHeaderDataProvider (org.eclipse.nebula.widgets.nattable.grid.data.DefaultColumnHeaderDataProvider)2 BackgroundPainter (org.eclipse.nebula.widgets.nattable.painter.cell.BackgroundPainter)2 PaddingDecorator (org.eclipse.nebula.widgets.nattable.painter.cell.decorator.PaddingDecorator)2 TextMatcherEditor (ca.odell.glazedlists.matchers.TextMatcherEditor)1 AbstractRegistryConfiguration (org.eclipse.nebula.widgets.nattable.config.AbstractRegistryConfiguration)1 ExtendedReflectiveColumnPropertyAccessor (org.eclipse.nebula.widgets.nattable.data.ExtendedReflectiveColumnPropertyAccessor)1 ListDataProvider (org.eclipse.nebula.widgets.nattable.data.ListDataProvider)1 ReflectiveColumnPropertyAccessor (org.eclipse.nebula.widgets.nattable.data.ReflectiveColumnPropertyAccessor)1 DefaultBooleanDisplayConverter (org.eclipse.nebula.widgets.nattable.data.convert.DefaultBooleanDisplayConverter)1 DefaultDisplayConverter (org.eclipse.nebula.widgets.nattable.data.convert.DefaultDisplayConverter)1 Person (org.eclipse.nebula.widgets.nattable.dataset.person.Person)1 PersonWithAddress (org.eclipse.nebula.widgets.nattable.dataset.person.PersonWithAddress)1 CheckBoxCellEditor (org.eclipse.nebula.widgets.nattable.edit.editor.CheckBoxCellEditor)1