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;
}
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;
}
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;
}
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;
}
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;
}
}
Aggregations