Search in sources :

Example 6 with Widget

use of org.eclipse.smarthome.model.sitemap.Widget in project smarthome by eclipse.

the class ItemUIRegistryImpl method getWidgetId.

@Override
public String getWidgetId(Widget widget) {
    Widget w2 = defaultWidgets.get(widget);
    if (w2 != null) {
        return getWidgetId(w2);
    }
    String id = "";
    Widget w = widget;
    while (w.eContainer() instanceof Widget) {
        Widget parent = (Widget) w.eContainer();
        String index = String.valueOf(((LinkableWidget) parent).getChildren().indexOf(w));
        if (index.length() == 1) {
            // make it two digits
            index = "0" + index;
        }
        id = index + id;
        w = parent;
    }
    if (w.eContainer() instanceof Sitemap) {
        Sitemap sitemap = (Sitemap) w.eContainer();
        String index = String.valueOf(sitemap.getChildren().indexOf(w));
        if (index.length() == 1) {
            // make it two digits
            index = "0" + index;
        }
        id = index + id;
    }
    // use the item name as the id
    if (w.eContainer() == null) {
        String itemName = w.getItem();
        id = itemName;
    }
    return id;
}
Also used : LinkableWidget(org.eclipse.smarthome.model.sitemap.LinkableWidget) Sitemap(org.eclipse.smarthome.model.sitemap.Sitemap) Widget(org.eclipse.smarthome.model.sitemap.Widget) LinkableWidget(org.eclipse.smarthome.model.sitemap.LinkableWidget)

Example 7 with Widget

use of org.eclipse.smarthome.model.sitemap.Widget in project smarthome by eclipse.

the class ItemUIRegistryImpl method getDefaultWidget.

@Override
public Widget getDefaultWidget(Class<? extends Item> targetItemType, String itemName) {
    for (ItemUIProvider provider : itemUIProviders) {
        Widget widget = provider.getDefaultWidget(targetItemType, itemName);
        if (widget != null) {
            return widget;
        }
    }
    // do some reasonable default, if no provider had an answer
    // if the itemType is not defined, try to get it from the item name
    Class<? extends Item> itemType = targetItemType;
    if (itemType == null) {
        itemType = getItemType(itemName);
    }
    if (itemType == null) {
        return null;
    }
    if (itemType.equals(SwitchItem.class)) {
        return SitemapFactory.eINSTANCE.createSwitch();
    }
    if (itemType.equals(GroupItem.class)) {
        return SitemapFactory.eINSTANCE.createGroup();
    }
    if (NumberItem.class.isAssignableFrom(itemType)) {
        return SitemapFactory.eINSTANCE.createText();
    }
    if (itemType.equals(ContactItem.class)) {
        return SitemapFactory.eINSTANCE.createText();
    }
    if (itemType.equals(DateTimeItem.class)) {
        return SitemapFactory.eINSTANCE.createText();
    }
    if (itemType.equals(RollershutterItem.class)) {
        return SitemapFactory.eINSTANCE.createSwitch();
    }
    if (itemType.equals(StringItem.class)) {
        return SitemapFactory.eINSTANCE.createText();
    }
    if (itemType.equals(LocationItem.class)) {
        return SitemapFactory.eINSTANCE.createText();
    }
    if (itemType.equals(CallItem.class)) {
        return SitemapFactory.eINSTANCE.createText();
    }
    if (itemType.equals(DimmerItem.class)) {
        Slider slider = SitemapFactory.eINSTANCE.createSlider();
        slider.setSwitchEnabled(true);
        return slider;
    }
    if (itemType.equals(ColorItem.class)) {
        return SitemapFactory.eINSTANCE.createColorpicker();
    }
    if (itemType.equals(PlayerItem.class)) {
        return createPlayerButtons();
    }
    if (itemType.equals(ImageItem.class)) {
        return SitemapFactory.eINSTANCE.createImage();
    }
    return null;
}
Also used : ItemUIProvider(org.eclipse.smarthome.ui.items.ItemUIProvider) Slider(org.eclipse.smarthome.model.sitemap.Slider) Widget(org.eclipse.smarthome.model.sitemap.Widget) LinkableWidget(org.eclipse.smarthome.model.sitemap.LinkableWidget)

