Search in sources :

Example 1 with ConfigDescriptionParameterGroup

use of org.eclipse.smarthome.config.core.ConfigDescriptionParameterGroup in project smarthome by eclipse.

the class ConfigDescriptionConverter method unmarshal.

@Override
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
    ConfigDescription configDescription = null;
    // read attributes
    Map<String, String> attributes = this.attributeMapValidator.readValidatedAttributes(reader);
    String uriText = attributes.get("uri");
    if (uriText == null) {
        // the URI could be overridden by a context field if it could be
        // automatically extracted
        uriText = (String) context.get("config-description.uri");
    }
    URI uri = null;
    try {
        uri = new URI(uriText);
    } catch (NullPointerException | URISyntaxException ex) {
        throw new ConversionException("The URI '" + uriText + "' in node '" + reader.getNodeName() + "' is invalid!", ex);
    }
    // create the lists to hold parameters and groups
    List<ConfigDescriptionParameter> configDescriptionParams = new ArrayList<ConfigDescriptionParameter>();
    List<ConfigDescriptionParameterGroup> configDescriptionGroups = new ArrayList<ConfigDescriptionParameterGroup>();
    // read values
    List<?> nodes = (List<?>) context.convertAnother(context, List.class);
    NodeIterator nodeIterator = new NodeIterator(nodes);
    // respective arrays
    while (nodeIterator.hasNext() == true) {
        Object node = nodeIterator.next();
        if (node instanceof ConfigDescriptionParameter) {
            configDescriptionParams.add((ConfigDescriptionParameter) node);
        }
        if (node instanceof ConfigDescriptionParameterGroup) {
            configDescriptionGroups.add((ConfigDescriptionParameterGroup) node);
        }
    }
    ConverterAssertion.assertEndOfType(reader);
    // create object
    configDescription = new ConfigDescription(uri, configDescriptionParams, configDescriptionGroups);
    return configDescription;
}
Also used : ConversionException(com.thoughtworks.xstream.converters.ConversionException) NodeIterator(org.eclipse.smarthome.config.xml.util.NodeIterator) ArrayList(java.util.ArrayList) ConfigDescription(org.eclipse.smarthome.config.core.ConfigDescription) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) ConfigDescriptionParameterGroup(org.eclipse.smarthome.config.core.ConfigDescriptionParameterGroup) ArrayList(java.util.ArrayList) List(java.util.List) ConfigDescriptionParameter(org.eclipse.smarthome.config.core.ConfigDescriptionParameter)

Example 2 with ConfigDescriptionParameterGroup

use of org.eclipse.smarthome.config.core.ConfigDescriptionParameterGroup in project smarthome by eclipse.

the class ConfigDescriptionsTest method ConfigDescriptionsShouldLoadProperly.

