最小要件は、2つのクラスを実装することです。1つはサービス宣言用で、もう1つは実装自体用です。
サンプルは、単に「Hello world」メッセージを表示するデータセットユーザーサービスであり、データセットのアクションメニューから起動できます。
サービス実装クラスは、インターフェイス UserService<DatasetEntitySelection>
を実装する必要があります。
/** * This service displays hello world! */ public class HelloWordService implements UserService<DatasetEntitySelection> { public HelloWordService() { } @Override public void setupDisplay( UserServiceSetupDisplayContext<DatasetEntitySelection> aContext, UserServiceDisplayConfigurator aConfigurator) { // Set bottom bar UIButtonSpecNavigation closeButton = aConfigurator.newCloseButton(); closeButton.setDefaultButton(true); aConfigurator.setLeftButtons(closeButton); // Set content callback aConfigurator.setContent(this::writeHelloWorld); } private void writeHelloWorld( UserServicePaneContext aPaneContext, UserServicePaneWriter aWriter) { // Display Hello World! aWriter.add("<div "); aWriter.addSafeAttribute("class", UICSSClasses.CONTAINER_WITH_TEXT_PADDING); aWriter.add(">"); aWriter.add("Hello World!"); aWriter.add("</div>"); } @Override public void setupObjectContext( UserServiceSetupObjectContext<DatasetEntitySelection> aContext, UserServiceObjectContextBuilder aBuilder) { // No context yet. } @Override public void validate(UserServiceValidateContext<DatasetEntitySelection> aContext) { // No custom validation is necessary. } @Override public UserServiceEventOutcome processEventOutcome( UserServiceProcessEventOutcomeContext<DatasetEntitySelection> aContext, UserServiceEventOutcome anEventOutcome) { // By default do not modify the outcome. return anEventOutcome; } }
宣言クラスは、インターフェイス UserServiceDeclaration.OnDataset
を実装する必要があります。
/** * Declaration for service hello world! */ public class HelloWorldServiceDeclaration implements UserServiceDeclaration.OnDataset { // The service key identifies the user service. private static final ServiceKey serviceKey = ServiceKey.forName("HelloWorld"); public HelloWorldServiceDeclaration() { } @Override public ServiceKey getServiceKey() { return serviceKey; } @Override public UserService<DatasetEntitySelection> createUserService() { // Creates an instance of the user service. return new HelloWordService(); } @Override public void defineActivation(ActivationContextOnDataset aContext) { // The service is activated for all datasets instanciated with // the associated data model (see next example). } @Override public void defineProperties(UserServicePropertiesDefinitionContext aContext) { // This label is displayed in menus that can execute the user service. aContext.setLabel("Hello World Service"); } @Override public void declareWebComponent(WebComponentDeclarationContext aContext) { } }
このサンプルでは,データモデルによってユーザサービスが登録されています.データモデルでは,以下のコードを実装したスキーマ拡張を定義する必要があります。
public class CustomSchemaExtensions implements SchemaExtensions { @Override public void defineExtensions(SchemaExtensionsContext aContext) { // Register the service. aContext.registerUserService(new HelloWorldServiceDeclaration()); } }
スキーマ拡張宣言の詳細については、SchemaExtensions
を参照してください。