Example 8 with Widget

use of org.eclipse.smarthome.model.sitemap.Widget in project smarthome by eclipse.

the class ItemUIRegistryImpl method getDynamicGroupChildren.

/**
 * This method creates a list of children for a group dynamically.
 * If there are no explicit children defined in a sitemap, the children
 * can thus be created on the fly by iterating over the members of the group item.
 *
 * @param group The group widget to get children for
 * @return a list of default widgets provided for the member items
 */
private EList<Widget> getDynamicGroupChildren(Group group) {
    EList<Widget> children = new BasicEList<Widget>();
    String itemName = group.getItem();
    try {
        if (itemName != null) {
            Item item = getItem(itemName);
            if (item instanceof GroupItem) {
                GroupItem groupItem = (GroupItem) item;
                for (Item member : groupItem.getMembers()) {
                    Widget widget = getDefaultWidget(member.getClass(), member.getName());
                    if (widget != null) {
                        widget.setItem(member.getName());
                        children.add(widget);
                    }
                }
            } else {
                logger.warn("Item '{}' is not a group.", item.getName());
            }
        } else {
            logger.warn("Group does not specify an associated item - ignoring it.");
        }
    } catch (ItemNotFoundException e) {
        logger.warn("Dynamic group with label '{}' will be ignored, because its item '{}' does not exist.", group.getLabel(), itemName);
    }
    return children;
}
Also used : NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) CallItem(org.eclipse.smarthome.core.library.items.CallItem) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) DateTimeItem(org.eclipse.smarthome.core.library.items.DateTimeItem) RollershutterItem(org.eclipse.smarthome.core.library.items.RollershutterItem) GroupItem(org.eclipse.smarthome.core.items.GroupItem) ColorItem(org.eclipse.smarthome.core.library.items.ColorItem) LocationItem(org.eclipse.smarthome.core.library.items.LocationItem) ContactItem(org.eclipse.smarthome.core.library.items.ContactItem) GenericItem(org.eclipse.smarthome.core.items.GenericItem) Item(org.eclipse.smarthome.core.items.Item) StringItem(org.eclipse.smarthome.core.library.items.StringItem) PlayerItem(org.eclipse.smarthome.core.library.items.PlayerItem) ImageItem(org.eclipse.smarthome.core.library.items.ImageItem) DimmerItem(org.eclipse.smarthome.core.library.items.DimmerItem) BasicEList(org.eclipse.emf.common.util.BasicEList) Widget(org.eclipse.smarthome.model.sitemap.Widget) LinkableWidget(org.eclipse.smarthome.model.sitemap.LinkableWidget) GroupItem(org.eclipse.smarthome.core.items.GroupItem) ItemNotFoundException(org.eclipse.smarthome.core.items.ItemNotFoundException)

Example 9 with Widget

use of org.eclipse.smarthome.model.sitemap.Widget in project smarthome by eclipse.

the class ItemUIRegistryImpl method getChildren.

@Override
public EList<Widget> getChildren(Sitemap sitemap) {
    EList<Widget> widgets = sitemap.getChildren();
    EList<Widget> result = new BasicEList<Widget>();
    for (Widget widget : widgets) {
        Widget resolvedWidget = resolveDefault(widget);
        if (resolvedWidget != null) {
            result.add(resolvedWidget);
        }
    }
    return result;
}
Also used : BasicEList(org.eclipse.emf.common.util.BasicEList) Widget(org.eclipse.smarthome.model.sitemap.Widget) LinkableWidget(org.eclipse.smarthome.model.sitemap.LinkableWidget)

Example 10 with Widget

use of org.eclipse.smarthome.model.sitemap.Widget in project smarthome by eclipse.

the class ProxyServletService method uriFromRequest.

/**
 * Determine which URI to address based on the request contents.
 *
 * @param request the servlet request. New attributes may be added to the request in order to cache the result for
 *            future calls.
 * @return the URI indicated by the request, or <code>null</code> if not possible
 */