@Test
public void ConfigDescriptionsShouldLoadProperly() throws Exception {
    int initialNumberOfConfigDescriptions = configDescriptionRegistry.getConfigDescriptions().size();
    // install test bundle
    Bundle bundle = SyntheticBundleInstaller.install(bundleContext, TEST_BUNDLE_NAME);
    assertThat(bundle, is(notNullValue()));
    Collection<ConfigDescription> configDescriptions = configDescriptionRegistry.getConfigDescriptions();
    assertThat(configDescriptions.size(), is(initialNumberOfConfigDescriptions + 3));
    URI bridgeURI = new URI("thing-type:hue:bridge");
    ConfigDescription bridgeConfigDescription = configDescriptions.stream().filter(it -> it.getUID().equals(bridgeURI)).findFirst().get();
    assertThat(bridgeConfigDescription, is(notNullValue()));
    Collection<ConfigDescriptionParameter> parameters = bridgeConfigDescription.getParameters();
    assertThat(parameters.size(), is(2));
    ConfigDescriptionParameter ipParameter = parameters.stream().filter(it -> it.getName().equals("ip")).findFirst().get();
    assertThat(ipParameter, is(notNullValue()));
    assertThat(ipParameter.getType(), is(Type.TEXT));
    assertThat(ipParameter.getContext(), is("network-address"));
    assertThat(ipParameter.getLabel(), is("Network Address"));
    assertThat(ipParameter.getDescription(), is("Network address of the hue bridge."));
    assertThat(ipParameter.isRequired(), is(true));
    ConfigDescriptionParameter userNameParameter = parameters.stream().filter(it -> it.getName().equals("username")).findFirst().get();
    assertThat(userNameParameter, is(notNullValue()));
    assertThat(userNameParameter.isAdvanced(), is(true));
    assertThat(userNameParameter.getType(), is(Type.TEXT));
    assertThat(userNameParameter.getContext(), is("password"));
    assertThat(userNameParameter.getLabel(), is("Username"));
    assertThat(userNameParameter.getDescription(), is("Name of a registered hue bridge user, that allows to access the API."));
    URI colorURI = new URI("channel-type:hue:color");
    ConfigDescription colorConfigDescription = configDescriptions.stream().filter(it -> it.getUID().equals(colorURI)).findFirst().get();
    assertThat(colorConfigDescription, is(notNullValue()));
    parameters = colorConfigDescription.getParameters();
    assertThat(parameters.size(), is(1));
    ConfigDescriptionParameter lastDimValueParameter = parameters.stream().filter(it -> it.getName().equals("lastDimValue")).findFirst().get();
    assertThat(lastDimValueParameter, is(notNullValue()));
    assertThat(lastDimValueParameter.getType(), is(Type.BOOLEAN));
    Collection<ConfigDescriptionParameterGroup> groups = bridgeConfigDescription.getParameterGroups();
    assertThat(groups.size(), is(2));
    ConfigDescriptionParameterGroup group1 = groups.stream().filter(it -> it.getName().equals("group1")).findFirst().get();
    assertThat(group1, is(notNullValue()));
    assertThat(group1.getContext(), is("Group1-context"));
    assertThat(group1.getLabel(), is("Group Label 1"));
    assertThat(group1.getDescription(), is("Group description 1"));
    // uninstall test bundle
    bundle.uninstall();
    assertThat(bundle.getState(), is(Bundle.UNINSTALLED));
}
Also used : ConfigDescriptionParameterGroup(org.eclipse.smarthome.config.core.ConfigDescriptionParameterGroup) Bundle(org.osgi.framework.Bundle) ConfigDescription(org.eclipse.smarthome.config.core.ConfigDescription) ConfigDescriptionParameter(org.eclipse.smarthome.config.core.ConfigDescriptionParameter) URI(java.net.URI) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 3 with ConfigDescriptionParameterGroup

use of org.eclipse.smarthome.config.core.ConfigDescriptionParameterGroup in project smarthome by eclipse.

the class ConfigI18nLocalizationService method getLocalizedConfigDescription.

/**
 * Localize a config description.
 *
 * @param bundle the bundle the i18n resources are located
 * @param configDescription the config description that should be localized
 * @param locale the locale it should be localized to
 * @return a localized configuration description on success, a non-localized one on error (e.g. no translation is
 *         found).
 */
public ConfigDescription getLocalizedConfigDescription(final Bundle bundle, final ConfigDescription configDescription, @Nullable final Locale locale) {
    final List<ConfigDescriptionParameter> localizedConfigDescriptionParameters = new ArrayList<>(configDescription.getParameters().size());
    // Loop through all the configuration parameters and localize them
    for (final ConfigDescriptionParameter configDescriptionParameter : configDescription.getParameters()) {
        final ConfigDescriptionParameter localizedConfigDescriptionParameter = getLocalizedConfigDescriptionParameter(bundle, configDescription, configDescriptionParameter, locale);
        localizedConfigDescriptionParameters.add(localizedConfigDescriptionParameter);
    }
    final List<ConfigDescriptionParameterGroup> localizedConfigDescriptionGroups = new ArrayList<>(configDescription.getParameterGroups().size());
    // Loop through all the configuration groups and localize them
    for (final ConfigDescriptionParameterGroup configDescriptionParameterGroup : configDescription.getParameterGroups()) {
        final ConfigDescriptionParameterGroup localizedConfigDescriptionGroup = getLocalizedConfigDescriptionGroup(bundle, configDescription, configDescriptionParameterGroup, locale);
        localizedConfigDescriptionGroups.add(localizedConfigDescriptionGroup);
    }
    return new ConfigDescription(configDescription.getUID(), localizedConfigDescriptionParameters, localizedConfigDescriptionGroups);
}
Also used : ConfigDescriptionParameterGroup(org.eclipse.smarthome.config.core.ConfigDescriptionParameterGroup) ArrayList(java.util.ArrayList) ConfigDescription(org.eclipse.smarthome.config.core.ConfigDescription) ConfigDescriptionParameter(org.eclipse.smarthome.config.core.ConfigDescriptionParameter)

