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

HybridManager: add context element converters for table rows and columns #1356

Merged
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
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2024 BSI Business Systems Integration AG
* Copyright (c) 2010, 2025 BSI Business Systems Integration AG
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
Expand All @@ -10,7 +10,7 @@
import {HybridActionContextElementConverters, ModelAdapter} from '../../../index';

/**
* Instances of this class are used to convert a model element to a JSON representation and vice-versa. Each
* Instances of this class are used to convert a model element to a JSON representation and vice versa. Each
* instance typically only handles a specific type of element. Because elements have no common structure, they are
* always accompanied by the widget that owns them (here represented by the corresponding {@link ModelAdapter}).
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (c) 2010, 2025 BSI Business Systems Integration AG
*
* 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
*/
import {Column, HybridActionContextElementConverter, HybridActionContextElementConverters, ModelAdapter, objects, scout, strings, TableAdapter} from '../../../index';

export class TableColumnContextElementConverter extends HybridActionContextElementConverter<TableAdapter, string, Column> {

static JSON_ELEMENT_PREFIX = 'col:';

override _acceptAdapter(adapter: ModelAdapter): adapter is TableAdapter {
return adapter instanceof TableAdapter;
}

override _acceptJsonElement(jsonElement: any): jsonElement is string {
return objects.isString(jsonElement) && strings.startsWith(jsonElement, TableColumnContextElementConverter.JSON_ELEMENT_PREFIX);
}

override _acceptModelElement(element: any): element is Column {
return element instanceof Column;
}

override _jsonToElement(adapter: TableAdapter, jsonElement: string): Column {
let table = adapter.widget;
let columnId = strings.removePrefix(jsonElement, TableColumnContextElementConverter.JSON_ELEMENT_PREFIX);
return scout.assertValue(table.columnById(columnId), `Unknown column with id "${columnId}" in table ${adapter.id}`);
}

override _elementToJson(adapter: TableAdapter, element: Column): string {
let columnId = element.id;
return strings.addPrefix(columnId, TableColumnContextElementConverter.JSON_ELEMENT_PREFIX);
}
}

HybridActionContextElementConverters.get().register(TableColumnContextElementConverter);
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (c) 2010, 2025 BSI Business Systems Integration AG
*
* 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
*/
import {HybridActionContextElementConverter, HybridActionContextElementConverters, ModelAdapter, objects, scout, strings, TableAdapter, TableRow} from '../../../index';

export class TableRowContextElementConverter extends HybridActionContextElementConverter<TableAdapter, string, TableRow> {

static JSON_ELEMENT_PREFIX = 'row:';

override _acceptAdapter(adapter: ModelAdapter): adapter is TableAdapter {
return adapter instanceof TableAdapter;
}

override _acceptJsonElement(jsonElement: any): jsonElement is string {
return objects.isString(jsonElement) && strings.startsWith(jsonElement, TableRowContextElementConverter.JSON_ELEMENT_PREFIX);
}

override _acceptModelElement(element: any): element is TableRow {
return element instanceof TableRow;
}

override _jsonToElement(adapter: TableAdapter, jsonElement: string): TableRow {
let table = adapter.widget;
let rowId = strings.removePrefix(jsonElement, TableRowContextElementConverter.JSON_ELEMENT_PREFIX);
return scout.assertValue(table.rowById(rowId), `Unknown row with id "${rowId}" in table ${adapter.id}`);
}

override _elementToJson(adapter: TableAdapter, element: TableRow): string {
let rowId = element.id;
return strings.addPrefix(rowId, TableRowContextElementConverter.JSON_ELEMENT_PREFIX);
}
}

HybridActionContextElementConverters.get().register(TableRowContextElementConverter);
4 changes: 3 additions & 1 deletion eclipse-scout-core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2024 BSI Business Systems Integration AG
* Copyright (c) 2010, 2025 BSI Business Systems Integration AG
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -1028,6 +1028,8 @@ export * from './desktop/hybrid/HybridActionContextElement';
export * from './desktop/hybrid/HybridActionContextElements';
export * from './desktop/hybrid/converter/HybridActionContextElementConverters';
export * from './desktop/hybrid/converter/HybridActionContextElementConverter';
export * from './desktop/hybrid/converter/TableColumnContextElementConverter';
export * from './desktop/hybrid/converter/TableRowContextElementConverter';
export * from './desktop/hybrid/converter/TreeNodeContextElementConverter';
export * from './desktop/navigation/DesktopNavigation';
export * from './desktop/navigation/DesktopNavigationModel';
Expand Down
38 changes: 27 additions & 11 deletions eclipse-scout-core/src/util/strings.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2024 BSI Business Systems Integration AG
* Copyright (c) 2010, 2025 BSI Business Systems Integration AG
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -379,20 +379,36 @@ export const strings = {
return strings.equals(a, b, true);
},