URI uriFromRequest(HttpServletRequest request) {
    try {
        // Return any URI we've already saved for this request
        URI uri = (URI) request.getAttribute(ATTR_URI);
        if (uri != null) {
            return uri;
        } else {
            ProxyServletException pse = (ProxyServletException) request.getAttribute(ATTR_SERVLET_EXCEPTION);
            if (pse != null) {
                // If we errored on this request before, there is no point continuing
                return null;
            }
        }
        String sitemapName = request.getParameter("sitemap");
        if (sitemapName == null) {
            throw new ProxyServletException(HttpServletResponse.SC_BAD_REQUEST, "Parameter 'sitemap' must be provided!");
        }
        String widgetId = request.getParameter("widgetId");
        if (widgetId == null) {
            throw new ProxyServletException(HttpServletResponse.SC_BAD_REQUEST, "Parameter 'widgetId' must be provided!");
        }
        Sitemap sitemap = (Sitemap) modelRepository.getModel(sitemapName);
        if (sitemap == null) {
            throw new ProxyServletException(HttpServletResponse.SC_NOT_FOUND, String.format("Sitemap '%s' could not be found!", sitemapName));
        }
        Widget widget = itemUIRegistry.getWidget(sitemap, widgetId);
        if (widget == null) {
            throw new ProxyServletException(HttpServletResponse.SC_NOT_FOUND, String.format("Widget '%s' could not be found!", widgetId));
        }
        String uriString = null;
        if (widget instanceof Image) {
            uriString = ((Image) widget).getUrl();
        } else if (widget instanceof Video) {
            uriString = ((Video) widget).getUrl();
        } else {
            throw new ProxyServletException(HttpServletResponse.SC_FORBIDDEN, String.format("Widget type '%s' is not supported!", widget.getClass().getName()));
        }
        String itemName = widget.getItem();
        if (itemName != null) {
            State state = itemUIRegistry.getItemState(itemName);
            if (state != null && state instanceof StringType) {
                try {
                    uri = URI.create(state.toString());
                    request.setAttribute(ATTR_URI, uri);
                    return uri;
                } catch (IllegalArgumentException ex) {
                // fall thru
                }
            }
        }
        try {
            uri = URI.create(uriString);
            request.setAttribute(ATTR_URI, uri);
            return uri;
        } catch (IllegalArgumentException iae) {
            throw new ProxyServletException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, String.format("URI '%s' is not a valid URI.", uriString));
        }
    } catch (ProxyServletException pse) {
        request.setAttribute(ATTR_SERVLET_EXCEPTION, pse);
        return null;
    }
}
Also used : Sitemap(org.eclipse.smarthome.model.sitemap.Sitemap) StringType(org.eclipse.smarthome.core.library.types.StringType) Video(org.eclipse.smarthome.model.sitemap.Video) State(org.eclipse.smarthome.core.types.State) Widget(org.eclipse.smarthome.model.sitemap.Widget) Image(org.eclipse.smarthome.model.sitemap.Image) URI(java.net.URI)

Aggregations

Widget (org.eclipse.smarthome.model.sitemap.Widget)24 LinkableWidget (org.eclipse.smarthome.model.sitemap.LinkableWidget)14 Sitemap (org.eclipse.smarthome.model.sitemap.Sitemap)9 Item (org.eclipse.smarthome.core.items.Item)8 BasicEList (org.eclipse.emf.common.util.BasicEList)6 Frame (org.eclipse.smarthome.model.sitemap.Frame)6 GenericItem (org.eclipse.smarthome.core.items.GenericItem)5 ColorItem (org.eclipse.smarthome.core.library.items.ColorItem)5 Test (org.junit.Test)5 EList (org.eclipse.emf.common.util.EList)4 ItemNotFoundException (org.eclipse.smarthome.core.items.ItemNotFoundException)4 EObject (org.eclipse.emf.ecore.EObject)3 GroupItem (org.eclipse.smarthome.core.items.GroupItem)3 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 ServletException (javax.servlet.ServletException)2 CallItem (org.eclipse.smarthome.core.library.items.CallItem)2 ContactItem (org.eclipse.smarthome.core.library.items.ContactItem)2 DateTimeItem (org.eclipse.smarthome.core.library.items.DateTimeItem)2