Example 4 with ConfigDescriptionParameterGroup

use of org.eclipse.smarthome.config.core.ConfigDescriptionParameterGroup in project smarthome by eclipse.

the class ConfigI18nLocalizationService method getLocalizedConfigDescriptionGroup.

/**
 * Localize a config description parameter group.
 *
 * @param bundle the bundle the i18n resources are located
 * @param configDescription the config description the parameter group is part of
 * @param group the parameter group that should be localized
 * @param locale the locale it should be localized to
 * @return a localized parameter group on success, a non-localized one on error (e.g. no translation is found).
 */
public ConfigDescriptionParameterGroup getLocalizedConfigDescriptionGroup(final Bundle bundle, final ConfigDescription configDescription, final ConfigDescriptionParameterGroup group, @Nullable final Locale locale) {
    final URI configDescriptionURI = configDescription.getUID();
    final String name = group.getName();
    final String label = this.configDescriptionGroupI18nUtil.getGroupLabel(bundle, configDescriptionURI, name, group.getLabel(), locale);
    final String description = this.configDescriptionGroupI18nUtil.getGroupDescription(bundle, configDescriptionURI, name, group.getDescription(), locale);
    final ConfigDescriptionParameterGroup localizedGroup = new ConfigDescriptionParameterGroup(name, group.getContext(), group.isAdvanced(), label, description);
    return localizedGroup;
}
Also used : ConfigDescriptionParameterGroup(org.eclipse.smarthome.config.core.ConfigDescriptionParameterGroup) URI(java.net.URI)

Example 5 with ConfigDescriptionParameterGroup

use of org.eclipse.smarthome.config.core.ConfigDescriptionParameterGroup in project smarthome by eclipse.

the class ConfigDescriptionParameterGroupConverter method unmarshal.

@Override
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext marshallingContext) {
    String name = reader.getAttribute("name");
    // Read values
    ConverterValueMap valueMap = new ConverterValueMap(reader, marshallingContext);
    String context = valueMap.getString("context");
    String description = valueMap.getString("description");
    String label = valueMap.getString("label");
    Boolean advanced = valueMap.getBoolean("advanced", false);
    return new ConfigDescriptionParameterGroup(name, context, advanced, label, description);
}
Also used : ConfigDescriptionParameterGroup(org.eclipse.smarthome.config.core.ConfigDescriptionParameterGroup) ConverterValueMap(org.eclipse.smarthome.config.xml.util.ConverterValueMap)

Aggregations

ConfigDescriptionParameterGroup (org.eclipse.smarthome.config.core.ConfigDescriptionParameterGroup)5 URI (java.net.URI)3 ConfigDescription (org.eclipse.smarthome.config.core.ConfigDescription)3 ConfigDescriptionParameter (org.eclipse.smarthome.config.core.ConfigDescriptionParameter)3 ArrayList (java.util.ArrayList)2 ConversionException (com.thoughtworks.xstream.converters.ConversionException)1 URISyntaxException (java.net.URISyntaxException)1 List (java.util.List)1 ConverterValueMap (org.eclipse.smarthome.config.xml.util.ConverterValueMap)1 NodeIterator (org.eclipse.smarthome.config.xml.util.NodeIterator)1 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)1 Test (org.junit.Test)1 Bundle (org.osgi.framework.Bundle)1