org.wings.session.SCursor Maven / Gradle / Ivy
The newest version!
package org.wings.session;
import org.wings.*;
import org.wings.plaf.CursorCG;
import java.util.*;
import java.util.stream.Collectors;
/**
* SCursor is a class that let's you display additional informations at the cursor position.
* It consists of 0-n SIcons, which are displayed at the top of the SCursor area and of a SLabel displayed below the
* SIcons. It offers several methods to add/remove SIcons and to set the label.
*/
public class SCursor extends SComponent {
// TODO: remove Scomponent
public static final int HIGHEST_PRIORITY = 0;
public static final int HIGH_PRIORITY = 50;
public static final int NORMAL_PRIORITY = 100;
public static final int LOW_PRIORITY = 200;
public static final int LOWEST_PRIORITY = 255;
private SLabel label;
private Map> icons;
private boolean visibility;
private final static class CategorizedSIcon {
private String category;
private SIcon icon;
public CategorizedSIcon(String category, SIcon icon) {
this.category = category;
this.icon = icon;
}
public String getCategory() {
return category;
}
public SIcon getIcon() {
return icon;
}
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
CategorizedSIcon that = (CategorizedSIcon) o;
if (category != null ? !category.equals(that.category) : that.category != null) return false;
return icon != null ? icon.equals(that.icon) : that.icon == null;
}
public int hashCode() {
int result = (category != null ? category.hashCode() : 0);
result = 31 * result + (icon != null ? icon.hashCode() : 0);
return result;
}
}
public SCursor() {
icons = new HashMap<>();
this.setParentFrame(getSession().getRootFrame());
this.setAttribute("z-index", "9999");
this.setAttribute("position", "absolute");
this.setAttribute("display", "none"); // only the default attributes, they'll be changed without setAttribute
this.setAttribute("visibility", "hidden");
this.visibility = false;
//this.setBorder(SBorderFactory.createSLineBorder(Color.BLACK, 3));
this.setShowCursor(false);
}
/**
* Adds a Icon in category with priority
* @param category Category of the item
* @param icon The Icon to add
* @param priority Priority of the Icon - SCursor.HIGHEST_PRIORITY means show first, LOWEST_PRIORITY means show last.
*/
public void addIcon(String category, SIcon icon, int priority) {
List list = icons.get(priority);
if(list == null) {
list = new LinkedList<>();
}
list.add(new CategorizedSIcon(category, icon));
icons.put(priority, list);
update(((CursorCG)getCG()).getContentUpdate(this));
}
/**
* Adds a Icon in the 'default' category with the given priority
* @param icon
* @param priority
*/
public void addIcon(SIcon icon, int priority) {
this.addIcon("default", icon, priority);
}
/**
* Sets the visibility of the cursor
* @param visible
*/
public void setShowCursor(boolean visible) {
update(((CursorCG)getCG()).getVisibilityUpdate(this, visible));
this.visibility = visible;
}
private boolean isEmpty() {
return getIconsByPriority().size() == 0 && label == null;
}
/**
* Hides the cursor, if there are neither icons nor a label to display.
*/
public void hideCursorIfPossible() {
if(isEmpty()) {
setShowCursor(false);
}
}
public boolean isShown() {
return this.visibility;
}
/**
* Removes a Icon from a category - if icon is null, all icons from that category are removed
* @param category
* @param icon
*/
public void removeIcon(String category, SIcon icon) {
for(int i=0; i<=255; ++i) {
List list = icons.get(i);
if(list == null)
continue;
list.remove(new CategorizedSIcon(category, icon));
}
update(((CursorCG)getCG()).getContentUpdate(this));
}
/**
* Removes all icons from the given category
* @param category
*/
public void removeIcons(String category) {
for(int i=0; i<=255; ++i) {
List list = icons.get(i);
if(list == null)
continue;
List tempList = list.stream().filter(icon -> icon.getCategory().equals(category)).map(CategorizedSIcon::getIcon).collect(Collectors.toCollection(LinkedList::new));
for(SIcon icon : tempList) {
removeIcon(category, icon);
}
}
update(((CursorCG)getCG()).getContentUpdate(this));
}
/**
* Sets a label to be displayed
* @param label
*/
public void setLabel(SLabel label) {
this.label = label;
update(((CursorCG)getCG()).getContentUpdate(this));
}
/**
* Returns the currently set label
* @return
*/
public SLabel getLabel() {
return label;
}
/**
* Returns the icons sorted by priority (highest priority first)
* @return
*/
public List getIconsByPriority() {
List retList = new LinkedList<>();
for(int i=0; i<=255; ++i) {
List list = icons.get(i);
if(list == null)
continue;
retList.addAll(list.stream().map(CategorizedSIcon::getIcon).collect(Collectors.toList()));
}
return retList;
}
@Override
public boolean isRecursivelyVisible() {
return isVisible();
}
}