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

Stab at sorting groups by label #78

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -14,6 +14,7 @@

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Comparator;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
Expand All @@ -25,7 +26,11 @@
import javax.servlet.http.HttpServletResponse;

import org.eclipse.emf.common.util.EList;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.smarthome.config.core.ConfigurableService;
import org.eclipse.smarthome.core.items.GroupItem;
import org.eclipse.smarthome.core.items.Item;
import org.eclipse.smarthome.core.items.ItemNotFoundException;
import org.eclipse.smarthome.core.items.ItemRegistry;
import org.eclipse.smarthome.io.http.HttpContextFactoryService;
import org.eclipse.smarthome.io.rest.sitemap.SitemapSubscriptionService;
Expand Down Expand Up @@ -206,6 +211,22 @@ protected void service(HttpServletRequest req, HttpServletResponse res) throws S
throw new RenderException("Widget '" + w + "' can not have any content");
}
EList<Widget> children = renderer.getItemUIRegistry().getChildren((LinkableWidget) w);

// Figure out if the item is a group and -- if so -- sort by child label.
String itemId = w.getItem();
if (itemId != null && !itemId.isEmpty()) {
try {
@NonNull
Item item = renderer.getItemUIRegistry().getItem(w.getItem());
if (item instanceof GroupItem) {
// Ideally, the comparator would be depending on a (new) group widget property.
children.sort(new LabelComparator());
}
} catch (ItemNotFoundException e) {
throw new RenderException("Item not found: " + itemId);
}
}

result.append(renderer.processPage(renderer.getItemUIRegistry().getWidgetId(w), sitemapName, label,
children, async));
}
Expand Down Expand Up @@ -255,4 +276,19 @@ public void unsetHttpContextFactoryService(HttpContextFactoryService HttpContext
super.unsetHttpContextFactoryService(HttpContextFactoryService);
}

// Can't be static: accesses the renderer.
class LabelComparator implements Comparator<Widget> {

@Override
public int compare(Widget w1, Widget w2) {
// Not using renderer.getLabel() here because it might contain HTML.
try {
@NonNull Item i1 = renderer.getItemUIRegistry().getItem(w1.getItem());
@NonNull Item i2 = renderer.getItemUIRegistry().getItem(w2.getItem());
return i1.getLabel().compareToIgnoreCase(i2.getLabel());
} catch (ItemNotFoundException e) {
throw new RuntimeException(e);
}
}
}
}