/**
* Adds the given prefix to the start of the given string and returns the result.
* If either of the given arguments is null or empty, the original string is returned unchanged, i.e. without prefix.
*/
addPrefix(string: string, prefix: string): string {
return string && prefix ? prefix + string : string;
bschwarzent marked this conversation as resolved.
Show resolved Hide resolved
},

/**
* Adds the given suffix to the end of the given string and returns the result.
* If either of the given arguments is null or empty, the original string is returned unchanged, i.e. without suffix.
*/
addSuffix(string: string, suffix: string): string {
return string && suffix ? string + suffix : string;
bschwarzent marked this conversation as resolved.
Show resolved Hide resolved
},

/**
* If the given string starts with the given prefix, the prefix is removed and the remaining string is returned.
* Otherwise, the string is returned unchanged. This method is case-sensitive and null-safe.
*/
removePrefix(string: string, prefix: string): string {
let s = string;
if (strings.startsWith(string, prefix)) {
s = string.substring(prefix.length);
}
return s;
return strings.startsWith(string, prefix) ? string.substring(prefix.length) : string;
},

/**
* If the given string ends with the given suffix, the suffix is removed and the remaining string is returned.
* Otherwise, the string is returned unchanged. This method is case-sensitive and null-safe.
*/
removeSuffix(string: string, suffix: string): string {
let s = string;
if (strings.endsWith(string, suffix)) {
s = string.substring(0, string.length - suffix.length);
}
return s;
return strings.endsWith(string, suffix) ? string.substring(0, string.length - suffix.length) : string;
},

/**
Expand Down
80 changes: 72 additions & 8 deletions eclipse-scout-core/test/util/stringsSpec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2024 BSI Business Systems Integration AG
* Copyright (c) 2010, 2025 BSI Business Systems Integration AG
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -447,18 +447,82 @@ describe('strings', () => {

});

describe('removePrefix and removeSuffix', () => {
describe('prefixes and suffixes', () => {
it('addPrefix adds a prefix', () => {
expect(strings.addPrefix(null, 'prefix')).toBe(null);
expect(strings.addPrefix(undefined, 'prefix')).toBe(undefined);
expect(strings.addPrefix(null, null)).toBe(null);
expect(strings.addPrefix(undefined, null)).toBe(undefined);
expect(strings.addPrefix('', null)).toBe('');
expect(strings.addPrefix('', undefined)).toBe('');
expect(strings.addPrefix('', 'prefix')).toBe('');
expect(strings.addPrefix(' ', ' ')).toBe(' ');
expect(strings.addPrefix(' ', 'prefix')).toBe('prefix ');
expect(strings.addPrefix('test', null)).toBe('test');
expect(strings.addPrefix('test', undefined)).toBe('test');
expect(strings.addPrefix('test', '')).toBe('test');
expect(strings.addPrefix('test', 'prefix-')).toBe('prefix-test');
expect(strings.addPrefix('test', 'test')).toBe('testtest');
expect(strings.addPrefix('CodeType', 'foo.')).toBe('foo.CodeType');
});

it('removePrefix', () => {
expect(strings.removePrefix('crm.CodeType', 'crm.')).toBe('CodeType');
expect(strings.removePrefix('crm.CodeType', 'foo.')).toBe('crm.CodeType');
it('addSuffix adds a suffix', () => {
expect(strings.addSuffix(null, 'suffix')).toBe(null);
expect(strings.addSuffix(undefined, 'suffix')).toBe(undefined);
expect(strings.addSuffix(null, null)).toBe(null);
expect(strings.addSuffix(undefined, null)).toBe(undefined);
expect(strings.addSuffix('', null)).toBe('');
expect(strings.addSuffix('', undefined)).toBe('');
expect(strings.addSuffix('', 'suffix')).toBe('');
expect(strings.addSuffix(' ', ' ')).toBe(' ');
expect(strings.addSuffix(' ', 'suffix')).toBe(' suffix');
expect(strings.addSuffix('test', null)).toBe('test');
expect(strings.addSuffix('test', undefined)).toBe('test');
expect(strings.addSuffix('test', '')).toBe('test');
expect(strings.addSuffix('test', '-suffix')).toBe('test-suffix');
expect(strings.addSuffix('test', 'test')).toBe('testtest');
expect(strings.addSuffix('avatar', '.jpg')).toBe('avatar.jpg');
});

it('removeSuffix', () => {
expect(strings.removeSuffix('avatar.gif', '.gif')).toBe('avatar');
expect(strings.removeSuffix('avatar.gif', '.exe')).toBe('avatar.gif');
it('removePrefix removes a prefix', () => {
expect(strings.removePrefix(null, 'prefix')).toBe(null);
expect(strings.removePrefix(undefined, 'prefix')).toBe(undefined);
expect(strings.removePrefix(null, null)).toBe(null);
expect(strings.removePrefix(undefined, null)).toBe(undefined);
expect(strings.removePrefix('', 'prefix')).toBe('');
expect(strings.removePrefix('test', null)).toBe('test');
expect(strings.removePrefix('test', undefined)).toBe('test');
expect(strings.removePrefix('test', '')).toBe('test');
expect(strings.removePrefix('test', 'abc')).toBe('test');
expect(strings.removePrefix('test', 'tester')).toBe('test');
expect(strings.removePrefix('test', 'T')).toBe('test');
expect(strings.removePrefix('test', 't')).toBe('est');
expect(strings.removePrefix('test', 'te')).toBe('st');
expect(strings.removePrefix('test', 'test')).toBe('');
expect(strings.removePrefix('foo.CodeType', 'foo.')).toBe('CodeType');
expect(strings.removePrefix('foo.CodeType', 'bar.')).toBe('foo.CodeType');
expect(strings.removePrefix('CodeType', 'codeType')).toBe('CodeType');
});

it('removeSuffix removes a suffix', () => {
expect(strings.removeSuffix(null, 'suffix')).toBe(null);
expect(strings.removeSuffix(undefined, 'suffix')).toBe(undefined);
expect(strings.removeSuffix(null, null)).toBe(null);
expect(strings.removeSuffix(undefined, null)).toBe(undefined);
expect(strings.removeSuffix('', 'suffix')).toBe('');
expect(strings.removeSuffix('test', null)).toBe('test');
expect(strings.removeSuffix('test', undefined)).toBe('test');
expect(strings.removeSuffix('test', '')).toBe('test');
expect(strings.removeSuffix('test', 'abc')).toBe('test');
expect(strings.removeSuffix('test', 'tester')).toBe('test');
expect(strings.removeSuffix('test', 'T')).toBe('test');
expect(strings.removeSuffix('test', 't')).toBe('tes');
expect(strings.removeSuffix('test', 'st')).toBe('te');
expect(strings.removeSuffix('test', 'test')).toBe('');
expect(strings.removeSuffix('avatar.jpg', '.jpg')).toBe('avatar');
expect(strings.removeSuffix('avatar.jpg', '.gif')).toBe('avatar.jpg');
expect(strings.removeSuffix('avatar.jpg', '.JPG')).toBe('avatar.jpg');
});
});

describe('truncateText', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2023 BSI Business Systems Integration AG
* Copyright (c) 2010, 2025 BSI Business Systems Integration AG
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -320,7 +320,7 @@ public void testReplaceTags() {
+ "<meta name=\"date.modified\" content=\"20130314\"/>";
assertEquals("", StringUtility.replaceTags(input, "meta", "").trim());

// ingore case test
// ignore case test
assertEquals("xbybz", StringUtility.replaceTags("x<A>asdf</A>y<a>jkl</a>z", "a", true, "b"));
}

Expand All @@ -329,14 +329,16 @@ public void testReplaceTags() {
*/
@Test
public void testNewLines() {
String text = "lorem " + '\n' + "ipsum";
assertTrue(StringUtility.containsNewLines(text));
text = "lorem" + System.getProperty("line.separator") + "ipsum";
assertTrue(StringUtility.containsNewLines(text));
text = "";
assertFalse(StringUtility.containsNewLines(text));
text = null;
assertFalse(StringUtility.containsNewLines(text));
assertTrue(StringUtility.containsNewLines("lorem " + '\n' + "ipsum"));
assertTrue(StringUtility.containsNewLines("lorem" + System.lineSeparator() + "ipsum"));
assertTrue(StringUtility.containsNewLines("lorem\nipsum"));
assertTrue(StringUtility.containsNewLines("lorem\n\nipsum"));
assertTrue(StringUtility.containsNewLines("lorem\ripsum"));
assertTrue(StringUtility.containsNewLines("lorem\r\nipsum"));
assertTrue(StringUtility.containsNewLines("\n"));
assertFalse(StringUtility.containsNewLines("lorem ipsum"));
assertFalse(StringUtility.containsNewLines(""));
assertFalse(StringUtility.containsNewLines(null));
}

/**
Expand Down Expand Up @@ -794,4 +796,70 @@ public void testRemoveSuffixes() {
assertEquals("Company", StringUtility.removeSuffixes("CompanyFormData", "Form", "Data"));
assertEquals("CompanyForm", StringUtility.removeSuffixes("CompanyFormData", "Data", "Form"));
}

@Test
public void testAddPrefix() {
assertEquals(null, StringUtility.addPrefix(null, "prefix"));
assertEquals(null, StringUtility.addPrefix(null, null));
assertEquals("", StringUtility.addPrefix("", null));
assertEquals("", StringUtility.addPrefix("", "prefix"));
assertEquals(" ", StringUtility.addPrefix(" ", " "));
assertEquals("prefix ", StringUtility.addPrefix(" ", "prefix"));
assertEquals("test", StringUtility.addPrefix("test", null));
assertEquals("test", StringUtility.addPrefix("test", ""));
assertEquals("prefix-test", StringUtility.addPrefix("test", "prefix-"));
assertEquals("testtest", StringUtility.addPrefix("test", "test"));
assertEquals("foo.CodeType", StringUtility.addPrefix("CodeType", "foo."));
}

@Test
public void testAddSuffix() {
assertEquals(null, StringUtility.addSuffix(null, "suffix"));
assertEquals(null, StringUtility.addSuffix(null, null));
assertEquals("", StringUtility.addSuffix("", null));
assertEquals("", StringUtility.addSuffix("", "suffix"));
assertEquals(" ", StringUtility.addSuffix(" ", " "));
assertEquals(" suffix", StringUtility.addSuffix(" ", "suffix"));
assertEquals("test-suffix", StringUtility.addSuffix("test", "-suffix"));
assertEquals("test", StringUtility.addSuffix("test", null));
assertEquals("test", StringUtility.addSuffix("test", ""));
assertEquals("testtest", StringUtility.addSuffix("test", "test"));
assertEquals("avatar.jpg", StringUtility.addSuffix("avatar", ".jpg"));
}

@Test
public void testRemovePrefix() {
assertEquals(null, StringUtility.removePrefix(null, "prefix"));
assertEquals(null, StringUtility.removePrefix(null, null));
assertEquals("", StringUtility.removePrefix("", "prefix"));
assertEquals("test", StringUtility.removePrefix("test", null));
assertEquals("test", StringUtility.removePrefix("test", ""));
assertEquals("test", StringUtility.removePrefix("test", "abc"));
assertEquals("test", StringUtility.removePrefix("test", "tester"));
assertEquals("test", StringUtility.removePrefix("test", "T"));
assertEquals("est", StringUtility.removePrefix("test", "t"));
assertEquals("st", StringUtility.removePrefix("test", "te"));
assertEquals("", StringUtility.removePrefix("test", "test"));
assertEquals("CodeType", StringUtility.removePrefix("foo.CodeType", "foo."));
assertEquals("foo.CodeType", StringUtility.removePrefix("foo.CodeType", "bar."));
assertEquals("CodeType", StringUtility.removePrefix("CodeType", "codeType"));
}

@Test
public void testRemoveSuffix() {
assertEquals(null, StringUtility.removeSuffix(null, "suffix"));
assertEquals(null, StringUtility.removeSuffix(null, null));
assertEquals("", StringUtility.removeSuffix("", "suffix"));
assertEquals("test", StringUtility.removeSuffix("test", null));
assertEquals("test", StringUtility.removeSuffix("test", ""));
assertEquals("test", StringUtility.removeSuffix("test", "abc"));
assertEquals("test", StringUtility.removeSuffix("test", "tester"));
assertEquals("test", StringUtility.removeSuffix("test", "T"));
assertEquals("tes", StringUtility.removeSuffix("test", "t"));
assertEquals("te", StringUtility.removeSuffix("test", "st"));
assertEquals("", StringUtility.removeSuffix("test", "test"));
assertEquals("avatar", StringUtility.removeSuffix("avatar.jpg", ".jpg"));
assertEquals("avatar.jpg", StringUtility.removePrefix("avatar.jpg", ".gif"));
assertEquals("avatar.jpg", StringUtility.removePrefix("avatar.jpg", ".JPG"));
}
}
Loading